Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

Similar documents
Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

FIBONACCI HEAPS. preliminaries insert extract the minimum decrease key bounding the rank meld and delete. Copyright 2013 Kevin Wayne

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Mon Tue Wed Thurs Fri

Algorithms Theory. 08 Fibonacci Heaps

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

25. Minimum Spanning Trees

25. Minimum Spanning Trees

L feb abhi shelat

2-INF-237 Vybrané partie z dátových štruktúr 2-INF-237 Selected Topics in Data Structures

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

Algorithm Design and Analysis

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

INF2220: algorithms and data structures Series 1

Assignment 5: Solutions

Heaps and Priority Queues

Dictionary: an abstract data type

Linear Time Selection

Single Source Shortest Paths

CS 580: Algorithm Design and Analysis

Priority queues implemented via heaps

Each internal node v with d(v) children stores d 1 keys. k i 1 < key in i-th sub-tree k i, where we use k 0 = and k d =.

Computing Connected Components Given a graph G = (V; E) compute the connected components of G. Connected-Components(G) 1 for each vertex v 2 V [G] 2 d

Data Structures 1 NTIN066

search trees. It is therefore important to obtain as full an understanding of the issue as possible. Additionally, there are practical applications fo

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Algorithm Design and Analysis

Dictionary: an abstract data type

A faster algorithm for the single source shortest path problem with few distinct positive lengths

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

The null-pointers in a binary search tree are replaced by pointers to special null-vertices, that do not carry any object-data

A Tight Lower Bound for Top-Down Skew Heaps

Shortest Path Algorithms

CMPUT 675: Approximation Algorithms Fall 2014

Splay Trees. CMSC 420: Lecture 8

CSE 202: Design and Analysis of Algorithms Lecture 3

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

Design and Analysis of Algorithms

Chapter 6. Self-Adjusting Data Structures

1 Introduction A priority queue is a data structure that maintains a set of elements and supports operations insert, decrease-key, and extract-min. Pr

CS213d Data Structures and Algorithms

Weak Heaps and Friends: Recent Developments

Greedy Alg: Huffman abhi shelat

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

Slides for CIS 675. Huffman Encoding, 1. Huffman Encoding, 2. Huffman Encoding, 3. Encoding 1. DPV Chapter 5, Part 2. Encoding 2

AMORTIZED ANALYSIS. binary counter multipop stack dynamic table. Lecture slides by Kevin Wayne. Last updated on 1/24/17 11:31 AM

Data Structures 1 NTIN066

Greedy Algorithms and Data Compression. Curs 2018

Non-context-Free Languages. CS215, Lecture 5 c

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

CS4311 Design and Analysis of Algorithms. Analysis of Union-By-Rank with Path Compression

Functional Data Structures

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case.

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

Disjoint-Set Forests

Disjoint Sets : ADT. Disjoint-Set : Find-Set

Algorithms and Data Structures in the noisy comparison model and applications

CS 151. Red Black Trees & Structural Induction. Thursday, November 1, 12

Single Source Shortest Paths

Breadth-First Search of Graphs

Binary Search Trees. Lecture 29 Section Robb T. Koether. Hampden-Sydney College. Fri, Apr 8, 2016

Fundamental Algorithms

Search Trees. EECS 2011 Prof. J. Elder Last Updated: 24 March 2015

Binary Search Trees. Motivation

Verified Analysis of Functional Data Structures

CPSC 320 Sample Final Examination December 2013

1 Some loose ends from last time

MA015: Graph Algorithms

6.889 Lecture 4: Single-Source Shortest Paths

Lecture 5: Splay Trees

Lecture 2 September 4, 2014

7.3 AVL-Trees. Definition 15. Lemma 16. AVL-trees are binary search trees that fulfill the following balance condition.

The Complexity of Constructing Evolutionary Trees Using Experiments

ne a priority queue for the nodes of G; ile (! PQ.is empty( )) select u PQ with d(u) minimal and remove it; I Informatik 1 Kurt Mehlhorn

Optimal Color Range Reporting in One Dimension

CS 161: Design and Analysis of Algorithms

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Part IA Algorithms Notes

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case

Efficient Reassembling of Graphs, Part 1: The Linear Case

Ordered Dictionary & Binary Search Tree

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques

CS 466/666 Course Notes

Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao

8: Splay Trees. Move-to-Front Heuristic. Splaying. Splay(12) CSE326 Spring April 16, Search for Pebbles: Move found item to front of list

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Random Dyadic Tilings of the Unit Square

Introduction to Algorithms

Inge Li Gørtz. Thank you to Kevin Wayne for inspiration to slides

Chapter 10 Search Structures

On Adaptive Integer Sorting

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

CS Data Structures and Algorithm Analysis

Transcription:

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

Priority Queues Operation mae-heap insert find- delete- union decrease-ey delete Lined List 1 1 N N 1 1 N Binary 1 log N 1 log N N log N log N Binomial 1 log N log N log N log N log N log N Heaps Fibonacci 1 1 1 log N 1 1 log N Relaxed is-empty 1 1 1 1 1 1 1 1 log N 1 1 log N amortized this time 2

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

Fibonacci Heaps: Structure Fibonacci heap. Set of -heap ordered trees. 1 24 3 26 46 mared 18 52 H 4

Fibonacci Heaps: Implementation Implementation. Represent trees using left-child, right sibling pointers and circular, doubly lined list. can quicly splice off subtrees Roots of trees connected with circular doubly lined list. fast union Pointer to root of tree with element. fast find- 1 24 3 26 46 18 52 H 5

Fibonacci Heaps: Potential Function Key quantities. Degree[x] = degree of node x. Mar[x] = mar of node x (blac or gray). t(h) = # trees. m(h) = # mared nodes. Φ(H) = t(h) + 2m(H) = potential function. t(h) = 5, m(h) = 3 Φ(H) = 11 degree = 3 1 24 3 26 46 18 52 H 6

Fibonacci Heaps: Insert Insert. Create a new singleton tree. Add to left of pointer. Update pointer. Insert 21 21 1 24 3 26 46 18 52 H

Fibonacci Heaps: Insert Insert. Create a new singleton tree. Add to left of pointer. Update pointer. Insert 21 1 24 21 3 26 46 18 52 H 8

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

Fibonacci Heaps: Union Union. Concatenate two Fibonacci heaps. Root lists are circular, doubly lined lists. 24 1 3 21 H H 26 46 18 52 10

Fibonacci Heaps: Union Union. Concatenate two Fibonacci heaps. Root lists are circular, doubly lined lists. Running time. O(1) amortized Actual cost = O(1). Change in potential = 0. Amortized cost = O(1). 24 1 3 21 H H 26 46 18 52 11

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 24 1 3 26 46 18 52 12

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. current 24 1 18 52 26 46 13

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 24 1 18 52 26 46 14

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. current 0 1 2 3 24 1 18 52 26 46 15

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 24 1 18 52 26 46 current 16

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 24 1 18 52 26 46 current Merge 1 and trees. 1

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 24 1 18 52 26 46 Merge and 1 trees. 18

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 24 18 52 26 46 1 Merge and 24 trees. 19

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 18 52 24 1 26 46 20

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 18 52 24 1 26 46 21

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 18 52 24 1 26 46 22

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 18 52 24 1 26 46 Merge and 18 trees.

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 52 18 24 1 26 46 24

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 0 1 2 3 current 52 18 24 1 26 46 25

Fibonacci Heaps: Delete Min Delete. Delete and concatenate its children into root list. Consolidate trees so that no two roots have same degree. 52 18 24 1 26 46 Stop. 26

Fibonacci Heaps: Delete Min Analysis Notation. D(n) = max degree of any node in Fibonacci heap with n nodes. t(h) = # trees in heap H. Φ(H) = t(h) + 2m(H). Actual cost. O(D(n) + t(h)) O(D(n)) wor adding s children into root list and updating. at most D(n) children of node O(D(n) + t(h)) wor consolidating trees. wor 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)) t(h ) D(n) + 1 since no two trees have same degree. Φ(H) D(n) + 1 - t(h). 2

Fibonacci Heaps: Delete Min Analysis Is amortized cost of O(D(n)) good? Yes, if only Insert, Delete-, 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 Yes, if we support Decrease-ey 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! 28

Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 0: -heap property not violated. decrease ey of x to change heap pointer if necessary 18 38 24 1 21 26 46 45 52 88 2 Decrease 46 to 45. 29

Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 1: parent of x is unmared. decrease ey of x to cut off lin between x and its parent mar parent add tree rooted at x to root list, updating heap pointer 18 38 24 1 21 26 45 15 52 88 2 Decrease 45 to 15.

Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 1: parent of x is unmared. decrease ey of x to cut off lin between x and its parent mar parent add tree rooted at x to root list, updating heap pointer 18 38 24 1 21 26 15 52 88 2 Decrease 45 to 15. 31

Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 1: parent of x is unmared. decrease ey of x to cut off lin between x and its parent mar parent add tree rooted at x to root list, updating heap pointer 15 18 38 2 24 1 21 26 52 88 Decrease 45 to 15. 32

Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 2: parent of x is mared. decrease ey of x to cut off lin between x and its parent p[x], and add x to root list cut off lin between p[x] and p[p[x]], add p[x] to root list! If p[p[x]] unmared, then mar it.! If p[p[x]] mared, cut off p[p[x]], unmar, and repeat. 15 18 38 2 24 1 21 26 52 5 88 Decrease to 5. 33

Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 2: parent of x is mared. decrease ey of x to cut off lin between x and its parent p[x], and add x to root list cut off lin between p[x] and p[p[x]], add p[x] to root list! If p[p[x]] unmared, then mar it.! If p[p[x]] mared, cut off p[p[x]], unmar, and repeat. 15 5 18 38 2 24 1 21 parent mared 26 52 88 Decrease to 5. 34

Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 2: parent of x is mared. decrease ey of x to cut off lin between x and its parent p[x], and add x to root list cut off lin between p[x] and p[p[x]], add p[x] to root list! If p[p[x]] unmared, then mar it.! If p[p[x]] mared, cut off p[p[x]], unmar, and repeat. 15 5 26 18 38 2 88 24 1 21 parent mared 52 Decrease to 5.

Fibonacci Heaps: Decrease Key Decrease ey of element x to. Case 2: parent of x is mared. decrease ey of x to cut off lin between x and its parent p[x], and add x to root list cut off lin between p[x] and p[p[x]], add p[x] to root list! If p[p[x]] unmared, then mar it.! If p[p[x]] mared, cut off p[p[x]], unmar, and repeat. 15 5 26 24 18 38 2 88 1 21 52 Decrease to 5. 36

Fibonacci Heaps: Decrease Key Analysis Notation. t(h) = # trees in heap H. m(h) = # mared nodes in heap H. Φ(H) = t(h) + 2m(H). Actual cost. O(c) O(1) time for decrease ey. O(1) time for each of c cascading cuts, plus reinserting in root list. Amortized cost. O(1) t(h ) = t(h) + c m(h ) m(h) - c + 2 each cascading cut unmars a node last cascading cut could potentially mar a node Φ c + 2(-c + 2) = 4 - c. 3

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

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- tae O(log N) amortized time. Lemma. Let x be a node with degree, and let y 1,..., y denote the children of x in the order in which they were lined to x. Then: Proof. When y i is lined to x, y 1,..., y i-1 already lined to x, degree(x) = i - 1 degree(y i ) = i - 1 since we only lin nodes of equal degree Since then, y i degree has lost at most one child otherwise it would have been cut from x Thus, degree(y i ) = i - 1 or i - 2 ( y i ) 0 i 2 if if i = 1 i 1

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 ey lemma. For any node x, we show that size(x) φ degree(x). size(x) = # node in subtree rooted at x taing base φ logs, degree(x) log φ (size(x)) log φ N. Let s be size of tree rooted at any degree node. trivial to see that s 0 = 1, s 1 = 2 s monotonically increases with Let x* be a degree node of size s, and let y 1,..., y be children in order that they were lined to x*. Assume 2 s = = = size ( x*) 2 + 2 + 2 + 2 + size( y i= 2 i= 2 s i= 2 2 s i= 0 s deg[ y i 2 i i i ] ) 40

Fibonacci Facts Definition. The Fibonacci sequence is: 1, 2, 3, 5, 8, 13, 21,... Slightly nonstandard definition. F = 1 2 F -1 + F -2 if if if = 0 = 1 2 Fact F1. F φ, where φ = (1 + 5) / 2 = 1.618 Fact F2. For 2, 2 F = 2 + F i i= 0 Consequence. s F φ. This implies that size(x) φ degree(x) for all nodes x. s = = = size ( x*) 2 + 2 + 2 + 2 + size( y i= 2 i= 2 s i= 2 2 s i= 0 s deg[ y i 2 i i i ] )

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

Fibonacci Facts 43

Fibonacci Numbers and Nature Pinecone Cauliflower

45 Fibonacci Proofs Fact F1. F φ. Proof. (by induction on ) Base cases: F 0 = 1, F 1 = 2 φ. Inductive hypotheses: F φ and F +1 φ +1 Fact F2. Proof. (by induction on ) Base cases: F 2 = 3, F 3 = 5 Inductive hypotheses: + = = 2 0 2 2, For i F F i 2 2 1 1 2 ) ( ) (1 + + + + = = + = + + = F F F ϕ ϕ ϕ ϕ ϕ ϕ ϕ φ 2 = φ + 1 + = = 2 0 2 i F F i + = + + = + = = = + + + i i i F F F F F F 0 2 0 1 1 2 2 2

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 tas is only to run the programs." R. E. Tarjan 46