Coupon collector’s 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 the probabilit y that a tossed ball lands in any given bin is 1 b the expected number of n balls n 1 b n 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 2 q 3 qq 4 qqq ] p [( 1 q q (1 qq qqq ) q qq ) qq (1 q ) qqq(1 ) ] (1 E p( p( 1 1 q qq q 1 1 q )( qqq ) 1 1 q )( 1 q qq qqq ) 1 1 q ) p 1 p 1 p 1 p 1 b 1 ( ) b 1 p 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 1 1 1 1 1 ( ) b b 1 b 2 2 1 1 1 1 1 1 ( ) 1 2 3 b 1 b i 1 b b2 2 1 1 i i 1 b x 1 1 x b dx 1 1 i i 1 b x 1 1 x dx [ln x ]1 c ln b c b c constant proof b b b 1 i i 1 b( 1 b 1 b 1 1 b2 1 1 ) 2 1 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