Download presentation
Presentation is loading. Please wait.
Published byMonica Franklin Modified over 5 years ago
1
Priority Queues Supports the following operations. Insert element x.
Return min element. Return and delete minimum element. Decrease key of element x to k. Applications. Dijkstra's shortest path algorithm. Prim's MST algorithm. Event-driven simulation. Huffman encoding. Heapsort. . . . Event driven simulation: generate random inter-arrival times between customers, generate random processing times per customer, Advance clock to next event, skipping intervening ticks
2
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
3
Priority Queues Heaps Operation Linked List Binary Binomial
Fibonacci * Relaxed make-heap 1 1 1 1 1 insert 1 log N log N 1 1 find-min N 1 log N 1 1 delete-min N log N log N log N log N union 1 N log N 1 1 not possible to get all ops O(1) if only use comparisons because of N log N sorting lower bound Brodal gives variant of relaxed heap that achieves worst-case bounds for all operations relaxed heap: relaxes heap ordering property decrease-key 1 log N log N 1 1 delete N log N log N log N log N is-empty 1 1 1 1 1 O(|V|2) O(|E| log |V|) O(|E| + |V| log |V|) Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key
4
Priority Queues Heaps Operation Linked List Binary Binomial
Fibonacci * Relaxed make-heap 1 1 1 1 1 insert 1 log N log N 1 1 find-min N 1 log N 1 1 delete-min N log N log N log N log N union 1 N log N 1 1 decrease-key 1 log N log N 1 1 delete N log N log N log N log N is-empty 1 1 1 1 1
5
Binomial Tree Binomial tree. Recursive definition: Bk-1 B0 Bk B0 B1 B2
not binary tree B0 B1 B2 B3 B4
6
Binomial Tree Useful properties of order k binomial tree Bk.
Number of nodes = 2k. Height = k. Degree of root = k. Deleting root yields binomial trees Bk-1, … , B0. Proof. By induction on k. B1 Bk Bk+1 B2 B0 B0 B1 B2 B3 B4
7
Binomial Tree A property useful for naming the data structure.
Bk has nodes at depth i. depth 0 depth 1 depth 2 depth 3 depth 4 B4
8
Binomial Heap Binomial heap. Vuillemin, 1978.
Sequence of binomial trees that satisfy binomial heap property. each tree is min-heap ordered 0 or 1 binomial tree of order k 55 45 32 30 24 23 22 50 48 31 17 44 8 29 10 6 3 18 sequence -> ordered 37 B4 B1 B0
9
Binomial Heap: Implementation
Represent trees using left-child, right sibling pointers. three links per node (parent, left, right) Roots of trees connected with singly linked list. degrees of trees strictly decreasing from left to right heap coding with a variable number of links per node would be more painful 6 3 18 6 3 18 Parent 37 29 10 44 37 29 Right Left 48 31 17 48 10 50 50 31 17 44 Binomial Heap Leftist Power-of-2 Heap
10
Binomial Heap: Properties
Properties of N-node binomial heap. Min key contained in root of B0, B1, , Bk. Contains binomial tree Bi iff bi = 1 where bn b2b1b0 is binary representation of N. At most log2 N + 1 binomial trees. Height log2 N. 55 45 32 30 24 23 22 50 48 31 17 44 8 29 10 6 3 18 37 N = 19 # trees = 3 height = 4 binary = 10011 B4 B1 B0
11
Binomial Heap: Union Create heap H that is union of heaps H' and H''.
"Mergeable heaps." 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 6 8 29 10 44 30 23 22 48 31 17 45 32 24 50 55 H' H''
12
+ Binomial Heap: Union 1 1 1 1 1 1 + 1 1 1 19 + 7 = 26 1 1 1
3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 amusing to consider "multiplying" binomial heaps or other arithmetic ops. Any uses??? 32 24 50 28 33 25 + 55 41 1 1 1 1 1 1 + 1 1 1 = 26 1 1 1
13
Binomial Heap: Union 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41
14
Binomial Heap: Union 12 18 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41
15
3 12 7 37 18 25 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41 12 18
16
3 3 12 15 7 37 7 37 18 28 33 25 25 41 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41 12 18
17
3 3 12 15 7 37 7 37 18 28 33 25 25 41 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41 3 12 15 7 37 18 28 33 25 41
18
3 3 12 15 7 37 7 37 18 28 33 25 25 41 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41 6 3 12 8 29 10 44 15 7 37 18 30 23 22 48 31 17 28 33 25 45 32 24 50 41 55
19
Binomial Heap: Union Create heap H that is union of heaps H' and H''.
Analogous to binary addition. Running time. O(log N) Proportional to number of trees in root lists 2( log2 N + 1). 1 1 1 1 1 1 + 1 1 1 = 26 1 1 1
20
Binomial Heap: Delete Min
Delete node with minimum key in binomial heap H. Find root x with min key in root list of H, and delete H' broken binomial trees H Union(H', H) Running time. O(log N) 3 37 6 18 55 45 32 30 24 23 22 50 48 31 17 44 8 29 10 H
21
Binomial Heap: Delete Min
Delete node with minimum key in binomial heap H. Find root x with min key in root list of H, and delete H' broken binomial trees H Union(H', H) Running time. O(log N) 6 18 8 29 10 44 37 H' 30 23 22 48 31 17 H 45 32 24 50 55
22
Binomial Heap: Decrease Key
Decrease key of node x in binomial heap H. Suppose x is in binomial tree Bk. Bubble node x up the tree if x is too small. Running time. O(log N) Proportional to depth of node x log2 N . Bk has height k and 2k nodes 3 6 18 depth = 3 8 29 10 44 37 30 23 22 48 31 17 H x 32 24 50 55
23
Binomial Heap: Delete Delete node x in binomial heap H.
Decrease key of x to -. Delete min. Running time. O(log N)
24
Binomial Heap: Insert Insert a new node x into binomial heap H.
H' MakeHeap(x) H Union(H', H) Running time. O(log N) 3 6 18 x 8 29 10 44 37 30 23 22 48 31 17 H H' 45 32 24 50 55
25
Binomial Heap: Sequence of Inserts
Insert a new node x into binomial heap H. If N = , then only 1 steps. If N = , then only 2 steps. If N = , then only 3 steps. If N = , then only 4 steps. Inserting 1 item can take (log N) time. If N = , then log2 N steps. But, inserting sequence of N items takes O(N) time! (N/2)(1) + (N/4)(2) + (N/8)(3) 2N Amortized analysis. Basis for getting most operations down to constant time. 3 6 x 29 10 44 37 48 31 17 50
26
Priority Queues Heaps Operation Linked List Binary Binomial
Fibonacci * Relaxed make-heap 1 1 1 1 1 insert 1 log N log N 1 1 find-min What about search? Idea 1 = use a hash table to keep track of item's position in the heap. Idea 2 = use a (balanced) search tree. N 1 log N 1 1 delete-min N log N log N log N log N union 1 N log N 1 1 decrease-key 1 log N log N 1 1 delete N log N log N log N log N is-empty 1 1 1 1 1 just did this
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.