Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Coupon collectors problem

CS658
Po-Ching Liu

Question
A person trying to collect each of b different
coupons must acquire approximately x
randomly obtained coupons in order to
succeed.
find x

Balls and bins


1st question:
If n identical balls are tossed randomly into b
bins, then how many balls will fall in a given bin?
n
Ans:
b

1
the probabilit y that a tossed ball lands in any given bin is ,
b
1 n
the expected number of n balls n .
b b

Second
Second question:
Find the expected number of balls we need to
toss (one ball for each toss) until a given bin
contains a ball.
The number of bins = b

Ans: E = b

proof

p=pro(success) = 1/b=1-q
q=pro( without success)=1-1/b=1- p
E[# of tosses until a given bin contains a ball]
E 1 p 2 qp 3 qqp 4 qqqp
p[1 2q 3qq 4qqq ]
p[(1 q qq qqq )
q (1

q
qq(1

qq )
q )

qqq(1 )
]
1
1
(1 q qq qqq )

1 q p
1
E p(
)(1 q qq qqq )
1 q
1
1
1 1 1
1
p(
)(
) p
b
1 q 1 q
p p p (1)
b

Third
the third question:
Find the expected number of balls we need to
toss (one ball for each toss) until every bin
contains at least one ball?
The number of bins = b
Ans: E = b(lnb+O(1))

proof
There are b stages
The ith stage consists of the tosses after
the (i-1)th hit until the ith hit.
how many balls do we have to toss in order to
move from the (i-1) th stage to the ith stage
there are (i-1) bins that contain balls and
b-(i-1) empty bins.
for each toss in the ith stage, the
probability of obtaining a hit is (b-i+1)/b.

proof

b
E[# of tosses in the ith stage]= b i 1

st to the bth stage]


E[total
#
of
tosses
in
the
1
b
b
1
1
1
1 1
=
b(

b i 1
b b 1 b 2
1
1
1
1 1
(

)
b b 1 b 2
2 1
1 1 1
1
1
(
)
1 2 3
b 1 b
b
1

i 1 i
i 1

2 1

b
1
1
1
dx 1 dx [ln x]1b c ln b c
x
x
i 1 i
x 1
x 1

c constant

proof
b

b
1
1
1
1 1
b(

b b 1 b 2
2 1
i 1 b 1 i
b(ln b c) b(ln b O(1))
We find the two following questions are the same:
Q:Find the expected number x of balls we need to toss (one
ball for each toss) until every bin contains at least one ball?
The number of bins = b
Q: A person trying to collect each of b different coupons
must acquire approximately x randomly obtained coupons
in order to succeed.

x b( ln b O(1 ))

Code by java compile: javac CouponCollector.java


Run: java CouponCollector

public class CouponCollector {


public static void main(String[] args) {
int N = 50; // number of different card types
boolean[] found = new boolean[N];
// found[i] = true ==> if card i has been collected ,
int cardcnt = 0;
// total number of cards collected
int valcnt = 0;
// number of distinct cards

false ==> the new type of card

// repeatedly choose a random card and check whether it's a new one
while (valcnt < N) {
int val = (int) (Math.random() * N);
// random card between 0 and N-1
cardcnt++;
// we collected one more card ==>total number +1
if (!found[val]) valcnt++;
// it's a new card type
found[val] = true;
// update found[]
}
// print the total number of cards collected
System.out.println(cardcnt);

}
}

references
Thomas H. Cormen,Charles E. Leiserson,
Ronald L. Rivest, Clifford Stein(1990),
Introduction to Algorithms, The MIT Press,
Cambridge, Massachusetts London, England,
pp. 109-110.
http://wwwstat.stanford.edu/~susan/surprise/Collector.ht
ml

You might also like