Fibonacci Heap.

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.
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
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
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
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.
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.
Two-tier relaxed heaps Presented by Claus Jensen Joint work with Amr Elmasry and Jyrki Katajainen Slides available at
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.
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.
Data structures Binomial Heaps - Binomial Trees B0B0 BkBk B k-1.
Fibonacci Heap. p2. Procedure Binary heap (worst-case) Fibonacci heap (amortized) MAKE-HEAP  (1) INSERT  (lg n)  (1) MINIMUM  (1) EXTRACT-MIN  (lg.
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
Binomial heaps, Fibonacci heaps, and applications
Chapter 11: Multiway Search Trees
Heaps Binomial Heaps Lazy Binomial Heaps 1.
Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn)
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

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

(a) 23 7 41 39 24 30 17 38 18 52 26 3 46 35 H.min (b) 23 7 41 39 24 30 17 38 18 52 26 3 46 35 H.min

H.min Insert key 21 H.min (a) 23 7 41 39 24 30 17 38 18 52 26 3 46 35 (b) 23 7 41 39 24 30 17 38 18 52 26 3 46 35 21

Fibonacci Heaps: A collection of 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

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

Unordered binomial tree: U0: a single node Uk: consists of 2 unordered binomial trees Uk-1 for which the root of one is made into any child of the root of the other 若只用 Make-Heap, Insert, Minimum, Extract-Min & Union. Fibonacci heap 只含 unordered binomial trees. Make-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.

Fib-Heap-Insert(H, x) x.degree = 0 x.p = NIL x.child = NIL x.mark = FALSE if H.min == NIL create a root list for H containing just x H.min = x else insert x into H’s root list if x.key < H.min.key H.n = H.n +1 Actual cost: O(1); Amortized cost: O(1) + 1

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

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

Extracting the minimum node: Fib-Heap-Extract-Min(H) z = H.min if z  NIL for each child x of z do { add x to the root list of H x.p = NIL } remove z from the root list of H if z == z.right H. min = NIL else H.min = z.right Consolidate(H) H.n = H.n – 1 return z

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) let A[0..D(H.n)] be a new array for i = 0 to D(n[H]) do A[i]=NIL for each node w in the root list of H do { x = w ; d = x.degree; while A[d]  NIL do { y = A[d] if x.key > y.key exchange xy Fib-Heap-Link(H, y, x) A[d] = NIL ; d = d+1; } A[d] = x; } H.min = NIL for i = 0 to D(H.n) do if A[i]  NIL if H.min==NIL create a root list for H containing just A[i]; H.min =A[i]; else insert A[i] into H’s root list if A[i].key < H.min.key H.min = A[i]

H.min (a) 23 7 41 39 24 30 17 38 18 52 26 3 46 35 21 H.min (b) 23 7 24 26 46 35 39 18 41 38 30 17 21 52

(c) 23 7 24 26 46 35 39 18 41 38 30 17 21 52 0 1 2 3 4 A w,x (d)

0 1 2 3 4 A (e) 23 7 24 26 46 35 39 18 41 38 30 17 21 52 w,x (f) x w

0 1 2 3 4 A (g) 23 7 24 26 46 35 39 18 41 38 30 17 21 52 x w (h)

0 1 2 3 4 35 (i) A 23 7 39 18 41 38 30 17 24 26 46 21 52 w, x (j)

0 1 2 3 4 35 (k) A 23 7 39 18 41 38 30 17 24 26 46 21 52 w, x 0 1 2 3 4 35 (l) A 23 7 39 18 41 38 30 17 24 26 46 21 52 w, x

35 (m) 23 7 39 18 41 38 30 17 24 26 46 21 52 H.min

Decreasing a key and deleting a node: do not preserve the property that all trees in the Fibonacci heap are unordered binomial trees. Fib-Heap-Decrease-key(H, x, k) if k>x.key error “new key is greater than current key” x.key = k y  x.p if yNIL and x.key< y.key { CUT(H, x, y) CASCADING-CUT(H, y) } if x.key< H.min.key H.min = x

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) z y.p if zNIL if y.mark == FALSE y.mark= TRUE else CUT(H, y, z) CASCADING-CUT(H, z) Fib-Heap-Delete(H, x) { Fib-Heap-Decrease-key(H, x, -) Fib-Heap-Extract-Min(H) }

35 (a) 23 7 39 18 41 38 30 17 24 26 46 21 52 H.min (b) 15

(c) 15 26 23 7 41 38 30 17 24 39 18 21 52 H.min 5 (d) 15 41 38 39 18 21 52 23 7 30 17 24 H.min 5 26

(e) 15 41 38 39 18 21 52 23 7 30 17 5 26 H.min 24

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)

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))

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

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

Fibonacci number: Lemma 2: For all integer k0, pf: By induction on k k=0, F2=F1+F0=1 = 1+F0 Suppose

x: any node in a Fibonacci heap, and let k=x.degree Lemma 3: x: any node in a Fibonacci heap, and let k=x.degree Then, size(x)  Fk+2  k pf: Sk : denote the min possible value of size(z) over all nodes z such that z.degree=k. Trivially, S0=1, S1=2, and S2=3 Sk size(x) , size(y1)  1 size(x)  Sk  2+i=2,…,k Si-2 By induction on k that SkFk+2 Clearly for k=0 and 1 Assume that k2 and that SiFi+2 for i=0,…,k-1 We have Sk 2+i=2,…,k Si-2  2+i=2,…,k Fi = 1+ i=0,…,k Fi = Fk+2 Thus, size(x)  Sk  Fk+2  k x y1 y2 yk  S0  Sk-2

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)