Coupon Problem and Hat Problem - Randomized algorithms
1.Coupon Problem
A company selling jeans gives a coupon for each pair of jeans. There are 'n' different coupons. Collecting 'n' different coupons would give you free jeans.How many jeans do you expect to buy before getting a free one?
Step-by-Step Explanation:
Coupon Collector Problem Setup:
- There are different coupons.
- To collect all coupons, you buy jeans repeatedly and receive a random coupon with each purchase.
- The problem is to find the expected number of purchases required to collect all coupons.
Breaking Down the Problem:
- To collect the first coupon, you always get a new one. This takes 1 purchase (expected value = 1).
- coupon. The probability of getting a new coupon is , and the expected number of purchases is
- To collect the third coupon, you already have coupons. The probability of getting a new coupon is , and the expected number of purchases is .
- This pattern continues until the -th coupon.
Summing the Contributions:
- The total expected number of purchases is the sum of the expected number of purchases for each new coupon:
Factoring Out :
- Factor out of each term:
The series inside the parentheses is the definition of the harmonic number
Hn:
Therefore, the expected number of purchases is:
1. Initialize Variables:
- Total Jeans Bought: Start with a counter set to zero to track how many pairs of jeans you have bought.
- Coupons Collected: Use a set to keep track of the different types of coupons you have received.
- Number of Coupons: The total number of different coupon types is n.
- Loop Until All Coupons Are Collected: Continue buying jeans until you have one of each type of coupon in your set.
- Each time you buy a pair of jeans, increase the counter for the total jeans bought by one.
- When you buy a pair of jeans, you get a coupon. Add this coupon to your set of collected coupons.
- Check if you have collected all n different types of coupons by comparing the size of your set to n.
- To get a reliable estimate, repeat the entire buying process many times (e.g., 100,000 times).
- Keep a running total of the number of jeans bought across all these repetitions.
- After completing all repetitions, calculate the average number of jeans bought by dividing the total number of jeans bought by the number of repetitions.
- The average number of jeans bought from the repeated simulations gives you a good estimate of how many pairs of jeans you would typically need to buy before collecting all 'n' coupons and getting a free pair.
2.Hat Problem
'n' people go to a party and drop off their hats to a hat-check person.When the party is over, a different hat-check person is on duty and returns then hats randomly back to each person. What is the expected number of people who get back their hats?
This problem is a classic example of a derangement problem, where the goal is to find how many people receive their own hat when hats are returned at random. Let's walk through the reasoning step by step to find the expected number of people who get back their own hats.
Key Points:
- We have people.
- Each person has dropped off their hat with a hat-check person.
- The hats are returned randomly.
We need to calculate the expected number of people who will get their own hat back.
Solution Outline:
Let be an indicator random variable for each person , where:
Thus, the total number of people who get their own hats back is the sum of all these indicator variables:
Now, we want to find the expected value of , i.e.,
Step 1: Calculate the Expected Value of
Since
Thus, for any :
Step 2: Calculate the Expected Value of
Since , we can use the linearity of expectation:
Each so:
Conclusion:
The expected number of people who get their own hats back is 1, regardless of how many people are at the party.
This result might be surprising, but it reflects the fact that, on average, only one person is expected to get their own hat back in this random hat-return scenario.
Algorithmic Solution:
1. Initialization:
- Set up variables to count the total number of correct matches across all simulations.
- Define the number of simulations to ensure statistical reliability.
- Define the number of people n.
2. Simulate the Process:
- Create a list of hats representing each person.
- Shuffle the list to simulate random distribution.
- Count how many people receive their own hat.
- Add this count to the total number of correct matches.
- Divide the total number of correct matches by the number of simulations to get the average.
- Print the expected number of people who get their own hats back.
- Suppose there are 5 people at the party.
- Initialize total_correct to 0, which will keep track of the total number of people who receive their own hat across multiple simulations and num_simulations to 100,000.
- For each simulation:
– Shuffle the list, e.g., [3, 1, 5, 2, 4].
– Initialize correct to 0.
- – Check each person:
∗ Person 2 (hat 1) - not correct.
∗ Person 3 (hat 5) - not correct.
∗ Person 4 (hat 2) - not correct.
∗ Person 5 (hat 4) - not correct.
- – Add correct (which is 0 for this run) to total_correct.
- Repeat the simulation 100,000 times, each time shuffling the hats and counting how many people get their own hats back.
- Sum the number of correct matches across all simulations.
- Divide total_correct by 100,000 to get the average number of people who receive their own hat.
Comments
Post a Comment