Algorithms Theory. 08 Fibonacci Heaps

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

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

Mon Tue Wed Thurs Fri

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

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

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

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

L feb abhi shelat

Binary Search Trees. Motivation

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

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Splay Trees. CMSC 420: Lecture 8

CS 161: Design and Analysis of Algorithms

Heaps and Priority Queues

CPSC 320 Sample Final Examination December 2013

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

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

Assignment 5: Solutions

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Binary Decision Diagrams. Graphs. Boolean Functions

Algorithm Design and Analysis

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 =.

CS213d Data Structures and Algorithms

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

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 ]

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

INF2220: algorithms and data structures Series 1

Data Structures 1 NTIN066

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

Priority queues implemented via heaps

Part IA Algorithms Notes

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

Solution suggestions for examination of Logic, Algorithms and Data Structures,

CS Data Structures and Algorithm Analysis

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

Binary Decision Diagrams

Fundamental Algorithms

Design and Analysis of Algorithms

Single Source Shortest Paths

Dynamic Ordered Sets with Exponential Search Trees

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques

Optimal Color Range Reporting in One Dimension

Algorithm Design and Analysis

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

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

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

HW #4. (mostly by) Salim Sarımurat. 1) Insert 6 2) Insert 8 3) Insert 30. 4) Insert S a.

Chapter 6. Self-Adjusting Data Structures

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

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Functional Data Structures

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

Data Structures 1 NTIN066

Fundamental Algorithms

Dictionary: an abstract data type

Shortest Path Algorithms

Greedy Alg: Huffman abhi shelat

9 Union Find. 9 Union Find. List Implementation. 9 Union Find. Union Find Data Structure P: Maintains a partition of disjoint sets over elements.

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

Dictionary: an abstract data type

Weight-balanced Binary Search Trees

Introduction to Algorithms

Introduction to Algorithms

A Simple Implementation Technique for Priority Search Queues

CS781 Lecture 3 January 27, 2011

CS 240 Data Structures and Data Management. Module 4: Dictionaries

Chapter 5 Arrays and Strings 5.1 Arrays as abstract data types 5.2 Contiguous representations of arrays 5.3 Sparse arrays 5.4 Representations of

Weight-balanced Binary Search Trees

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Weak Heaps and Friends: Recent Developments

arxiv: v1 [math.co] 10 Sep 2010

Data Structure. Mohsen Arab. January 13, Yazd University. Mohsen Arab (Yazd University ) Data Structure January 13, / 86

Lecture VI AMORTIZATION

BFS Dijkstra. Oct abhi shelat

Ordered Dictionary & Binary Search Tree

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

Problem. Maintain the above set so as to support certain

Data Structures and Algorithms " Search Trees!!

Introduction to Algorithms

Equivalence Relations. Equivalence Classes. A new question. Building Equivalence Classes. CSE 326: Data Structures Disjoint Union/Find

AVL trees. AVL trees

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

CS 466/666 Course Notes

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

Data Structures and and Algorithm Xiaoqing Zheng

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Union-Find Structures

Equivalence Relation. Equivalence Classes. Building Equivalence Classes. Disjoint Union Find 2016/2/9. Determining equivalence classes

A Tight Lower Bound for Top-Down Skew Heaps

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

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

Reversing Lists Haskell: The craft of functional programming, by Simon Thompson, page 140

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

CS361 Homework #3 Solutions

! Insert. ! Remove largest. ! Copy. ! Create. ! Destroy. ! Test if empty. ! Fraud detection: isolate $$ transactions.

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

Propositional Logic: Models and Proofs

Transcription:

Algorithms Theory 08 Fibonacci Heaps Prof. Dr. S. Albers

Priority queues: operations Priority queue Q Operations: Q.initialize(): initializes an empty queue Q Q.isEmpty(): returns true iff Q is empty Q.insert(e): inserts element e into Q and returns a pointer to the node containing e Q.deletemin(): returns the element of Q with minimum key and deletes it Q.min(): returns the element of Q with minimum key Q.decreasekey(v,k): decreases the value of v s key to the new value k 2

Priority queues: operations Additional operations: Q.delete(v) : deletes node v and its element from Q (without searching for v) Q.meld(Q ): unites Q and Q (concatenable queue) Q.search(k) : searches for the element with key k in Q (searchable queue) And many more, e.g. predecessor, successor, max, deletemax 3

Priority queues: implementations List Heap Bin. Q. Fib.-Hp. insert O(1) O(1) min O(n) O(1) O(1) deletemin O(n) * meld (m n) O(1) O(n) or O(m log n) O(1) decr.-key O(1) O(1)* *= amortized cost Q.delete(e) = Q.decreasekey(e, - ) + Q.deletemin( ) 4

Fibonacci heaps Lazy-meld version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition A Fibonacci heap Q is a collection heap-ordered trees. Variables Q.min: root of the tree containing the minimum key Q.rootlist: circular, doubly linked, unordered list containing the roots of all trees Q.size: number of nodes currently in Q 5

Trees in Fibonacci heaps Let B be a heap-ordered tree in Q.rootlist: B.childlist: circular, doubly linked and unordered list of the children of B Structure of a node parent left key degree right child mark Advantages of circular, doubly linked lists: 1. Deleting an element takes constant time. 2. Concatenating two lists takes constant time. 6

Implementation of Fibonacci heaps: Example 7

Operations on Fibonacci heaps Q.initialize(): Q.rootlist = Q.min = null Q.meld(Q ): 1. concatenate Q.rootlist and Q.rootlist 2. update Q.min Q.insert(e): 1. generate a new node with element e Q 2. Q.meld(Q ) Q.min(): return Q.min.key 8

Fibonacci heaps: deletemin Q.deletemin() /*Delete the node with minimum key from Q and return its element.*/ 1 m = Q.min() 2 if Q.size() > 0 3 then remove Q.min() from Q.rootlist 4 add Q.min.childlist to Q.rootlist 5 Q.consolidate() /* Repeatedly meld nodes in the root list having the same degree. Then determine the element with minimum key. */ 6 return m 9

Fibonacci heaps: maximum degree of a node rank(v) = degree of node v in Q rank(q) = maximum degree of any node in Q Assumption: rank(q) 2 log n, if Q.size = n. 10

Fibonacci heaps: operation link rank(b) = degree of the root of B Heap-ordered trees B,B with rank(b) = rank(b ) link B B B 1. rank(b) = rank(b) + 1 2. B.mark = false B 11

Consolidation of the root list 12

Consolidation of the root list 13

Fibonacci heaps: deletemin Find roots having the same rank: Array A: 0 1 2 log n Q.consolidate() 1 A = array of length 2 log n pointing to Fibonacci heap nodes 2 for i = 0 to 2 log n do A[i] = null 3 while Q.rootlist do 4 B = Q.delete-first() 5 while A[rank(B)] is not null do 6 B = A[rank(B)]; A[rank(B)] = null; B = link(b,b ) 7 end while 8 A[rang(B)] = B 9 end while 10 determine Q.min 14

Fibonacci heap: Example 15

Fibonacci heap: Example 16

Fibonacci heaps: decreasekey Q.decreasekey(v,k) 1 if k > v.key then return 2 v.key = k 3 update Q.min 4 if v Q.rootlist or k v.parent.key then return 5 do /* cascading cuts */ 6 parent = v.parent 7 Q.cut(v) 8 v = parent 9 while v.mark and v Q.rootlist 10 if v Q.rootlist then v.mark = true 17

Fibonacci heaps: cut Q.cut(v) 1 if v Q.rootlist 2 then /* cut the link between v and its parent */ 3 rank (v.parent) = rank (v.parent) 1 4 v.parent = null 5 remove v from v.parent.childlist 6 add v to Q.rootlist 18

Fibonacci heaps: marks History of a node: v is being linked to a node v.mark = false a child of v is cut v.mark = true a second child of v is cut cut v The boolean value mark indicates whether node v has lost a child since the last time v was made the child of another node. 19

Rank of the children of a node Lemma Let v be a node in a Fibonacci-Heap Q. Let u 1,...,u k denote the children of v in the order in which they were linked to v. Then: Proof: rank(u i ) i - 2. At the time when u i was linked to v: # children of v (rank(v)): i - 1 # children of u i (rank(u i )): i - 1 # children u i may have lost: 1 20

Maximum rank of a node Theorem Let v be a node in a Fibonacci heap Q, and let rank(v) = k. Then v is the root of a subtree that has at least F k+2 nodes. The number of descendants of a node grows exponentially in the number of children. Implication: The maximum rank k of any node v in a Fibonacci heap Q with n nodes satisfies: 21

Maximum rank of a node Proof S k = minimum possible size of a subtree whose root has rank k S 0 = 1 S 1 = 2 There is: S k 2 2 + k i= 0 S i for k 2 (1) Fibonacci numbers: F k + 2 k = 1+ Fi = 1+ i= 0 F 0 + F 1 + K+ F k (2) (1) + (2) + induction S k F k+2 22

Analysis of Fibonacci heaps Potential method to analyze Fibonacci heap operations. Potential Φ Q of Fibonacci heap Q: Φ Q = r Q + 2 m Q where r Q = number of nodes in Q.rootlist m Q = number of all marked nodes in Q, that are not in the root list. 23

Amortized analysis Amortized cost a i of the i-th operation: a i = t i + Φ i - Φ i-1 = t i + (r i r i-1 ) + 2(m i m i-1 ) 24

Analysis of insert insert t i = 1 r i r i-1 = 1 m i m i-1 = 0 a i = 1 + 1 + 0 = O(1) 25

Analysis of deletemin deletemin: t i = r i-1 + 2 log n r i r i-1 2 log n - r i-1 m i m i-1 0 a i r i-1 + 2 log n + 2 log n r i-1 + 0 = 26

Analysis of decreasekey decreasekey: t i = c + 2 r i r i-1 = c + 1 m i m i-1 -c + 1 a i c + 2 + c + 1 + 2 (- c +1) = O(1) 27

Priority queues: comparison List Heap Bin. Q. Fib.-Hp. insert O(1) O(1) min O(n) O(1) O(1) deletemin O(n) * meld (m n) O(1) O(n) or O(m log n) O(1) decr.-key O(1) O(1)* * = amortized cost 28