1 Binomial heaps, Fibonacci heaps, and applications.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Fibonacci Heaps Especially desirable when the number of calls to Extract-Min & Delete is small (note that all other operations run in O(1) This arises.
Graph Algorithms - 4 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 14Feb 14, 2014Carnegie Mellon University.
Single Source Shortest Paths
Priority Queues  MakeQueuecreate 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.
Advanced Data structure
Rank-Pairing Heaps Robert Tarjan, Princeton University & HP Labs Joint work with Bernhard Haeupler and Siddhartha Sen, ESA
By Amber McKenzie and Laura Boccanfuso. Dijkstra’s Algorithm Question: How do you know that Dijkstra’s algorithm finds the shortest path and is optimal.
CS38 Introduction to Algorithms Lecture 5 April 15, 2014.
Fibonacci Heaps CS 252: Algorithms Geetika Tewari 252a-al Smith College December, 2000.
1 Pertemuan 20 Binomial Heap Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Rank-Pairing Heaps Bernhard Haeupler, Siddhartha Sen, and Robert Tarjan, ESA
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.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement.
Fibonacci Heaps. Single Source All Destinations Shortest Paths
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
ANALYSIS OF SOFT HEAP Varun Mishra April 16,2009.
Algorithms 2005 Ramesh Hariharan. Amortization in Dynamic Algorithms A single insertion/deletion might take say O(log n) time Does a sequence of n insertions.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
1 Fibonacci heaps, and applications. 2 Yet a better MST algorithm (Fredman and Tarjan) Iteration i: We grow a forest, tree by tree, as follows. Start.
Binomial heaps, Fibonacci heaps, and applications
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
CS 473Lecture X1 CS473-Algorithms Lecture BINOMIAL HEAPS.
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.
Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another.
1 Fat heaps (K & Tarjan 96). 2 Goal Want to achieve the performance of Fibonnaci heaps but on the worst case. Why ? Theoretical curiosity and some applications.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
Fibonacci Heaps. Analysis FibonacciAnalysis.ppt Video  iew/cop5536sahni
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
1 Fibonacci heaps: idea List of multiway trees which are all heap-ordered. Definition: A tree is called heap-ordered if the key of each node is greater.
Fibonacci Heap Fibonacci heapshave better asymptotic time bounds than binary heaps for the INSERT, UNION, and DECREASE-KEY operations, and they.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
Fibonacci Heaps. Fibonacci Binary insert O(1) O(log(n)) find O(1) N/A union O(1) N/A minimum O(1) O(1) decrease key O(1) O(log(n)) delete O(log(n) O(log(n))
Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick
Binomial heaps, Fibonacci heaps, and applications
Heaps Binomial Heaps Lazy Binomial Heaps 1.
Binomial heaps, Fibonacci heaps, and applications
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Priority Queues MakeQueue create new empty queue
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures and may also be used to do an increase key in a min structure (remove.
Minimum-Cost Spanning Tree
ערמות בינומיות ופיבונצ'י
Outline This topic covers Prim’s algorithm:
Minimum-Cost Spanning Tree
Strict Fibonacci Heaps
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures.
Binary and Binomial Heaps
Binomial heaps, Fibonacci heaps, and applications
Binomial heaps, Fibonacci heaps, and applications
Fibonacci Heaps.
Binomial heaps, Fibonacci heaps, and applications
Priority Queues Supports the following operations. Insert element x.
Minimum-Cost Spanning Tree
Presentation transcript:

1 Binomial heaps, Fibonacci heaps, and applications

2 Binomial trees B0B0 B1B1 B (i-1) BiBi

3 Binomial trees B0B0 B1B1 B (i-2) B (i-1)... BiBi Can prove by induction

4 Properties of binomial trees 1) | B k | = 2 k 2) degree(root(B k )) = k 3) depth(B k ) = k ==> The degree and depth of a binomial tree with at most n nodes is at most log(n). Define the rank of B k to be k

5 Binomial heaps (def) A collection of binomial trees at most one of every rank. Items at the nodes, heap-min ordered Possible rep: Doubly link roots and children of every node. Parent pointers needed for delete. B0 B1B1 min B3

6 Binomial heaps (operations) Operations are defined via a basic operation, called linking, of binomial trees: Produce a B k from two B k-1, keep heap order

7 Binomial heaps (ops cont.) Basic operation is meld(h1,h2): Like addition of binary numbers. B0B0 B1B1 B3B3 B4B4 B0B0 B3B3 h1: h2: + B1B1 B4B4 B2B2 B2B2 B4B4 B4B4 B5B5 B5B5 Use: every number has a unique binary representation

8 Binomial heaps (ops cont.) Findmin(h): obvious Insert(x,h) : meld a new heap with a single B 0 containing x, with h deletemin(h) : Chop off the minimal root. Meld the subtrees with h. Update minimum pointer if needed. delete(x,h) : Bubble x up and continue like delete-min decrease-key(x,h,  ) : Bubble up, update min ptr if needed All operations take O(log n) time in the worst case, except find-min(h) that takes O(1) time.

9 Amortized analysis We are interested in the worst case running time of a sequence of operations (inserts) Example: binary counter single operation -- increment

10 Amortized analysis: k = #bits Obs 1 : At every increment exactly one bit changes from 0 to 1. Obs 2: The number of 0  1 changes and the number of 1  0 changes is the same (upto # bits)  # operations = O(# 0  1 changes)  after N increments have N changes 0  1  O(N) operations  O(1) amortized complexity

11 Amortized analysis: implications Amortized complexity of insert = O(1)  building a tree via a sequence of inserts = O(N)

12 Amortized analysis (Cont.) On the worst case increment takes O(k). k = #digits What is the complexity of a sequence of increments (on the worst case) ? Define a potential of the counter: Amortized(increment) = actual(increment) +   (c) = ?

13 Amortized analysis (Cont.) Amortized(increment 1 ) = actual(increment 1 ) +  1 -  0 Amortized(increment 2 ) = actual(increment 2 ) +  2 -  1 Amortized(increment n ) = actual(increment n ) +  n -  (n-1) ………… +  i Amortized(increment i ) =  i actual(increment i ) +  n -  0  i Amortized(increment i )   i actual(increment i ) if  n -  0  0

14 Amortized analysis (Cont.) Define a potential of the counter:  (c) = #(ones) Amortized(increment) = actual(increment) +  Amortized(increment) = 1+ #(1 => 0) #(1 => 0) = O(1) ==> Sequence of n increments takes O(n) time

15 Binomial heaps - amortized ana.  (collection of heaps) = #(trees) Amortized cost of insert O(1) Amortized cost of other operations still O(log n)

16 Binomial heaps + lazy meld Allow more than one tree of each rank. Meld (h1,h2) : Concatenate the lists of binomial trees. Update the minimum pointer to be the smaller of the minimums O(1) worst case and amortized.

17 Binomial heaps + lazy meld As long as we do not do a delete-min our heaps are just doubly linked lists: Delete-min : Chop off the minimum root, add its children to the list of trees. Successive linking: Traverse the forest keep linking trees of the same rank, maintain a pointer to the minimum root.

18 Binomial heaps + lazy meld Possible implementation of delete-min is using an array indexed by rank to keep at most one binomial tree of each rank that we already traversed. Once we encounter a second tree of some rank we link them and keep linking until we do not have two trees of the same rank. We record the resulting tree in the array Amortized(delete-min) = = (#links + max-rank) - #links = O(log(n))

19 Fibonacci heaps (Fredman & Tarjan 84) Want to do decrease-key(x,h,  ) faster than delete+insert. Ideally in O(1) time. Why ?

20 Dijkstra’s shortest path algorithm Let G = (V,E) be a weighted (weights are non-negative) undirected graph, let s  V. Want to find the distance (length of the shortest path), d(s,v) from s to every other vertex. s

21 Application #2 : Prim’s algorithm for MST Start with T a singleton vertex. Grow a tree by repeating the following step: Add the minimum cost edge connecting a vertex in T to a vertex out of T.

22 Application #2 : Prim’s algorithm for MST Maintain the vertices out of T but adjacent to T in a heap. The key of a vertex v is the weight of the lightest edge (v,w) where w is in the tree. Iteration: Do a delete-min. Let v be the minimum vertex and (v,w) the lightest edge as above. Add (v,w) to T. For each edge (w,u) where u  T, if key(u) =  insert u into the heap with key(u) = w(w,u) if w(w,u) < key(u) decrease the key of u to be w(w,u). With regular heaps O(m log(n)). With F-heaps O(n log(n) + m).

23 Suggested implementation for decrease-key(x,h,  ): If x with its new key is smaller than its parent, cut the subtree rooted at x and add it to the forest. Update the minimum pointer if necessary.

25 Decrease-key (cont.) Does it work ? Obs1: Trees need not be binomial trees any more.. Do we need the trees to be binomial ? Where have we used it ? In the analysis of delete-min we used the fact that at most log(n) new trees are added to the forest. This was obvious since trees were binomial and contained at most n nodes.

26 Decrease-key (cont.) Such trees are now legitimate. So our analysis breaks down.

27 Fibonacci heaps (cont.) We shall allow non-binomial trees, but will keep the degrees logarithmic in the number of nodes. Rank of a tree = degree of the root. Delete-min: do successive linking of trees of the same rank and update the minimum pointer as before. Insert and meld also work as before.

28 Fibonacci heaps (cont.) Decrease-key (x,h,  ): indeed cuts the subtree rooted by x if necessary as we showed. in addition we maintain a mark bit for every node. When we cut the subtree rooted by x we check the mark bit of p(x). If it is set then we cut p(x) too. We continue this way until either we reach an unmarked node in which case we mark it, or we reach the root. This mechanism is called cascading cuts.

30 Fibonacci heaps (delete) Delete(x,h) : Cut the subtree rooted at x and then proceed with cascading cuts as for decrease key. Chop off x from being the root of its subtree and add the subtrees rooted by its children to the forest If x is the minimum node do successive linking

31 Fibonacci heaps (analysis)  (collection of heaps) = #(trees) + 2#(marked nodes) Want everything to be O(1) time except for delete and delete-min. ==> cascading cuts should pay for themselves Actual(decrease-key) = O(1) + #(cascading cuts)  (decrease-key) = O(1) - #(cascading cuts) ==> amortized(decrease-key) = O(1) !

32 Fibonacci heaps (analysis) Cascading cuts and successive linking will pay for themselves. The only question is what is the maximum degree of a node ? How many trees are being added into the forest when we chop off a root ? What about delete and delete-min ?

33 Fibonacci heaps (analysis) Lemma 1 : Let x be any node in an F-heap. Arrange the children of x in the order they were linked to x, from earliest to latest. Then the i-th child of x has rank at least i-2. x 12 Proof: When the i-th node was linked it must have had at least i-1 children. Since then it could have lost at most one.

34 Fibonacci heaps (analysis) Corollary1 : A node x of rank k in a F-heap has at least  k descendants, where  = (1 +  5)/2 is the golden ratio. Proof: Let s k be the minimum number of descendants of a node of rank k in a F-heap. By Lemma 1 s k   i=0 s i + 2 k-2 x s 0 =1, s 1 = 2

35 Fibonacci heaps (analysis) Proof (cont): Fibonnaci numbers satisfy F k+2 =  i=2 F i + 2, for k  2, and F 2 =1 so by induction s k  F k+2 It is well known that F k+2   k k It follows that the maximum degree k in a F-heap with n nodes is such that  k  n so k  log(n) / log(  ) = log(n)