Priority Queues (heaps) Heaps in Action Dijkstra's Shortest Path Algorithm Binomial Heaps Supports the following operations.  Insert element x. HeapInit () /*Initialized */  Return min element. for each v ∈∈∈ V  Return and delete minimum element. d[v] ←←←∞∞∞ ;

 Decrease key of element x to k. HeapInsert (v)

CLRS, Chapters 6, 19 Applications. d[s] ←←← 0

 Dijkstra's shortest path algorithm. while (! HeapIsEmpty ()) u = Heap_Extract_Min () Thanks to Kevin Wayne for  Prim's MST algorithm. the slides. for each v ∈∈∈ V(G) s.t (u,v) ∈∈∈ E(G)  Event-driven simulation. if d[v] > d[u] + w(u,v)  Huffman encoding. Heap_Decrease (v, d[u] + w(v,w))  Heapsort.

 . . .

2 3

Priority Queues Binomial Binomial Tree Binomial tree - definition. Heaps B Useful properties of order k binomial tree B k.  Recursive definition: 0 Bi Operation Binary Binomial Fibonacci * Relaxed k  Number of nodes = 2 . make- 1 1 1 1 1  Bi is created from two Bi-1 by Bk connect the root of the  Height = k. insert 1 log N log N 1 1 Bi-1 +1 find-min N 1 log N 1 1 larger key is a child of the  Degree of root = k. Bi-1 delete-min N log N log N log N log N second.  Deleting root yields binomial B1 B B0 union 1 N log N 1 1 2  So the degree could be trees Bk-1, … , B 0. Bk decrease-key 1 log N log N 1 1 rather large. delete N log N log N log N log N Proof. is-empty 1 1 1 1 1

 By induction on k.

Dijkstra/Prim 2 1 make-heap O(|V| ) O(|E| log |V|) O(|E| + |V| log |V|) |V| insert |V| delete-min |E| decrease-key B B B B B 0 1 2 3 4 B0 B1 B2 B3 B4 4 5 6 Binomial Tree Binomial Heap Binomial Heap: Implementation Implementation. Binomial heap. Vuillemin, 1978. A property useful for naming the .  Represent trees using pointers to k   Sequence of binomial trees that satisfy binomial  Bk has   nodes at depth i. –left-sibling, right-sibling, parent, a child. i heap property.    –each tree is min-heap ordered (parent ≤≤≤ each (need a pointer only to one child)  All siblings are in a doubly connected list, called sibling 4 child)   === 6 list (so it is enough to point to only one of them) 2 –0 or 1 binomial tree of order k depth 0 6 3 18  Roots of trees connected with linked list. –degrees of trees strictly decreasing from left to right depth 1 8 29 10 44 37 6 3 18 depth 2 30 23 22 48 31 17 29 10 44 37 depth 3 45 32 24 50 48 31 17 depth 4 B4 55 B4 B1 B0 50 7 8 9

Binomial Heap: Properties Binomial Heap: Union Binomial Heap: Union

Properties of N-node binomial heap. Create heap H that is union of heaps H' and H'' .

 Min key contained in root of B0, B 1, . . . , B k.  "Mergeable heaps." ⋅ 6 3 18  Contains binomial tree Bi iff bi = 1 where bn b2b1b0 is  Easy if H' and H'' are each order k binomial trees. binary representation of N. –connect roots of H' and H'' 8 29 10 44 37

 At most log 2 N + 1 binomial trees. –choose smaller key to be root of H 30 23 22 48 31 17 ≤ 15 7 12  Height log 2 N. 6 3 18 6 45 32 24 50 55 28 33 25 8 29 10 44 37 8 29 10 44 + 41

30 23 22 48 31 17 N = 19 30 23 22 48 31 17 # trees = 3 1 1 1 1 0 0 1 1 45 32 24 50 height = 4 45 32 24 50 binary = 10011 19 + 7 = 26 + 0 0 1 1 1 55 55 1 1 0 1 0 B B B H' H'' 4 1 0 10 11 12 Binomial Heap: Union Binomial Heap: Union 12 3 12 18 7 37 18 25

6 3 18 6 3 18 6 3 18 8 29 10 44 37 8 29 10 44 37 8 29 10 44 37 30 23 22 48 31 17 30 23 22 48 31 17 30 23 22 48 31 17 15 7 12 15 7 12 15 7 12 45 32 24 50 45 32 24 50 45 32 24 50 55 28 33 25 55 28 33 25 55 28 33 25 + 41 + 41 + 41

12 18

13 14 15

3 3 12 3 3 12 3 3 12

15 7 37 7 37 18 15 7 37 7 37 18 15 7 37 7 37 18 28 33 25 25 28 33 25 25 28 33 25 25 41 41 41 6 3 18 6 3 18 6 3 18

8 29 10 44 37 8 29 10 44 37 8 29 10 44 37 30 23 22 48 31 17 30 23 22 48 31 17 30 23 22 48 31 17 15 7 12 15 7 12 15 7 12 45 32 24 50 45 32 24 50 45 32 24 50 28 33 25 28 33 25 28 33 25 55 + 55 55 + 41 41 + 41

12 3 12 6 3 12

18 15 7 37 18 8 29 10 44 15 7 37 18 28 33 25 30 23 22 48 31 17 28 33 25 41 45 32 24 50 41

16 17 55 18 Binomial Heap: Union Binomial Heap: Delete Min Binomial Heap: Delete Min

Create heap H that is union of heaps H' and H'' . Delete node with minimum key in binomial heap H. Delete node with minimum key in binomial heap H.

 Analogous to binary addition.  Find root x with min key in root list of H, and delete  Find root x with min key in root list of H, and delete

 H' ← broken binomial trees  H' ← broken binomial trees Running time. O(log N)  H ← Union( H', H )  H ← Union( H', H )

 Proportional to number of trees in root lists ≤≤≤   2( log 2 N + 1 ). Running time. O(log N) Running time. O(log N) 3 6 18 6 18

8 29 10 44 37 8 29 10 44 37

1 1 1 30 23 22 48 31 17 H 30 23 22 48 31 17 H 1 0 0 1 1 45 32 24 50 45 32 24 50 19 + 7 = 26 + 0 0 1 1 1 1 1 0 1 0 55 55 19 20 H' 21

Binomial Heap: Decrease Key Binomial Heap: Delete Binomial Heap: Insert

Decrease key of node x in binomial heap H. Delete node x in binomial heap H. Insert a new node x into binomial heap H. ∞∞∞ ←←←  Suppose x is in binomial tree Bk.  Decrease key of x to - .  H' MakeHeap(x)

 Bubble node x up the tree if x is too small.  Delete min.  H ←←← Union( H', H )

 Running time: O(log N) ≤  Proportional to depth of node x log 2 N . Running time. O(log N) Running time. O(log N) 3 6 18 3 6 18 x depth = 3 8 29 10 44 37 8 29 10 44 37

30 23 22 48 31 17 H 30 23 22 48 31 17 H H' x 32 24 50 45 32 24 50 55 55 22 23 24 Binomial Heap: Sequence of Inserts Priority Queues

Insert a new node x into binomial heap H.

 If N = ...... 0 , then only 1 steps. 3 6 x

 If N = ...... 01 , then only 2 steps. 29 10 44 37 Heaps  If N = .....011 , then only 3 steps. Operation Linked List Binary Binomial Fibonacci * Relaxed 48 31 17  If N = ....0111 , then only 4 steps. make-heap 1 1 1 1 1 50 insert 1 log N log N 1 1 Inserting 1 item can take Ω(log N) time. find-min N 1 log N 1 1

 If N = 11...111 , then log 2 N steps. delete-min N log N log N log N log N But, inserting sequence of N items takes O( N) time! union 1 N log N 1 1

 (N/2)(1) + (N/4)(2) + (N/8)(3) + . . . ≤ 2N decrease-key 1 log N log N 1 1

 Amortized analysis. delete N log N log N log N log N

 Basis for getting most operations is-empty 1 1 1 1 1 N down to constant time. ∑∑∑ n === −−− N −−− 1 n 2 N N−−−1 n===1 2 2 2 ≤≤≤ 2 just did this

25 26