512
Cardinality Estimation with special thanks to Jérémie Lumbroso Robert Sedgewick Princeton University

Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Cardinality Estimation

with special thanks to Jérémie Lumbroso

Robert Sedgewick Princeton University

Page 2: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Philippe Flajolet 1948-2011

Philippe Flajolet, mathematician and computer scientist extraordinaire

Page 3: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Page 4: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:

Page 5: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.

Page 6: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Page 7: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Analytic Engine

how many times do we have to turn the crank?

Page 8: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Knuth’s insight: AofA is a scientific endeavor.

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Analytic Engine

how many times do we have to turn the crank?

Page 9: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Knuth’s insight: AofA is a scientific endeavor.• Start with a working program (algorithm implementation).

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Analytic Engine

how many times do we have to turn the crank?

Page 10: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Knuth’s insight: AofA is a scientific endeavor.• Start with a working program (algorithm implementation).• Develop mathematical model of its behavior.

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Analytic Engine

how many times do we have to turn the crank?

Page 11: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Knuth’s insight: AofA is a scientific endeavor.• Start with a working program (algorithm implementation).• Develop mathematical model of its behavior.• Use the model to formulate hypotheses on resource usage.

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Analytic Engine

how many times do we have to turn the crank?

Page 12: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Knuth’s insight: AofA is a scientific endeavor.• Start with a working program (algorithm implementation).• Develop mathematical model of its behavior.• Use the model to formulate hypotheses on resource usage.• Use the program to validate hypotheses.

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Analytic Engine

how many times do we have to turn the crank?

Page 13: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Knuth’s insight: AofA is a scientific endeavor.• Start with a working program (algorithm implementation).• Develop mathematical model of its behavior.• Use the model to formulate hypotheses on resource usage.• Use the program to validate hypotheses.• Iterate on basis of insights gained.

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Analytic Engine

how many times do we have to turn the crank?

Page 14: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Knuth’s insight: AofA is a scientific endeavor.• Start with a working program (algorithm implementation).• Develop mathematical model of its behavior.• Use the model to formulate hypotheses on resource usage.• Use the program to validate hypotheses.• Iterate on basis of insights gained.

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Analytic Engine

how many times do we have to turn the crank?

Page 15: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Knuth’s insight: AofA is a scientific endeavor.• Start with a working program (algorithm implementation).• Develop mathematical model of its behavior.• Use the model to formulate hypotheses on resource usage.• Use the program to validate hypotheses.• Iterate on basis of insights gained.

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Analytic Engine

how many times do we have to turn the crank?

Page 16: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Knuth’s insight: AofA is a scientific endeavor.• Start with a working program (algorithm implementation).• Develop mathematical model of its behavior.• Use the model to formulate hypotheses on resource usage.• Use the program to validate hypotheses.• Iterate on basis of insights gained.

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Analytic Engine

how many times do we have to turn the crank?

Page 17: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Knuth’s insight: AofA is a scientific endeavor.• Start with a working program (algorithm implementation).• Develop mathematical model of its behavior.• Use the model to formulate hypotheses on resource usage.• Use the program to validate hypotheses.• Iterate on basis of insights gained.

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Analytic Engine

how many times do we have to turn the crank?

Page 18: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Knuth’s insight: AofA is a scientific endeavor.• Start with a working program (algorithm implementation).• Develop mathematical model of its behavior.• Use the model to formulate hypotheses on resource usage.• Use the program to validate hypotheses.• Iterate on basis of insights gained.

3

Don Knuth’s legacy: Analysis of Algorithms (AofA)

Understood since Babbage:• Computational resources are limited.• Method (algorithm) used matters.

Analytic Engine

how many times do we have to turn the crank?

Difficult to overstate the significance of this insight.

Page 19: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

AofA has played a critical role

in the development of our computational infrastructure

4

Page 20: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

AofA has played a critical role

in the development of our computational infrastructure

4

how many times to turn the crank?

Page 21: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

AofA has played a critical role

in the development of our computational infrastructure

4

how many times to turn the crank?

how long to compile my program?

Page 22: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

AofA has played a critical role

in the development of our computational infrastructure

4

how many times to turn the crank?

how long to sort random data for cryptanalysis preprocessing?

how long to compile my program?

Page 23: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

AofA has played a critical role

in the development of our computational infrastructure

4

how many times to turn the crank?

how long to sort random data for cryptanalysis preprocessing?

how long to compile my program? how long to check

that my VLSI circuit follows the rules?

Page 24: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

AofA has played a critical role

in the development of our computational infrastructure

4

how many times to turn the crank?

how long to sort random data for cryptanalysis preprocessing?

how long to compile my program? how long to check

that my VLSI circuit follows the rules?

how many bodies in motion can I

simulate?

and the advance of scientific knowledge

Page 25: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

AofA has played a critical role

in the development of our computational infrastructure

4

how many times to turn the crank?

how long to sort random data for cryptanalysis preprocessing?

how long to compile my program? how long to check

that my VLSI circuit follows the rules?

how quickly can I find clusters?how many bodies

in motion can I simulate?

and the advance of scientific knowledge

Page 26: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

AofA has played a critical role

in the development of our computational infrastructure

4

how many times to turn the crank?

how long to sort random data for cryptanalysis preprocessing?

how long to compile my program? how long to check

that my VLSI circuit follows the rules?

how quickly can I find clusters?how many bodies

in motion can I simulate?

and the advance of scientific knowledge

“ PEOPLE WHO ANALYZE ALGORITHMS have double happiness. They experience the sheer beauty of elegant mathematical patterns that surround elegant computational procedures. Then they receive a practical payoff when their theories make it possible to get other jobs done more quickly and more economically.”

− Don Knuth

Page 27: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Page 28: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Practical computing

Practical computing

Page 29: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Practical computing

• Real code on real machines

Practical computing

Page 30: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Practical computing

• Real code on real machines

• Thorough validation

Practical computing

Page 31: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Practical computing

• Real code on real machines

• Thorough validation

• Limited math models

Practical computing

Page 32: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Theoretical Computer Science

Practical computing

• Real code on real machines

• Thorough validation

• Limited math models

Theoretical computer science

Practical computing

Page 33: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Theoretical Computer Science

Practical computing

• Real code on real machines

• Thorough validation

• Limited math models

Theoretical computer science

• Theorems

Practical computing

Page 34: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Theoretical Computer Science

Practical computing

• Real code on real machines

• Thorough validation

• Limited math models

Theoretical computer science

• Theorems

• Abstract math models

Practical computing

Page 35: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Theoretical Computer Science

Practical computing

• Real code on real machines

• Thorough validation

• Limited math models

Theoretical computer science

• Theorems

• Abstract math models

• Limited experimentation

Practical computing

Page 36: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Theoretical Computer Science

Practical computing

• Real code on real machines

• Thorough validation

• Limited math models

Theoretical computer science

• Theorems

• Abstract math models

• Limited experimentation

AofA

Practical computing AofA

Page 37: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Theoretical Computer Science

Practical computing

• Real code on real machines

• Thorough validation

• Limited math models

Theoretical computer science

• Theorems

• Abstract math models

• Limited experimentation

AofA• Theorems and code

Practical computing AofA

Page 38: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Theoretical Computer Science

Practical computing

• Real code on real machines

• Thorough validation

• Limited math models

Theoretical computer science

• Theorems

• Abstract math models

• Limited experimentation

AofA• Theorems and code• Precise math models

Practical computing AofA

Page 39: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

5

Analysis of Algorithms (present-day context)

Theoretical Computer Science

Practical computing

• Real code on real machines

• Thorough validation

• Limited math models

Theoretical computer science

• Theorems

• Abstract math models

• Limited experimentation

AofA• Theorems and code• Precise math models• Experiment, validate, iterate

Practical computing AofA

Page 40: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

OF

Cardinality Estimation

•Warmup: exact cardinality count •Probabilistic counting •Stochastic averaging •Refinements •Final frontier

Page 41: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

7

Cardinality counting

Q. In a given stream of data values, how many different values are present?

Page 42: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

7

Cardinality counting

Q. In a given stream of data values, how many different values are present?

Reference application. How many unique visitors in a web log?

Page 43: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

7

Cardinality counting

Q. In a given stream of data values, how many different values are present?

Reference application. How many unique visitors in a web log?

log.07.f3.txt

6 million strings

Page 44: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

7

Cardinality counting

Q. In a given stream of data values, how many different values are present?

Reference application. How many unique visitors in a web log?

State of the art in the wild for decades. Sort, then count.

log.07.f3.txt

6 million strings

Page 45: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

7

Cardinality counting

Q. In a given stream of data values, how many different values are present?

Reference application. How many unique visitors in a web log?

State of the art in the wild for decades. Sort, then count.

log.07.f3.txt

6 million strings

% sort -u log.07.f3.txt | wc -l 1112365

UNIX (1970s-present)

“unique”

Page 46: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

7

Cardinality counting

Q. In a given stream of data values, how many different values are present?

Reference application. How many unique visitors in a web log?

State of the art in the wild for decades. Sort, then count.

SELECT DATE_TRUNC(‘day’,event_time), COUNT(DISTINCT user_id), COUNT(DISTINCT url)FROM weblog

SQL (1970s-present)

log.07.f3.txt

6 million strings

% sort -u log.07.f3.txt | wc -l 1112365

UNIX (1970s-present)

“unique”

Page 47: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

8

Standard “optimal” solution: Use a hash table

Page 48: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing

Page 49: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.

Page 50: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.

Page 51: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.

example: multiply by a prime, then take remainder after dividing by M.

Page 52: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.

example: multiply by a prime, then take remainder after dividing by M.

Page 53: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

example: multiply by a prime, then take remainder after dividing by M.

Page 54: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

example: multiply by a prime, then take remainder after dividing by M.

Page 55: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

example: multiply by a prime, then take remainder after dividing by M.

Page 56: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17)

example: multiply by a prime, then take remainder after dividing by M.

Page 57: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17)

example: multiply by a prime, then take remainder after dividing by M.

Page 58: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17)

example: multiply by a prime, then take remainder after dividing by M.

count 0

Page 59: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15

example: multiply by a prime, then take remainder after dividing by M.

count 0

Page 60: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15

example: multiply by a prime, then take remainder after dividing by M.

P

count 01

Page 61: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6

example: multiply by a prime, then take remainder after dividing by M.

P

count 01

Page 62: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6

example: multiply by a prime, then take remainder after dividing by M.

J P

count 012

Page 63: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6

example: multiply by a prime, then take remainder after dividing by M.

J P

count 012

Page 64: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14

example: multiply by a prime, then take remainder after dividing by M.

J P

count 012

Page 65: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14

example: multiply by a prime, then take remainder after dividing by M.

J E P

count 0123

Page 66: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1

example: multiply by a prime, then take remainder after dividing by M.

J E P

count 0123

Page 67: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1

example: multiply by a prime, then take remainder after dividing by M.

J EK P

count 01234

Page 68: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6

example: multiply by a prime, then take remainder after dividing by M.

J EK P

count 01234

Page 69: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13

example: multiply by a prime, then take remainder after dividing by M.

J EK P

count 01234

Page 70: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13

example: multiply by a prime, then take remainder after dividing by M.

J EK L P

count 012345

Page 71: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7

example: multiply by a prime, then take remainder after dividing by M.

J EK L P

count 012345

Page 72: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7

example: multiply by a prime, then take remainder after dividing by M.

J EK L PC

count 012345

Page 73: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1

example: multiply by a prime, then take remainder after dividing by M.

J EK L PC

count 012345

Page 74: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15

example: multiply by a prime, then take remainder after dividing by M.

J EK L PC

count 012345

Page 75: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15

example: multiply by a prime, then take remainder after dividing by M.

J EK L

O

PC

count 012345

Page 76: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15

example: multiply by a prime, then take remainder after dividing by M.

J EK L OPC

count 0123456

Page 77: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8

example: multiply by a prime, then take remainder after dividing by M.

J EK L OPC

count 0123456

Page 78: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8

example: multiply by a prime, then take remainder after dividing by M.

J EK LM OPC

count 01234567

Page 79: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7

example: multiply by a prime, then take remainder after dividing by M.

J EK LM OPC

count 01234567

Page 80: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7

example: multiply by a prime, then take remainder after dividing by M.

T

J EK LM OPC

count 01234567

Page 81: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7

example: multiply by a prime, then take remainder after dividing by M.

T

J EK LM OPC

count 01234567

Page 82: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7

example: multiply by a prime, then take remainder after dividing by M.

T

J EK LM OPC

count 01234567

Page 83: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPC

count 012345678

Page 84: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPC

count 012345678

Page 85: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPC

count 012345678

Page 86: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPCG

count 0123456789

Page 87: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4 13

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPCG

count 0123456789

Page 88: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4 13 6

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPCG

count 0123456789

Page 89: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4 13 6 11

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPCG

count 0123456789

Page 90: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4 13 6 11

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPCG I

count 012345678910

Page 91: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4 13 6 11 9

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPCG I

count 012345678910

Page 92: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4 13 6 11 9

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPCG I

Fcount 012345678910

Page 93: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4 13 6 11 9

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPCG I

Fcount 012345678910

Page 94: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4 13 6 11 9

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPCG IF

count 01234567891011

Page 95: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4 13 6 11 9 1

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPCG IF

count 01234567891011

Page 96: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4 13 6 11 9 1 7

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPCG IF

count 01234567891011

Page 97: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

hash table (M = 17)

8

Standard “optimal” solution: Use a hash table

Hashing with linear probing• Create a table of size M.• Transform each value into a “random” table index.• Move right to find space if value collides.• Count values new to the table.

small example data stream P J J E K J L C K O M T P G L J I F K C

hash values (x-(‘A’))*97 % 17) 15 6 6 14 1 6 13 7 1 15 8 7 15 4 13 6 11 9 1 7

Additional (key) idea. Keep searches short by doubling table size when it becomes half full.

example: multiply by a prime, then take remainder after dividing by M.

TJ EK LM OPCG IF

count 01234567891011

Page 98: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

9

Mathematical analysis of exact cardinality count with linear probing

Theorem. Expected time and space cost is linear.

Page 99: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

9

Mathematical analysis of exact cardinality count with linear probing

Theorem. Expected time and space cost is linear.

Proof. Follows from classic Knuth Theorem 6.4.K.

Page 100: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

9

Mathematical analysis of exact cardinality count with linear probing

Theorem. Expected time and space cost is linear.

Proof. Follows from classic Knuth Theorem 6.4.K.

ptg999

From the Library of Robert Sedgewick

Page 101: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

9

Mathematical analysis of exact cardinality count with linear probing

Theorem. Expected time and space cost is linear.

Proof. Follows from classic Knuth Theorem 6.4.K.

ptg999

From the Library of Robert Sedgewick

“ I first formulated [this] derivation in 1962. Since this was the first nontrivial algorithm I had ever analyzed satisfactorily, it had a strong influence on the structure of these books. Ever since that day, the analysis of algorithms has in fact been one of the major themes of my life.”

− Knuth, TAOCP volume 3

Page 102: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

9

Mathematical analysis of exact cardinality count with linear probing

Theorem. Expected time and space cost is linear.

Proof. Follows from classic Knuth Theorem 6.4.K.

ptg999

From the Library of Robert Sedgewick

“ I first formulated [this] derivation in 1962. Since this was the first nontrivial algorithm I had ever analyzed satisfactorily, it had a strong influence on the structure of these books. Ever since that day, the analysis of algorithms has in fact been one of the major themes of my life.”

− Knuth, TAOCP volume 3

Page 103: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

9

Mathematical analysis of exact cardinality count with linear probing

Theorem. Expected time and space cost is linear.

Proof. Follows from classic Knuth Theorem 6.4.K.

ptg999

From the Library of Robert Sedgewick

Q. Do the hash functions that we use uniformly and independently distribute keys in the table?

“ I first formulated [this] derivation in 1962. Since this was the first nontrivial algorithm I had ever analyzed satisfactorily, it had a strong influence on the structure of these books. Ever since that day, the analysis of algorithms has in fact been one of the major themes of my life.”

− Knuth, TAOCP volume 3

Page 104: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

9

Mathematical analysis of exact cardinality count with linear probing

Theorem. Expected time and space cost is linear.

Proof. Follows from classic Knuth Theorem 6.4.K.

ptg999

From the Library of Robert Sedgewick

Q. Do the hash functions that we use uniformly and independently distribute keys in the table?

A. Not likely.

“ I first formulated [this] derivation in 1962. Since this was the first nontrivial algorithm I had ever analyzed satisfactorily, it had a strong influence on the structure of these books. Ever since that day, the analysis of algorithms has in fact been one of the major themes of my life.”

− Knuth, TAOCP volume 3

Page 105: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Page 106: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

Page 107: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args)

Driver to read N strings and count distinct values

Page 108: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){

Driver to read N strings and count distinct values

Page 109: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]);

Driver to read N strings and count distinct values

get problem size

Page 110: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N);

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

Page 111: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

Page 112: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

Page 113: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis();

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

Page 114: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0;

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

Page 115: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 116: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 117: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 118: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt483477

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 119: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 120: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 121: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

% java Hash 4000000 < log.07.f3.txt

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 122: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

% java Hash 4000000 < log.07.f3.txt883071

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 123: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

% java Hash 4000000 < log.07.f3.txt8830716.55 seconds

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 124: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

% java Hash 4000000 < log.07.f3.txt8830716.55 seconds

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 125: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

% java Hash 4000000 < log.07.f3.txt8830716.55 seconds

% java Hash 6000000 < log.07.f3.txt

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 126: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

% java Hash 4000000 < log.07.f3.txt8830716.55 seconds

% java Hash 6000000 < log.07.f3.txt1097944

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 127: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

% java Hash 4000000 < log.07.f3.txt8830716.55 seconds

% java Hash 6000000 < log.07.f3.txt10979449.49 seconds

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 128: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

% java Hash 4000000 < log.07.f3.txt8830716.55 seconds

% java Hash 6000000 < log.07.f3.txt10979449.49 seconds ✓

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 129: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

% java Hash 4000000 < log.07.f3.txt8830716.55 seconds

% java Hash 6000000 < log.07.f3.txt10979449.49 seconds ✓

% sort -u log.07.f3 | wc -l 1097944

sort-based method takes about 3 minutes

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 130: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Q. Is hashing with linear probing effective?

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

% java Hash 4000000 < log.07.f3.txt8830716.55 seconds

% java Hash 6000000 < log.07.f3.txt10979449.49 seconds ✓

% sort -u log.07.f3 | wc -l 1097944

sort-based method takes about 3 minutes

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 131: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

10

Scientific validation of exact cardinality count with linear probing

Hypothesis. Time and space cost is linear for the hash functions we use and the data we have.

Q. Is hashing with linear probing effective?

A. Yes. Validated in countless applications for over half a century.

Quick experiment. Doubling the problem size should double the running time.

public static void main(String[] args){ int N = Integer.parseInt(args[0]); StringStream stream = new StringStream(N); long start = System.currentTimeMillis();

StdOut.println(count(stream));

long now = System.currentTimeMillis(); double time = (now - start) / 1000.0; StdOut.println(time + " seconds”);}

% java Hash 2000000 < log.07.f3.txt4834773.322 seconds

% java Hash 4000000 < log.07.f3.txt8830716.55 seconds

% java Hash 6000000 < log.07.f3.txt10979449.49 seconds ✓

% sort -u log.07.f3 | wc -l 1097944

sort-based method takes about 3 minutes

Driver to read N strings and count distinct values

get problem sizeinitialize input stream

get current time

print count

print elapsed time

Page 132: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

11

Complexity of exact cardinality count

Q. Does there exist an optimal algorithm for this problem?Upper bound

Lower bound

Page 133: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

11

Complexity of exact cardinality count

Q. Does there exist an optimal algorithm for this problem?

A. Depends on definition of “optimal”.

Upper bound

Lower bound

Page 134: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

11

Complexity of exact cardinality count

Q. Does there exist an optimal algorithm for this problem?

Guaranteed linear-time? NO. Linearithmic lower bound.

A. Depends on definition of “optimal”.

Upper bound

Lower bound

Page 135: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

11

Complexity of exact cardinality count

Q. Does there exist an optimal algorithm for this problem?

Guaranteed linear-time? NO. Linearithmic lower bound.

A. Depends on definition of “optimal”.

Guaranteed linearithmic? YES. Balanced BSTs or mergesort.

Upper bound

Lower bound

Page 136: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

11

Complexity of exact cardinality count

Q. Does there exist an optimal algorithm for this problem?

Guaranteed linear-time? NO. Linearithmic lower bound.

A. Depends on definition of “optimal”.

Linear-time with high probability assuming the existence of random bits? YES. Dynamic perfect hashing.

Guaranteed linearithmic? YES. Balanced BSTs or mergesort.

Upper bound

Lower bound

Page 137: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

11

Complexity of exact cardinality count

Q. Does there exist an optimal algorithm for this problem?

Guaranteed linear-time? NO. Linearithmic lower bound.

A. Depends on definition of “optimal”.

Linear-time with high probability assuming the existence of random bits? YES. Dynamic perfect hashing. Dietzfelbinger, Karlin, Mehlhorn, Meyer auf der Heide, Rohnert, and Tarjan

Dynamic Perfect Hashing: Upper and Lower Bounds

SICOMP 1994.

Guaranteed linearithmic? YES. Balanced BSTs or mergesort.

Upper bound

Lower bound

Page 138: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

11

Complexity of exact cardinality count

Q. Does there exist an optimal algorithm for this problem?

Guaranteed linear-time? NO. Linearithmic lower bound.

A. Depends on definition of “optimal”.

Linear-time with high probability assuming the existence of random bits? YES. Dynamic perfect hashing.

Linear with a small constant factor in practical situations?

Dietzfelbinger, Karlin, Mehlhorn, Meyer auf der Heide, Rohnert, and TarjanDynamic Perfect Hashing: Upper and Lower Bounds

SICOMP 1994.

Guaranteed linearithmic? YES. Balanced BSTs or mergesort.

Upper bound

Lower bound

Page 139: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

11

Complexity of exact cardinality count

Q. Does there exist an optimal algorithm for this problem?

Guaranteed linear-time? NO. Linearithmic lower bound.

A. Depends on definition of “optimal”.

Linear-time with high probability assuming the existence of random bits? YES. Dynamic perfect hashing.

Linear with a small constant factor in practical situations? YES. Hashing with linear probing.

Dietzfelbinger, Karlin, Mehlhorn, Meyer auf der Heide, Rohnert, and TarjanDynamic Perfect Hashing: Upper and Lower Bounds

SICOMP 1994.

Guaranteed linearithmic? YES. Balanced BSTs or mergesort.

Upper bound

Lower bound

Page 140: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

11

Complexity of exact cardinality count

Q. Does there exist an optimal algorithm for this problem?

Guaranteed linear-time? NO. Linearithmic lower bound.

A. Depends on definition of “optimal”.

Linear-time with high probability assuming the existence of random bits? YES. Dynamic perfect hashing.

Linear with a small constant factor in practical situations? YES. Hashing with linear probing. M. Mitzenmacher and S. Vadhan

Why Simple Hash Functions Work: Exploiting the Entropy in a Data Stream. SODA 2008.

Dietzfelbinger, Karlin, Mehlhorn, Meyer auf der Heide, Rohnert, and TarjanDynamic Perfect Hashing: Upper and Lower Bounds

SICOMP 1994.

Guaranteed linearithmic? YES. Balanced BSTs or mergesort.

Upper bound

Lower bound

Page 141: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

11

Complexity of exact cardinality count

Q. Does there exist an optimal algorithm for this problem?

Guaranteed linear-time? NO. Linearithmic lower bound.

A. Depends on definition of “optimal”.

Linear-time with high probability assuming the existence of random bits? YES. Dynamic perfect hashing.

Linear with a small constant factor in practical situations? YES. Hashing with linear probing. M. Mitzenmacher and S. Vadhan

Why Simple Hash Functions Work: Exploiting the Entropy in a Data Stream. SODA 2008.

Dietzfelbinger, Karlin, Mehlhorn, Meyer auf der Heide, Rohnert, and TarjanDynamic Perfect Hashing: Upper and Lower Bounds

SICOMP 1994.

Hypothesis. Hashing with linear probing is “optimal”.

Guaranteed linearithmic? YES. Balanced BSTs or mergesort.

Upper bound

Lower bound

Page 142: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

11

Complexity of exact cardinality count

Q. Does there exist an optimal algorithm for this problem?

Guaranteed linear-time? NO. Linearithmic lower bound.

A. Depends on definition of “optimal”.

Linear-time with high probability assuming the existence of random bits? YES. Dynamic perfect hashing.

Linear with a small constant factor in practical situations? YES. Hashing with linear probing. M. Mitzenmacher and S. Vadhan

Why Simple Hash Functions Work: Exploiting the Entropy in a Data Stream. SODA 2008.

Dietzfelbinger, Karlin, Mehlhorn, Meyer auf der Heide, Rohnert, and TarjanDynamic Perfect Hashing: Upper and Lower Bounds

SICOMP 1994.

Hypothesis. Hashing with linear probing is “optimal”.

Guaranteed linearithmic? YES. Balanced BSTs or mergesort.

Upper bound

Lower bound

but TSTs may give a sublinear algorithm

Page 143: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

12

Exact cardinality count requires linear space

Q. I can’t use a hash table. The stream is much too big to fit all values in memory. Now what?

Help!

Page 144: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

12

Exact cardinality count requires linear space

Q. I can’t use a hash table. The stream is much too big to fit all values in memory. Now what?

Help!

Page 145: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

12

Exact cardinality count requires linear space

Q. I can’t use a hash table. The stream is much too big to fit all values in memory. Now what?

A. Bad news: You cannot get an exact count.

Help!

Page 146: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

12

Exact cardinality count requires linear space

Q. I can’t use a hash table. The stream is much too big to fit all values in memory. Now what?

A. Bad news: You cannot get an exact count.

A. (Bloom, 1970) You can get an accurate estimate using a few bits per distinct value.

Help!

Page 147: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

12

Exact cardinality count requires linear space

Q. I can’t use a hash table. The stream is much too big to fit all values in memory. Now what?

A. Bad news: You cannot get an exact count.

A. (Bloom, 1970) You can get an accurate estimate using a few bits per distinct value.

Help!

A. Much better news: You can get an accurate estimate using only a handful of bits (stay tuned).

Page 148: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

OF

Cardinality Estimation

•Warmup: exact cardinality count •Probabilistic counting •Stochastic averaging •Refinements •Final frontier

Page 149: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

14

is a fundamental problem with many applications where memory is limited.

Cardinality estimation

Q. About how many different values appear in a given stream?

Page 150: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

14

is a fundamental problem with many applications where memory is limited.

Cardinality estimation

Q. About how many different values appear in a given stream?

Constraints

Page 151: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

14

is a fundamental problem with many applications where memory is limited.

Cardinality estimation

Q. About how many different values appear in a given stream?

Constraints• Make one pass through the stream.

Page 152: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

14

is a fundamental problem with many applications where memory is limited.

Cardinality estimation

Q. About how many different values appear in a given stream?

Constraints• Make one pass through the stream.• Use as few operations per value as possible

Page 153: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

14

is a fundamental problem with many applications where memory is limited.

Cardinality estimation

Q. About how many different values appear in a given stream?

Constraints• Make one pass through the stream.• Use as few operations per value as possible• Use as little memory as possible.

Page 154: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

14

is a fundamental problem with many applications where memory is limited.

Cardinality estimation

Q. About how many different values appear in a given stream?

Constraints• Make one pass through the stream.• Use as few operations per value as possible• Use as little memory as possible.• Produce as accurate an estimate as possible.

Page 155: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

typical applications

14

is a fundamental problem with many applications where memory is limited.

Cardinality estimation

Q. About how many different values appear in a given stream?

Constraints• Make one pass through the stream.• Use as few operations per value as possible• Use as little memory as possible.• Produce as accurate an estimate as possible.

Page 156: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

typical applications

14

is a fundamental problem with many applications where memory is limited.

Cardinality estimation

Q. About how many different values appear in a given stream?

Constraints• Make one pass through the stream.• Use as few operations per value as possible• Use as little memory as possible.• Produce as accurate an estimate as possible.

How many unique visitors to my website?

Page 157: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

typical applications

14

is a fundamental problem with many applications where memory is limited.

Cardinality estimation

Q. About how many different values appear in a given stream?

Constraints• Make one pass through the stream.• Use as few operations per value as possible• Use as little memory as possible.• Produce as accurate an estimate as possible.

How many unique visitors to my website?

How many different values for a database join?

Page 158: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

typical applications

14

is a fundamental problem with many applications where memory is limited.

Cardinality estimation

Q. About how many different values appear in a given stream?

Constraints• Make one pass through the stream.• Use as few operations per value as possible• Use as little memory as possible.• Produce as accurate an estimate as possible.

How many unique visitors to my website?

How many different websites visited by each customer? How many different values

for a database join?

Page 159: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

typical applications

14

is a fundamental problem with many applications where memory is limited.

Cardinality estimation

Q. About how many different values appear in a given stream?

Constraints• Make one pass through the stream.• Use as few operations per value as possible• Use as little memory as possible.• Produce as accurate an estimate as possible.

How many unique visitors to my website?

How many different websites visited by each customer? How many different values

for a database join?

Which sites are the most/least popular?

Page 160: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

typical applications

14

is a fundamental problem with many applications where memory is limited.

Cardinality estimation

Q. About how many different values appear in a given stream?

Constraints• Make one pass through the stream.• Use as few operations per value as possible• Use as little memory as possible.• Produce as accurate an estimate as possible.

How many unique visitors to my website?

How many different websites visited by each customer? How many different values

for a database join?

To fix ideas on scope: Think of billions of streams each having trillions of values.

Which sites are the most/least popular?

Page 161: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

15

Probabilistic counting with stochastic averaging (PCSA)

Page 162: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

15

Probabilistic counting with stochastic averaging (PCSA)

Flajolet and Martin, Probabilistic Counting Algorithms for Data Base Applications FOCS 1983, JCSS 1985.

Philippe Flajolet 1948-2011

Page 163: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

15

Probabilistic counting with stochastic averaging (PCSA)

Contributions

Flajolet and Martin, Probabilistic Counting Algorithms for Data Base Applications FOCS 1983, JCSS 1985.

Philippe Flajolet 1948-2011

Page 164: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

15

Probabilistic counting with stochastic averaging (PCSA)

Contributions• Introduced problem

Flajolet and Martin, Probabilistic Counting Algorithms for Data Base Applications FOCS 1983, JCSS 1985.

Philippe Flajolet 1948-2011

Page 165: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

15

Probabilistic counting with stochastic averaging (PCSA)

Contributions• Introduced problem• Idea of streaming algorithm

Flajolet and Martin, Probabilistic Counting Algorithms for Data Base Applications FOCS 1983, JCSS 1985.

Philippe Flajolet 1948-2011

Page 166: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

15

Probabilistic counting with stochastic averaging (PCSA)

Contributions• Introduced problem• Idea of streaming algorithm• Idea of “small” sketch of “big” data

Flajolet and Martin, Probabilistic Counting Algorithms for Data Base Applications FOCS 1983, JCSS 1985.

Philippe Flajolet 1948-2011

Page 167: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

15

Probabilistic counting with stochastic averaging (PCSA)

Contributions• Introduced problem• Idea of streaming algorithm• Idea of “small” sketch of “big” data• Detailed analysis that yields tight bounds on accuracy

Flajolet and Martin, Probabilistic Counting Algorithms for Data Base Applications FOCS 1983, JCSS 1985.

Philippe Flajolet 1948-2011

Page 168: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

15

Probabilistic counting with stochastic averaging (PCSA)

Contributions• Introduced problem• Idea of streaming algorithm• Idea of “small” sketch of “big” data• Detailed analysis that yields tight bounds on accuracy• Full validation of mathematical results with experimentation

Flajolet and Martin, Probabilistic Counting Algorithms for Data Base Applications FOCS 1983, JCSS 1985.

Philippe Flajolet 1948-2011

Page 169: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

15

Probabilistic counting with stochastic averaging (PCSA)

Contributions• Introduced problem• Idea of streaming algorithm• Idea of “small” sketch of “big” data• Detailed analysis that yields tight bounds on accuracy• Full validation of mathematical results with experimentation• Practical algorithm that has remained effective for decades

Flajolet and Martin, Probabilistic Counting Algorithms for Data Base Applications FOCS 1983, JCSS 1985.

Philippe Flajolet 1948-2011

Page 170: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

15

Probabilistic counting with stochastic averaging (PCSA)

Contributions• Introduced problem• Idea of streaming algorithm• Idea of “small” sketch of “big” data• Detailed analysis that yields tight bounds on accuracy• Full validation of mathematical results with experimentation• Practical algorithm that has remained effective for decades

Bottom line: Quintessential example of the effectiveness of scientific approach to algorithm design.

Flajolet and Martin, Probabilistic Counting Algorithms for Data Base Applications FOCS 1983, JCSS 1985.

Philippe Flajolet 1948-2011

Page 171: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

16

PCSA first step: Use hashing

Page 172: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

16

PCSA first step: Use hashing

Transform value to a “random” computer word.

Page 173: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

16

PCSA first step: Use hashing

Transform value to a “random” computer word.• Compute a hash function that transforms

data value into a 32- or 64-bit value.

Page 174: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

16

PCSA first step: Use hashing

Transform value to a “random” computer word.• Compute a hash function that transforms

data value into a 32- or 64-bit value.• Cardinality count is unaffected (with high probability).

Page 175: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

16

PCSA first step: Use hashing

Transform value to a “random” computer word.• Compute a hash function that transforms

data value into a 32- or 64-bit value.• Cardinality count is unaffected (with high probability).• Built-in capability in modern systems.

Page 176: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

16

PCSA first step: Use hashing

Transform value to a “random” computer word.• Compute a hash function that transforms

data value into a 32- or 64-bit value.• Cardinality count is unaffected (with high probability).• Built-in capability in modern systems.• Allows use of fast machine-code operations.

Page 177: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

16

PCSA first step: Use hashing

Transform value to a “random” computer word.• Compute a hash function that transforms

data value into a 32- or 64-bit value.• Cardinality count is unaffected (with high probability).• Built-in capability in modern systems.• Allows use of fast machine-code operations.

21st century: use 64 bits (quadrillions of values)20th century: use 32 bits (millions of values)

Page 178: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

16

PCSA first step: Use hashing

Transform value to a “random” computer word.• Compute a hash function that transforms

data value into a 32- or 64-bit value.• Cardinality count is unaffected (with high probability).• Built-in capability in modern systems.• Allows use of fast machine-code operations.

21st century: use 64 bits (quadrillions of values)20th century: use 32 bits (millions of values)

Example: Java

Page 179: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

16

PCSA first step: Use hashing

Transform value to a “random” computer word.• Compute a hash function that transforms

data value into a 32- or 64-bit value.• Cardinality count is unaffected (with high probability).• Built-in capability in modern systems.• Allows use of fast machine-code operations.

21st century: use 64 bits (quadrillions of values)20th century: use 32 bits (millions of values)

Example: Java

• All data types implement a hashCode() method (though we often override the default).

Page 180: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

16

PCSA first step: Use hashing

Transform value to a “random” computer word.• Compute a hash function that transforms

data value into a 32- or 64-bit value.• Cardinality count is unaffected (with high probability).• Built-in capability in modern systems.• Allows use of fast machine-code operations.

21st century: use 64 bits (quadrillions of values)20th century: use 32 bits (millions of values)

Example: Java

• All data types implement a hashCode() method (though we often override the default).

• String data type stores value (computed once).

Page 181: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

16

PCSA first step: Use hashing

Transform value to a “random” computer word.• Compute a hash function that transforms

data value into a 32- or 64-bit value.• Cardinality count is unaffected (with high probability).• Built-in capability in modern systems.• Allows use of fast machine-code operations.

21st century: use 64 bits (quadrillions of values)20th century: use 32 bits (millions of values)

String value = “gsearch.CS.Princeton.EDU” int x = value.hashCode();

current Java default is 32-bit int value

Example: Java

• All data types implement a hashCode() method (though we often override the default).

• String data type stores value (computed once).

Page 182: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

16

PCSA first step: Use hashing

Transform value to a “random” computer word.• Compute a hash function that transforms

data value into a 32- or 64-bit value.• Cardinality count is unaffected (with high probability).• Built-in capability in modern systems.• Allows use of fast machine-code operations.

21st century: use 64 bits (quadrillions of values)20th century: use 32 bits (millions of values)

String value = “gsearch.CS.Princeton.EDU” int x = value.hashCode();

current Java default is 32-bit int value

Bottom line: Do cardinality estimation on streams of (binary) integers.

Example: Java

• All data types implement a hashCode() method (though we often override the default).

• String data type stores value (computed once).

Page 183: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

00010000111001101000111010010011 00001001011011100000010010010111 00001001011011100000010010010111 00111000101001001011010101001100 00111000101001001011010101001100 01101001001000011100110100110011 00001000011101100110110010100101 00001001011011100000010010010111 00001001001011010110111101111110 00001001001011010110111101111110 01101001001000011100110100110011 01100000110010011101011001011111 01110111001110110010000010111111 01011001010110101100010110110100 00101101101111101111001110001100 01101001001000011100110100110011 01110110000011101101011001000101 00001001001011010110111101111110 00001001001011010110111101111110 00001001001011010110111101111110 01101001001000011100110100110011 01101001001000011100110100110011 01101001001000011100110100110011 01101001001000011100110100110011 01101001001000011100110100110011 01110111001110110010000010111111 01101001001000011100110100110011 00001000011101100110110010100101 00101101101111101111001110001100 01101001001000011100110100110011 00001000011101100110110010100101 01101001001000011100110100110011 01101001001000011100110100110011 01100001000111001001110010100000

16

PCSA first step: Use hashing

Transform value to a “random” computer word.• Compute a hash function that transforms

data value into a 32- or 64-bit value.• Cardinality count is unaffected (with high probability).• Built-in capability in modern systems.• Allows use of fast machine-code operations.

21st century: use 64 bits (quadrillions of values)20th century: use 32 bits (millions of values)

String value = “gsearch.CS.Princeton.EDU” int x = value.hashCode();

current Java default is 32-bit int value

Bottom line: Do cardinality estimation on streams of (binary) integers.

Example: Java

• All data types implement a hashCode() method (though we often override the default).

• String data type stores value (computed once).

Page 184: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

00010000111001101000111010010011 00001001011011100000010010010111 00001001011011100000010010010111 00111000101001001011010101001100 00111000101001001011010101001100 01101001001000011100110100110011 00001000011101100110110010100101 00001001011011100000010010010111 00001001001011010110111101111110 00001001001011010110111101111110 01101001001000011100110100110011 01100000110010011101011001011111 01110111001110110010000010111111 01011001010110101100010110110100 00101101101111101111001110001100 01101001001000011100110100110011 01110110000011101101011001000101 00001001001011010110111101111110 00001001001011010110111101111110 00001001001011010110111101111110 01101001001000011100110100110011 01101001001000011100110100110011 01101001001000011100110100110011 01101001001000011100110100110011 01101001001000011100110100110011 01110111001110110010000010111111 01101001001000011100110100110011 00001000011101100110110010100101 00101101101111101111001110001100 01101001001000011100110100110011 00001000011101100110110010100101 01101001001000011100110100110011 01101001001000011100110100110011 01100001000111001001110010100000

16

PCSA first step: Use hashing

Transform value to a “random” computer word.• Compute a hash function that transforms

data value into a 32- or 64-bit value.• Cardinality count is unaffected (with high probability).• Built-in capability in modern systems.• Allows use of fast machine-code operations.

21st century: use 64 bits (quadrillions of values)20th century: use 32 bits (millions of values)

String value = “gsearch.CS.Princeton.EDU” int x = value.hashCode();

current Java default is 32-bit int value

Bottom line: Do cardinality estimation on streams of (binary) integers.

Example: Java

• All data types implement a hashCode() method (though we often override the default).

• String data type stores value (computed once).

“Random” except for the fact that some values are equal.

Page 185: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

17

Initial hypothesis

Page 186: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

17

Initial hypothesis

Hypothesis. Uniform hashing assumption is reasonable in this context.

Page 187: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

17

Initial hypothesis

Hypothesis. Uniform hashing assumption is reasonable in this context.

Implication. Need to run experiments to validate any hypotheses about performance.

Page 188: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

17

Initial hypothesis

No problem!

Hypothesis. Uniform hashing assumption is reasonable in this context.

Implication. Need to run experiments to validate any hypotheses about performance.

Page 189: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

17

Initial hypothesis

No problem!

• AofA is a scientific endeavor (we always validate hypotheses).

Hypothesis. Uniform hashing assumption is reasonable in this context.

Implication. Need to run experiments to validate any hypotheses about performance.

Page 190: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

17

Initial hypothesis

No problem!

• AofA is a scientific endeavor (we always validate hypotheses).

• End goal is development of algorithms that are useful in practice.

Hypothesis. Uniform hashing assumption is reasonable in this context.

Implication. Need to run experiments to validate any hypotheses about performance.

Page 191: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

17

Initial hypothesis

No problem!

• AofA is a scientific endeavor (we always validate hypotheses).

• End goal is development of algorithms that are useful in practice.

• It is the responsibility of the designer to validate utility before claiming it.

Hypothesis. Uniform hashing assumption is reasonable in this context.

Implication. Need to run experiments to validate any hypotheses about performance.

Page 192: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

17

Initial hypothesis

No problem!

• AofA is a scientific endeavor (we always validate hypotheses).

• End goal is development of algorithms that are useful in practice.

• It is the responsibility of the designer to validate utility before claiming it.

• After decades of experience, discovering a performance problem due toa bad hash function would be a significant research result.

Hypothesis. Uniform hashing assumption is reasonable in this context.

Implication. Need to run experiments to validate any hypotheses about performance.

Page 193: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

17

Initial hypothesis

No problem!

• AofA is a scientific endeavor (we always validate hypotheses).

• End goal is development of algorithms that are useful in practice.

• It is the responsibility of the designer to validate utility before claiming it.

• After decades of experience, discovering a performance problem due toa bad hash function would be a significant research result.

Hypothesis. Uniform hashing assumption is reasonable in this context.

Implication. Need to run experiments to validate any hypotheses about performance.

Unspoken bedrock principle of AofA. Experimenting to validate hypotheses is WHAT WE DO!

Page 194: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

17

Initial hypothesis

No problem!

• AofA is a scientific endeavor (we always validate hypotheses).

• End goal is development of algorithms that are useful in practice.

• It is the responsibility of the designer to validate utility before claiming it.

• After decades of experience, discovering a performance problem due toa bad hash function would be a significant research result.

Hypothesis. Uniform hashing assumption is reasonable in this context.

Implication. Need to run experiments to validate any hypotheses about performance.

Unspoken bedrock principle of AofA. Experimenting to validate hypotheses is WHAT WE DO!

Page 195: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Page 196: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. p (x) is the number of 1s in the binary representation of x.

Page 197: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. p (x) is the number of 1s in the binary representation of x.

Page 198: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x. position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

Page 199: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

Page 200: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

Page 201: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

Page 202: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

Page 203: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

Page 204: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

Bit-whacking magic: R(x) is easy to compute.

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

Page 205: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

Bit-whacking magic: R(x) is easy to compute.

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 x

Page 206: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

Bit-whacking magic: R(x) is easy to compute.

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 x1 0 0 1 0 1 1 0 1 0 1 0 0 0 0 0 ~x

Page 207: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

Bit-whacking magic: R(x) is easy to compute.

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 x1 0 0 1 0 1 1 0 1 0 1 0 0 0 0 0 ~x0 1 1 0 1 0 0 1 0 1 1 0 0 0 0 0 x + 1

Page 208: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

Bit-whacking magic: R(x) is easy to compute.

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 x1 0 0 1 0 1 1 0 1 0 1 0 0 0 0 0 ~x0 1 1 0 1 0 0 1 0 1 1 0 0 0 0 0 x + 10 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 ~x & (x + 1)

Page 209: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

Bit-whacking magic: R(x) is easy to compute.

3 instructions on a typical computer

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 x1 0 0 1 0 1 1 0 1 0 1 0 0 0 0 0 ~x0 1 1 0 1 0 0 1 0 1 1 0 0 0 0 0 x + 10 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 ~x & (x + 1)

Page 210: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

Bit-whacking magic: R(x) is easy to compute.

3 instructions on a typical computer

Exercise: Compute p (x) as easily.

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 x1 0 0 1 0 1 1 0 1 0 1 0 0 0 0 0 ~x0 1 1 0 1 0 0 1 0 1 1 0 0 0 0 0 x + 10 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 ~x & (x + 1)

Page 211: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

Bit-whacking magic: R(x) is easy to compute.

3 instructions on a typical computer

Exercise: Compute p (x) as easily.

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 x1 0 0 1 0 1 1 0 1 0 1 0 0 0 0 0 ~x0 1 1 0 1 0 0 1 0 1 1 0 0 0 0 0 x + 10 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 ~x & (x + 1)

Beeler, Gosper, and SchroeppelHAKMEM item 169, MIT AI Laboratory AIM 239, 1972

http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html

Page 212: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

Bit-whacking magic: R(x) is easy to compute.

3 instructions on a typical computer

Exercise: Compute p (x) as easily.

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 x1 0 0 1 0 1 1 0 1 0 1 0 0 0 0 0 ~x0 1 1 0 1 0 0 1 0 1 1 0 0 0 0 0 x + 10 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 ~x & (x + 1)

Note: r (x ) = p (R(x ) − 1).

Beeler, Gosper, and SchroeppelHAKMEM item 169, MIT AI Laboratory AIM 239, 1972

http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html

Page 213: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

Bit-whacking magic: R(x) is easy to compute.

3 instructions on a typical computer

Exercise: Compute p (x) as easily.

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 x1 0 0 1 0 1 1 0 1 0 1 0 0 0 0 0 ~x0 1 1 0 1 0 0 1 0 1 1 0 0 0 0 0 x + 10 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 ~x & (x + 1)

Note: r (x ) = p (R(x ) − 1).

Beeler, Gosper, and SchroeppelHAKMEM item 169, MIT AI Laboratory AIM 239, 1972

http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html

see also Knuth volume 4A

Page 214: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

18

Probabilistic counting starting point: three integer functions

Definition. r (x) is the number of trailing 1s in the binary representation of x.

Definition. R(x) = 2r (x)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 p (x) r(x) R (x) R (x)21 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 12 1 2 1 0

1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 8 0 1 1

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 10 5 32 1 0 0 0 0 0

Bit-whacking magic: R(x) is easy to compute.

3 instructions on a typical computer

Exercise: Compute p (x) as easily.

position of rightmost 0

Definition. p (x) is the number of 1s in the binary representation of x.

0 1 1 0 1 0 0 1 0 1 0 1 1 1 1 1 x1 0 0 1 0 1 1 0 1 0 1 0 0 0 0 0 ~x0 1 1 0 1 0 0 1 0 1 1 0 0 0 0 0 x + 10 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 ~x & (x + 1)

Note: r (x ) = p (R(x ) − 1).

Bottom line: p (x ), r (x ), and R(x ) all can be computed with just a few machine instructions.

Beeler, Gosper, and SchroeppelHAKMEM item 169, MIT AI Laboratory AIM 239, 1972

http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html

see also Knuth volume 4A

Page 215: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Page 216: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …

Page 217: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …• For each xN in the stream, update sketch by bitwise or with R(xN).

Page 218: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …• For each xN in the stream, update sketch by bitwise or with R(xN).• Use position of rightmost 0 (with slight correction factor) to estimate lg N.

Page 219: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …• For each xN in the stream, update sketch by bitwise or with R(xN).• Use position of rightmost 0 (with slight correction factor) to estimate lg N.

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

sketch 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1typical sketch

N = 106

Page 220: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …• For each xN in the stream, update sketch by bitwise or with R(xN).• Use position of rightmost 0 (with slight correction factor) to estimate lg N.

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

sketch 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

xN 0 0 1 1 0 1 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1typical sketch N = 106

Page 221: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …• For each xN in the stream, update sketch by bitwise or with R(xN).• Use position of rightmost 0 (with slight correction factor) to estimate lg N.

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

sketch 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

xN 0 0 1 1 0 1 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1

R(xN) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

typical sketch N = 106

Page 222: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …• For each xN in the stream, update sketch by bitwise or with R(xN).• Use position of rightmost 0 (with slight correction factor) to estimate lg N.

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

sketch 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

xN 0 0 1 1 0 1 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1

R(xN) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

sketch | R(xN) 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

typical sketch N = 106

Page 223: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …• For each xN in the stream, update sketch by bitwise or with R(xN).• Use position of rightmost 0 (with slight correction factor) to estimate lg N.

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

sketch 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

xN 0 0 1 1 0 1 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1

R(xN) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

sketch | R(xN) 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

typical sketch N = 106 R(x) = 2k

with probability 1/ 2k

Page 224: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …• For each xN in the stream, update sketch by bitwise or with R(xN).• Use position of rightmost 0 (with slight correction factor) to estimate lg N.

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

sketch 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

xN 0 0 1 1 0 1 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1

R(xN) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

sketch | R(xN) 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

typical sketch N = 106

leading bits almost surely 0

R(x) = 2k with probability

1/ 2k

Page 225: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …• For each xN in the stream, update sketch by bitwise or with R(xN).• Use position of rightmost 0 (with slight correction factor) to estimate lg N.

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

sketch 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

xN 0 0 1 1 0 1 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1

R(xN) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

sketch | R(xN) 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

typical sketch N = 106

leading bits almost surely 0 trailing bits almost surely 1

R(x) = 2k with probability

1/ 2k

Page 226: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …• For each xN in the stream, update sketch by bitwise or with R(xN).• Use position of rightmost 0 (with slight correction factor) to estimate lg N.

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

sketch 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

xN 0 0 1 1 0 1 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1

R(xN) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

sketch | R(xN) 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

typical sketch N = 106

leading bits almost surely 0 trailing bits almost surely 1

estimate of lg N

R(x) = 2k with probability

1/ 2k

Rough estimate of lgN is r (sketch).

Page 227: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …• For each xN in the stream, update sketch by bitwise or with R(xN).• Use position of rightmost 0 (with slight correction factor) to estimate lg N.

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

sketch 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

xN 0 0 1 1 0 1 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1

R(xN) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

sketch | R(xN) 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

typical sketch N = 106

leading bits almost surely 0 trailing bits almost surely 1

estimate of lg N

R(x) = 2k with probability

1/ 2k

Rough estimate of lgN is r (sketch).

Rough estimate of N is R(sketch).

Page 228: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

19

Probabilistic counting (Flajolet and Martin, 1983)

Maintain a single-word sketch that summarizes a data stream x0, x1, …, xN, …• For each xN in the stream, update sketch by bitwise or with R(xN).• Use position of rightmost 0 (with slight correction factor) to estimate lg N.

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

sketch 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

xN 0 0 1 1 0 1 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1

R(xN) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

sketch | R(xN) 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

typical sketch N = 106

leading bits almost surely 0 trailing bits almost surely 1

estimate of lg N

R(x) = 2k with probability

1/ 2k

Rough estimate of lgN is r (sketch).

correction factor needed (stay tuned) Rough estimate of N is R(sketch).

Page 229: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

20

Probabilistic counting trace

Page 230: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

20

Probabilistic counting trace

x r(x) R(x) sketch

01100010011000111010011110111011 2 100 00000000000000000000000000000100

Page 231: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

20

Probabilistic counting trace

x r(x) R(x) sketch

01100010011000111010011110111011 2 100 00000000000000000000000000000100

01100111001000110001111100000101 1 10 00000000000000000000000000000110

Page 232: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

20

Probabilistic counting trace

x r(x) R(x) sketch

01100010011000111010011110111011 2 100 00000000000000000000000000000100

01100111001000110001111100000101 1 10 00000000000000000000000000000110

00010001000111000110110110110011 2 100 00000000000000000000000000000110

Page 233: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

20

Probabilistic counting trace

x r(x) R(x) sketch

01100010011000111010011110111011 2 100 00000000000000000000000000000100

01100111001000110001111100000101 1 10 00000000000000000000000000000110

00010001000111000110110110110011 2 100 00000000000000000000000000000110

01000100011101110000000111011111 5 100000 00000000000000000000000000100110

Page 234: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

20

Probabilistic counting trace

x r(x) R(x) sketch

01100010011000111010011110111011 2 100 00000000000000000000000000000100

01100111001000110001111100000101 1 10 00000000000000000000000000000110

00010001000111000110110110110011 2 100 00000000000000000000000000000110

01000100011101110000000111011111 5 100000 0000000000000000000000000010011001101000001011000101110001000100 0 1 00000000000000000000000000100111

Page 235: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

20

Probabilistic counting trace

x r(x) R(x) sketch

01100010011000111010011110111011 2 100 00000000000000000000000000000100

01100111001000110001111100000101 1 10 00000000000000000000000000000110

00010001000111000110110110110011 2 100 00000000000000000000000000000110

01000100011101110000000111011111 5 100000 0000000000000000000000000010011001101000001011000101110001000100 0 1 00000000000000000000000000100111

00110111101100000000101001010101 1 10 00000000000000000000000000100111

Page 236: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

20

Probabilistic counting trace

x r(x) R(x) sketch

01100010011000111010011110111011 2 100 00000000000000000000000000000100

01100111001000110001111100000101 1 10 00000000000000000000000000000110

00010001000111000110110110110011 2 100 00000000000000000000000000000110

01000100011101110000000111011111 5 100000 0000000000000000000000000010011001101000001011000101110001000100 0 1 00000000000000000000000000100111

00110111101100000000101001010101 1 10 0000000000000000000000000010011100110100011000111010101111111100 0 1 00000000000000000000000000100111

Page 237: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

20

Probabilistic counting trace

x r(x) R(x) sketch

01100010011000111010011110111011 2 100 00000000000000000000000000000100

01100111001000110001111100000101 1 10 00000000000000000000000000000110

00010001000111000110110110110011 2 100 00000000000000000000000000000110

01000100011101110000000111011111 5 100000 0000000000000000000000000010011001101000001011000101110001000100 0 1 00000000000000000000000000100111

00110111101100000000101001010101 1 10 0000000000000000000000000010011100110100011000111010101111111100 0 1 00000000000000000000000000100111

00011000010000100001011100110111 3 1000 00000000000000000000000000101111

Page 238: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

20

Probabilistic counting trace

x r(x) R(x) sketch

01100010011000111010011110111011 2 100 00000000000000000000000000000100

01100111001000110001111100000101 1 10 00000000000000000000000000000110

00010001000111000110110110110011 2 100 00000000000000000000000000000110

01000100011101110000000111011111 5 100000 0000000000000000000000000010011001101000001011000101110001000100 0 1 00000000000000000000000000100111

00110111101100000000101001010101 1 10 0000000000000000000000000010011100110100011000111010101111111100 0 1 00000000000000000000000000100111

00011000010000100001011100110111 3 1000 00000000000000000000000000101111

00011001100110011110010000111111 6 1000000 00000000000000000000000001101111

Page 239: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

20

Probabilistic counting trace

x r(x) R(x) sketch

01100010011000111010011110111011 2 100 00000000000000000000000000000100

01100111001000110001111100000101 1 10 00000000000000000000000000000110

00010001000111000110110110110011 2 100 00000000000000000000000000000110

01000100011101110000000111011111 5 100000 0000000000000000000000000010011001101000001011000101110001000100 0 1 00000000000000000000000000100111

00110111101100000000101001010101 1 10 0000000000000000000000000010011100110100011000111010101111111100 0 1 00000000000000000000000000100111

00011000010000100001011100110111 3 1000 00000000000000000000000000101111

00011001100110011110010000111111 6 1000000 0000000000000000000000000110111101000101110001001010110011111100 0 1 00000000000000000000000001101111

Page 240: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

20

Probabilistic counting trace

x r(x) R(x) sketch

01100010011000111010011110111011 2 100 00000000000000000000000000000100

01100111001000110001111100000101 1 10 00000000000000000000000000000110

00010001000111000110110110110011 2 100 00000000000000000000000000000110

01000100011101110000000111011111 5 100000 0000000000000000000000000010011001101000001011000101110001000100 0 1 00000000000000000000000000100111

00110111101100000000101001010101 1 10 0000000000000000000000000010011100110100011000111010101111111100 0 1 00000000000000000000000000100111

00011000010000100001011100110111 3 1000 00000000000000000000000000101111

00011001100110011110010000111111 6 1000000 0000000000000000000000000110111101000101110001001010110011111100 0 1 00000000000000000000000001101111

R(sketch) = 100002 = 16

Page 241: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

21

Probabilistic counting (Flajolet and Martin, 1983)

public long R(long x) { return ~x & (x+1); }

public long estimate(Iterable<String> stream) { long sketch; for (s : stream) sketch = sketch | R(s.hashCode()); return R(sketch); }

Page 242: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

21

Probabilistic counting (Flajolet and Martin, 1983)

public long R(long x) { return ~x & (x+1); }

public long estimate(Iterable<String> stream) { long sketch; for (s : stream) sketch = sketch | R(s.hashCode()); return R(sketch); }

Maintain a sketch of the data

Page 243: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

21

Probabilistic counting (Flajolet and Martin, 1983)

public long R(long x) { return ~x & (x+1); }

public long estimate(Iterable<String> stream) { long sketch; for (s : stream) sketch = sketch | R(s.hashCode()); return R(sketch); }

Maintain a sketch of the data• A single word

Page 244: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

21

Probabilistic counting (Flajolet and Martin, 1983)

public long R(long x) { return ~x & (x+1); }

public long estimate(Iterable<String> stream) { long sketch; for (s : stream) sketch = sketch | R(s.hashCode()); return R(sketch); }

Maintain a sketch of the data• A single word• OR of all values of R(x) in the stream

Page 245: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

21

Probabilistic counting (Flajolet and Martin, 1983)

public long R(long x) { return ~x & (x+1); }

public long estimate(Iterable<String> stream) { long sketch; for (s : stream) sketch = sketch | R(s.hashCode()); return R(sketch); }

Maintain a sketch of the data• A single word• OR of all values of R(x) in the stream• Return smallest value not seen

Page 246: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

21

Probabilistic counting (Flajolet and Martin, 1983)

public long R(long x) { return ~x & (x+1); }

public long estimate(Iterable<String> stream) { long sketch; for (s : stream) sketch = sketch | R(s.hashCode()); return R(sketch); }

Early example of “a simple algorithm whose analysis isn’t”

Maintain a sketch of the data• A single word• OR of all values of R(x) in the stream• Return smallest value not seen

Page 247: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

21

Probabilistic counting (Flajolet and Martin, 1983)

public long R(long x) { return ~x & (x+1); }

public long estimate(Iterable<String> stream) { long sketch; for (s : stream) sketch = sketch | R(s.hashCode()); return R(sketch); }

Early example of “a simple algorithm whose analysis isn’t”

Maintain a sketch of the data• A single word• OR of all values of R(x) in the stream• Return smallest value not seen

Q. (Martin) Estimate seems a bit low. How much?

Page 248: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

21

Probabilistic counting (Flajolet and Martin, 1983)

public long R(long x) { return ~x & (x+1); }

public long estimate(Iterable<String> stream) { long sketch; for (s : stream) sketch = sketch | R(s.hashCode()); return R(sketch); }

Early example of “a simple algorithm whose analysis isn’t”

Maintain a sketch of the data• A single word• OR of all values of R(x) in the stream• Return smallest value not seen

Q. (Martin) Estimate seems a bit low. How much?

A. (unsatisfying) Obtain correction factor empirically.

Page 249: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

21

Probabilistic counting (Flajolet and Martin, 1983)

public long R(long x) { return ~x & (x+1); }

public long estimate(Iterable<String> stream) { long sketch; for (s : stream) sketch = sketch | R(s.hashCode()); return R(sketch); }

Early example of “a simple algorithm whose analysis isn’t”

Maintain a sketch of the data• A single word• OR of all values of R(x) in the stream• Return smallest value not seen

Q. (Martin) Estimate seems a bit low. How much?

A. (unsatisfying) Obtain correction factor empirically.

A. (Flajolet) Without the analysis, there is no algorithm!

/.77351;with correction for bias

Page 250: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

22

Mathematical analysis of probabilistic counting

Theorem. The expected number of trailing 1s in the PC sketch is

and P is an oscillating function of lg N of very small amplitude.

lg(�N) + P(lgN) + o(1) where 𝜙 ≐�.77351

Page 251: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

22

Mathematical analysis of probabilistic counting

Theorem. The expected number of trailing 1s in the PC sketch is

and P is an oscillating function of lg N of very small amplitude.

Proof (omitted).

lg(�N) + P(lgN) + o(1) where 𝜙 ≐�.77351

Page 252: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

22

Mathematical analysis of probabilistic counting

Theorem. The expected number of trailing 1s in the PC sketch is

and P is an oscillating function of lg N of very small amplitude.

Proof (omitted).

1980s: Flajolet tour de force

lg(�N) + P(lgN) + o(1) where 𝜙 ≐�.77351

Page 253: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

22

Mathematical analysis of probabilistic counting

Theorem. The expected number of trailing 1s in the PC sketch is

and P is an oscillating function of lg N of very small amplitude.

Proof (omitted).

1980s: Flajolet tour de force

1990s: trie parameter

lg(�N) + P(lgN) + o(1) where 𝜙 ≐�.77351

Page 254: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

22

Mathematical analysis of probabilistic counting

Theorem. The expected number of trailing 1s in the PC sketch is

and P is an oscillating function of lg N of very small amplitude.

Proof (omitted).

1980s: Flajolet tour de force

1990s: trie parameter

lg(�N) + P(lgN) + o(1) where 𝜙 ≐�.77351

Page 255: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

22

Mathematical analysis of probabilistic counting

Theorem. The expected number of trailing 1s in the PC sketch is

and P is an oscillating function of lg N of very small amplitude.

Proof (omitted).

1980s: Flajolet tour de force

1990s: trie parameter

lg(�N) + P(lgN) + o(1) where 𝜙 ≐�.77351

highest null left of

right spine

Page 256: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

22

Mathematical analysis of probabilistic counting

Theorem. The expected number of trailing 1s in the PC sketch is

and P is an oscillating function of lg N of very small amplitude.

Proof (omitted).

1980s: Flajolet tour de force

1990s: trie parameter

lg(�N) + P(lgN) + o(1) where 𝜙 ≐�.77351

Kirschenhofer, Prodinger, and Szpankowski

Analysis of a splitting process arising in probabilistic counting and other related algorithms, ICALP 1992.

highest null left of

right spine

trailing 1s in sketch

Page 257: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

22

Mathematical analysis of probabilistic counting

Theorem. The expected number of trailing 1s in the PC sketch is

and P is an oscillating function of lg N of very small amplitude.

Proof (omitted).

1980s: Flajolet tour de force

1990s: trie parameter

21st century: standard AC

lg(�N) + P(lgN) + o(1) where 𝜙 ≐�.77351

Kirschenhofer, Prodinger, and Szpankowski

Analysis of a splitting process arising in probabilistic counting and other related algorithms, ICALP 1992.

highest null left of

right spine

trailing 1s in sketch

stay tuned for Szpankowski talk

Jacquet and Szpankowski

Analytical depoissonization and its applications, TCS 1998.

Page 258: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

22

Mathematical analysis of probabilistic counting

Theorem. The expected number of trailing 1s in the PC sketch is

and P is an oscillating function of lg N of very small amplitude.

Proof (omitted).

1980s: Flajolet tour de force

1990s: trie parameter

21st century: standard AC

In other words. In PC code, R(sketch)/.77351 is an unbiased statistical estimator of N.

lg(�N) + P(lgN) + o(1) where 𝜙 ≐�.77351

Kirschenhofer, Prodinger, and Szpankowski

Analysis of a splitting process arising in probabilistic counting and other related algorithms, ICALP 1992.

highest null left of

right spine

trailing 1s in sketch

stay tuned for Szpankowski talk

Jacquet and Szpankowski

Analytical depoissonization and its applications, TCS 1998.

Page 259: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

23

Validation of probabilistic counting

Hypothesis. Expected value returned is N for random values from a large range.

Page 260: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

23

Validation of probabilistic counting

Quick experiment. 100,000 31-bit random values (20 trials)

Hypothesis. Expected value returned is N for random values from a large range.

Page 261: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

23

Validation of probabilistic counting

Quick experiment. 100,000 31-bit random values (20 trials)

Hypothesis. Expected value returned is N for random values from a large range.

Page 262: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

23

Validation of probabilistic counting

Flajolet and Martin: Result is “typically one binary order of magnitude off.”

Quick experiment. 100,000 31-bit random values (20 trials)

Hypothesis. Expected value returned is N for random values from a large range.

Page 263: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

23

Validation of probabilistic counting

Flajolet and Martin: Result is “typically one binary order of magnitude off.”

Of course! (Always returns a power of 2 divided by .77351.)

Quick experiment. 100,000 31-bit random values (20 trials)

Hypothesis. Expected value returned is N for random values from a large range.

Page 264: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

23

Validation of probabilistic counting

Flajolet and Martin: Result is “typically one binary order of magnitude off.”

Of course! (Always returns a power of 2 divided by .77351.)

Quick experiment. 100,000 31-bit random values (20 trials)

16384/.77351 = 21181

32768/.77351 = 42362

65536/.77351 = 84725

Hypothesis. Expected value returned is N for random values from a large range.

Page 265: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

23

Validation of probabilistic counting

Flajolet and Martin: Result is “typically one binary order of magnitude off.”

Of course! (Always returns a power of 2 divided by .77351.)

Quick experiment. 100,000 31-bit random values (20 trials)

16384/.77351 = 21181

32768/.77351 = 42362

65536/.77351 = 84725

Need to incorporate more experiments for more accuracy.

Hypothesis. Expected value returned is N for random values from a large range.

Page 266: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

OF

Cardinality Estimation

•Rules of the game •Probabilistic counting •Stochastic averaging •Refinements •Final frontier

Page 267: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

25

Stochastic averaging

Goal: Perform M independent PC experiments and average results.

Page 268: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

25

Stochastic averaging

Goal: Perform M independent PC experiments and average results.

Alternative 1: M independent hash functions? No, too expensive.

Page 269: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

25

Stochastic averaging

Goal: Perform M independent PC experiments and average results.

Alternative 1: M independent hash functions? No, too expensive.

Alternative 2: M-way alternation? No, bad results for certain inputs.

Page 270: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

25

Stochastic averaging

Goal: Perform M independent PC experiments and average results.

Alternative 1: M independent hash functions? No, too expensive.

Alternative 2: M-way alternation? No, bad results for certain inputs.

01 02 03 04 01 02 03 04

01 01

02 02

03 03

04 04

01 02 03 04

01

02

03

04

Page 271: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

25

Stochastic averaging

Goal: Perform M independent PC experiments and average results.

Alternative 3: Stochastic averaging

Alternative 1: M independent hash functions? No, too expensive.

Alternative 2: M-way alternation? No, bad results for certain inputs.

01 02 03 04 01 02 03 04

01 01

02 02

03 03

04 04

01 02 03 04

01

02

03

04

Page 272: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

25

Stochastic averaging

Goal: Perform M independent PC experiments and average results.

Alternative 3: Stochastic averaging• Use second hash to divide stream into 2m independent streams

Alternative 1: M independent hash functions? No, too expensive.

Alternative 2: M-way alternation? No, bad results for certain inputs.

01 02 03 04 01 02 03 04

01 01

02 02

03 03

04 04

01 02 03 04

01

02

03

04

Page 273: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

25

Stochastic averaging

Goal: Perform M independent PC experiments and average results.

Alternative 3: Stochastic averaging• Use second hash to divide stream into 2m independent streams

Alternative 1: M independent hash functions? No, too expensive.

Alternative 2: M-way alternation? No, bad results for certain inputs.

01 02 03 04 01 02 03 04

01 01

02 02

03 03

04 04

01 02 03 04

01

02

03

04

10 11 39 21

09 07 07

11

23 22 22

31

11 09 07 23 31 07 22 2221

39

10 11

Page 274: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

25

Stochastic averaging

Goal: Perform M independent PC experiments and average results.

Alternative 3: Stochastic averaging• Use second hash to divide stream into 2m independent streams

key point: equal values all go to the same stream

Alternative 1: M independent hash functions? No, too expensive.

Alternative 2: M-way alternation? No, bad results for certain inputs.

01 02 03 04 01 02 03 04

01 01

02 02

03 03

04 04

01 02 03 04

01

02

03

04

10 11 39 21

09 07 07

11

23 22 22

31

11 09 07 23 31 07 22 2221

39

10 11

Page 275: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

25

Stochastic averaging

Goal: Perform M independent PC experiments and average results.

Alternative 3: Stochastic averaging• Use second hash to divide stream into 2m independent streams• Use PC on each stream, yielding 2m sketches .

key point: equal values all go to the same stream

Alternative 1: M independent hash functions? No, too expensive.

Alternative 2: M-way alternation? No, bad results for certain inputs.

01 02 03 04 01 02 03 04

01 01

02 02

03 03

04 04

01 02 03 04

01

02

03

04

10 11 39 21

09 07 07

11

23 22 22

31

11 09 07 23 31 07 22 2221

39

10 11

Page 276: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

25

Stochastic averaging

Goal: Perform M independent PC experiments and average results.

Alternative 3: Stochastic averaging• Use second hash to divide stream into 2m independent streams• Use PC on each stream, yielding 2m sketches .• Compute mean = average number of trailing bits in the sketches.

key point: equal values all go to the same stream

Alternative 1: M independent hash functions? No, too expensive.

Alternative 2: M-way alternation? No, bad results for certain inputs.

01 02 03 04 01 02 03 04

01 01

02 02

03 03

04 04

01 02 03 04

01

02

03

04

10 11 39 21

09 07 07

11

23 22 22

31

11 09 07 23 31 07 22 2221

39

10 11

Page 277: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

25

Stochastic averaging

Goal: Perform M independent PC experiments and average results.

Alternative 3: Stochastic averaging• Use second hash to divide stream into 2m independent streams• Use PC on each stream, yielding 2m sketches .• Compute mean = average number of trailing bits in the sketches.• Return 2mean/.77531.

key point: equal values all go to the same stream

Alternative 1: M independent hash functions? No, too expensive.

Alternative 2: M-way alternation? No, bad results for certain inputs.

01 02 03 04 01 02 03 04

01 01

02 02

03 03

04 04

01 02 03 04

01

02

03

04

10 11 39 21

09 07 07

11

23 22 22

31

11 09 07 23 31 07 22 2221

39

10 11

Page 278: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

Page 279: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

M = 4use initial m bits for second hash

Page 280: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

0001111100000101 10 0000000000000010 0000000000000000 0000000000000100 0000000000000000

M = 4use initial m bits for second hash

Page 281: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

0001111100000101 10 0000000000000010 0000000000000000 0000000000000100 0000000000000000

0110110110110011 100 0000000000000010 0000000000000100 0000000000000100 0000000000000000

M = 4use initial m bits for second hash

Page 282: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

0001111100000101 10 0000000000000010 0000000000000000 0000000000000100 0000000000000000

0110110110110011 100 0000000000000010 0000000000000100 0000000000000100 0000000000000000

0000000111011111 100000 0000000000100010 0000000000000100 0000000000000100 0000000000000000

M = 4use initial m bits for second hash

Page 283: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

0001111100000101 10 0000000000000010 0000000000000000 0000000000000100 0000000000000000

0110110110110011 100 0000000000000010 0000000000000100 0000000000000100 0000000000000000

0000000111011111 100000 0000000000100010 0000000000000100 0000000000000100 0000000000000000

0101110001000100 1 0000000000100010 0000000000000101 0000000000000100 0000000000000000

M = 4use initial m bits for second hash

Page 284: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

0001111100000101 10 0000000000000010 0000000000000000 0000000000000100 0000000000000000

0110110110110011 100 0000000000000010 0000000000000100 0000000000000100 0000000000000000

0000000111011111 100000 0000000000100010 0000000000000100 0000000000000100 0000000000000000

0101110001000100 1 0000000000100010 0000000000000101 0000000000000100 0000000000000000

0000101001010101 10 0000000000100010 0000000000000101 0000000000000100 0000000000000000

M = 4use initial m bits for second hash

Page 285: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

0001111100000101 10 0000000000000010 0000000000000000 0000000000000100 0000000000000000

0110110110110011 100 0000000000000010 0000000000000100 0000000000000100 0000000000000000

0000000111011111 100000 0000000000100010 0000000000000100 0000000000000100 0000000000000000

0101110001000100 1 0000000000100010 0000000000000101 0000000000000100 0000000000000000

0000101001010101 10 0000000000100010 0000000000000101 0000000000000100 0000000000000000

1010101111111100 1 0000000000100010 0000000000000101 0000000000000101 0000000000000000

M = 4use initial m bits for second hash

Page 286: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

0001111100000101 10 0000000000000010 0000000000000000 0000000000000100 0000000000000000

0110110110110011 100 0000000000000010 0000000000000100 0000000000000100 0000000000000000

0000000111011111 100000 0000000000100010 0000000000000100 0000000000000100 0000000000000000

0101110001000100 1 0000000000100010 0000000000000101 0000000000000100 0000000000000000

0000101001010101 10 0000000000100010 0000000000000101 0000000000000100 0000000000000000

1010101111111100 1 0000000000100010 0000000000000101 0000000000000101 0000000000000000

0001011100110111 1000 0000000000101010 0000000000000101 0000000000000101 0000000000000000

M = 4use initial m bits for second hash

Page 287: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

0001111100000101 10 0000000000000010 0000000000000000 0000000000000100 0000000000000000

0110110110110011 100 0000000000000010 0000000000000100 0000000000000100 0000000000000000

0000000111011111 100000 0000000000100010 0000000000000100 0000000000000100 0000000000000000

0101110001000100 1 0000000000100010 0000000000000101 0000000000000100 0000000000000000

0000101001010101 10 0000000000100010 0000000000000101 0000000000000100 0000000000000000

1010101111111100 1 0000000000100010 0000000000000101 0000000000000101 0000000000000000

0001011100110111 1000 0000000000101010 0000000000000101 0000000000000101 0000000000000000

1110010000111111 1000000 0000000000101010 0000000000000101 0000000000000101 0000000001000000

M = 4use initial m bits for second hash

Page 288: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

0001111100000101 10 0000000000000010 0000000000000000 0000000000000100 0000000000000000

0110110110110011 100 0000000000000010 0000000000000100 0000000000000100 0000000000000000

0000000111011111 100000 0000000000100010 0000000000000100 0000000000000100 0000000000000000

0101110001000100 1 0000000000100010 0000000000000101 0000000000000100 0000000000000000

0000101001010101 10 0000000000100010 0000000000000101 0000000000000100 0000000000000000

1010101111111100 1 0000000000100010 0000000000000101 0000000000000101 0000000000000000

0001011100110111 1000 0000000000101010 0000000000000101 0000000000000101 0000000000000000

1110010000111111 1000000 0000000000101010 0000000000000101 0000000000000101 0000000001000000

1010110011111101 10 0000000000101010 0000000000000101 0000000000000111 0000000001000000

M = 4use initial m bits for second hash

Page 289: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

0001111100000101 10 0000000000000010 0000000000000000 0000000000000100 0000000000000000

0110110110110011 100 0000000000000010 0000000000000100 0000000000000100 0000000000000000

0000000111011111 100000 0000000000100010 0000000000000100 0000000000000100 0000000000000000

0101110001000100 1 0000000000100010 0000000000000101 0000000000000100 0000000000000000

0000101001010101 10 0000000000100010 0000000000000101 0000000000000100 0000000000000000

1010101111111100 1 0000000000100010 0000000000000101 0000000000000101 0000000000000000

0001011100110111 1000 0000000000101010 0000000000000101 0000000000000101 0000000000000000

1110010000111111 1000000 0000000000101010 0000000000000101 0000000000000101 0000000001000000

1010110011111101 10 0000000000101010 0000000000000101 0000000000000111 0000000001000000

0001110100110100 1 0000000000101011 0000000000000101 0000000000000111

M = 4use initial m bits for second hash

Page 290: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

0001111100000101 10 0000000000000010 0000000000000000 0000000000000100 0000000000000000

0110110110110011 100 0000000000000010 0000000000000100 0000000000000100 0000000000000000

0000000111011111 100000 0000000000100010 0000000000000100 0000000000000100 0000000000000000

0101110001000100 1 0000000000100010 0000000000000101 0000000000000100 0000000000000000

0000101001010101 10 0000000000100010 0000000000000101 0000000000000100 0000000000000000

1010101111111100 1 0000000000100010 0000000000000101 0000000000000101 0000000000000000

0001011100110111 1000 0000000000101010 0000000000000101 0000000000000101 0000000000000000

1110010000111111 1000000 0000000000101010 0000000000000101 0000000000000101 0000000001000000

1010110011111101 10 0000000000101010 0000000000000101 0000000000000111 0000000001000000

0001110100110100 1 0000000000101011 0000000000000101 0000000000000111

0000000000101011 0000000000000101 0000000000000111 0000000001000000

M = 4use initial m bits for second hash

Page 291: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

26

PCSA trace

x R(x) sketch[0] sketch[1] sketch[2] sketch[3]

1010011110111011 100 0000000000000000 0000000000000000 0000000000000100 0000000000000000

0001111100000101 10 0000000000000010 0000000000000000 0000000000000100 0000000000000000

0110110110110011 100 0000000000000010 0000000000000100 0000000000000100 0000000000000000

0000000111011111 100000 0000000000100010 0000000000000100 0000000000000100 0000000000000000

0101110001000100 1 0000000000100010 0000000000000101 0000000000000100 0000000000000000

0000101001010101 10 0000000000100010 0000000000000101 0000000000000100 0000000000000000

1010101111111100 1 0000000000100010 0000000000000101 0000000000000101 0000000000000000

0001011100110111 1000 0000000000101010 0000000000000101 0000000000000101 0000000000000000

1110010000111111 1000000 0000000000101010 0000000000000101 0000000000000101 0000000001000000

1010110011111101 10 0000000000101010 0000000000000101 0000000000000111 0000000001000000

0001110100110100 1 0000000000101011 0000000000000101 0000000000000111

0000000000101011 0000000000000101 0000000000000111 0000000001000000

r (sketch[ ] ) 2 1 3 0

M = 4use initial m bits for second hash

Page 292: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

27

Probabilistic counting with stochastic averaging in Java

public static long estimate(Iterable<Long> stream, int M) { long[] sketch = new long[M]; for (long x : stream) { int k = hash2(x, M); sketch[k] = sketch[k] | R(x); } int sum = 0; for (int k = 0; k < M; k++) sum += r(sketch[k]); double mean = 1.0 * sum / M; return (int) (M * Math.pow(2, mean)/.77351); }

Flajolet-Martin 1983

Page 293: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

27

Probabilistic counting with stochastic averaging in Java

public static long estimate(Iterable<Long> stream, int M) { long[] sketch = new long[M]; for (long x : stream) { int k = hash2(x, M); sketch[k] = sketch[k] | R(x); } int sum = 0; for (int k = 0; k < M; k++) sum += r(sketch[k]); double mean = 1.0 * sum / M; return (int) (M * Math.pow(2, mean)/.77351); }

Flajolet-Martin 1983

Idea. Stochastic averaging

Page 294: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

27

Probabilistic counting with stochastic averaging in Java

public static long estimate(Iterable<Long> stream, int M) { long[] sketch = new long[M]; for (long x : stream) { int k = hash2(x, M); sketch[k] = sketch[k] | R(x); } int sum = 0; for (int k = 0; k < M; k++) sum += r(sketch[k]); double mean = 1.0 * sum / M; return (int) (M * Math.pow(2, mean)/.77351); }

Flajolet-Martin 1983

Idea. Stochastic averaging

• Use second hash to split into M = 2m independent streams

Page 295: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

27

Probabilistic counting with stochastic averaging in Java

public static long estimate(Iterable<Long> stream, int M) { long[] sketch = new long[M]; for (long x : stream) { int k = hash2(x, M); sketch[k] = sketch[k] | R(x); } int sum = 0; for (int k = 0; k < M; k++) sum += r(sketch[k]); double mean = 1.0 * sum / M; return (int) (M * Math.pow(2, mean)/.77351); }

Flajolet-Martin 1983

Idea. Stochastic averaging

• Use second hash to split into M = 2m independent streams

• Use PC on each stream, yielding 2m sketches .

Page 296: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

27

Probabilistic counting with stochastic averaging in Java

public static long estimate(Iterable<Long> stream, int M) { long[] sketch = new long[M]; for (long x : stream) { int k = hash2(x, M); sketch[k] = sketch[k] | R(x); } int sum = 0; for (int k = 0; k < M; k++) sum += r(sketch[k]); double mean = 1.0 * sum / M; return (int) (M * Math.pow(2, mean)/.77351); }

Flajolet-Martin 1983

Idea. Stochastic averaging

• Use second hash to split into M = 2m independent streams

• Use PC on each stream, yielding 2m sketches .

• Compute mean = average # trailing 1 bits in the sketches.

Page 297: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

27

Probabilistic counting with stochastic averaging in Java

public static long estimate(Iterable<Long> stream, int M) { long[] sketch = new long[M]; for (long x : stream) { int k = hash2(x, M); sketch[k] = sketch[k] | R(x); } int sum = 0; for (int k = 0; k < M; k++) sum += r(sketch[k]); double mean = 1.0 * sum / M; return (int) (M * Math.pow(2, mean)/.77351); }

Flajolet-Martin 1983

Idea. Stochastic averaging

• Use second hash to split into M = 2m independent streams

• Use PC on each stream, yielding 2m sketches .

• Compute mean = average # trailing 1 bits in the sketches.

• Return 2mean/.77351.

Page 298: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

27

Probabilistic counting with stochastic averaging in Java

public static long estimate(Iterable<Long> stream, int M) { long[] sketch = new long[M]; for (long x : stream) { int k = hash2(x, M); sketch[k] = sketch[k] | R(x); } int sum = 0; for (int k = 0; k < M; k++) sum += r(sketch[k]); double mean = 1.0 * sum / M; return (int) (M * Math.pow(2, mean)/.77351); }

Flajolet-Martin 1983

Idea. Stochastic averaging

• Use second hash to split into M = 2m independent streams

• Use PC on each stream, yielding 2m sketches .

• Compute mean = average # trailing 1 bits in the sketches.

• Return 2mean/.77351.

Q. Accuracy improves as M increases.

Page 299: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

27

Probabilistic counting with stochastic averaging in Java

public static long estimate(Iterable<Long> stream, int M) { long[] sketch = new long[M]; for (long x : stream) { int k = hash2(x, M); sketch[k] = sketch[k] | R(x); } int sum = 0; for (int k = 0; k < M; k++) sum += r(sketch[k]); double mean = 1.0 * sum / M; return (int) (M * Math.pow(2, mean)/.77351); }

Flajolet-Martin 1983

Idea. Stochastic averaging

• Use second hash to split into M = 2m independent streams

• Use PC on each stream, yielding 2m sketches .

• Compute mean = average # trailing 1 bits in the sketches.

• Return 2mean/.77351.

Q. Accuracy improves as M increases.

Q. How much?

Page 300: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

27

Probabilistic counting with stochastic averaging in Java

public static long estimate(Iterable<Long> stream, int M) { long[] sketch = new long[M]; for (long x : stream) { int k = hash2(x, M); sketch[k] = sketch[k] | R(x); } int sum = 0; for (int k = 0; k < M; k++) sum += r(sketch[k]); double mean = 1.0 * sum / M; return (int) (M * Math.pow(2, mean)/.77351); }

Flajolet-Martin 1983

Idea. Stochastic averaging

• Use second hash to split into M = 2m independent streams

• Use PC on each stream, yielding 2m sketches .

• Compute mean = average # trailing 1 bits in the sketches.

• Return 2mean/.77351.

Q. Accuracy improves as M increases.

Q. How much?

Theorem (paraphrased to fit context of this talk).

Under the uniform hashing assumption, PCSA

• Uses 64M bits.

• Produces estimate with a relative accuracy close to .0.78/

�M

Page 301: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.0.78/�M

Page 302: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

0.78/�M

Page 303: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

% java PCSA 1000000 31 1024 10

0.78/�M

Page 304: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

% java PCSA 1000000 31 1024 10964416

0.78/�M

Page 305: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

% java PCSA 1000000 31 1024 10964416997616

0.78/�M

Page 306: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

% java PCSA 1000000 31 1024 10964416997616959857

0.78/�M

Page 307: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

% java PCSA 1000000 31 1024 109644169976169598571024303

0.78/�M

Page 308: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

% java PCSA 1000000 31 1024 109644169976169598571024303972940

0.78/�M

Page 309: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

% java PCSA 1000000 31 1024 109644169976169598571024303972940985534

0.78/�M

Page 310: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

% java PCSA 1000000 31 1024 109644169976169598571024303972940985534998291

0.78/�M

Page 311: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

% java PCSA 1000000 31 1024 109644169976169598571024303972940985534998291996266

0.78/�M

Page 312: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

% java PCSA 1000000 31 1024 109644169976169598571024303972940985534998291996266959208

0.78/�M

Page 313: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

% java PCSA 1000000 31 1024 1096441699761695985710243039729409855349982919962669592081015329

0.78/�M

Page 314: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

28

Validation of PCSA analysis

Hypothesis. Value returned is accurate to for random values from a large range.

Experiment. 1,000,000 31-bit random values, M = 1024 (10 trials)

% java PCSA 1000000 31 1024 1096441699761695985710243039729409855349982919962669592081015329

0.78/�M

Page 315: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

29

Space-accuracy tradeoff for probabilistic counting with stochastic averaging

10245122561286432

10%

5%

Relative accuracy:0.78�M

Page 316: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

29

Space-accuracy tradeoff for probabilistic counting with stochastic averaging

10245122561286432

10%

5%

Relative accuracy:0.78�M

Bottom line.

Page 317: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

29

Space-accuracy tradeoff for probabilistic counting with stochastic averaging

10245122561286432

10%

5%

Relative accuracy:0.78�M

Bottom line.• Attain 10% relative accuracy with a sketch consisting of 64 words.

M = 64

Page 318: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

29

Space-accuracy tradeoff for probabilistic counting with stochastic averaging

10245122561286432

10%

5%

Relative accuracy:0.78�M

Bottom line.• Attain 10% relative accuracy with a sketch consisting of 64 words.• Attain 2.4% relative accuracy with a sketch consisting of 1024 words.

M = 64

M = 1024

Page 319: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

30

Scientific validation of PCSA

Hypothesis. Accuracy is as specified for the hash functions we use and the data we have.

Page 320: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

30

Scientific validation of PCSA

Hypothesis. Accuracy is as specified for the hash functions we use and the data we have.

Validation (Flajolet and Martin, 1985). Extensive reproducible scientific experiments (!)

Page 321: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

30

Scientific validation of PCSA

Hypothesis. Accuracy is as specified for the hash functions we use and the data we have.

Validation (Flajolet and Martin, 1985). Extensive reproducible scientific experiments (!)

Validation (RS, this morning).

Page 322: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

log.07.f3.txt

30

Scientific validation of PCSA

Hypothesis. Accuracy is as specified for the hash functions we use and the data we have.

Validation (Flajolet and Martin, 1985). Extensive reproducible scientific experiments (!)

Validation (RS, this morning).

Page 323: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

log.07.f3.txt

30

Scientific validation of PCSA

Hypothesis. Accuracy is as specified for the hash functions we use and the data we have.

Validation (Flajolet and Martin, 1985). Extensive reproducible scientific experiments (!)

% java PCSA 6000000 1024 < log.07.f3.txt 1106474

Validation (RS, this morning).

Page 324: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

log.07.f3.txt

30

Scientific validation of PCSA

Hypothesis. Accuracy is as specified for the hash functions we use and the data we have.

Validation (Flajolet and Martin, 1985). Extensive reproducible scientific experiments (!)

% java PCSA 6000000 1024 < log.07.f3.txt 1106474

Validation (RS, this morning).

<1% larger than actual value

Page 325: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

log.07.f3.txt

30

Scientific validation of PCSA

Hypothesis. Accuracy is as specified for the hash functions we use and the data we have.

Q. Is PCSA effective?

Validation (Flajolet and Martin, 1985). Extensive reproducible scientific experiments (!)

% java PCSA 6000000 1024 < log.07.f3.txt 1106474

Validation (RS, this morning).

<1% larger than actual value

Page 326: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com CPE001cdfbc55ac-CM0011ae926e6c.cpe.net.cable.rogers.com 118-171-27-8.dynamic.hinet.net cpe-76-170-182-222.socal.res.rr.com 203-88-22-144.live.vodafone.in 117.211.88.36 cpe-76-170-182-222.socal.res.rr.com 117.211.88.36 220.248.45.69 HSI-KBW-078-042-025-130.hsi3.kabel-badenwuerttemberg.de 220.248.45.69 59.7.197.132 cpe-76-170-182-222.socal.res.rr.com 87-206-191-72.dynamic.chello.pl pool-71-104-94-246.lsanca.dsl-w.verizon.net adsl-76-212-198-113.dsl.sndg02.sbcglobal.net telemedia-ap-Static-113.5.175.122.airtelbroadband.in ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in cs27063206.pp.htv.fi pool-71-104-94-246.lsanca.dsl-w.verizon.net gsearch.CS.Princeton.EDU 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net cm56-159-216.liwest.at 109-162-6-189-ter.broadband.kyivstar.net 109-162-6-189-ter.broadband.kyivstar.net crawl-66-249-71-36.googlebot.com ABTS-KK-Dynamic-095.93.179.122.airtelbroadband.in telemedia-ap-Static-113.5.175.122.airtelbroadband.in c-71-231-40-193.hsd1.wa.comcast.net 29-72-93-178.pool.ukrtel.net 118-171-27-8.dynamic.hinet.net

log.07.f3.txt

30

Scientific validation of PCSA

Hypothesis. Accuracy is as specified for the hash functions we use and the data we have.

Q. Is PCSA effective?

A. ABSOLUTELY!

Validation (Flajolet and Martin, 1985). Extensive reproducible scientific experiments (!)

% java PCSA 6000000 1024 < log.07.f3.txt 1106474

Validation (RS, this morning).

<1% larger than actual value

Page 327: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Page 328: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Q. About how many different values are present in a given stream?

Page 329: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Q. About how many different values are present in a given stream?

PCSA

Page 330: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Q. About how many different values are present in a given stream?

PCSA

• Makes one pass through the stream.

Page 331: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Q. About how many different values are present in a given stream?

PCSA

• Makes one pass through the stream.• Uses a few machine instructions per value

Page 332: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Q. About how many different values are present in a given stream?

PCSA

• Makes one pass through the stream.• Uses a few machine instructions per value• Uses M words to achieve relative accuracy 0.78/

�M

Page 333: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Q. About how many different values are present in a given stream?

PCSA

• Makes one pass through the stream.• Uses a few machine instructions per value• Uses M words to achieve relative accuracy ✓0.78/

�M

Page 334: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Q. About how many different values are present in a given stream?

PCSA

• Makes one pass through the stream.• Uses a few machine instructions per value• Uses M words to achieve relative accuracy ✓

Results validated through extensive experimentation.

0.78/�M

Page 335: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Q. About how many different values are present in a given stream?

PCSA

• Makes one pass through the stream.• Uses a few machine instructions per value• Uses M words to achieve relative accuracy ✓

Results validated through extensive experimentation.

0.78/�M

A poster child for AofA/AC

Page 336: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Q. About how many different values are present in a given stream?

PCSA

• Makes one pass through the stream.• Uses a few machine instructions per value• Uses M words to achieve relative accuracy

Open questions

✓Results validated through extensive experimentation.

0.78/�M

A poster child for AofA/AC

Page 337: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Q. About how many different values are present in a given stream?

PCSA

• Makes one pass through the stream.• Uses a few machine instructions per value• Uses M words to achieve relative accuracy

Open questions• Better space-accuracy tradeoffs?

✓Results validated through extensive experimentation.

0.78/�M

A poster child for AofA/AC

Page 338: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Q. About how many different values are present in a given stream?

PCSA

• Makes one pass through the stream.• Uses a few machine instructions per value• Uses M words to achieve relative accuracy

Open questions• Better space-accuracy tradeoffs?• Support other operations?

✓Results validated through extensive experimentation.

0.78/�M

A poster child for AofA/AC

Page 339: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

31

is a demonstrably effective approach to cardinality estimation

Summary: PCSA (Flajolet-Martin, 1983)

Q. About how many different values are present in a given stream?

PCSA

• Makes one pass through the stream.• Uses a few machine instructions per value• Uses M words to achieve relative accuracy

Open questions• Better space-accuracy tradeoffs?• Support other operations?

✓Results validated through extensive experimentation.

0.78/�M

A poster child for AofA/AC

“ IT IS QUITE CLEAR that other observable regularities on hashed values of records could have been used…

− Flajolet and Martin

Page 340: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

32

Small sample of work on related problems

Page 341: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

32

Small sample of work on related problems

1970 Bloom set membership

Page 342: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

32

Small sample of work on related problems

1970 Bloom set membership

1984 Wegman unbiased sampling estimate

Page 343: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

32

Small sample of work on related problems

1970 Bloom set membership

1984 Wegman unbiased sampling estimate

1996– many authors refinements (stay tuned)

Page 344: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

32

Small sample of work on related problems

1970 Bloom set membership

1984 Wegman unbiased sampling estimate

1996– many authors refinements (stay tuned)

2000 Indyk L1 norm

Page 345: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

32

Small sample of work on related problems

1970 Bloom set membership

1984 Wegman unbiased sampling estimate

1996– many authors refinements (stay tuned)

2000 Indyk L1 norm

2004 Cormode–Muthukrishnan

frequency estimation deletion and other operations

Page 346: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

32

Small sample of work on related problems

1970 Bloom set membership

1984 Wegman unbiased sampling estimate

1996– many authors refinements (stay tuned)

2000 Indyk L1 norm

2004 Cormode–Muthukrishnan

frequency estimation deletion and other operations

2005 Giroire fast stream processing

Page 347: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

32

Small sample of work on related problems

1970 Bloom set membership

1984 Wegman unbiased sampling estimate

1996– many authors refinements (stay tuned)

2000 Indyk L1 norm

2004 Cormode–Muthukrishnan

frequency estimation deletion and other operations

2005 Giroire fast stream processing

2012 Lumbroso full range, asymptotically unbiased

Page 348: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

32

Small sample of work on related problems

1970 Bloom set membership

1984 Wegman unbiased sampling estimate

1996– many authors refinements (stay tuned)

2000 Indyk L1 norm

2004 Cormode–Muthukrishnan

frequency estimation deletion and other operations

2005 Giroire fast stream processing

2012 Lumbroso full range, asymptotically unbiased

2014 Helmi–Lumbroso–Martinez–Viola uses neither sampling nor hashing

Page 349: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

OF

Cardinality Estimation

•Rules of the game •Probabilistic counting •Stochastic averaging •Refinements •Final frontier

Page 350: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

34

We can do better (in theory)

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

Page 351: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

34

We can do better (in theory)

Contributions

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

Page 352: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

Page 353: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

• Formalized idea of randomized streaming algorithms

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

Page 354: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

• Formalized idea of randomized streaming algorithms

• Won Gödel Prize in 2005 for “foundational contribution”

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

Page 355: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Theorem (paraphrased to fit context of this talk).

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

• Formalized idea of randomized streaming algorithms

• Won Gödel Prize in 2005 for “foundational contribution”

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

Page 356: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, PC, for any c >2,

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

• Formalized idea of randomized streaming algorithms

• Won Gödel Prize in 2005 for “foundational contribution”

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

Page 357: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, PC, for any c >2,• Uses O(log N ) bits.

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

• Formalized idea of randomized streaming algorithms

• Won Gödel Prize in 2005 for “foundational contribution”

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

Page 358: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, PC, for any c >2,• Uses O(log N ) bits.• Is accurate to a factor of c, with probability at least 2/c.

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

• Formalized idea of randomized streaming algorithms

• Won Gödel Prize in 2005 for “foundational contribution”

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

Page 359: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, PC, for any c >2,• Uses O(log N ) bits.• Is accurate to a factor of c, with probability at least 2/c.

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

• Formalized idea of randomized streaming algorithms

• Won Gödel Prize in 2005 for “foundational contribution”

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

Replaces “uniform hashing” assumption with “random bit existence” assumption

Page 360: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, PC, for any c >2,• Uses O(log N ) bits.• Is accurate to a factor of c, with probability at least 2/c.

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

• Formalized idea of randomized streaming algorithms

• Won Gödel Prize in 2005 for “foundational contribution”

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

BUT, no impact on cardinality estimation in practice

Replaces “uniform hashing” assumption with “random bit existence” assumption

Page 361: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, PC, for any c >2,• Uses O(log N ) bits.• Is accurate to a factor of c, with probability at least 2/c.

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

• Formalized idea of randomized streaming algorithms

• Won Gödel Prize in 2005 for “foundational contribution”

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

BUT, no impact on cardinality estimation in practice

• “Algorithm” just changes hash function for PC

Replaces “uniform hashing” assumption with “random bit existence” assumption

Page 362: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, PC, for any c >2,• Uses O(log N ) bits.• Is accurate to a factor of c, with probability at least 2/c.

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

• Formalized idea of randomized streaming algorithms

• Won Gödel Prize in 2005 for “foundational contribution”

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

BUT, no impact on cardinality estimation in practice

• “Algorithm” just changes hash function for PC

• Accuracy estimate is too weak to be useful

Replaces “uniform hashing” assumption with “random bit existence” assumption

Page 363: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, PC, for any c >2,• Uses O(log N ) bits.• Is accurate to a factor of c, with probability at least 2/c.

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

• Formalized idea of randomized streaming algorithms

• Won Gödel Prize in 2005 for “foundational contribution”

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

BUT, no impact on cardinality estimation in practice

• “Algorithm” just changes hash function for PC

• Accuracy estimate is too weak to be useful

• No validation

Replaces “uniform hashing” assumption with “random bit existence” assumption

Page 364: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, PC, for any c >2,• Uses O(log N ) bits.• Is accurate to a factor of c, with probability at least 2/c.

34

We can do better (in theory)

Contributions

• Studied problem of estimating higher moments

• Formalized idea of randomized streaming algorithms

• Won Gödel Prize in 2005 for “foundational contribution”

Alon, Matias, and Szegedy The Space Complexity of Approximating the Frequency Moments STOC 1996; JCSS 1999.

BUT, no impact on cardinality estimation in practice

• “Algorithm” just changes hash function for PC

• Accuracy estimate is too weak to be useful

• No validation

Replaces “uniform hashing” assumption with “random bit existence” assumption

???!

Page 365: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

35

Interesting quote

“ Flajolet and Martin [assume] that one may use in the algorithm an explicit family of hash functions which exhibits some ideal random properties. Since we are not aware of the existence of such a family of hash functions …”

− Alon, Matias, and Szegedy

Page 366: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

35

Interesting quote

No! They hypothesized that practical hash functions would be as effective as random ones.

“ Flajolet and Martin [assume] that one may use in the algorithm an explicit family of hash functions which exhibits some ideal random properties. Since we are not aware of the existence of such a family of hash functions …”

− Alon, Matias, and Szegedy

Page 367: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

35

Interesting quote

No! They hypothesized that practical hash functions would be as effective as random ones.They then validated that hypothesis by proving tight bounds that match experimental results.

“ Flajolet and Martin [assume] that one may use in the algorithm an explicit family of hash functions which exhibits some ideal random properties. Since we are not aware of the existence of such a family of hash functions …”

− Alon, Matias, and Szegedy

Page 368: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

35

Interesting quote

Points of view re hashing

No! They hypothesized that practical hash functions would be as effective as random ones.They then validated that hypothesis by proving tight bounds that match experimental results.

“ Flajolet and Martin [assume] that one may use in the algorithm an explicit family of hash functions which exhibits some ideal random properties. Since we are not aware of the existence of such a family of hash functions …”

− Alon, Matias, and Szegedy

Page 369: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

35

Interesting quote

Points of view re hashing

• Theoretical computer science. Uniform hashing assumption is not proved.

No! They hypothesized that practical hash functions would be as effective as random ones.They then validated that hypothesis by proving tight bounds that match experimental results.

“ Flajolet and Martin [assume] that one may use in the algorithm an explicit family of hash functions which exhibits some ideal random properties. Since we are not aware of the existence of such a family of hash functions …”

− Alon, Matias, and Szegedy

Page 370: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

35

Interesting quote

Points of view re hashing

• Theoretical computer science. Uniform hashing assumption is not proved.

• Practical computing. Hashing works for many common data types.

No! They hypothesized that practical hash functions would be as effective as random ones.They then validated that hypothesis by proving tight bounds that match experimental results.

“ Flajolet and Martin [assume] that one may use in the algorithm an explicit family of hash functions which exhibits some ideal random properties. Since we are not aware of the existence of such a family of hash functions …”

− Alon, Matias, and Szegedy

Page 371: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

35

Interesting quote

Points of view re hashing

• Theoretical computer science. Uniform hashing assumption is not proved.

• Practical computing. Hashing works for many common data types.

• AofA. Extensive experiments have validated precise analytic models.

No! They hypothesized that practical hash functions would be as effective as random ones.They then validated that hypothesis by proving tight bounds that match experimental results.

“ Flajolet and Martin [assume] that one may use in the algorithm an explicit family of hash functions which exhibits some ideal random properties. Since we are not aware of the existence of such a family of hash functions …”

− Alon, Matias, and Szegedy

Page 372: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

35

Interesting quote

Points of view re hashing

• Theoretical computer science. Uniform hashing assumption is not proved.

• Practical computing. Hashing works for many common data types.

• AofA. Extensive experiments have validated precise analytic models.

Points of view re random bits

No! They hypothesized that practical hash functions would be as effective as random ones.They then validated that hypothesis by proving tight bounds that match experimental results.

“ Flajolet and Martin [assume] that one may use in the algorithm an explicit family of hash functions which exhibits some ideal random properties. Since we are not aware of the existence of such a family of hash functions …”

− Alon, Matias, and Szegedy

Page 373: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

35

Interesting quote

Points of view re hashing

• Theoretical computer science. Uniform hashing assumption is not proved.

• Practical computing. Hashing works for many common data types.

• AofA. Extensive experiments have validated precise analytic models.

Points of view re random bits

• Theoretical computer science. Axiomatic that random bits exist.

No! They hypothesized that practical hash functions would be as effective as random ones.They then validated that hypothesis by proving tight bounds that match experimental results.

“ Flajolet and Martin [assume] that one may use in the algorithm an explicit family of hash functions which exhibits some ideal random properties. Since we are not aware of the existence of such a family of hash functions …”

− Alon, Matias, and Szegedy

Page 374: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

35

Interesting quote

Points of view re hashing

• Theoretical computer science. Uniform hashing assumption is not proved.

• Practical computing. Hashing works for many common data types.

• AofA. Extensive experiments have validated precise analytic models.

Points of view re random bits

• Theoretical computer science. Axiomatic that random bits exist.

• Practical computing. No, they don’t ! And randomized algorithms are inconvenient, btw.

No! They hypothesized that practical hash functions would be as effective as random ones.They then validated that hypothesis by proving tight bounds that match experimental results.

“ Flajolet and Martin [assume] that one may use in the algorithm an explicit family of hash functions which exhibits some ideal random properties. Since we are not aware of the existence of such a family of hash functions …”

− Alon, Matias, and Szegedy

Page 375: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

35

Interesting quote

Points of view re hashing

• Theoretical computer science. Uniform hashing assumption is not proved.

• Practical computing. Hashing works for many common data types.

• AofA. Extensive experiments have validated precise analytic models.

Points of view re random bits

• Theoretical computer science. Axiomatic that random bits exist.

• Practical computing. No, they don’t ! And randomized algorithms are inconvenient, btw.

• AofA. More effective path forward is to validate precise analysis even if stronger assumptions are needed.

No! They hypothesized that practical hash functions would be as effective as random ones.They then validated that hypothesis by proving tight bounds that match experimental results.

“ Flajolet and Martin [assume] that one may use in the algorithm an explicit family of hash functions which exhibits some ideal random properties. Since we are not aware of the existence of such a family of hash functions …”

− Alon, Matias, and Szegedy

Page 376: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Page 377: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Relevant quantities

Page 378: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Relevant quantities• N is the number of items in the data stream.

Page 379: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Relevant quantities• N is the number of items in the data stream. • lg N is the number of bits needed to represent numbers less than N in binary.

Page 380: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Relevant quantities• N is the number of items in the data stream. • lg N is the number of bits needed to represent numbers less than N in binary. • lg lg N is the number of bits needed to represent numbers less than lg N in binary.

Page 381: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Relevant quantities• N is the number of items in the data stream. • lg N is the number of bits needed to represent numbers less than N in binary. • lg lg N is the number of bits needed to represent numbers less than lg N in binary.

For real-world applications

Page 382: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Relevant quantities• N is the number of items in the data stream. • lg N is the number of bits needed to represent numbers less than N in binary. • lg lg N is the number of bits needed to represent numbers less than lg N in binary.

For real-world applications• N is less than 264.

Page 383: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Relevant quantities• N is the number of items in the data stream. • lg N is the number of bits needed to represent numbers less than N in binary. • lg lg N is the number of bits needed to represent numbers less than lg N in binary.

For real-world applications• N is less than 264. • lg N is less than 64.

Page 384: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Relevant quantities• N is the number of items in the data stream. • lg N is the number of bits needed to represent numbers less than N in binary. • lg lg N is the number of bits needed to represent numbers less than lg N in binary.

For real-world applications• N is less than 264. • lg N is less than 64. • lg lg N is less than 8.

Page 385: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Relevant quantities• N is the number of items in the data stream. • lg N is the number of bits needed to represent numbers less than N in binary. • lg lg N is the number of bits needed to represent numbers less than lg N in binary.

For real-world applications• N is less than 264. • lg N is less than 64. • lg lg N is less than 8.

Typical PCSA implementations

Page 386: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Relevant quantities• N is the number of items in the data stream. • lg N is the number of bits needed to represent numbers less than N in binary. • lg lg N is the number of bits needed to represent numbers less than lg N in binary.

For real-world applications• N is less than 264. • lg N is less than 64. • lg lg N is less than 8.

Typical PCSA implementations• Could use M lg N bits, in theory.

Page 387: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Relevant quantities• N is the number of items in the data stream. • lg N is the number of bits needed to represent numbers less than N in binary. • lg lg N is the number of bits needed to represent numbers less than lg N in binary.

For real-world applications• N is less than 264. • lg N is less than 64. • lg lg N is less than 8.

Typical PCSA implementations• Could use M lg N bits, in theory.• Use 64-bit words to take advantage of machine-language efficiencies.

Page 388: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

36

logs and loglogs

To improve space-time tradeoffs, we need to carefully count bits.

Relevant quantities• N is the number of items in the data stream. • lg N is the number of bits needed to represent numbers less than N in binary. • lg lg N is the number of bits needed to represent numbers less than lg N in binary.

For real-world applications• N is less than 264. • lg N is less than 64. • lg lg N is less than 8.

Typical PCSA implementations• Could use M lg N bits, in theory.• Use 64-bit words to take advantage of machine-language efficiencies. • Use (therefore) 64*64 = 4096 bits with M = 64 (for 10% accuracy with N < 264).

Page 389: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

37

We can do better (in theory)

Bar-Yossef, Jayram, Kumar, Sivakumar, and Trevisan Counting Distinct Elements in a Data Stream RANDOM 2002.

Page 390: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

37

We can do better (in theory)

Bar-Yossef, Jayram, Kumar, Sivakumar, and Trevisan Counting Distinct Elements in a Data Stream RANDOM 2002.

Contribution Improves space-accuracy tradeoff at extra stream-processing expense.

Page 391: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

37

We can do better (in theory)

Bar-Yossef, Jayram, Kumar, Sivakumar, and Trevisan Counting Distinct Elements in a Data Stream RANDOM 2002.

Contribution Improves space-accuracy tradeoff at extra stream-processing expense.

Theorem (paraphrased to fit context of this talk).

Page 392: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

37

We can do better (in theory)

Bar-Yossef, Jayram, Kumar, Sivakumar, and Trevisan Counting Distinct Elements in a Data Stream RANDOM 2002.

Contribution Improves space-accuracy tradeoff at extra stream-processing expense.

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, there exists an algorithm that

Page 393: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

37

We can do better (in theory)

Bar-Yossef, Jayram, Kumar, Sivakumar, and Trevisan Counting Distinct Elements in a Data Stream RANDOM 2002.

Contribution Improves space-accuracy tradeoff at extra stream-processing expense.

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, there exists an algorithm that• Uses O(M log log N ) bits.

Page 394: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

37

We can do better (in theory)

Bar-Yossef, Jayram, Kumar, Sivakumar, and Trevisan Counting Distinct Elements in a Data Stream RANDOM 2002.

Contribution Improves space-accuracy tradeoff at extra stream-processing expense.

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, there exists an algorithm that• Uses O(M log log N ) bits. PCSA uses M lg N bits

Page 395: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

37

We can do better (in theory)

Bar-Yossef, Jayram, Kumar, Sivakumar, and Trevisan Counting Distinct Elements in a Data Stream RANDOM 2002.

Contribution Improves space-accuracy tradeoff at extra stream-processing expense.

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, there exists an algorithm that• Uses O(M log log N ) bits.• Achieves relative accuracy .O(1/

�M)

PCSA uses M lg N bits

Page 396: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

37

We can do better (in theory)

Bar-Yossef, Jayram, Kumar, Sivakumar, and Trevisan Counting Distinct Elements in a Data Stream RANDOM 2002.

STILL no impact on cardinality estimation in practice

Contribution Improves space-accuracy tradeoff at extra stream-processing expense.

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, there exists an algorithm that• Uses O(M log log N ) bits.• Achieves relative accuracy .O(1/

�M)

PCSA uses M lg N bits

Page 397: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

37

We can do better (in theory)

Bar-Yossef, Jayram, Kumar, Sivakumar, and Trevisan Counting Distinct Elements in a Data Stream RANDOM 2002.

STILL no impact on cardinality estimation in practice

• Infeasible because of high stream-processing expense.

Contribution Improves space-accuracy tradeoff at extra stream-processing expense.

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, there exists an algorithm that• Uses O(M log log N ) bits.• Achieves relative accuracy .O(1/

�M)

PCSA uses M lg N bits

Page 398: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

37

We can do better (in theory)

Bar-Yossef, Jayram, Kumar, Sivakumar, and Trevisan Counting Distinct Elements in a Data Stream RANDOM 2002.

STILL no impact on cardinality estimation in practice

• Infeasible because of high stream-processing expense.

• Big constants hidden in O-notation

Contribution Improves space-accuracy tradeoff at extra stream-processing expense.

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, there exists an algorithm that• Uses O(M log log N ) bits.• Achieves relative accuracy .O(1/

�M)

PCSA uses M lg N bits

Page 399: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

37

We can do better (in theory)

Bar-Yossef, Jayram, Kumar, Sivakumar, and Trevisan Counting Distinct Elements in a Data Stream RANDOM 2002.

STILL no impact on cardinality estimation in practice

• Infeasible because of high stream-processing expense.

• Big constants hidden in O-notation

• No validation

Contribution Improves space-accuracy tradeoff at extra stream-processing expense.

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, there exists an algorithm that• Uses O(M log log N ) bits.• Achieves relative accuracy .O(1/

�M)

PCSA uses M lg N bits

Page 400: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

37

We can do better (in theory)

Bar-Yossef, Jayram, Kumar, Sivakumar, and Trevisan Counting Distinct Elements in a Data Stream RANDOM 2002.

STILL no impact on cardinality estimation in practice

• Infeasible because of high stream-processing expense.

• Big constants hidden in O-notation

• No validation

???!

Contribution Improves space-accuracy tradeoff at extra stream-processing expense.

Theorem (paraphrased to fit context of this talk). With strongly universal hashing, there exists an algorithm that• Uses O(M log log N ) bits.• Achieves relative accuracy .O(1/

�M)

PCSA uses M lg N bits

Page 401: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Page 402: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

Page 403: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

Page 404: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

PCSA saves sketches (lg N bits each)00000000000000000000000001101111

Page 405: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

PCSA saves sketches (lg N bits each)00000000000000000000000001101111

LogLog saves r() values (lglg N bits each)

00100 ( = 4)

Page 406: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

• Improves space-accuracy tradeoff without extra expense per value

PCSA saves sketches (lg N bits each)00000000000000000000000001101111

LogLog saves r() values (lglg N bits each)

00100 ( = 4)

Page 407: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

• Improves space-accuracy tradeoff without extra expense per value

• Full analysis, fully validated with experimentation

PCSA saves sketches (lg N bits each)00000000000000000000000001101111

LogLog saves r() values (lglg N bits each)

00100 ( = 4)

Page 408: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

• Improves space-accuracy tradeoff without extra expense per value

• Full analysis, fully validated with experimentation

Theorem (paraphrased to fit context of this talk). PCSA saves sketches (lg N bits each)00000000000000000000000001101111

LogLog saves r() values (lglg N bits each)

00100 ( = 4)

Page 409: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

• Improves space-accuracy tradeoff without extra expense per value

• Full analysis, fully validated with experimentation

Theorem (paraphrased to fit context of this talk). Under the uniform hashing assumption, LogLog

PCSA saves sketches (lg N bits each)00000000000000000000000001101111

LogLog saves r() values (lglg N bits each)

00100 ( = 4)

Page 410: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

• Improves space-accuracy tradeoff without extra expense per value

• Full analysis, fully validated with experimentation

Theorem (paraphrased to fit context of this talk). Under the uniform hashing assumption, LogLog

• Uses M lg lg N bits.

PCSA saves sketches (lg N bits each)00000000000000000000000001101111

LogLog saves r() values (lglg N bits each)

00100 ( = 4)

Page 411: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

• Improves space-accuracy tradeoff without extra expense per value

• Full analysis, fully validated with experimentation

Theorem (paraphrased to fit context of this talk). Under the uniform hashing assumption, LogLog

• Uses M lg lg N bits.• Achieves relative accuracy close to .1.30/

�M

PCSA saves sketches (lg N bits each)00000000000000000000000001101111

LogLog saves r() values (lglg N bits each)

00100 ( = 4)

Page 412: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Not much impact on cardinality estimation in practice only because

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

• Improves space-accuracy tradeoff without extra expense per value

• Full analysis, fully validated with experimentation

Theorem (paraphrased to fit context of this talk). Under the uniform hashing assumption, LogLog

• Uses M lg lg N bits.• Achieves relative accuracy close to .1.30/

�M

PCSA saves sketches (lg N bits each)00000000000000000000000001101111

LogLog saves r() values (lglg N bits each)

00100 ( = 4)

Page 413: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Not much impact on cardinality estimation in practice only because

• PCSA was effectively deployed in practical systems

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

• Improves space-accuracy tradeoff without extra expense per value

• Full analysis, fully validated with experimentation

Theorem (paraphrased to fit context of this talk). Under the uniform hashing assumption, LogLog

• Uses M lg lg N bits.• Achieves relative accuracy close to .1.30/

�M

PCSA saves sketches (lg N bits each)00000000000000000000000001101111

LogLog saves r() values (lglg N bits each)

00100 ( = 4)

Page 414: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Not much impact on cardinality estimation in practice only because

• PCSA was effectively deployed in practical systems

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

• Improves space-accuracy tradeoff without extra expense per value

• Full analysis, fully validated with experimentation

I like PCSA

Theorem (paraphrased to fit context of this talk). Under the uniform hashing assumption, LogLog

• Uses M lg lg N bits.• Achieves relative accuracy close to .1.30/

�M

PCSA saves sketches (lg N bits each)00000000000000000000000001101111

LogLog saves r() values (lglg N bits each)

00100 ( = 4)

Page 415: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

38

We can do better (in theory and in practice)

Not much impact on cardinality estimation in practice only because

• PCSA was effectively deployed in practical systems

• Idea led to a better algorithm a few years later (stay tuned)

Durand and Flajolet LogLog Counting of Large Cardinalities ESA 2003; LNCS volume 2832.

Contributions (independent of BYJKST)

• Presents LogLog algorithm, an easy variant of PCSA

• Improves space-accuracy tradeoff without extra expense per value

• Full analysis, fully validated with experimentation

I like PCSA

Theorem (paraphrased to fit context of this talk). Under the uniform hashing assumption, LogLog

• Uses M lg lg N bits.• Achieves relative accuracy close to .1.30/

�M

PCSA saves sketches (lg N bits each)00000000000000000000000001101111

LogLog saves r() values (lglg N bits each)

00100 ( = 4)

Page 416: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

39

We can do better (in theory and in practice): HyperLogLog algorithm (2007)

public static long estimate(Iterable<Long> stream, int M) { int[] bytes = new int[M]; for (long x : stream) { int k = hash2(x, M); if (bytes[k] < Bits.r(x)) bytes[k] = Bits.r(x); } double sum = 0.0; for (int k = 0; k < M; k++) sum += Math.pow(2, -1.0 - bytes[k]); return (int) (alpha * M * M / sum); }

Flajolet-Fusy-Gandouet-Meunier 2007

Page 417: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

39

We can do better (in theory and in practice): HyperLogLog algorithm (2007)

public static long estimate(Iterable<Long> stream, int M) { int[] bytes = new int[M]; for (long x : stream) { int k = hash2(x, M); if (bytes[k] < Bits.r(x)) bytes[k] = Bits.r(x); } double sum = 0.0; for (int k = 0; k < M; k++) sum += Math.pow(2, -1.0 - bytes[k]); return (int) (alpha * M * M / sum); }

Flajolet-Fusy-Gandouet-Meunier 2007

Idea. Harmonic mean of r() values

Page 418: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

39

We can do better (in theory and in practice): HyperLogLog algorithm (2007)

public static long estimate(Iterable<Long> stream, int M) { int[] bytes = new int[M]; for (long x : stream) { int k = hash2(x, M); if (bytes[k] < Bits.r(x)) bytes[k] = Bits.r(x); } double sum = 0.0; for (int k = 0; k < M; k++) sum += Math.pow(2, -1.0 - bytes[k]); return (int) (alpha * M * M / sum); }

Flajolet-Fusy-Gandouet-Meunier 2007

Idea. Harmonic mean of r() values

• Use stochastic splitting

8-bit bytes (code to pack into M loglogN bits omitted)

Page 419: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

39

We can do better (in theory and in practice): HyperLogLog algorithm (2007)

public static long estimate(Iterable<Long> stream, int M) { int[] bytes = new int[M]; for (long x : stream) { int k = hash2(x, M); if (bytes[k] < Bits.r(x)) bytes[k] = Bits.r(x); } double sum = 0.0; for (int k = 0; k < M; k++) sum += Math.pow(2, -1.0 - bytes[k]); return (int) (alpha * M * M / sum); }

Flajolet-Fusy-Gandouet-Meunier 2007

Idea. Harmonic mean of r() values

• Use stochastic splitting

• Keep track of min(r (x)) for each stream

8-bit bytes (code to pack into M loglogN bits omitted)

Page 420: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

39

We can do better (in theory and in practice): HyperLogLog algorithm (2007)

public static long estimate(Iterable<Long> stream, int M) { int[] bytes = new int[M]; for (long x : stream) { int k = hash2(x, M); if (bytes[k] < Bits.r(x)) bytes[k] = Bits.r(x); } double sum = 0.0; for (int k = 0; k < M; k++) sum += Math.pow(2, -1.0 - bytes[k]); return (int) (alpha * M * M / sum); }

Flajolet-Fusy-Gandouet-Meunier 2007

Idea. Harmonic mean of r() values

• Use stochastic splitting

• Keep track of min(r (x)) for each stream

• Return harmonic mean.

8-bit bytes (code to pack into M loglogN bits omitted)

Page 421: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

39

We can do better (in theory and in practice): HyperLogLog algorithm (2007)

public static long estimate(Iterable<Long> stream, int M) { int[] bytes = new int[M]; for (long x : stream) { int k = hash2(x, M); if (bytes[k] < Bits.r(x)) bytes[k] = Bits.r(x); } double sum = 0.0; for (int k = 0; k < M; k++) sum += Math.pow(2, -1.0 - bytes[k]); return (int) (alpha * M * M / sum); }

Flajolet-Fusy-Gandouet-Meunier 2007

about .709 for M = 64

Idea. Harmonic mean of r() values

• Use stochastic splitting

• Keep track of min(r (x)) for each stream

• Return harmonic mean.

8-bit bytes (code to pack into M loglogN bits omitted)

Page 422: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

39

We can do better (in theory and in practice): HyperLogLog algorithm (2007)

public static long estimate(Iterable<Long> stream, int M) { int[] bytes = new int[M]; for (long x : stream) { int k = hash2(x, M); if (bytes[k] < Bits.r(x)) bytes[k] = Bits.r(x); } double sum = 0.0; for (int k = 0; k < M; k++) sum += Math.pow(2, -1.0 - bytes[k]); return (int) (alpha * M * M / sum); }

Flajolet-Fusy-Gandouet-Meunier 2007

Flajolet, Fusy, Gandouet, and Meunier

HyperLogLog: the analysis of a near-

optimal cardinality estimation algorithm

AofA 2007; DMTCS 2007.about .709 for M = 64

Idea. Harmonic mean of r() values

• Use stochastic splitting

• Keep track of min(r (x)) for each stream

• Return harmonic mean.

8-bit bytes (code to pack into M loglogN bits omitted)

Page 423: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

39

We can do better (in theory and in practice): HyperLogLog algorithm (2007)

public static long estimate(Iterable<Long> stream, int M) { int[] bytes = new int[M]; for (long x : stream) { int k = hash2(x, M); if (bytes[k] < Bits.r(x)) bytes[k] = Bits.r(x); } double sum = 0.0; for (int k = 0; k < M; k++) sum += Math.pow(2, -1.0 - bytes[k]); return (int) (alpha * M * M / sum); }

Flajolet-Fusy-Gandouet-Meunier 2007

Flajolet, Fusy, Gandouet, and Meunier

HyperLogLog: the analysis of a near-

optimal cardinality estimation algorithm

AofA 2007; DMTCS 2007.

Theorem (paraphrased to fit context of this talk).

about .709 for M = 64

Idea. Harmonic mean of r() values

• Use stochastic splitting

• Keep track of min(r (x)) for each stream

• Return harmonic mean.

8-bit bytes (code to pack into M loglogN bits omitted)

Page 424: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

39

We can do better (in theory and in practice): HyperLogLog algorithm (2007)

public static long estimate(Iterable<Long> stream, int M) { int[] bytes = new int[M]; for (long x : stream) { int k = hash2(x, M); if (bytes[k] < Bits.r(x)) bytes[k] = Bits.r(x); } double sum = 0.0; for (int k = 0; k < M; k++) sum += Math.pow(2, -1.0 - bytes[k]); return (int) (alpha * M * M / sum); }

Flajolet-Fusy-Gandouet-Meunier 2007

Flajolet, Fusy, Gandouet, and Meunier

HyperLogLog: the analysis of a near-

optimal cardinality estimation algorithm

AofA 2007; DMTCS 2007.

Theorem (paraphrased to fit context of this talk). Under the uniform hashing assumption, HyperLogLog

about .709 for M = 64

Idea. Harmonic mean of r() values

• Use stochastic splitting

• Keep track of min(r (x)) for each stream

• Return harmonic mean.

8-bit bytes (code to pack into M loglogN bits omitted)

Page 425: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

39

We can do better (in theory and in practice): HyperLogLog algorithm (2007)

public static long estimate(Iterable<Long> stream, int M) { int[] bytes = new int[M]; for (long x : stream) { int k = hash2(x, M); if (bytes[k] < Bits.r(x)) bytes[k] = Bits.r(x); } double sum = 0.0; for (int k = 0; k < M; k++) sum += Math.pow(2, -1.0 - bytes[k]); return (int) (alpha * M * M / sum); }

Flajolet-Fusy-Gandouet-Meunier 2007

Flajolet, Fusy, Gandouet, and Meunier

HyperLogLog: the analysis of a near-

optimal cardinality estimation algorithm

AofA 2007; DMTCS 2007.

Theorem (paraphrased to fit context of this talk). Under the uniform hashing assumption, HyperLogLog

• Uses M log log N bits.

about .709 for M = 64

Idea. Harmonic mean of r() values

• Use stochastic splitting

• Keep track of min(r (x)) for each stream

• Return harmonic mean.

8-bit bytes (code to pack into M loglogN bits omitted)

Page 426: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

39

We can do better (in theory and in practice): HyperLogLog algorithm (2007)

public static long estimate(Iterable<Long> stream, int M) { int[] bytes = new int[M]; for (long x : stream) { int k = hash2(x, M); if (bytes[k] < Bits.r(x)) bytes[k] = Bits.r(x); } double sum = 0.0; for (int k = 0; k < M; k++) sum += Math.pow(2, -1.0 - bytes[k]); return (int) (alpha * M * M / sum); }

Flajolet-Fusy-Gandouet-Meunier 2007

Flajolet, Fusy, Gandouet, and Meunier

HyperLogLog: the analysis of a near-

optimal cardinality estimation algorithm

AofA 2007; DMTCS 2007.

Theorem (paraphrased to fit context of this talk). Under the uniform hashing assumption, HyperLogLog

• Uses M log log N bits.• Achieves relative accuracy close to .1.02/

�M

about .709 for M = 64

Idea. Harmonic mean of r() values

• Use stochastic splitting

• Keep track of min(r (x)) for each stream

• Return harmonic mean.

8-bit bytes (code to pack into M loglogN bits omitted)

Page 427: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

40

Space-accuracy tradeoff for HyperLogLog

102451225612864

10%

5%

Relative accuracy:1.02�M

Page 428: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

40

Space-accuracy tradeoff for HyperLogLog

102451225612864

10%

5%

Relative accuracy:

Bottom line (for N < 264).

1.02�M

Page 429: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

40

Space-accuracy tradeoff for HyperLogLog

102451225612864

10%

5%

Relative accuracy:

Bottom line (for N < 264).• Attain 10% relative accuracy with a sketch consisting of 108x6 = 648 bits.

1.02�M

M = 64

Page 430: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

40

Space-accuracy tradeoff for HyperLogLog

102451225612864

10%

5%

Relative accuracy:

Bottom line (for N < 264).• Attain 10% relative accuracy with a sketch consisting of 108x6 = 648 bits.• Attain 3.1% relative accuracy with a sketch consisting of 1024x6 = 6144 bits.

1.02�M

M = 64

M = 1024

Page 431: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

40

Space-accuracy tradeoff for HyperLogLog

102451225612864

10%

5%

Relative accuracy:

Bottom line (for N < 264).• Attain 10% relative accuracy with a sketch consisting of 108x6 = 648 bits.• Attain 3.1% relative accuracy with a sketch consisting of 1024x6 = 6144 bits.

1.02�M

Yay!

M = 64

M = 1024

Page 432: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

41

PCSA vs Hyperloglog

Page 433: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

41

PCSA vs Hyperloglog

Typical PCSA implementations

Page 434: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

41

PCSA vs Hyperloglog

Typical PCSA implementations• Could use M lg N bits, in theory.

Page 435: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

41

PCSA vs Hyperloglog

Typical PCSA implementations• Could use M lg N bits, in theory.• Use 64-bit words to take advantage of machine-language efficiencies.

Page 436: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

41

PCSA vs Hyperloglog

Typical PCSA implementations• Could use M lg N bits, in theory.• Use 64-bit words to take advantage of machine-language efficiencies. • Use (therefore) 64*64 = 4096 bits with M = 64 (for 10% accuracy with N < 264).

Page 437: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

41

PCSA vs Hyperloglog

Typical PCSA implementations• Could use M lg N bits, in theory.• Use 64-bit words to take advantage of machine-language efficiencies. • Use (therefore) 64*64 = 4096 bits with M = 64 (for 10% accuracy with N < 264).

Typical Hyperloglog implementations

Page 438: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

41

PCSA vs Hyperloglog

Typical PCSA implementations• Could use M lg N bits, in theory.• Use 64-bit words to take advantage of machine-language efficiencies. • Use (therefore) 64*64 = 4096 bits with M = 64 (for 10% accuracy with N < 264).

Typical Hyperloglog implementations• Could use M lg lg N bits, in theory.

Page 439: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

41

PCSA vs Hyperloglog

Typical PCSA implementations• Could use M lg N bits, in theory.• Use 64-bit words to take advantage of machine-language efficiencies. • Use (therefore) 64*64 = 4096 bits with M = 64 (for 10% accuracy with N < 264).

Typical Hyperloglog implementations• Could use M lg lg N bits, in theory.• Use 8-bit bytes to take advantage of machine-language efficiencies.

Page 440: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

41

PCSA vs Hyperloglog

Typical PCSA implementations• Could use M lg N bits, in theory.• Use 64-bit words to take advantage of machine-language efficiencies. • Use (therefore) 64*64 = 4096 bits with M = 64 (for 10% accuracy with N < 264).

Typical Hyperloglog implementations• Could use M lg lg N bits, in theory.• Use 8-bit bytes to take advantage of machine-language efficiencies. • Use (therefore) 108*8 = 864 bits with M = 108 (for 10% accuracy with N < 264).

Page 441: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

42

Validation of Hyperloglog

Page 442: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

42

Validation of Hyperloglog

S. Heule, M. Nunkesser and A. HallHyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm.Extending Database Technology/International Conference on Database Theory 2013.

Page 443: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

42

Validation of Hyperloglog

S. Heule, M. Nunkesser and A. HallHyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm.Extending Database Technology/International Conference on Database Theory 2013.

Page 444: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

42

Validation of Hyperloglog

S. Heule, M. Nunkesser and A. HallHyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm.Extending Database Technology/International Conference on Database Theory 2013.

Page 445: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

42

Validation of Hyperloglog

S. Heule, M. Nunkesser and A. HallHyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm.Extending Database Technology/International Conference on Database Theory 2013.

Page 446: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

42

Validation of Hyperloglog

S. Heule, M. Nunkesser and A. HallHyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm.Extending Database Technology/International Conference on Database Theory 2013.

Page 447: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

42

Validation of Hyperloglog

S. Heule, M. Nunkesser and A. HallHyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm.Extending Database Technology/International Conference on Database Theory 2013.

Page 448: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

42

Validation of Hyperloglog

S. Heule, M. Nunkesser and A. HallHyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm.Extending Database Technology/International Conference on Database Theory 2013.

Page 449: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

42

Validation of Hyperloglog

S. Heule, M. Nunkesser and A. HallHyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm.Extending Database Technology/International Conference on Database Theory 2013.

Page 450: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

42

Validation of Hyperloglog

S. Heule, M. Nunkesser and A. HallHyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm.Extending Database Technology/International Conference on Database Theory 2013.

Philippe Flajolet, mathematician, data scientist, and computer scientist extraordinaire

Page 451: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

OF

Cardinality Estimation

•Rules of the game •Probabilistic counting •Stochastic averaging •Refinements •Final frontier

Page 452: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

44

We can do a bit better (in theory) but not much better

Upper bound

Lower bound

Page 453: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

44

We can do a bit better (in theory) but not much better

Upper bound

Lower bound

Indyk and Woodruff Tight Lower Bounds for the Distinct Elements Problem, FOCS 2003.

Page 454: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

44

We can do a bit better (in theory) but not much better

Upper bound

Lower bound

Indyk and Woodruff Tight Lower Bounds for the Distinct Elements Problem, FOCS 2003.

Theorem (paraphrased to fit context of this talk). Any algorithm that achieves relative accuracy must use bitsO(1/

�M) Ω(M)

Page 455: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

44

We can do a bit better (in theory) but not much better

Upper bound

Lower bound

Indyk and Woodruff Tight Lower Bounds for the Distinct Elements Problem, FOCS 2003.

Theorem (paraphrased to fit context of this talk). Any algorithm that achieves relative accuracy must use bitsO(1/

�M) Ω(M)

loglogN improvement possible

Page 456: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

44

We can do a bit better (in theory) but not much better

Kane, Nelson, and Woodruff Optimal Algorithm for the Distinct Elements Problem, PODS 2010.

Upper bound

Lower bound

Indyk and Woodruff Tight Lower Bounds for the Distinct Elements Problem, FOCS 2003.

Theorem (paraphrased to fit context of this talk). Any algorithm that achieves relative accuracy must use bitsO(1/

�M) Ω(M)

loglogN improvement possible

Page 457: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

44

We can do a bit better (in theory) but not much better

Kane, Nelson, and Woodruff Optimal Algorithm for the Distinct Elements Problem, PODS 2010.

Upper bound

Lower bound

Theorem (paraphrased to fit context of this talk). With strongly universal hashing there exists an algorithm that • Uses O(M ) bits. • Achieves relative accuracy .O(1/

�M)

Indyk and Woodruff Tight Lower Bounds for the Distinct Elements Problem, FOCS 2003.

Theorem (paraphrased to fit context of this talk). Any algorithm that achieves relative accuracy must use bitsO(1/

�M) Ω(M)

loglogN improvement possible

Page 458: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

44

We can do a bit better (in theory) but not much better

Kane, Nelson, and Woodruff Optimal Algorithm for the Distinct Elements Problem, PODS 2010.

Upper bound

Lower bound

Theorem (paraphrased to fit context of this talk). With strongly universal hashing there exists an algorithm that • Uses O(M ) bits. • Achieves relative accuracy .O(1/

�M)

Indyk and Woodruff Tight Lower Bounds for the Distinct Elements Problem, FOCS 2003.

Theorem (paraphrased to fit context of this talk). Any algorithm that achieves relative accuracy must use bitsO(1/

�M) Ω(M)

loglogN improvement possible

optimal

Page 459: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

44

We can do a bit better (in theory) but not much better

Kane, Nelson, and Woodruff Optimal Algorithm for the Distinct Elements Problem, PODS 2010.

Upper bound

Lower bound

Theorem (paraphrased to fit context of this talk). With strongly universal hashing there exists an algorithm that • Uses O(M ) bits. • Achieves relative accuracy .

Unlikely to have impact on cardinality estimation in practice

O(1/�M)

Indyk and Woodruff Tight Lower Bounds for the Distinct Elements Problem, FOCS 2003.

Theorem (paraphrased to fit context of this talk). Any algorithm that achieves relative accuracy must use bitsO(1/

�M) Ω(M)

loglogN improvement possible

optimal

Page 460: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

44

We can do a bit better (in theory) but not much better

Kane, Nelson, and Woodruff Optimal Algorithm for the Distinct Elements Problem, PODS 2010.

Upper bound

Lower bound

Theorem (paraphrased to fit context of this talk). With strongly universal hashing there exists an algorithm that • Uses O(M ) bits. • Achieves relative accuracy .

Unlikely to have impact on cardinality estimation in practice

• Tough to beat HyperLogLog’s low stream-processing expense.

O(1/�M)

Indyk and Woodruff Tight Lower Bounds for the Distinct Elements Problem, FOCS 2003.

Theorem (paraphrased to fit context of this talk). Any algorithm that achieves relative accuracy must use bitsO(1/

�M) Ω(M)

loglogN improvement possible

optimal

Page 461: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

44

We can do a bit better (in theory) but not much better

Kane, Nelson, and Woodruff Optimal Algorithm for the Distinct Elements Problem, PODS 2010.

Upper bound

Lower bound

Theorem (paraphrased to fit context of this talk). With strongly universal hashing there exists an algorithm that • Uses O(M ) bits. • Achieves relative accuracy .

Unlikely to have impact on cardinality estimation in practice

• Tough to beat HyperLogLog’s low stream-processing expense.

• Constants hidden in O-notation not likely to be < 6

O(1/�M)

Indyk and Woodruff Tight Lower Bounds for the Distinct Elements Problem, FOCS 2003.

Theorem (paraphrased to fit context of this talk). Any algorithm that achieves relative accuracy must use bitsO(1/

�M) Ω(M)

loglogN improvement possible

optimal

Page 462: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

44

We can do a bit better (in theory) but not much better

Kane, Nelson, and Woodruff Optimal Algorithm for the Distinct Elements Problem, PODS 2010.

Upper bound

Lower bound

Theorem (paraphrased to fit context of this talk). With strongly universal hashing there exists an algorithm that • Uses O(M ) bits. • Achieves relative accuracy .

Unlikely to have impact on cardinality estimation in practice

• Tough to beat HyperLogLog’s low stream-processing expense.

• Constants hidden in O-notation not likely to be < 6

• No validation

O(1/�M)

Indyk and Woodruff Tight Lower Bounds for the Distinct Elements Problem, FOCS 2003.

Theorem (paraphrased to fit context of this talk). Any algorithm that achieves relative accuracy must use bitsO(1/

�M) Ω(M)

loglogN improvement possible

optimal

Page 463: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

45

Can we beat HyperLogLog in practice?

Page 464: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

45

Can we beat HyperLogLog in practice?

Necessary characteristics of a better algorithm

Page 465: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

45

Can we beat HyperLogLog in practice?

Necessary characteristics of a better algorithm• Makes one pass through the stream.

Page 466: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

45

Can we beat HyperLogLog in practice?

Necessary characteristics of a better algorithm• Makes one pass through the stream.• Uses a few dozen machine instructions per value

Page 467: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

45

Can we beat HyperLogLog in practice?

Necessary characteristics of a better algorithm• Makes one pass through the stream.• Uses a few dozen machine instructions per value• Uses a few hundred bits

Page 468: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

45

Can we beat HyperLogLog in practice?

Necessary characteristics of a better algorithm• Makes one pass through the stream.• Uses a few dozen machine instructions per value• Uses a few hundred bits• Achieves 10% relative accuracy or better

Page 469: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

45

Can we beat HyperLogLog in practice?

Necessary characteristics of a better algorithm• Makes one pass through the stream.• Uses a few dozen machine instructions per value• Uses a few hundred bits• Achieves 10% relative accuracy or better

“ I’ve long thought that there should be a simple algorithm that uses a small constant times M bits…”

− Jérémie Lumbroso

Page 470: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

45

Can we beat HyperLogLog in practice?

Necessary characteristics of a better algorithm• Makes one pass through the stream.• Uses a few dozen machine instructions per value• Uses a few hundred bits• Achieves 10% relative accuracy or better

“ I’ve long thought that there should be a simple algorithm that uses a small constant times M bits…”

− Jérémie Lumbroso

Page 471: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

45

Can we beat HyperLogLog in practice?

Necessary characteristics of a better algorithm• Makes one pass through the stream.• Uses a few dozen machine instructions per value• Uses a few hundred bits• Achieves 10% relative accuracy or better

machine instructions per stream element

memory bound

memory bound when N < 264

# bits for 10% accuracy when N < 264

HyperLogLog 20–30 M loglog N 6M 648

“ I’ve long thought that there should be a simple algorithm that uses a small constant times M bits…”

− Jérémie Lumbroso

Page 472: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

45

Can we beat HyperLogLog in practice?

Necessary characteristics of a better algorithm• Makes one pass through the stream.• Uses a few dozen machine instructions per value• Uses a few hundred bits• Achieves 10% relative accuracy or better

machine instructions per stream element

memory bound

memory bound when N < 264

# bits for 10% accuracy when N < 264

HyperLogLog 20–30 M loglog N 6M 648

BetterAlgorithm a few dozen a few hundred

“ I’ve long thought that there should be a simple algorithm that uses a small constant times M bits…”

− Jérémie Lumbroso

Page 473: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

45

Can we beat HyperLogLog in practice?

Also, results need to be validated through extensive experimentation.

Necessary characteristics of a better algorithm• Makes one pass through the stream.• Uses a few dozen machine instructions per value• Uses a few hundred bits• Achieves 10% relative accuracy or better

machine instructions per stream element

memory bound

memory bound when N < 264

# bits for 10% accuracy when N < 264

HyperLogLog 20–30 M loglog N 6M 648

BetterAlgorithm a few dozen a few hundred

I love HyperLogLog

“ I’ve long thought that there should be a simple algorithm that uses a small constant times M bits…”

− Jérémie Lumbroso

Page 474: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

46

A proposal: HyperBitBit (Sedgewick, 2016)

Page 475: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

46

A proposal: HyperBitBit (Sedgewick, 2016)

public static long estimate(Iterable<String> stream, int M) { int lgN = 5; long sketch = 0; long sketch2 = 0; for (String x : stream) { long x = hash(s); int k = hash2(x, 64); if (r(x) > lgN) sketch = sketch | (1L << k); if (r(x) > lgN + 1) sketch2 = sketch2 | (1L << k); if (p(sketch) > 31) { sketch = sketch2; lgN++; sketch2 = 0; } } return (int) (Math.pow(2, lgN + 5.4 + p(sketch)/32.0)); }

Page 476: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

46

A proposal: HyperBitBit (Sedgewick, 2016)

public static long estimate(Iterable<String> stream, int M) { int lgN = 5; long sketch = 0; long sketch2 = 0; for (String x : stream) { long x = hash(s); int k = hash2(x, 64); if (r(x) > lgN) sketch = sketch | (1L << k); if (r(x) > lgN + 1) sketch2 = sketch2 | (1L << k); if (p(sketch) > 31) { sketch = sketch2; lgN++; sketch2 = 0; } } return (int) (Math.pow(2, lgN + 5.4 + p(sketch)/32.0)); }

Idea.

Page 477: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

46

A proposal: HyperBitBit (Sedgewick, 2016)

public static long estimate(Iterable<String> stream, int M) { int lgN = 5; long sketch = 0; long sketch2 = 0; for (String x : stream) { long x = hash(s); int k = hash2(x, 64); if (r(x) > lgN) sketch = sketch | (1L << k); if (r(x) > lgN + 1) sketch2 = sketch2 | (1L << k); if (p(sketch) > 31) { sketch = sketch2; lgN++; sketch2 = 0; } } return (int) (Math.pow(2, lgN + 5.4 + p(sketch)/32.0)); }

Idea.

• lgN is estimate of lgN

Page 478: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

46

A proposal: HyperBitBit (Sedgewick, 2016)

public static long estimate(Iterable<String> stream, int M) { int lgN = 5; long sketch = 0; long sketch2 = 0; for (String x : stream) { long x = hash(s); int k = hash2(x, 64); if (r(x) > lgN) sketch = sketch | (1L << k); if (r(x) > lgN + 1) sketch2 = sketch2 | (1L << k); if (p(sketch) > 31) { sketch = sketch2; lgN++; sketch2 = 0; } } return (int) (Math.pow(2, lgN + 5.4 + p(sketch)/32.0)); }

Idea.

• lgN is estimate of

• sketch is 64 indicators whether to increment lgN

lgN

Page 479: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

46

A proposal: HyperBitBit (Sedgewick, 2016)

public static long estimate(Iterable<String> stream, int M) { int lgN = 5; long sketch = 0; long sketch2 = 0; for (String x : stream) { long x = hash(s); int k = hash2(x, 64); if (r(x) > lgN) sketch = sketch | (1L << k); if (r(x) > lgN + 1) sketch2 = sketch2 | (1L << k); if (p(sketch) > 31) { sketch = sketch2; lgN++; sketch2 = 0; } } return (int) (Math.pow(2, lgN + 5.4 + p(sketch)/32.0)); }

Idea.

• lgN is estimate of

• sketch is 64 indicators whether to increment lgN

• sketch2 is is 64 indicators whether to increment lgN by 2

lgN

Page 480: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

46

A proposal: HyperBitBit (Sedgewick, 2016)

public static long estimate(Iterable<String> stream, int M) { int lgN = 5; long sketch = 0; long sketch2 = 0; for (String x : stream) { long x = hash(s); int k = hash2(x, 64); if (r(x) > lgN) sketch = sketch | (1L << k); if (r(x) > lgN + 1) sketch2 = sketch2 | (1L << k); if (p(sketch) > 31) { sketch = sketch2; lgN++; sketch2 = 0; } } return (int) (Math.pow(2, lgN + 5.4 + p(sketch)/32.0)); }

Idea.

• lgN is estimate of

• sketch is 64 indicators whether to increment lgN

• sketch2 is is 64 indicators whether to increment lgN by 2

• Update when half the bits in sketch are 1

lgN

Page 481: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

46

A proposal: HyperBitBit (Sedgewick, 2016)

public static long estimate(Iterable<String> stream, int M) { int lgN = 5; long sketch = 0; long sketch2 = 0; for (String x : stream) { long x = hash(s); int k = hash2(x, 64); if (r(x) > lgN) sketch = sketch | (1L << k); if (r(x) > lgN + 1) sketch2 = sketch2 | (1L << k); if (p(sketch) > 31) { sketch = sketch2; lgN++; sketch2 = 0; } } return (int) (Math.pow(2, lgN + 5.4 + p(sketch)/32.0)); }

Idea.

• lgN is estimate of

• sketch is 64 indicators whether to increment lgN

• sketch2 is is 64 indicators whether to increment lgN by 2

• Update when half the bits in sketch are 1

• correct with p(sketch)

lgN

bias factor (determined empirically)

and bias factor

Page 482: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

46

A proposal: HyperBitBit (Sedgewick, 2016)

public static long estimate(Iterable<String> stream, int M) { int lgN = 5; long sketch = 0; long sketch2 = 0; for (String x : stream) { long x = hash(s); int k = hash2(x, 64); if (r(x) > lgN) sketch = sketch | (1L << k); if (r(x) > lgN + 1) sketch2 = sketch2 | (1L << k); if (p(sketch) > 31) { sketch = sketch2; lgN++; sketch2 = 0; } } return (int) (Math.pow(2, lgN + 5.4 + p(sketch)/32.0)); }

Idea.

• lgN is estimate of

• sketch is 64 indicators whether to increment lgN

• sketch2 is is 64 indicators whether to increment lgN by 2

• Update when half the bits in sketch are 1

• correct with p(sketch)

lgN

bias factor (determined empirically)

and bias factor

Q. Does this even work?

Page 483: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

Page 484: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example HyperBitBit estimates

Page 485: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt

HyperBitBit estimates

Page 486: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219

HyperBitBit estimates

Page 487: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt

HyperBitBit estimates

Page 488: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889

HyperBitBit estimates

Page 489: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889% java HyperBitBit 4000000 < log.07.f3.txt

HyperBitBit estimates

Page 490: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889% java HyperBitBit 4000000 < log.07.f3.txt916801

HyperBitBit estimates

Page 491: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889% java HyperBitBit 4000000 < log.07.f3.txt916801% java HyperBitBit 6000000 < log.07.f3.txt

HyperBitBit estimates

Page 492: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889% java HyperBitBit 4000000 < log.07.f3.txt916801% java HyperBitBit 6000000 < log.07.f3.txt1044043

HyperBitBit estimates

Page 493: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889% java HyperBitBit 4000000 < log.07.f3.txt916801% java HyperBitBit 6000000 < log.07.f3.txt1044043

HyperBitBit estimates

1,000,000 2,000,000 4,000,000 6,000,000

Page 494: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889% java HyperBitBit 4000000 < log.07.f3.txt916801% java HyperBitBit 6000000 < log.07.f3.txt1044043

HyperBitBit estimates

1,000,000 2,000,000 4,000,000 6,000,000

Exact 242,601 483,477 883,071 1,097,944

Page 495: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889% java HyperBitBit 4000000 < log.07.f3.txt916801% java HyperBitBit 6000000 < log.07.f3.txt1044043

HyperBitBit estimates

1,000,000 2,000,000 4,000,000 6,000,000

Exact 242,601 483,477 883,071 1,097,944

HyperBitBit 234,219 499,889 916,801 1,044,043

Page 496: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889% java HyperBitBit 4000000 < log.07.f3.txt916801% java HyperBitBit 6000000 < log.07.f3.txt1044043

HyperBitBit estimates

1,000,000 2,000,000 4,000,000 6,000,000

Exact 242,601 483,477 883,071 1,097,944

HyperBitBit 234,219 499,889 916,801 1,044,043

ratio 1.05 1.03 0.96 1.03

Page 497: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889% java HyperBitBit 4000000 < log.07.f3.txt916801% java HyperBitBit 6000000 < log.07.f3.txt1044043

HyperBitBit estimates

Conjecture. On practical data, HyperBitBit, for N < 264,

1,000,000 2,000,000 4,000,000 6,000,000

Exact 242,601 483,477 883,071 1,097,944

HyperBitBit 234,219 499,889 916,801 1,044,043

ratio 1.05 1.03 0.96 1.03

Page 498: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889% java HyperBitBit 4000000 < log.07.f3.txt916801% java HyperBitBit 6000000 < log.07.f3.txt1044043

HyperBitBit estimates

Conjecture. On practical data, HyperBitBit, for N < 264, • Uses 128 + 6 bits.

1,000,000 2,000,000 4,000,000 6,000,000

Exact 242,601 483,477 883,071 1,097,944

HyperBitBit 234,219 499,889 916,801 1,044,043

ratio 1.05 1.03 0.96 1.03

Page 499: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889% java HyperBitBit 4000000 < log.07.f3.txt916801% java HyperBitBit 6000000 < log.07.f3.txt1044043

HyperBitBit estimates

Conjecture. On practical data, HyperBitBit, for N < 264, • Uses 128 + 6 bits.• Estimates cardinality within 10% of the actual.

1,000,000 2,000,000 4,000,000 6,000,000

Exact 242,601 483,477 883,071 1,097,944

HyperBitBit 234,219 499,889 916,801 1,044,043

ratio 1.05 1.03 0.96 1.03

Page 500: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

47

Initial experiments

% java Hash 1000000 < log.07.f3.txt 242601 % java Hash 2000000 < log.07.f3.txt 483477 % java Hash 4000000 < log.07.f3.txt 883071 % java Hash 6000000 < log.07.f3.txt 1097944

Exact values for web log example

% java HyperBitBit 1000000 < log.07.f3.txt234219% java HyperBitBit 2000000 < log.07.f3.txt499889% java HyperBitBit 4000000 < log.07.f3.txt916801% java HyperBitBit 6000000 < log.07.f3.txt1044043

HyperBitBit estimates

Conjecture. On practical data, HyperBitBit, for N < 264, • Uses 128 + 6 bits.• Estimates cardinality within 10% of the actual.

1,000,000 2,000,000 4,000,000 6,000,000

Exact 242,601 483,477 883,071 1,097,944

HyperBitBit 234,219 499,889 916,801 1,044,043

ratio 1.05 1.03 0.96 1.03

Next steps. • Analyze. • Experiment. • Iterate

Page 501: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

48

Summary/timeline for cardinality estimation

Page 502: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

48

Summary/timeline for cardinality estimation

hashing assumption

feasible and

validated?memory

bound (bits)relative

accuracy constant

# bits for 10% accuracy when N < 264

Page 503: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

48

Summary/timeline for cardinality estimation

hashing assumption

feasible and

validated?memory

bound (bits)relative

accuracy constant

# bits for 10% accuracy when N < 264

1970 Bloom Bloom filter uniform ✓ kN > 264

Page 504: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

48

Summary/timeline for cardinality estimation

hashing assumption

feasible and

validated?memory

bound (bits)relative

accuracy constant

# bits for 10% accuracy when N < 264

1970 Bloom Bloom filter uniform ✓ kN > 264

1985 Flajolet-Martin PCSA uniform ✓ M log N 0.78 4096

Page 505: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

48

Summary/timeline for cardinality estimation

hashing assumption

feasible and

validated?memory

bound (bits)relative

accuracy constant

# bits for 10% accuracy when N < 264

1970 Bloom Bloom filter uniform ✓ kN > 264

1985 Flajolet-Martin PCSA uniform ✓ M log N 0.78 4096

1996 Alon–Matias–Szegedy [theorem] strong universal ✗ O(M log N ) O(1) ?

Page 506: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

48

Summary/timeline for cardinality estimation

hashing assumption

feasible and

validated?memory

bound (bits)relative

accuracy constant

# bits for 10% accuracy when N < 264

1970 Bloom Bloom filter uniform ✓ kN > 264

1985 Flajolet-Martin PCSA uniform ✓ M log N 0.78 4096

1996 Alon–Matias–Szegedy [theorem] strong universal ✗ O(M log N ) O(1) ?

2002Bar–Yossef–Jayram–Kumar–Sivakumar–

Trevisan[theorem] strong

universal ✗ O(M log log N ) O(1) ?

Page 507: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

48

Summary/timeline for cardinality estimation

hashing assumption

feasible and

validated?memory

bound (bits)relative

accuracy constant

# bits for 10% accuracy when N < 264

1970 Bloom Bloom filter uniform ✓ kN > 264

1985 Flajolet-Martin PCSA uniform ✓ M log N 0.78 4096

1996 Alon–Matias–Szegedy [theorem] strong universal ✗ O(M log N ) O(1) ?

2002Bar–Yossef–Jayram–Kumar–Sivakumar–

Trevisan[theorem] strong

universal ✗ O(M log log N ) O(1) ?

2003 Durand–Flajolet LogLog uniform ✓ M lglg N 1.30 1536

Page 508: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

48

Summary/timeline for cardinality estimation

hashing assumption

feasible and

validated?memory

bound (bits)relative

accuracy constant

# bits for 10% accuracy when N < 264

1970 Bloom Bloom filter uniform ✓ kN > 264

1985 Flajolet-Martin PCSA uniform ✓ M log N 0.78 4096

1996 Alon–Matias–Szegedy [theorem] strong universal ✗ O(M log N ) O(1) ?

2002Bar–Yossef–Jayram–Kumar–Sivakumar–

Trevisan[theorem] strong

universal ✗ O(M log log N ) O(1) ?

2003 Durand–Flajolet LogLog uniform ✓ M lglg N 1.30 1536

2007 Flajolet–Fusy–Gandouet–Meunier HyperLogLog uniform ✓ M lglg N 1.04 648

Page 509: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

48

Summary/timeline for cardinality estimation

hashing assumption

feasible and

validated?memory

bound (bits)relative

accuracy constant

# bits for 10% accuracy when N < 264

1970 Bloom Bloom filter uniform ✓ kN > 264

1985 Flajolet-Martin PCSA uniform ✓ M log N 0.78 4096

1996 Alon–Matias–Szegedy [theorem] strong universal ✗ O(M log N ) O(1) ?

2002Bar–Yossef–Jayram–Kumar–Sivakumar–

Trevisan[theorem] strong

universal ✗ O(M log log N ) O(1) ?

2003 Durand–Flajolet LogLog uniform ✓ M lglg N 1.30 1536

2007 Flajolet–Fusy–Gandouet–Meunier HyperLogLog uniform ✓ M lglg N 1.04 648

2010 Kane–Nelson–Woodruff [theorem] strong

universal ✗ O(M ) + lglg N O(1) ?

Page 510: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

48

Summary/timeline for cardinality estimation

hashing assumption

feasible and

validated?memory

bound (bits)relative

accuracy constant

# bits for 10% accuracy when N < 264

1970 Bloom Bloom filter uniform ✓ kN > 264

1985 Flajolet-Martin PCSA uniform ✓ M log N 0.78 4096

1996 Alon–Matias–Szegedy [theorem] strong universal ✗ O(M log N ) O(1) ?

2002Bar–Yossef–Jayram–Kumar–Sivakumar–

Trevisan[theorem] strong

universal ✗ O(M log log N ) O(1) ?

2003 Durand–Flajolet LogLog uniform ✓ M lglg N 1.30 1536

2007 Flajolet–Fusy–Gandouet–Meunier HyperLogLog uniform ✓ M lglg N 1.04 648

2010 Kane–Nelson–Woodruff [theorem] strong

universal ✗ O(M ) + lglg N O(1) ?

2018+ RS–? HyperBitBit uniform ✓(?) 2M + lglg N ? 134 (?)

Page 511: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

49

Happy Birthday, Don!

Anders Björner Mireille Bousquet-Mélou

Erik DemainePersi Diaconis

Michael DrmotaRon Graham

Yannis Haralambous Susan HolmesSvante Janson

Dick KarpJohn Knuth

Svante LinussonLaci Lovász

Jan OverduinMike Paterson

Tim Roughgarden Martin RuckertBob Sedgewick

Jeffrey ShallitRichard Stanley

Wojtek Szpankowski Bob Tarjan

Greg TuckerAndrew Yao

Colloquium forDon Knuth’s

80th birthday

AlgorithmsCombinatoricsInformation

http://knuth80.elfbrink.se

Page 512: Robert Sedgewick Princeton Universityrs/talks/CardinalityX.pdf · Knuth’s insight: AofA is a scientific endeavor. •Start with a working program (algorithm implementation). •Develop

Cardinality Estimation

with special thanks to Jérémie Lumbroso

Robert Sedgewick Princeton University