1 chapter 6 heapsort. 2 about this lecture introduce heap shape property and heap property heap...
DESCRIPTION
3 Heap A heap (or binary heap) is a binary tree that satisfies both: (1) Shape Property – All levels, except deepest, are fully filled – Deepest level is filled from left to right (2) Heap Property – Value of a node ≤ Value of its childrenTRANSCRIPT
![Page 1: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/1.jpg)
1
Chapter 6 Heapsort
![Page 2: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/2.jpg)
2
About this lecture• Introduce Heap– Shape Property and Heap Property– Heap Operations
• Heapsort: Use Heap to Sort• Fixing heap property for all nodes• Use Array to represent Heap• Introduce Priority Queue
![Page 3: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/3.jpg)
3
HeapA heap (or binary heap) is a binary tree that
satisfies both:(1) Shape Property– All levels, except deepest, are fully filled– Deepest level is filled from left to right
(2) Heap Property– Value of a node ≤ Value of its children
![Page 4: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/4.jpg)
4
Satisfying Shape Property
Example of a tree with shape property
![Page 5: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/5.jpg)
5
Not Satisfying Shape Property
This level (not deepest) is NOT fully filled
![Page 6: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/6.jpg)
6
Not Satisfying Shape Property
Deepest level NOT filled from left to right
![Page 7: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/7.jpg)
7
Satisfying Heap Property
![Page 8: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/8.jpg)
8
Not Satisfying Heap Property
Help!
![Page 9: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/9.jpg)
9
Min-Heap
Q.Q. Given a heap, what is so special about the root’s value?
A.A. … always the minimum
Because of this, the previous heap is also called a min-heap
![Page 10: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/10.jpg)
10
Heap Operations• Find-Min : find the minimum value
(1) time• Extract-Min : delete the minimum value
(log n) time (how??)
• Insert : insert a new value into heap
(log n) time (how??)
n = # nodes in the heap
![Page 11: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/11.jpg)
11
How to do Extract-Min?3
8 4
2313 12 24
3843
Heap before Extract-Min
![Page 12: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/12.jpg)
12
Step 1: Restore Shape Property3
8 4
2313 12 24
3843
Copy value of last node to root. Next, remove last node
![Page 13: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/13.jpg)
13
Next, highlight the root Only this node may violate heap
property
Step 2: Restore Heap Property38
8 4
2313 12 24
43
If violates, swap highlighted node with “smaller” child (if not, everything done)
![Page 14: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/14.jpg)
14
Step 2: Restore Heap Property4
8 38
2313 12 24
43 After swapping, only the highlighted node may violate
heap property
If violates, swap highlighted node with “smaller” child (if not, everything done)
![Page 15: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/15.jpg)
15
Step 2: Restore Heap Property4
8 12
2313 38 24
43 As soon as the highlighted node
satisfies the heap property
Everything done !!!
![Page 16: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/16.jpg)
16
How to do Insert?3
8 4
2313 12 24
3843
Heap before Insert
![Page 17: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/17.jpg)
17
Step 1: Restore Shape Property3
8 4
2313 12 24
3843
Create a new node with the new value. Next, add it to the heap at correct
position
5
![Page 18: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/18.jpg)
18
Highlight the new node Only this node’s parent may violate heap property
Step 2: Restore Heap Property3
8 4
2313 12 24
3843 5
If violates, swap highlighted node with parent (if not, everything done)
![Page 19: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/19.jpg)
19
After swapping, only highlighted node’s parent may violate heap property
3
8 4
513 12 24
3843 23
Step 2: Restore Heap Property
If violates, swap highlighted node with parent (if not, everything done)
![Page 20: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/20.jpg)
20
As soon as highlighted node’s parent satisfies heap
property
3
5 4
813 12 24
3843 23
Step 2: Restore Heap Property
Everything done !!!
![Page 21: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/21.jpg)
21
Running TimeLet h = node-height of heap• Both Extract-Min and Insert require (h) time
to performSince h = (log n) (why??)
Both require (log n) time
n = # nodes in the heap
![Page 22: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/22.jpg)
22
Heapsort
Q.Q. Given n numbers, can we use heap to sort them, say, in ascending order?
A.A. Yes, and extremely easy !!!1. Call Insert to insert n numbers into heap2. Call Extract-Min n times
numbers are output in sorted order
Runtime: n x (log n) + n x (log n) = (n log n)
This sorting algorithm is called heapsort
![Page 23: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/23.jpg)
23
Challenge (Fixing heap property for all nodes)
Suppose that we are given a binary tree which satisfies the shape property
However, the heap property of the nodes may not be satisfied …
Question: Can we make the tree into a heap in (n) time?
n = # nodes in the tree
![Page 24: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/24.jpg)
24
How to make it a heap?
12
4 13
2343 8 3
3824
![Page 25: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/25.jpg)
25
Observation u = root of a binary tree L = subtree rooted at u’s
left child R = subtree rooted at u’s
right child
u
RL
Obs: If L and R satisfy heap property, we can make the tree rooted at u satisfy heap property in ( max { height(L), height(R) } ) time.We denote the above operation by
Heapify(u)
![Page 26: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/26.jpg)
26
Heapify
Then, for any tree T, we can make T satisfy the heap property as follows:
Step 1. h = node_height(T) ; Step 2. for k = h, h-1, …, 1
for each node u at level k Heapify(u) ;
Why is the above algorithm correct?
![Page 27: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/27.jpg)
27
Example Run12
4 13
2343 8 3
3824
First, heapify this tree
![Page 28: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/28.jpg)
28
Example Run12
4 13
2343 8 3
3824
Next, heapify this tree
![Page 29: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/29.jpg)
29
Example Run12
4 13
2343 8 3
3824
Next, heapify this tree
![Page 30: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/30.jpg)
30
Example Run12
4 13
2324 8 3
3843 Next, heapify this tree
![Page 31: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/31.jpg)
31
Example Run12
4 13
2324 8 3
3843Next, heapify this tree
![Page 32: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/32.jpg)
32
Example Run12
4 13
2324 8 3
3843Next, heapify this tree
![Page 33: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/33.jpg)
33
Example Run12
4 13
2324 8 3
3843
Next, heapify this tree
![Page 34: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/34.jpg)
34
Example Run12
4 13
2324 8 3
3843Next, heapify this tree
![Page 35: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/35.jpg)
35
Example Run12
4 3
2324 8 13
3843
Finally, heapify the whole tree
![Page 36: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/36.jpg)
36
Example Run3
4 12
2324 8 13
3843
Finally, heapify the whole tree
![Page 37: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/37.jpg)
37
Example Run3
4 8
2324 12 13
3843
Everything Done !
![Page 38: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/38.jpg)
38
Back to the Challenge (Fixing heap property for all nodes)
Suppose that we are given a binary tree which satisfies the shape property
However, the heap property of the nodes may not be satisfied …
Question: Can we make the tree into a heap in (n) time?
n = # nodes in the tree
![Page 39: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/39.jpg)
39
Back to the Challenge (Fixing heap property for all nodes)
Let h = node-height of a treeSo, 2h-1 ≤ n ≤ 2h - 1 (why??)For a tree with shape property,
at most 2h-1 nodes at level h, exactly 2h-2 nodes at level h-1, exactly 2h-3 nodes at level h-2, …
![Page 40: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/40.jpg)
40
Back to the Challenge (Fixing heap property for all nodes)
Using the previous algorithm to solve the challenge, the total time is at most
2h-1 x 1 + 2h-2 x 2 + 2h-3 x 3+ … + 1 x h [why??]
= 2h ( 1x½ + 2x(½)2 + 3x(½)3 + … + hx(½)h )
≤ 2h k=1 to ∞ k x (½)k = 2h x 2 ≤ 4n
Thus, total time is O(n)
![Page 41: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/41.jpg)
41
Array Representation of Heap
??
??
??
??
??
??
??
??
??
1
2 3
4 5 6 7
8 9
Given a heap.Suppose we mark the position of root as 1, and mark other nodes in a way as shown in the right figure. (BFS order)
Anything special about this marking?
![Page 42: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/42.jpg)
42
Array Representation of Heap
??
??
??
??
??
??
??
??
??
1
2 3
4 5 6 7
8 9
Yes, something special:1. If the heap has n
nodes, the marks are from 1 to n
2. Children of x, if exist, are 2x and 2x+1
3. Parent of x is 2/x
![Page 43: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/43.jpg)
43
• The special properties of the marking allow us to use an array A[1..n] to store a heap of size n
Array Representation of Heap
Advantage: Avoid storing or using tree pointers !!
Try this at home:Write codes for Insert and Extract-Min, assuming the heap is stored in an array
![Page 44: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/44.jpg)
44
Max-Heap
We can also define a max-heap, by changing the heap property to:
Value of a node ≥Value of its children
Max heap supports the following operations:
(1)Find Max, (2) Extract Max, (3) Insert
Do you know how to do these operations?
![Page 45: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/45.jpg)
45
Priority Queue
Consider S = a set of items, each has a key
Priority queue on S supports: Min(S): return item with min key Extract-Min(S): remove item with min
key Insert(S,x,k): insert item x with key k Decrease-Key(S,x,k): decrease key of
item x to k
![Page 46: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/46.jpg)
46
Using Heap as Priority Queue1. Store the items in an array2. Use a heap to store keys of the items3. Store links between an item and its
keyE.g.,
item 9
item 2
item 1
This node store item 1’s
key
![Page 47: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/47.jpg)
47
Previous scheme supports Min in O(1) time, Extract-Min and Insert in O(log n) time
It can support Decrease-Key in O(log n) time
E.g., Node storing key value of item x
How do we decrease the key to k ??
Using Heap as Priority Queue
![Page 48: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/48.jpg)
48
• In algorithm classes (or perhaps later lectures), we will look at other ways to implement a priority queue
• with different time bounds for the operations
Remark: Priority Queue can be used for finding MST or shortest paths, and job scheduling
Other Schemes?
![Page 49: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/49.jpg)
49
Homework• Problem 6-2 (a, b, c) (Due Oct. 24)• Bonus (1 point): Write a Heapsort program to
sort n numbers (Due Oct. 31)• Practice at home: 6.2-6, 6.4-4, 6.5-4, 6.5-9
![Page 50: 1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap](https://reader034.vdocuments.net/reader034/viewer/2022050720/5a4d1b767f8b9ab0599b728f/html5/thumbnails/50.jpg)
50
Homework• Consider the following balls-and-bin game. We start
with one black ball and one white ball in a bin. We repeatedly do the following: choose one ball from the bin uniformly at random, and then put the ball back in the bin with another ball of the same color. We repeat until there are n balls in the bin. Show that the number of white balls is equally likely to be any number between 1 and n-1. (Hint: Prove by mathematical induction)