CS711008Z Algorithm Design and Analysis Lecture 7
Total Page:16
File Type:pdf, Size:1020Kb
CS711008Z Algorithm Design and Analysis Lecture 7. Binary heap, binomial heap, and Fibonacci heap Dongbo Bu Institute of Computing Technology Chinese Academy of Sciences, Beijing, China . 1 / 124 Outline Introduction to priority queue Various implementations of priority queue: Linked list: a list having n items is too long to support efficient ExtractMin and Insert operations simultaneously; Binary heap: using a tree rather than a linked list; Binomial heap: allowing multiple trees rather than a single tree to support efficient Union operation Fibonacci heap: implement DecreaseKey via simply cutting an edge rather than exchanging nodes, and control a “bushy” tree shape via allowing at most one child losing for any node. 2 / 124 Priority queue . 3 / 124 Priority queue: motivation Motivation: It is usually a case to extract the minimum from a set S of n numbers, dynamically. Here, the word “dynamically” means that on S, we might perform Insertion, Deletion and DecreaseKey operations. The question is how to organize the data to efficiently support these operations. 4 / 124 Priority queue Priority queue is an abstract data type similar to stack or queue, but each element has a priority associated with its name. A min-oriented priority queue must support the following core operations: 1 H=MakeHeap(): to create a new heap H; 2 Insert(H; x): to insert into H an element x together with its priority 3 ExtractMin(H): to extract the element with the highest priority; 4 DecreaseKey(H; x; k): to decrease the priority of element x; 5 Union(H1; H2): return a new heap containing all elements of heaps H1 and H2, and destroy the input heaps . 5 / 124 Priority queue is very useful Priority queue has extensive applications, such as: Dijkstra’s shortest path algorithm Prim’s MST algorithm Huffman coding A∗ searching algorithm HeapSort ...... 6 / 124 An example: Dijkstra’s algorithm . 7 / 124 Dijkstra’s algorithm [1959] Dijkstra(G; s; t) 1: key(s) = 0; //key(u) stores an upper bound of the shortest distance from s to u; 2: PQ: Insert (s); 3: for all node v =6 s do 4: key(v) = +1 5: PQ: Insert (v) //n times 6: end for 7: S = fg; // Let S be the set of explored nodes; 8: while S =6 V do 9: v∗ = PQ: ExtractMin(); //n times 10: S = S [ fv∗g; 11: for all v 2= S and < v∗; v >2 E do 12: if key(v∗) + d(v∗; v) < key(v) then 13: PQ:DecreaseKey(v; key(v∗) + d(v∗; v)); //m times 14: end if 15: end for 16: end while Here PQ denotes a min-priority queue. 8 / 124 Dijkstra’s algorithm: an example 9 24 s a d 14 18 2 15 19 b 30 11 f 6 20 e 16 44 c t . 9 / 124 Initialization S = fg PQ = fs(0); a(1); b(1); c(1); d(1); e(1); f(1); t(1)g 0 1 1 9 24 s a d 14 18 1 2 15 19 b 1 30 1 11 f 6 1 20 e 16 1 44 c t . 10 / 124 Step 1: ExtractMin S = fg PQ = fs(0); a(1); b(1); c(1); d(1); e(1); f(1); t(1)g 0 1 1 9 24 s a d ExtractMin returns s 14 18 1 2 15 19 b 1 30 1 11 f 6 1 20 e 16 1 44 c t . 11 / 124 Step 1: DecreaseKey S = fsg PQ = fa(1); b(1); c(1); d(1); e(1); f(1); t(1)g 0 1 1 9 24 s a d 14 18 DecreaseKey(a; 9) DecreaseKey(b; 14) 1 2 15 19 DecreaseKey(c; 15) b 1 30 1 11 f 6 1 20 e 16 1 44 c t . 12 / 124 ExtractMin returns a Step 2: ExtractMin S = fsg PQ = fa(9); b(14); c(15); d(1); e(1); f(1); t(1)g 0 9 1 9 24 s a d 14 18 14 2 15 19 b 1 30 1 11 f 6 15 20 e 16 1 44 c t . 13 / 124 Step 2: ExtractMin S = fsg PQ = fa(9); b(14); c(15); d(1); e(1); f(1); t(1)g 0 9 1 9 24 s a d ExtractMin returns a 14 18 14 2 15 19 b 1 30 1 11 f 6 15 20 e 16 1 44 c t . 13 / 124 Step 2: DecreaseKey S = fs; ag PQ = fb(14); c(15); d(1); e(1); f(1); t(1)g 0 9 1 9 24 s a d 14 18 DecreaseKey(d; 33) 14 2 15 19 b 1 30 1 11 f 6 15 20 e 16 1 44 c t . 14 / 124 ExtractMin returns b Step 3: ExtractMin S = fs; ag PQ = fb(14); c(15); d(33); e(1); f(1); t(1)g 0 9 33 9 24 s a d 14 18 14 2 15 19 b 1 30 1 11 f 6 15 20 e 16 1 44 c t . 15 / 124 Step 3: ExtractMin S = fs; ag PQ = fb(14); c(15); d(33); e(1); f(1); t(1)g 0 9 33 9 24 s a d ExtractMin returns b 14 18 14 2 15 19 b 1 30 1 11 f 6 15 20 e 16 1 44 c t . 15 / 124 Step 3: DecreaseKey S = fs; a; bg PQ = fc(15); d(33); e(1); f(1); t(1)g 0 9 33 9 24 s a d 14 18 DecreaseKey(d; 32) DecreaseKey(e; 44) 14 2 15 19 b 1 30 1 11 f 6 15 20 e 16 1 44 c t . 16 / 124 ExtractMin returns c Step 4: ExtractMin S = fs; a; bg PQ = fc(15); d(32); e(44); f(1); t(1)g 0 9 32 9 24 s a d 14 18 14 2 15 19 b 1 30 44 11 f 6 15 20 e 16 1 44 c t . 17 / 124 Step 4: ExtractMin S = fs; a; bg PQ = fc(15); d(32); e(44); f(1); t(1)g 0 9 32 9 24 s a d ExtractMin returns c 14 18 14 2 15 19 b 1 30 44 11 f 6 15 20 e 16 1 44 c t . 17 / 124 Step 4: DecreaseKey S = fs; a; b; cg PQ = fd(32); e(44); f(1); t(1)g 0 9 32 9 24 s a d 14 18 DecreaseKey(e; 35) DecreaseKey(t; 59) 14 2 15 19 b 1 30 44 11 f 6 15 20 e 16 1 44 c t . 18 / 124 ExtractMin returns d Step 5: ExtractMin S = fs; a; b; cg PQ = fd(32); e(35); t(59); f(1)g 0 9 32 9 24 s a d 14 18 14 2 15 19 b 1 30 35 11 f 6 15 20 e 16 59 44 c t . 19 / 124 Step 5: ExtractMin S = fs; a; b; cg PQ = fd(32); e(35); t(59); f(1)g 0 9 32 9 24 s a d ExtractMin returns d 14 18 14 2 15 19 b 1 30 35 11 f 6 15 20 e 16 59 44 c t . 19 / 124 Step 5: DecreaseKey S = fs; a; b; c; dg PQ = fe(35); t(59); f(1)g 0 9 32 9 24 s a d 14 18 DecreaseKey(t; 51) DecreaseKey(e; 34) 14 2 15 19 b 1 30 35 11 f 6 15 20 e 16 59 44 c t . 20 / 124 ExtractMin returns e Step 6: ExtractMin S = fs; a; b; c; dg PQ = fe(34); t(51); f(1)g 0 9 32 9 24 s a d 14 18 14 2 15 19 b 1 30 34 11 f 6 15 20 e 16 51 44 c t . 21 / 124 Step 6: ExtractMin S = fs; a; b; c; dg PQ = fe(34); t(51); f(1)g 0 9 32 9 24 s a d ExtractMin returns e 14 18 14 2 15 19 b 1 30 34 11 f 6 15 20 e 16 51 44 c t . 21 / 124 Step 6: DecreaseKey S = fs; a; b; c; d; eg PQ = ff(45); t(50)g 0 9 32 9 24 s a d 14 18 DecreaseKey(f; 45) DecreaseKey(t; 50) 14 2 15 19 b 1 30 34 11 f 6 15 20 e 16 51 44 c t . 22 / 124 ExtractMin returns f Step 7: ExtractMin S = fs; a; b; c; d; eg PQ = ff(45); t(50)g 0 9 32 9 24 s a d 14 18 14 2 15 19 b 30 45 34 11 f 6 15 20 e 16 50 44 c t . 23 / 124 Step 7: ExtractMin S = fs; a; b; c; d; eg PQ = ff(45); t(50)g 0 9 32 9 24 s a d ExtractMin returns f 14 18 14 2 15 19 b 30 45 34 11 f 6 15 20 e 16 50 44 c t . 23 / 124 Step 7: DecreaseKey S = fs; a; b; c; d; e; fg PQ = ft(50)g 0 9 32 9 24 s a d 14 18 14 2 15 19 b 30 45 34 11 f 6 15 20 e 16 50 44 c t . 24 / 124 Step 8: ExtractMin S = fs; a; b; c; d; e; f; tg PQ = fg 0 9 32 9 24 s a d 14 18 14 2 15 19 b 30 45 34 11 f 6 15 20 e 16 50 44 c t .