Fibonacci Heap 1. 2 3 Fibonacci heapshave better asymptotic time bounds than binary heaps for the INSERT, UNION, and DECREASE-KEY operations, and they.

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.
COL 106 Shweta Agrawal and Amit Kumar
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
Pairing Heaps. Experimental results suggest that pairing heaps are actually faster than Fibonacci heaps.  Simpler to implement.  Smaller runtime overheads.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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.
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 =
1 Theory I Algorithm Design and Analysis (9 – Priority queues: Fibonacci heaps) T. Lauer.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Rank-Pairing Heaps Bernhard Haeupler, Siddhartha Sen, and Robert Tarjan, ESA
Nick Harvey & Kevin Zatloukal
Binomial Heaps. Min Binomial Heap Collection of min trees
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.
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
Fibonacci Heap.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
5.9 Heaps of optimal complexity
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
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.
ANALYSIS OF SOFT HEAP Varun Mishra April 16,2009.
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.
1 CS 6234 Advanced Algorithms: Splay Trees, Fibonacci Heaps, Persistent Data Structures.
Interval Trees.
Chapter 9 Heap Structures
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Lecture X Fibonacci Heaps
CS 473Lecture X1 CS473-Algorithms Lecture BINOMIAL HEAPS.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
Change Keys in heaps Fibonacci heap Zhao Xiaobin.
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.
H EAPS. T WO KINDS OF HEAPS : MAX AND MIN Max: Every child is smaller than its parent Meaning the max is the root of the tree 10 / \ 9 7 / \ 6 8 / \ 2.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
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.
Fibonacci Heap. p2. Procedure Binary heap (worst-case) Fibonacci heap (amortized) MAKE-HEAP  (1) INSERT  (lg n)  (1) MINIMUM  (1) EXTRACT-MIN  (lg.
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))
Leftist Trees Linked binary tree.
Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick
Binomial heaps, Fibonacci heaps, and applications
Binomial Heaps On the surface it looks like Binomial Heaps are great if you have no remove mins. But, in this case you need only keep track of the current.
Heaps Binomial Heaps Lazy Binomial Heaps 1.
CMSC 341 Lecture 13 Leftist Heaps
Binomial Heaps Chapter 19.
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.
Pairing Heaps Actual Complexity.
A simpler implementation and analysis of Chazelle’s
Fibonacci Heap.
ערמות בינומיות ופיבונצ'י
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
Pairing Heaps Actual Complexity
Presentation transcript:

Fibonacci Heap 1

2

3

Fibonacci heapshave better asymptotic time bounds than binary heaps for the INSERT, UNION, and DECREASE-KEY operations, and they have the same asymptotic running times for the remaining operations. Note, however, that the running times for Fibonacci heaps in Figure 19.1 are amortized time bounds, not worst-case per-operation time bounds. The UNION operation takes only constant amortized time in a Fibonacci heap, which is significantly better than the linear worst-case time required in a binary heap (assuming, of course, that an amortized time bound suffices). 4

5

Structure of Fibonacci heaps We access a given Fibonacci heap H by a pointer H:min to the root of a tree containing the minimum key; we call this node the minimum node of the Fibonacci heap. If more than one root has a key with the minimum value, then any such root may serve as the minimum node. When a Fibonacci heap H is empty, H:min is NIL. The roots of all the trees in a Fibonacci heap are linked together using their left and right pointers into a circular, doubly linked list called the root list of the Fibonacci heap. The pointer H:min thus points to the node in the root list whose key is minimum. Trees may appear in any order within a root list. We rely on one other attribute for a Fibonacci heap H: H:n, the number of nodes currently in H. 6

7

Heap H Fibonacci Heaps: Structure Fibonacci heap. n Set of heap-ordered trees. n Maintain pointer to minimum element. n Set of marked nodes. roots heap-ordered tree each parent larger than its children

Heap H Fibonacci Heaps: Structure Fibonacci heap. n Set of heap-ordered trees. n Maintain pointer to minimum element. n Set of marked nodes. min find-min takes O(1) time

Heap H Fibonacci Heaps: Structure Fibonacci heap. n Set of heap-ordered trees. n Maintain pointer to minimum element. n Set of marked nodes. min use to keep heaps flat (stay tuned) marked

11 Fibonacci Heaps: Notation Notation. n = number of nodes in heap. rank(x) =degree D(x) = number of children of node x. rank(H) = max rank of any node in heap H. trees(H) = number of trees in heap H. marks(H) = number of marked nodes in heap H rank = 3 min Heap H trees(H) = 5 marks(H) = 3 marked n = 14

12

13 Fibonacci Heaps: Potential Function  (H) =  3 = min Heap H  (H) = trees(H) + 2  marks(H) potential of heap H trees(H) = 5 marks(H) = 3 marked

Mergeable-heap operations The mergeable-heap operations on Fibonacci heaps delay work as long as possible. The various operations have performance trade-offs. For example, we insert a node by adding it to the root list, which takes just constant time. If we were to start with an empty Fibonacci heap and then insert k nodes, the Fibonacci heap would consist of just a root list of k nodes. 14

15

16 Fibonacci Heaps: Insert Insert. n Create a new singleton tree. n Add to root list; update min pointer (if necessary) insert 21 min Heap H

17 Fibonacci Heaps: Insert Insert. n Create a new singleton tree. n Add to root list; update min pointer (if necessary) min Heap H insert 21

18 Fibonacci Heaps: Insert Analysis Actual cost. O(1) Change in potential. +1 Amortized cost. O(1) min Heap H  (H) = trees(H) + 2  marks(H) potential of heap H

19 Union

20 Fibonacci Heaps: Union min Heap H'Heap H''

21

22 Fibonacci Heaps: Union Union. Combine two Fibonacci heaps. Representation. Root lists are circular, doubly linked lists min Heap H

23 Fibonacci Heaps: Union Actual cost. O(1) Change in potential. 0 Amortized cost. O(1)  (H) = trees(H) + 2  marks(H) potential function min Heap H

24 Extracting the minimum node

25

26

27 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min

28 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min

29 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current

30 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank current min rank

31 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank

32 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank

33 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank link 23 into 17

34 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank link 17 into 7

35 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank current min rank link 24 into 7

36 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank

37 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank

38 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank

39 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank link 41 into 18

40 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank

41 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min rank current

42 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min stop

43 Fibonacci Heaps: Extract Delete min. Actual cost. O(rank(H)) + O(trees(H)) O(rank(H)) to meld min's children into root list. O(rank(H)) + O(trees(H)) to update min. O(rank(H)) + O(trees(H)) to consolidate trees. Change in potential. O(rank(H)) - trees(H) trees(H' )  rank(H) + 1 since no two trees have same rank. Amortized cost. O(rank(H))= O(D(H))  (H) = trees(H) + 2  marks(H) potential function

44 Decrease Key

45

46 Intuition for decreasing the key of node x. If heap-order is not violated, just decrease the key of x. Otherwise, cut tree rooted at x and meld into root list. n To keep trees flat: as soon as a node has its second child cut, cut it off and meld into root list (and unmark it) Fibonacci Heaps: Decrease Key 35 min marked node: one child already cut

47 Case 1. [heap order not violated] Decrease key of x. n Change heap min pointer (if necessary) Fibonacci Heaps: Decrease Key min x decrease-key of x from 46 to 29

48 Case 1. [heap order not violated] Decrease key of x. n Change heap min pointer (if necessary) Fibonacci Heaps: Decrease Key 35 min x decrease-key of x from 46 to 29

49 Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key min decrease-key of x from 29 to 15 p x

50 Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key 35 min decrease-key of x from 29 to 15 p x

51 Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key 35 min decrease-key of x from 29 to 15 p x

52 Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key 35 min decrease-key of x from 29 to 15 p x mark parent 24

53 35 Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key 5 min x p decrease-key of x from 35 to 5

54 5 Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key min x p decrease-key of x from 35 to 5

55 Fibonacci Heaps: Decrease Key decrease-key of x from 35 to 5 x p min Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child).

56 Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key decrease-key of x from 35 to 5 x p second child cut min

57 Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key decrease-key of x from 35 to 5 xp min

58 Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key decrease-key of x from 35 to 5 xp p' second child cut min

59 Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key decrease-key of x from 35 to 5 xpp' min don't mark parent if it's a root p''

60 Decrease-key. Actual cost. O(c) n O(1) time for changing the key. O(1) time for each of c cuts, plus melding into root list. Change in potential. O(1) - c trees(H') = trees(H) + c. marks(H')  marks(H) - c + 2. Amortized cost. O(1) Fibonacci Heaps: Decrease Key Analysis  (H) = trees(H) + 2  marks(H) potential function

61

62 Delete

63

64 Delete node x. decrease-key of x to - . delete-min element in heap. Amortized cost. O(rank(H)) O(1) amortized for decrease-key. O(rank(H)) amortized for delete-min. Fibonacci Heaps: Delete  (H) = trees(H) + 2  marks(H) potential function