110 likes | 226 Views
Priority Queues. MakeQueue create new empty queue Insert ( Q , k , p ) insert key k with priority p Delete ( Q, k ) delete key k (given a pointer) DeleteMin (Q) delete key with min priority Meld( Q 1 , Q 2 ) merge two sets Empty ( Q ) returns if empty Size ( Q ) returns # keys
E N D
PriorityQueues MakeQueuecreate new emptyqueue Insert(Q,k,p) insertkeykwithpriorityp Delete(Q,k) deletekeyk (given a pointer) DeleteMin(Q) deletekeywith min priority Meld(Q1,Q2) mergetwo sets Empty(Q) returnsifempty Size(Q) returns #keys FindMin(Q) returnskeywith min priority
PriorityQueues – Ideal Times Thm 1) Meld O(n1-ε) DeleteMinΩ(log n) • 2) Insert, DeleteO(t) FindMinΩ(n/2O(t)) Follows from Ω(n∙logn) sortinglowerbound 2) [G.S. Brodal, S. Chaudhuri, J. Radhakrishnan,The Randomized Complexity of Maintaining the Minimum. In Proc. 5th Scandinavian Workshop on Algorithm Theory, volume 1097 of Lecture Notes in Computer Science, pages 4-15. Springer Verlag, Berlin, 1996] MakeQueue, Meld, Insert, Empty, Size, FindMin: O(1) Delete, DeleteMin: O(log n)
0 3 1 Binomial Queues 3 2 9 2 0 0 1 4 5 7 8 1 0 0 • [Jean Vuillemin, A data structure for manipulating priority queues, • Communications of the ACM archive, Volume 21(4), 309-315, 1978] 6 5 8 0 7 • Binomial tree • each node stores a (k,p) and satisfiesheaporderwithrespect to priorities • all nodes have a rankr (leaf = rank 0, a rank r node has exactlyonechild of each of the ranks 0..r-1) • Binomial queue • forest of binomial treeswithrootsstored in a list withstrictlyincreasingrootranks
Problem • Hints • Two rank itreescanbelinked to create a rank i+1 tree in O(1) time • Potential Φ = max rank + #roots x y x y r+1 r r link x ≥ y r Implement binomial queue operations to achieve the ideal times in the amortizedsense
Dijkstra’sAlgorithm(Single sourceshortestpath problem) AlgorithmDijkstra(V, E, w, s) Q := MakeQueue dist[s] := 0 Insert(Q, s, 0) forvV \ { s } do dist[v] := +∞ Insert(Q, v, +∞) whileQ ≠ do v := DeleteMin(Q) foreachu : (v, u) Edo ifu Qanddist[v]+w(v, u) < dist[u] then dist[u] := dist[v]+w(v, u) DecreaseKey(u, dist[u]) n x Insert + n x DeleteMin+ m x DecreaseKey Binaryheaps / Binomial queues : O((n + m)∙logn)
PriorityBounds AmortizedWorst-case Dijkstra’sAlgorithm O(m + n∙logn) (and Minimum SpanningTree O(m∙log* n)) Empty, FindMin, Size, MakeQueue – O(1) worst-case time
3 FibonacciHeaps 3 2 0 • [Fredman, Tarjan, Fibonacci Heaps and Their Use in Improved Network Algorithms, • Journal of the ACM, Volume 34(3), 596-615, 1987] 4 7 1 0 6 5 0 7 • F-tree • heaporderwithrespect to priorities • all nodes have a rankr {degree, degree + 1} (r = degree + 1 node is marked as having lost a child) • The i’thchildof a node from the right has rank≥i - 1 • FibonacciHeap • forest (list) of F-trees (treescan have equal rank)
FibonnaciHeap Property Thm Max rank of a node in an F-tree is O(log n) ProofA rank r node has at least 2 children of rank ≥ r – 3. By inductionsubtreesize is at least 2└r/3┘□ ( in fact the size is at leastr , where=(1+5)/2 )
Problem • Hints • Two rank itreescanbelinked to create a ranki+1 tree in O(1) time • Eliminating nodes violating orderor nodes having lost twochildren • Potential Φ = 2∙marks + #roots x x y x y x r+1 r y r y link x ≥ y r d r cut degree(x) = d ≤ r-2 ImplementFibonacciHeap operations withamortized O(1) time for all operations, except O(log n) for deletions
Implementation of FibonacciHeap Operations FindMinMaintain pointer to min root InsertCreate new tree = new rank 0 node +1 JoinConcatenatetwoforestsunchanged DeleteDecreaseKey -∞ + DeleteMin DeleteMinRemove min root-1 + addchildren to forest+O(log n ) + bucketsortroots by rankonly O(log n ) not linkedbelow + link whiletworootsequal rank -1 each DecreaseKeyUpdatepriority + cut edge to parent +3 + ifparentnow has r – 2 children, recursively cut parentedges -1 each, +1 final cut * = potential change
Worst-Case Operations (withoutJoin) • [Driscoll, Gabow, Shrairman, Tarjan, Relaxed Heaps: An Alternative to Fibonacci Heaps with Applications to Parallel Computation, • Communications of the ACM, Volume 34(3), 596-615, 1987] Basicideas Require ≤ max-rank + 1 trees in forest(otherwise rank r where two trees can be linked) Replacecutting in F-trees by having O(log n) nodes violating heaporder Transformationreplacingtwo rank rviolations by one rank r+1 violation