Princeton University • COS 423 • Theory of Algorithms • Spring 2002 • Kevin Wayne
Binary and Binomial Heaps
These lecture ...
5
Binary Heap: Definition
Binary heap.
s Almost complete binary tree.
– filled on all levels, except last, where filled fr...
9
Binary Heap: Insertion
Insert element x into heap.
s Insert into next available slot.
s Bubble up until it’s heap ordere...
13
Binary Heap: Delete Min
Delete minimum element from heap.
s Exchange root with rightmost leaf.
s Bubble root down until...
17
Binary Heap: Delete Min
Delete minimum element from heap.
s Exchange root with rightmost leaf.
s Bubble root down until...
21
Binomial Tree
Binomial tree.
s Recursive definition:
Bk-1
Bk-1
B0
Bk
B0 B1 B2 B3 B4
22
Binomial Tree
Useful properties ...
25
Binomial Heap: Implementation
Implementation.
s Represent trees using left-child, right sibling pointers.
– three links...
29
Binomial Heap: Union
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
37
3 18
41
3328
15
25
7 12
+
30
Binomial Heap: Union
...
33
55
45 32
30
24
23 22
50
48 31 17
448 29 10
6
37
3
41
3328
15
25
7
+
18
12
41
28 33 25
3715 7
3
12
18
25
377
3
41
28 33 ...
37
Binomial Heap: Delete Min
Delete node with minimum key in binomial heap H.
s Find root x with min key in root list of H...
41
Binomial Heap: Sequence of Inserts
Insert a new node x into binomial heap H.
s If N = .......0, then only 1 steps.
s If...
Upcoming SlideShare
Loading in …5
×

Heaps 4up

503 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
503
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Heaps 4up

  1. 1. Princeton University • COS 423 • Theory of Algorithms • Spring 2002 • Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters 6, 19. 2 Priority Queues Supports the following operations. s Insert element x. s Return min element. s Return and delete minimum element. s Decrease key of element x to k. Applications. s Dijkstra’s shortest path algorithm. s Prim’s MST algorithm. s Event-driven simulation. s Huffman encoding. s Heapsort. s . . . 3 Priority Queues in Action PQinit() for each v ∈ V key(v) ← ∞ PQinsert(v) key(s) ← 0 while (!PQisempty()) v = PQdelmin() for each w ∈ Q s.t (v,w) ∈ E if π(w) > π(v) + c(v,w) PQdecrease(w, π(v) + c(v,w)) Dijkstra’s Shortest Path Algorithm 4 Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary log N 1 log N N log N log N 1 Binomial log N log N log N log N log N log N 1 Fibonacci * 1 1 log N 1 1 log N 1 Relaxed 1 1 log N 1 1 log N 1 Linked List 1 N N 1 1 N is-empty 1 1 1 11 Heaps O(|E| + |V| log |V|)O(|E| log |V|)O(|V|2)
  2. 2. 5 Binary Heap: Definition Binary heap. s Almost complete binary tree. – filled on all levels, except last, where filled from left to right s Min-heap ordered. – every child greater than (or equal to) parent 06 14 78 18 81 7791 45 5347 6484 9983 6 Binary Heap: Properties Properties. s Min element is in root. s Heap with N elements has height = log2 N. 06 14 78 18 81 7791 45 5347 6484 9983 N = 14 Height = 3 7 Binary Heaps: Array Implementation Implementing binary heaps. s Use an array: no need for explicit parent or child pointers. – Parent(i) = i/2 – Left(i) = 2i – Right(i) = 2i + 1 06 14 78 18 81 7791 45 5347 6484 9983 1 2 3 4 5 6 7 8 9 10 11 12 13 14 8 Binary Heap: Insertion Insert element x into heap. s Insert into next available slot. s Bubble up until it’s heap ordered. – Peter principle: nodes rise to level of incompetence 06 14 78 18 81 7791 45 5347 6484 9983 42 next free slot
  3. 3. 9 Binary Heap: Insertion Insert element x into heap. s Insert into next available slot. s Bubble up until it’s heap ordered. – Peter principle: nodes rise to level of incompetence 06 14 78 18 81 7791 45 5347 6484 9983 4242 swap with parent 10 Binary Heap: Insertion Insert element x into heap. s Insert into next available slot. s Bubble up until it’s heap ordered. – Peter principle: nodes rise to level of incompetence 06 14 78 18 81 7791 45 4247 6484 9983 4253 swap with parent 11 Binary Heap: Insertion Insert element x into heap. s Insert into next available slot. s Bubble up until it’s heap ordered. – Peter principle: nodes rise to level of incompetence s O(log N) operations. 06 14 78 18 81 7791 42 4547 6484 9983 53 stop: heap ordered 12 Binary Heap: Decrease Key Decrease key of element x to k. s Bubble up until it’s heap ordered. s O(log N) operations. 06 14 78 18 81 7791 42 4547 6484 9983 53
  4. 4. 13 Binary Heap: Delete Min Delete minimum element from heap. s Exchange root with rightmost leaf. s Bubble root down until it’s heap ordered. – power struggle principle: better subordinate is promoted 06 14 78 18 81 7791 42 4547 6484 9983 53 14 Binary Heap: Delete Min Delete minimum element from heap. s Exchange root with rightmost leaf. s Bubble root down until it’s heap ordered. – power struggle principle: better subordinate is promoted 53 14 78 18 81 7791 42 4547 6484 9983 06 15 Binary Heap: Delete Min Delete minimum element from heap. s Exchange root with rightmost leaf. s Bubble root down until it’s heap ordered. – power struggle principle: better subordinate is promoted 53 14 78 18 81 7791 42 4547 6484 9983 exchange with left child 16 Binary Heap: Delete Min Delete minimum element from heap. s Exchange root with rightmost leaf. s Bubble root down until it’s heap ordered. – power struggle principle: better subordinate is promoted 14 53 78 18 81 7791 42 4547 6484 9983 exchange with right child
  5. 5. 17 Binary Heap: Delete Min Delete minimum element from heap. s Exchange root with rightmost leaf. s Bubble root down until it’s heap ordered. – power struggle principle: better subordinate is promoted s O(log N) operations. 14 18 78 53 81 7791 42 4547 6484 9983 stop: heap ordered 18 Binary Heap: Heapsort Heapsort. s Insert N items into binary heap. s Perform N delete-min operations. s O(N log N) sort. s No extra storage. 19 H1 H2 14 78 18 81 7791 11 6253 6484 9941 Binary Heap: Union Union. s Combine two binary heaps H1 and H2 into a single heap. s No easy solution. – Ω(N) operations apparently required s Can support fast union with fancier heaps. 20 Priority Queues make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary log N 1 log N N log N log N 1 Binomial log N log N log N log N log N log N 1 Fibonacci * 1 1 log N 1 1 log N 1 Relaxed 1 1 log N 1 1 log N 1 Linked List 1 N N 1 1 N is-empty 1 1 1 11 Heaps
  6. 6. 21 Binomial Tree Binomial tree. s Recursive definition: Bk-1 Bk-1 B0 Bk B0 B1 B2 B3 B4 22 Binomial Tree Useful properties of order k binomial tree Bk. s Number of nodes = 2k. s Height = k. s Degree of root = k. s Deleting root yields binomial trees Bk-1, … , B0. Proof. s By induction on k. B0 B1 B2 B3 B4 B1 Bk Bk+1 B2 B0 23 Binomial Tree A property useful for naming the data structure. s Bk has nodes at depth i. B4       i k 6 2 4 =      depth 2 depth 3 depth 4 depth 0 depth 1 24 Binomial Heap Binomial heap. Vuillemin, 1978. s Sequence of binomial trees that satisfy binomial heap property. – each tree is min-heap ordered – 0 or 1 binomial tree of order k B4 B0B1 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 18
  7. 7. 25 Binomial Heap: Implementation Implementation. s Represent trees using left-child, right sibling pointers. – three links per node (parent, left, right) s Roots of trees connected with singly linked list. – degrees of trees strictly decreasing from left to right 50 48 31 17 4410 6 37 3 18 29 6 37 3 18 48 3150 10 4417 heap 29 Leftist Power-of-2 HeapBinomial Heap Parent Left Right 26 Binomial Heap: Properties Properties of N-node binomial heap. s Min key contained in root of B0, B1, . . . , Bk. s Contains binomial tree Bi iff bi = 1 where bn⋅ b2b1b0 is binary representation of N. s At most log2 N + 1 binomial trees. s Height ≤ log2 N. B4 B0B1 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 18 N = 19 # trees = 3 height = 4 binary = 10011 27 Binomial Heap: Union Create heap H that is union of heaps H’ and H’’. s "Mergeable heaps." s Easy if H’ and H’’ are each order k binomial trees. – connect roots of H’ and H’’ – choose smaller key to be root of H H’’ 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 H’ 28 Binomial Heap: Union 001 1 100 1+ 011 1 11 1 1 0 1 19 + 7 = 26 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 18 41 3328 15 25 7 12 +
  8. 8. 29 Binomial Heap: Union 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 18 41 3328 15 25 7 12 + 30 Binomial Heap: Union 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 41 3328 15 25 7 + 12 18 18 12 31 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 41 3328 15 25 7 + 12 18 25 377 3 18 12 18 12 32 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 41 3328 15 25 7 12 + 18 25 377 3 41 28 33 25 3715 7 3 18 12 18 12
  9. 9. 33 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 41 3328 15 25 7 + 18 12 41 28 33 25 3715 7 3 12 18 25 377 3 41 28 33 25 3715 7 3 18 12 34 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 37 3 41 3328 15 25 7 + 18 12 41 28 33 25 3715 7 3 12 18 25 377 3 41 28 33 25 3715 7 3 55 45 32 30 24 23 22 50 48 31 17 448 29 10 6 18 12 35 Binomial Heap: Union Create heap H that is union of heaps H’ and H’’. s Analogous to binary addition. Running time. O(log N) s Proportional to number of trees in root lists ≤ 2( log2 N + 1). 001 1 100 1+ 011 1 11 1 1 0 1 19 + 7 = 26 36 3 37 6 18 55 45 32 30 24 23 22 50 48 31 17 448 29 10 H Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. s Find root x with min key in root list of H, and delete s H’ ← broken binomial trees s H ← Union(H’, H) Running time. O(log N)
  10. 10. 37 Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. s Find root x with min key in root list of H, and delete s H’ ← broken binomial trees s H ← Union(H’, H) Running time. O(log N) 55 45 32 30 24 23 22 50 48 31 17 37 6 18 448 29 10 H H’ 38 3 37 6 18 55 x 32 30 24 23 22 50 48 31 17 448 29 10 H Binomial Heap: Decrease Key Decrease key of node x in binomial heap H. s Suppose x is in binomial tree Bk. s Bubble node x up the tree if x is too small. Running time. O(log N) s Proportional to depth of node x ≤ log2 N . depth = 3 39 Binomial Heap: Delete Delete node x in binomial heap H. s Decrease key of x to -∞. s Delete min. Running time. O(log N) 40 Binomial Heap: Insert Insert a new node x into binomial heap H. s H’ ← MakeHeap(x) s H ← Union(H’, H) Running time. O(log N) 3 37 6 18 55 45 32 30 24 23 22 50 48 31 17 448 29 10 H x H’
  11. 11. 41 Binomial Heap: Sequence of Inserts Insert a new node x into binomial heap H. s If N = .......0, then only 1 steps. s If N = ......01, then only 2 steps. s If N = .....011, then only 3 steps. s If N = ....0111, then only 4 steps. Inserting 1 item can take Ω(log N) time. s If N = 11...111, then log2 N steps. But, inserting sequence of N items takes O(N) time! s (N/2)(1) + (N/4)(2) + (N/8)(3) + . . . ≤ 2N s Amortized analysis. s Basis for getting most operations down to constant time. 50 48 31 17 4429 10 3 37 6 x 2 2 1 2 2 2 1 1 ≤ −−=∑ − = NN N n n Nn 42 Priority Queues make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary log N 1 log N N log N log N 1 Binomial log N log N log N log N log N log N 1 Fibonacci * 1 1 log N 1 1 log N 1 Relaxed 1 1 log N 1 1 log N 1 Linked List 1 N N 1 1 N is-empty 1 1 1 11 Heaps just did this

×