Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

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

COL 106 Shweta Agrawal and Amit Kumar
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
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.
Fibonacci Heaps CS 252: Algorithms Geetika Tewari 252a-al Smith College December, 2000.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
Rank-Pairing Heaps Bernhard Haeupler, Siddhartha Sen, and Robert Tarjan, ESA
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
CSE 373 Data Structures Lecture 12
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.
5.9 Heaps of optimal complexity
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
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.
Strict Fibonacci Heaps Gerth Stølting Brodal Aarhus University George Lagogiannis Robert Endre Tarjan Agricultural University of Athens Princeton University.
Chapter 9 Heap Structures
Binomial heaps, Fibonacci heaps, and applications
Lecture X Fibonacci Heaps
CS 473Lecture X1 CS473-Algorithms Lecture BINOMIAL HEAPS.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Chapter 19: Fibonacci Heap Many of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore.
Change Keys in heaps Fibonacci heap Zhao Xiaobin.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
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.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
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.
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
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.
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))
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.
Binomial heaps, Fibonacci heaps, and applications
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.
Fibonacci Heap.
ערמות בינומיות ופיבונצ'י
CS 583 Analysis of Algorithms
Fundamental Structures of Computer Science
§3 Worst-Case vs. Amortized
Strict Fibonacci Heaps
CS 583 Analysis of Algorithms
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures.
21장. Fibonacci Heaps 숭실대학교 인공지능 연구실 석사 2학기 김완섭.
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.
Presentation transcript:

Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

2 Priority Queues make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary log N 1 N 1 Binomial log N 1 Fibonacci † 1 1 log N Relaxed 1 1 log N Linked List 1 N N 1 1 N is-empty11111 Heaps this time † amortized

3 Fibonacci Heaps Fibonacci heap history. Fredman and Tarjan (1986) n Ingenious data structure and analysis. n Original motivation: O(m + n log n) shortest path algorithm. – also led to faster algorithms for MST, weighted bipartite matching n Still ahead of its time. Fibonacci heap intuition. n Similar to binomial heaps, but less structured. n Decrease-key and union run in O(1) time. n "Lazy" unions.

4 Fibonacci Heaps: Structure Fibonacci heap. n Set of min-heap ordered trees H min marked

5 Fibonacci Heaps: Implementation Implementation. n Represent trees using left-child, right sibling pointers and circular, doubly linked list. – can quickly splice off subtrees n Roots of trees connected with circular doubly linked list. – fast union n Pointer to root of tree with min element. – fast find-min H min

6 Fibonacci Heaps: Potential Function Key quantities. n Degree[x] = degree of node x. n Mark[x] = mark of node x (black or gray). n t(H)= # trees. n m(H)= # marked nodes. n  (H)= t(H) + 2m(H) = potential function H t(H) = 5, m(H) = 3  (H) = mindegree = 3

7 Fibonacci Heaps: Insert Insert. n Create a new singleton tree. n Add to left of min pointer. n Update min pointer H min 21 Insert 21

8 Fibonacci Heaps: Insert Insert. n Create a new singleton tree. n Add to left of min pointer. n Update min pointer min H 21 Insert 21

9 Fibonacci Heaps: Insert Insert. n Create a new singleton tree. n Add to left of min pointer. n Update min pointer. Running time. O(1) amortized n Actual cost = O(1). n Change in potential = +1. n Amortized cost = O(1) min H Insert 21

10 Fibonacci Heaps: Union Union. n Concatenate two Fibonacci heaps. n Root lists are circular, doubly linked lists min H'H'' 21 min

11 Fibonacci Heaps: Union Union. n Concatenate two Fibonacci heaps. n Root lists are circular, doubly linked lists. Running time. O(1) amortized n Actual cost = O(1). n Change in potential = 0. n Amortized cost = O(1) min H'H'' 21

12 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree min

13 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current min

14 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current 0123 min

15 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current 0123 min

16 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current 0123 min

17 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current 0123 Merge 17 and 23 trees. min

18 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current 0123 Merge 7 and 17 trees. min

19 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current Merge 7 and 24 trees. min

20 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current 0123 min

21 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current 0123 min

22 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current 0123 min

23 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current 0123 Merge 41 and 18 trees. min

24 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current 0123 min

25 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree current 0123 min

26 Fibonacci Heaps: Delete Min Delete min. n Delete min and concatenate its children into root list. n Consolidate trees so that no two roots have same degree min Stop.

27 Fibonacci Heaps: Delete Min Analysis Notation. n D(n)= max degree of any node in Fibonacci heap with n nodes. n t(H)= # trees in heap H. n  (H)= t(H) + 2m(H). Actual cost. O(D(n) + t(H)) n O(D(n)) work adding min's children into root list and updating min. – at most D(n) children of min node n O(D(n) + t(H)) work consolidating trees. – work is proportional to size of root list since number of roots decreases by one after each merging –  D(n) + t(H) - 1 root nodes at beginning of consolidation Amortized cost. O(D(n)) n t(H')  D(n) + 1 since no two trees have same degree. n  (H)  D(n) t(H).

28 Fibonacci Heaps: Delete Min Analysis Is amortized cost of O(D(n)) good? n Yes, if only Insert, Delete-min, and Union operations supported. – in this case, Fibonacci heap contains only binomial trees since we only merge trees of equal root degree – this implies D(n)   log 2 N  n Yes, if we support Decrease-key in clever way. – we'll show that D(n)   log  N , where  is golden ratio –  2 = 1 +  –  = (1 +  5) / 2 = 1.618… – limiting ratio between successive Fibonacci numbers!

29 Decrease key of element x to k. n Case 0: min-heap property not violated. – decrease key of x to k – change heap min pointer if necessary Decrease 46 to Fibonacci Heaps: Decrease Key min

30 Decrease key of element x to k. n Case 1: parent of x is unmarked. – decrease key of x to k – cut off link between x and its parent – mark parent – add tree rooted at x to root list, updating heap min pointer Decrease 45 to Fibonacci Heaps: Decrease Key min

31 Decrease key of element x to k. n Case 1: parent of x is unmarked. – decrease key of x to k – cut off link between x and its parent – mark parent – add tree rooted at x to root list, updating heap min pointer Decrease 45 to Fibonacci Heaps: Decrease Key 35 min

32 Decrease key of element x to k. n Case 1: parent of x is unmarked. – decrease key of x to k – cut off link between x and its parent – mark parent – add tree rooted at x to root list, updating heap min pointer Decrease 45 to Fibonacci Heaps: Decrease Key 35 min 15 72

33 35 Decrease key of element x to k. n Case 2: parent of x is marked. – decrease key of x to k – cut off link between x and its parent p[x], and add x to root list – cut off link between p[x] and p[p[x]], add p[x] to root list  If p[p[x]] unmarked, then mark it.  If p[p[x]] marked, cut off p[p[x]], unmark, and repeat Decrease 35 to Fibonacci Heaps: Decrease Key 5 min

34 Decrease key of element x to k. n Case 2: parent of x is marked. – decrease key of x to k – cut off link between x and its parent p[x], and add x to root list – cut off link between p[x] and p[p[x]], add p[x] to root list  If p[p[x]] unmarked, then mark it.  If p[p[x]] marked, cut off p[p[x]], unmark, and repeat Decrease 35 to Fibonacci Heaps: Decrease Key 88 parent marked min

35 Decrease key of element x to k. n Case 2: parent of x is marked. – decrease key of x to k – cut off link between x and its parent p[x], and add x to root list – cut off link between p[x] and p[p[x]], add p[x] to root list  If p[p[x]] unmarked, then mark it.  If p[p[x]] marked, cut off p[p[x]], unmark, and repeat Decrease 35 to Fibonacci Heaps: Decrease Key parent marked min

36 Decrease key of element x to k. n Case 2: parent of x is marked. – decrease key of x to k – cut off link between x and its parent p[x], and add x to root list – cut off link between p[x] and p[p[x]], add p[x] to root list  If p[p[x]] unmarked, then mark it.  If p[p[x]] marked, cut off p[p[x]], unmark, and repeat Decrease 35 to Fibonacci Heaps: Decrease Key min

37 Notation. n t(H) = # trees in heap H. n m(H) = # marked nodes in heap H. n  (H) = t(H) + 2m(H). Actual cost. O(c) n O(1) time for decrease key. n O(1) time for each of c cascading cuts, plus reinserting in root list. Amortized cost. O(1) n t(H') = t(H) + c n m(H')  m(H) - c + 2 – each cascading cut unmarks a node – last cascading cut could potentially mark a node n   c + 2(-c + 2) = 4 - c. Fibonacci Heaps: Decrease Key Analysis

38 Delete node x. n Decrease key of x to - . n Delete min element in heap. Amortized cost. O(D(n)) n O(1) for decrease-key. n O(D(n)) for delete-min. n D(n) = max degree of any node in Fibonacci heap. Fibonacci Heaps: Delete

39 Fibonacci Heaps: Bounding Max Degree Definition. D(N) = max degree in Fibonacci heap with N nodes. Key lemma. D(N)  log  N, where  = (1 +  5) / 2. Corollary. Delete and Delete-min take O(log N) amortized time. Lemma. Let x be a node with degree k, and let y 1,..., y k denote the children of x in the order in which they were linked to x. Then: Proof. n When y i is linked to x, y 1,..., y i-1 already linked to x,  degree(x) = i - 1  degree(y i ) = i - 1 since we only link nodes of equal degree n Since then, y i has lost at most one child – otherwise it would have been cut from x n Thus, degree(y i ) = i - 1 or i - 2

40 Fibonacci Heaps: Bounding Max Degree Key lemma. In a Fibonacci heap with N nodes, the maximum degree of any node is at most log  N, where  = (1 +  5) / 2. Proof of key lemma. n For any node x, we show that size(x)   degree(x). – size(x) = # node in subtree rooted at x – taking base  logs, degree(x)  log  (size(x))  log  N. n Let s k be min size of tree rooted at any degree k node. – trivial to see that s 0 = 1, s 1 = 2 – s k monotonically increases with k n Let x* be a degree k node of size s k, and let y 1,..., y k be children in order that they were linked to x*. Assume k  2

41 Fibonacci Facts Definition. The Fibonacci sequence is: n 1, 2, 3, 5, 8, 13, 21,... Slightly nonstandard definition. Fact F1. F k   k, where  = (1 +  5) / 2 = 1.618… Fact F2. Consequence. s k  F k   k. n This implies that size(x)   degree(x) for all nodes x.

42 Golden Ratio Definition. The Fibonacci sequence is: 1, 2, 3, 5, 8, 13, 21,... Definition. The golden ratio  = (1 +  5) / 2 = 1.618… n Divide a rectangle into a square and smaller rectangle such that the smaller rectangle has the same ratio as original one. Parthenon, Athens Greece

43 Fibonacci Facts

44 Fibonacci Numbers and Nature Pinecone Cauliflower

45 Fibonacci Proofs Fact F1. F k   k. Proof. (by induction on k) n Base cases: – F 0 = 1, F 1 = 2  . n Inductive hypotheses: – F k   k and F k+1   k+1 Fact F2. Proof. (by induction on k) n Base cases: – F 2 = 3, F 3 = 5 n Inductive hypotheses:  2 =  + 1

46 On Complicated Algorithms "Once you succeed in writing the programs for [these] complicated algorithms, they usually run extremely fast. The computer doesn't need to understand the algorithm, its task is only to run the programs." R. E. Tarjan