SlideShare uma empresa Scribd logo
1 de 73
Baixar para ler offline
EstruturasdeDadosprobabilísticas
Vocênãoprecisasabertudo
DiegoRocha
@kiwi
@kiwi-Desafios
100M+deeventos/dia
Temporeal Baixocusto
Abordagemconvencional
(Notso)olddog,
newtricks
Estruturasdedados
probabilísticas
Estruturasdedados
probabilísticas
Precisamdepoucamemória
Estruturasdedados
probabilísticas
Precisamdepoucamemória
Tempodeacessoéconstante
Estruturasdedados
probabilísticas
Precisamdepoucamemória
Tempodeacessoéconstante
Completamentecool
- membershiptest
- Códigopromocionaljáfoiutilizado?
- Usuáriojárealizouumaação?
- Cardinalityestimation
- Quantaspessoasdiferentesjáassistiramaumvídeo?
- Frequencyestimation
- Quaissãoastagsmaisutilizadasemcomentários?
Paraqueserve?
Quantaspessoasdiferentes queleramanotíciaprincipal publicaram
comentárioscom atagmaisfrequente?
Getcrazy!
Cardinalityestimation
Membershiptest
Frequencyestimation
LinearProbabilisticCounter
LinearProbabilisticCounter
BloomFilter
LinearProbabilisticCounter
BloomFilter
Count-minsketch
LinearProbabilisticCounter
BloomFilter
LogLogCounter
Count-minsketch
Vamosreveralgunsconceitos
- Bitset
- Funçãohash
Antesdemaisnada...
Sequênciadebits,cadaumcomsuaposição
Bitset
Sequênciadebits,cadaumcomsuaposição
Bitset
0 1 0 0 1 0 1 0 1 1 0 0
0 1 2 3 4 5 6 7 8 9 10 11
Bitset
0 1 0 0 1 0 0 1 1 1 0 1OR =
0 1 1 0 0 1 0 1 0 1 0 0AND =
0 1 0 0 0 1 1 0 0 0 1 0XOR =
Funçãodeterminística,sobrejetora,comdomíniodetamanho
arbitrárioecontradomíniofinito
Funçãohash
Funçãodeterminística,sobrejetora,comdomíniodetamanho
arbitrárioecontradomíniofinito
Funçãohash
Waat?
Funçãohash
João
Renato
Teresa
Denise
Fábio
4
15
8
23
16
...
42Eli
Luana
Hash
Funçãohash
João
Renato
Teresa
Denise
Fábio
4
15
8
23
16
...
42Eli
Luana
Hash
Colisão!!
LinearProbabilisticCounter
BloomFilter
LogLogCounter
Count-minsketch
LinearProbabilisticCounter
BloomFilter
LogLogCounter
Count-minsketch
LinearProbabilisticCounter
Estruturadedadosquepermiterealizar:
- membershiptest
- cardinalityestimation
LinearProbabilisticCounter
Bitsetdetamanhom,comtodasasposiçõesinicialmentezeradas
0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12
LinearProbabilisticCounter
0 0 0 1 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12
Adicionamos“joão”
hash(João)=>3
hash(João)=>3
LinearProbabilisticCounter
0 0 0 1 0 0 0 0 0 0 1 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12
Adicionamos“Teresa”
hash(Teresa)=>10
hash(João)=>3
LinearProbabilisticCounter
0 0 0 1 0 0 0 0 0 0 1 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12
Adicionamos“Renato”
hash(Teresa)=>10hash(Renato)=>3
LinearProbabilisticCounter
0 0 0 1 0 0 0 0 0 0 1 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12
Membershiptest:“Luanajáfoiinserida?”
hash(Luana)=>7
LinearProbabilisticCounter
0 0 0 1 0 0 0 0 0 0 1 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12
Membershiptest:“Luanajáfoiinserida?”
hash(Luana)=>7
Nops
LinearProbabilisticCounter
0 0 0 1 0 0 0 0 0 0 1 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12
Membershiptest:“Elijáfoiinserido?”
hash(Eli)=>10
LinearProbabilisticCounter
0 0 0 1 0 0 0 0 0 0 1 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12
Membershiptest:“Elijáfoiinserido?”
hash(Eli)=>10
Talvez
LinearProbabilisticCounter
0 0 0 1 0 0 0 0 0 0 1 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12
Membershiptest:“Elijáfoiinserido?”
hash(Eli)=>10
Talvez
FalsosPositivossãopossíveis
Taxadeerroédefinidapelataxadecolisão
LinearProbabilisticCounter
0 0 0 1 0 0 0 0 0 0 1 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12
Cardinalityestimation:“Quantosnomesdiferentesforam
inseridos?”
LinearProbabilisticCounter
0 0 0 1 0 0 0 0 0 0 1 0
0 1 2 3 4 5 6 7 8 9 10 11
m(tamanhodobitset)=12
Cardinalityestimation:“Quantosnomesdiferentesforam
inseridos?”
card(m, w) = -m ln m - w
m
w(nºdebits1)=2
LinearProbabilisticCounter
BloomFilter
LogLogCounter
Count-minsketch
Bloomfilter
Estruturadedadosmuitoparecidacomolinearprobabilistic
counterquepermiterealizarMembershiptest
0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12;k=3
Bloomfilter
Cadainserçãomarca kbits,aoinvésdeumúnico
0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12;k=3
Bloomfilter
Cadainserçãomarcakbits,aoinvésdeumúnico
0 0 0 1 0 1 1 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11
m=12;k=3
hash(João)=>[3,5,6]
Bloomfilter
Cadainserçãomarcakbits,aoinvésdeumúnico
0 0 0 1 0 1 1 0 0 0 1 1
0 1 2 3 4 5 6 7 8 9 10 11
m=12;k=3
hash(Renato)=>[5,10,11]
Bloomfilter
MEmbershiptest:“Luizjáfoiinserido?”
0 0 0 1 0 1 1 0 0 0 1 1
0 1 2 3 4 5 6 7 8 9 10 11
m=12;k=3
hash(Luiz)=>[3,4,6]
Bloomfilter
MEmbershiptest:“Luizjáfoiinserido?”
0 0 0 1 0 1 1 0 0 0 1 1
0 1 2 3 4 5 6 7 8 9 10 11
m=12;k=3
hash(Luiz)=>[3,4,6]
Nah
Bloomfilter
MEmbershiptest:“Luanajáfoiinserida?”
0 0 0 1 0 1 1 0 0 0 1 1
0 1 2 3 4 5 6 7 8 9 10 11
m=12;k=3
hash(Luana)=>[6,10,11]
Bloomfilter
MEmbershiptest:“Luanajáfoiinserida?”
0 0 0 1 0 1 1 0 0 0 1 1
0 1 2 3 4 5 6 7 8 9 10 11
m=12;k=3
hash(Luana)=>[6,10,11]
Provavelmente
Bloomfilter
MEmbershiptest:“Luanajáfoiinserida?”
0 0 0 1 0 1 1 0 0 0 1 1
0 1 2 3 4 5 6 7 8 9 10 11
m=12;k=3
hash(Luana)=>[6,10,11]
Provavelmente
Tambémpodeapresentarfalsopositivos,
masgeralmentesãomaisraros
LinearProbabilisticCounterssãoBloomFilterscomk=1
LinearProbabilisticCounter
BloomFilter
LogLogCounter
Count-minsketch
Count-minsketch
Estruturadedadosutilizadapararealizarfrequencyestimation
Count-minsketch
Começamoscomumamatrizmxndeinteiros
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Count-minsketch
Paracadaelementoobservado,incrementamosnposições
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0
hash(João)=>[2,0,1,3]
Count-minsketch
Paracadaelementoobservado,incrementamosnposições
1 0 2 0
0 0 0 1
1 1 0 1
0 1 0 0
hash(Eli)=>[1,2,1,2]
Count-minsketch
Eventualmente...
11 0 2 5
7 6 4 5
4 6 15 13
9 19 10 8
Count-minsketch
…obtemosumaestimativadafrequênciadeumelemento,
observandoamenorcontagemparaaqueleelemento
hash(João)=>[2,0,1,3]
11 0 2 5
7 6 4 5
4 6 15 13
9 19 10 8
Count-minsketch
Aestimativasempreserámaiorouigualàfrequênciareal
11 0 2 5
7 6 4 5
4 6 15 13
9 19 10 8
hash(João)=>[2,0,1,3]
Count-minsketch
Aestimativasempreserámaiorouigualàfrequênciareal
11 0 2 5
7 6 4 5
4 6 15 13
9 19 10 8
hash(João)=>[2,0,1,3]
Tendeasermelhor
quandoalguns
elementossãomuito
maisfrequentesque
outros
LinearProbabilisticCounter
BloomFilter
LogLogCounter
Count-minsketch
LogLogCounter
Fornececardinalityestimationutilizandomuuuuitopouca
memóriaecomboaprecisãoparaconjuntoscommuitoselementos
LogLogCounter
BAseia-senaideiadeque,sabendoqualfoioelementomaisraro
encontrado,épossívelobterumaestimativadequantoselementos
foramvistos
LogLogCounter
Rank(X)-“raridadedoelemento”
LogLogCounter
Rank(X)-“raridadedoelemento”
Rankdebinários
11111111
11111110
...
01111111
...
00111111
...
00000001
1
2
3
8
Raridadedefinidapelonúmerozerosiniciais
n/(2^K)elementoscomeçandocomk-1zeros
LogLogCounter
Estimativainstável:
card(k) = 2^k
k = maior rank encontrado
LogLogCounter
Estimativainstável:
card(k) = 2^k
k = maior rank encontrado
Podemosobterumaestimativaprecisarealizandoumamédiaentrevárias
estimativasinstáveis
LogLogCounter
Buckets
Cadabucketarmazenaomaiorrank
observadodentrodeumconjuntode
valores
0
0
0
0
0
0
1
2
3
4
LogLogCounter
0
2
0
0
0
0
1
2
3
4
hash(João)=>3
Rank(João)=>2
LogLogCounter
4
2
0
0
0
0
1
2
3
4
hash(Luana)=>1
Rank(Luana)=>4
LogLogCounter
4
2
0
0
0
0
1
2
3
4
hash(Eli)=>3
Rank(eli)=>1
LogLogCounter
Estimativaprecisa:
card(m, buckets) = αm.m.2^(∑buckets[j] / m)
m = número de buckets
αm =
j
Γ(−1/m) 1−2 -m
log 2
1/m
( ) ~~ 0.39701
LogLogCounter
Estimativaprecisa:
card(m, buckets) = αm.m.2^(∑buckets[j] / m)
m = número de buckets
αm =
j
Γ(−1/m) 1−2 -m
log 2
1/m
( ) ~~ 0.39701
Crazymathtotallyworthit
Outrasestruturasdedados
Streamsummary
dezenasdevariaçõesdebloomfilter
Count-mean-minsketch
HyperLogLogCounter
Referências
- LoglogCountingofLargeCardinalities-MarianneDurand,PhilippeFlajolet2003
- Space/timetrade-offsinhashcodingwithallowableerrors-BurtonH.Bloom1970
- ALinear-TimeProbabilisticCountingAlgorithmforDatabaseApplications -KYU-
YOUNGWHANG,HOWARDM.TAYLOR1990
- Count-MinSketch-GrahamCormode2003
- PROBABILISTICDATASTRUCTURESFORWEBANALYTICSANDDATAMINING(https:
//highlyscalable.wordpress.com/2012/05/01/probabilistic-structures-web-analytics-
data-mining/)-IlyaKatsov
perguntas?

Mais conteúdo relacionado

Destaque

Vaastu shastra in modern buildng construction
Vaastu shastra in modern buildng constructionVaastu shastra in modern buildng construction
Vaastu shastra in modern buildng constructionKamlesh Jingar
 
Estudo batismo oficialmontesinai
Estudo batismo oficialmontesinaiEstudo batismo oficialmontesinai
Estudo batismo oficialmontesinaiElisa Schenk
 
Agenda rottamatore 6 10 giugno 2016
Agenda rottamatore  6   10 giugno  2016Agenda rottamatore  6   10 giugno  2016
Agenda rottamatore 6 10 giugno 2016Reti
 
Luận văn tốt nghiệp: Phân tích kết quả hoạt động kinh doanh của NH SGTT Kiên ...
Luận văn tốt nghiệp: Phân tích kết quả hoạt động kinh doanh của NH SGTT Kiên ...Luận văn tốt nghiệp: Phân tích kết quả hoạt động kinh doanh của NH SGTT Kiên ...
Luận văn tốt nghiệp: Phân tích kết quả hoạt động kinh doanh của NH SGTT Kiên ...Nguyễn Công Huy
 
Latihan soal pkn untuk persiapan uts genap tahun 2015
Latihan soal pkn untuk persiapan uts genap tahun 2015Latihan soal pkn untuk persiapan uts genap tahun 2015
Latihan soal pkn untuk persiapan uts genap tahun 2015Catur Prasetyo
 

Destaque (8)

Fusaro Referendum
Fusaro ReferendumFusaro Referendum
Fusaro Referendum
 
Vaastu shastra in modern buildng construction
Vaastu shastra in modern buildng constructionVaastu shastra in modern buildng construction
Vaastu shastra in modern buildng construction
 
Estudo batismo oficialmontesinai
Estudo batismo oficialmontesinaiEstudo batismo oficialmontesinai
Estudo batismo oficialmontesinai
 
Agenda rottamatore 6 10 giugno 2016
Agenda rottamatore  6   10 giugno  2016Agenda rottamatore  6   10 giugno  2016
Agenda rottamatore 6 10 giugno 2016
 
Opasnosti
OpasnostiOpasnosti
Opasnosti
 
Luận văn tốt nghiệp: Phân tích kết quả hoạt động kinh doanh của NH SGTT Kiên ...
Luận văn tốt nghiệp: Phân tích kết quả hoạt động kinh doanh của NH SGTT Kiên ...Luận văn tốt nghiệp: Phân tích kết quả hoạt động kinh doanh của NH SGTT Kiên ...
Luận văn tốt nghiệp: Phân tích kết quả hoạt động kinh doanh của NH SGTT Kiên ...
 
Latihan soal pkn untuk persiapan uts genap tahun 2015
Latihan soal pkn untuk persiapan uts genap tahun 2015Latihan soal pkn untuk persiapan uts genap tahun 2015
Latihan soal pkn untuk persiapan uts genap tahun 2015
 
5 глобальные тенденций, меняющих технологию продаж
5 глобальные тенденций, меняющих технологию продаж5 глобальные тенденций, меняющих технологию продаж
5 глобальные тенденций, меняющих технологию продаж
 

Estruturas de Dados Probabilísticas: Linear Probabilistic Counter, Bloom Filter, LogLog Counter e Count-min Sketch