Fibonacci Heap. p2. Procedure Binary heap (worst-case) Fibonacci heap (amortized) MAKE-HEAP  (1) INSERT  (lg n)  (1) MINIMUM  (1) EXTRACT-MIN  (lg.

Slides:



Advertisements
Similar presentations
Binomial Heaps. Heap Under most circumstances you would use a “normal” binary heap Except some algorithms that may use heaps might require a “Union” operation.
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.
Fibonacci Heap BH&FH. Why BH&FH -- 程式目的 MinPQ Mergeable MinPQ MaxPQ DEPQ Min-LeftistMin-Skew Min-B-Heap Min-F-Heap Min Heap DeapMinMax Symmetric Max Data.
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
Advanced Data structure
Rank-Pairing Heaps Robert Tarjan, Princeton University & HP Labs Joint work with Bernhard Haeupler and Siddhartha Sen, ESA
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis Of Fibonacci Heaps. MaxDegree Let N i = min # of nodes in any min (sub)tree whose root has i children. N 0 = 1. N 1 =
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.
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.
Fibonacci Heaps. Single Source All Destinations Shortest Paths
Fibonacci Heap.
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.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
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.
1 Binomial heaps, Fibonacci heaps, and applications.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
Interval Trees.
Binomial Heaps Melalite Ayenew Dec 14, 2000.
Chapter 9 Heap Structures
Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap.
Binomial Heaps Referred to “MIT Press: Introduction to Algorithms 2 nd Edition”
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Lecture X Fibonacci Heaps
CS 473Lecture X1 CS473-Algorithms Lecture BINOMIAL HEAPS.
Chapter 19: Fibonacci Heap Many of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore.
Advanced Data Structures CHAPTER 2. Jaruloj ChongstitvatanaChapter 2: Advanced Data Structures2 Outlines Binary search trees B-trees Heaps and priority.
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
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.
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Fibonacci Heaps.
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.
Heapsort Lecture 4 Asst. Prof. Dr. İlker Kocabaş.
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.
Lecture: Priority Queue. Questions Is array a data structure? What is a data structure? What data structures are implemented by array? Priority queue.
Data structures Binomial Heaps - Binomial Trees B0B0 BkBk B k-1.
CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Fibonacci Heap Fibonacci heapshave better asymptotic time bounds than binary heaps for the INSERT, UNION, and DECREASE-KEY operations, and they.
DATA STRUCTURES II UNIT 4 – Heaps SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
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))
Analysis of Algorithms
Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick
Chapter 11: Multiway Search Trees
Binomial Heaps Chapter 19.
Priority Queues MakeQueue create new empty queue
Lecture 29 Heaps Chapter 12 of textbook Concept of heaps Binary heaps
Binomial Heap.
Chapter 20 Binomial Heaps
Fibonacci Heap.
ערמות בינומיות ופיבונצ'י
CS 583 Analysis of Algorithms
CS 583 Analysis of Algorithms
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures.
21장. Fibonacci Heaps 숭실대학교 인공지능 연구실 석사 2학기 김완섭.
Binomial heaps, Fibonacci heaps, and applications
Fibonacci Heaps.
Binomial heaps, Fibonacci heaps, and applications
Presentation transcript:

Fibonacci Heap

p2. Procedure Binary heap (worst-case) Fibonacci heap (amortized) MAKE-HEAP  (1) INSERT  (lg n)  (1) MINIMUM  (1) EXTRACT-MIN  (lg n) O(lg n) UNION  (n)  (1) DECREASE-KEY  (lg n)  (1) DELETE  (lg n) O(lg n)

p3. (a) H.min (b) H.min

p4. (a) H.min (b) Insert key 21

p5. Fibonacci Heaps: a collection of min-heap ordered trees. trees: rooted but unordered Each node x: x.p points to its parent x.child points to any one of its children children of x are linked together in a circular doubly linked list x.left, x.right: points to its left and right siblings. x.degree: number of children in the child list of x x.mark: indicate whether node x has lost a child since the last time x was mode the child of another node H.min: points to the root of the tree containing a minimum key H.n: number of nodes in H

p6. Potential function: D(n): upper bound on the max degree of any node in an n-node Fibonacci heap Fibonacci heap # of trees in the rooted list of H # of marked nodes in H  (H) = t(H) + 2m(H)

p7. Mergeable-heap operations: Create a new Fibonacci heap: Make-Fib-Heap(H) Allocate and return the Fibonacci heap object H with H.n=0 and H.min=nil t(H)=0, m(H)=0 so  (H)=0  The amortized cost of Make-Fib-Heap is equal to its O(1) actual cost.

p8. Fib-Heap-Insert(H, x) 1. x.degree = 0 2. x.p = NIL 3. x.child = NIL 4. x.mark = FALSE 5. if H.min == NIL 6. create a root list for H containing just x 7. H.min = x 8. else insert x into H’s root list 9. if x.key < H.min.key 10. H.min = x 11. H.n = H.n +1 Actual cost: O(1); Amortized cost: O(1) + 1

p9. Finding the minimum node: H.min  O(1) Amortized cost O(1)  is not changed Uniting 2 Fibonacci heaps: Fib-Heap-Union(H 1, H 2 ) 1. H = Make-Fib-Heap( ) 2. H.min = H 1.min 3. concatenate the root list of H 2 with the root list of H 4. if (H 1.min == NIL) or (H 2.min  NIL and H 2.min.key<H 1.min.key) 5. H.min = H 2.min 6. H.n = H 1.n + H 2.n 7. return H

p10.  (H) - (  (H 1 )+  (H 2 )) = (t(H)+2m(H)) – ((t(H 1 )+2m(H 1 )) + (t(H 2 )+2m(H 2 ))) = 0 t(H) = t(H 1 ) + t(H 2 ), m(H) = m(H 1 ) + m(H 2 ) Thus the amortized cost of Fib-Heap-Union is therefore O(1)  actual cost

p11. Extracting the minimum node: Fib-Heap-Extract-Min(H) 1. z = H.min 2. if z  NIL 3. for each child x of z 4. do { add x to the root list of H 5. x.p = NIL } 6. remove z from the root list of H 7. if z == z.right 8. H. min = NIL 9. else H.min = z.right 10. Consolidate(H) 11. H.n = H.n – return z

p12. Fib-Heap-Link(H, y, x) {1. remove y from the root list of H; 2. make y a child of x; x.degree =x.degree+1; 3. y.mark = FALSE; } Consolidate(H) 1. let A[0..D(H.n)] be a new array 2. for i = 0 to D(n[H]) do A[i]=NIL 3. for each node w in the root list of H 4. do { x = w ; d = x.degree; 5. while A[d]  NIL 6. do { y = A[d] 7. if x.key > y.key exchange x  y 8. Fib-Heap-Link(H, y, x) 9. A[d] = NIL ; d = d+1; } 10. A[d] = x; } 11. H.min = NIL 12. for i = 0 to D(H.n) do 13. if A[i]  NIL 14. if H.min==NIL 15. create a root list for H containing just A[i]; H.min =A[i]; 16. else insert A[i] into H’s root list 17. if A[i].key < H.min.key H.min = A[i]

p13. (a) (b) H.min

p14. (c) A w,x A (d) w,x

p A (e) w,x (f) A x w

p A (g) x w 35 (h) A x w

p (i) A w, x (j) A w, x

p (k) A w, x (l) A w, x

p (m) H.min

p20. Analysis of Fib-Heap-Extract-Min: H : n-node Fib-Heap Actual cost : O(D(n)) : for-loop in Fib-Heap-Extract-Min D(n)+t(H)-1 : size of the root list Total actual cost: O(D(n))+t(H) Potential before extracting : t(H)+2m(H) Potential after extracting :  D(n)+1+2m(H) At most D(n)+1 nodes remain on the list and no nodes become marked Thus the amortized cost is at most: O(D(n))+t(H)+[(D(n)+1+2m(H)) – (t(H)+2m(H))] = O(D(n)+t(H)-t(H)) = O(D(n))

p21. Decreasing a key and deleting a node: Decrease a key in O(1) amortized time and delete a node in O(D(n)) amortized time. Fib-Heap-Decrease-key(H, x, k) 1. if k>x.key 2. error “new key is greater than current key” 3. x.key = k 4. y  x.p 5. if y  NIL and x.key< y.key 6. { CUT(H, x, y) 7. CASCADING-CUT(H, y) } 8. if x.key< H.min.key 9. H.min = x

p22. CUT(H, x, y) 1. remove x from the child list of y, decrease y.degree 2. add x to the root list of H 3. x.p = NIL 4. x.mark = FALSE CASCADING-CUT(H, y) 1. z  y.p 2. if z  NIL 3. if y.mark == FALSE 4. y.mark= TRUE 5. else CUT(H, y, z) 6. CASCADING-CUT(H, z) Fib-Heap-Delete(H, x) { Fib-Heap-Decrease-key(H, x, -  ) Fib-Heap-Extract-Min(H) }

p (a) H.min (b) H.min

p24. (c) H.min 5 (d) H.min 5 26

p25. (e) H.min 24

p26. Analysis of Decrease-key: Actual cost : O(c) suppose CASCADING-CUT is called c times Each recursive call of CASCADING-CUT except for the last one, cuts a marked node and clears the mark bit. After Decrease-key, there are at most t(H)+c trees, and at most m(H)-c+2 marked nodes. Last call of CASCADING-CUT may have marked a node Thus; the potential change is : [t(H)+c+2(m(H)-c+2)] - [t(H)+2m(H)] = 4-c Amortized cost: O(c)+4-c = O(1) By scaling up the units of potential to dominate the constant hidden in O(c)

p27. Bounding the maximum degree: Goal : D(n)   log  n ,  = Let size(x) be the number of nodes, including x itself, in the subtree rooted at x

p28. Lemma 1 x : any node in a Fibonacci heap and x.degree=k y 1, …, y k : children of x in the order in which they were linked to x. (from the earliest to the latest) Then, y 1.degree  0 and y i.degree  i-2 for i=2,3,…,k Pf: Clearly, y 1.degree  0 For i  2, note that when y i was linked to x, all of y 1, …, y i-1 were children of x, so we MUST have had x.degree  i-1. Node y i is linked to x only if x.degree = y i.degree, thus y i.degree  i-1 at that time. Since then, node y i has lost at most ONE child, since it would have been cut from x if it had lost two children. We conclude that y i.degree  i-2 x y1y1 y2y2 ykyk

p29. Lemma 2: For all integer k  0, pf: By induction on k k=0, F 2 =F 1 +F 0 =1 = 1+F 0 Suppose Fibonacci number:

p30. Lemma 3: x: any node in a Fibonacci heap, and let k=x.degree Then, size(x)  F k+2   k pf: S k : denote the min possible value of size(z) over all nodes z such that z.degree=k. Trivially, S 0 =1, S 1 =2, and S 2 =3 S k  size(x), size(y 1 )  1 size(x)  S k  2+  i=2,…,k S i-2 By induction on k that S k  F k+2. Clearly for k=0 and 1 Assume that k  2 and that S i  F i+2 for i=0,…,k-1 We have S k  2+  i=2,…,k S i-2  2+  i=2,…,k F i = 1+  i=0,…,k F i = F k+2 Thus, size(x)  S k  F k+2   k x y1y1 y2y2 ykyk  S 0  S k-2

p31. Corollary 4: The max degree D(n) of any node in an n-node Fibonacci heap is O(lg n) pf: x: any node in an n-node Fibonacci heap k=degree[x] n  size(x)   k log  n  k Thus the max degree D(n) of any node is O(lg n)