Fibonacci Heap.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Advanced Data structure
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.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters.
1 Binomial heaps, Fibonacci heaps, and applications.
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.
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.
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.
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?
Fibonacci Heaps.
Fibonacci Heaps. Analysis FibonacciAnalysis.ppt Video  iew/cop5536sahni
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.
Fibonacci Heap Fibonacci heapshave better asymptotic time bounds than binary heaps for the INSERT, UNION, and DECREASE-KEY operations, and they.
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
"Teachers open the door, but you must enter by yourself. "
Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick
Binomial heaps, Fibonacci heaps, and applications
Chapter 11: Multiway Search Trees
Summary of General Binary search tree
Heaps Binomial Heaps Lazy Binomial Heaps 1.
Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn)
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
Binomial Heaps Chapter 19.
The Heap Data Structure
Binomial Tree Adapted from: Kevin Wayne Bk-1 B0 Bk
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.
CS 583 Analysis of Algorithms
Ch 6: Heapsort Ming-Te Chi
Lecture 29 Heaps Chapter 12 of textbook Concept of heaps Binary heaps
Binomial Heap.
Lecture 3 / 4 Algorithm Analysis
Chapter 20 Binomial Heaps
ערמות בינומיות ופיבונצ'י
CS 583 Analysis of Algorithms
"Teachers open the door, but you must enter by yourself. "
§3 Worst-Case vs. Amortized
CS 583 Analysis of Algorithms
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures.
CS6045: Advanced Algorithms
21장. Fibonacci Heaps 숭실대학교 인공지능 연구실 석사 2학기 김완섭.
Topic 6: Binary Search Tree Data structure Operations
Binary and Binomial Heaps
Topic 5: Heap data structure heap sort Priority queue
Binomial heaps, Fibonacci heaps, and applications
HEAPS.
Binomial heaps, Fibonacci heaps, and applications
Binary SearchTrees [CLRS] – Chap 12.
Fibonacci Heaps.
Binomial heaps, Fibonacci heaps, and applications
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees
Asst. Prof. Dr. İlker Kocabaş
Presentation transcript:

Fibonacci Heap

Binary heap (worst-case) Fibonacci heap (amortized) Procedure Binary heap (worst-case) Fibonacci heap (amortized) MAKE-HEAP  (1) INSERT  (lg n) MINIMUM EXTRACT-MIN O(lg n) 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 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

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

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.

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: H.min  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

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

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

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)