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

Similar documents
Algorithms Theory. 08 Fibonacci Heaps

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

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

Mon Tue Wed Thurs Fri

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

Priority queues implemented via heaps

25. Minimum Spanning Trees

25. Minimum Spanning Trees

INF2220: algorithms and data structures Series 1

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

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Heaps and Priority Queues

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

Assignment 5: Solutions

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

L feb abhi shelat

Lecture 5: Splay Trees

Dictionary: an abstract data type

Data Structures 1 NTIN066

Dictionary: an abstract data type

Binary Search Trees. Motivation

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

CS213d Data Structures and Algorithms

Data Structures 1 NTIN066

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

Data Structures and Algorithms " Search Trees!!

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

Part IA Algorithms Notes

Splay Trees. CMSC 420: Lecture 8

Weak Heaps and Friends: Recent Developments

CPSC 320 Sample Final Examination December 2013

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

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

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

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

Algorithm Theory - Exercise Class

Single Source Shortest Paths

Algorithm Design and Analysis

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

Dynamic Ordered Sets with Exponential 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

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

Shortest Path Algorithms

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

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

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Chapter 6. Self-Adjusting Data Structures

Problem. Maintain the above set so as to support certain

Divide-and-Conquer Algorithms Part Two

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

Optimal Color Range Reporting in One Dimension

Functional Data Structures

CS Data Structures and Algorithm Analysis

The Complexity of Constructing Evolutionary Trees Using Experiments

Single Source Shortest Paths

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

On Adaptive Integer Sorting

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

A Simple Implementation Technique for Priority Search Queues

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Notes on Logarithmic Lower Bounds in the Cell Probe Model

Graphs and Trees Binary Search Trees AVL-Trees (a,b)-trees Splay-Trees. Search Trees. Tobias Lieber. April 14, 2008

Design and Analysis of Algorithms

COMP 250. Lecture 24. heaps 2. Nov. 3, 2017

Amortized Complexity Main Idea

CS 466/666 Course Notes

Advanced Implementations of Tables: Balanced Search Trees and Hashing

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

Fundamental Algorithms

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

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

Amortized analysis. Amortized analysis

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Lower Bounds for Dynamic Connectivity (2004; Pǎtraşcu, Demaine)

Tutorial 4. Dynamic Set: Amortized Analysis

Outline for Today. Static Optimality. Splay Trees. Properties of Splay Trees. Dynamic Optimality (ITA) Balanced BSTs aren't necessarily optimal!

Advanced Algorithms (2IL45)

Algorithm efficiency analysis

Lecture VI AMORTIZATION

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

searching algorithms

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

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2013

Biased Quantiles. Flip Korn Graham Cormode S. Muthukrishnan

2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

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

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

Amortized Analysis (chap. 17)

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

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

Scanning and Traversing: Maintaining Data for Traversals in a Memory Hierarchy

CSE 202: Design and Analysis of Algorithms Lecture 3

Data Structures and and Algorithm Xiaoqing Zheng

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Transcription:

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

Priority Queue / Heap Stores (key,data) pairs (like dictionary) But, different set of operations: Initialize-Heap: creates new empty heap Is-Empty: returns true if heap is empty Insert(key,data): inserts (key,data)-pair, returns pointer to entry Get-Min: returns (key,data)-pair with minimum key Delete-Min: deletes minimum (key,data)-pair Decrease-Key(entry,newkey): decreases key of entry to newkey Merge: merges two heaps into one Algorithm Theory, WS 2017/18 Fabian Kuhn 2

Priority Queue Implementation Implementation as min-heap: complete binary tree, e.g., stored in an array Initialize-Heap: Is-Empty: Insert: Get-Min: Delete-Min: Decrease-Key: O(1) O(1) O log n O 1 O log n O log n Merge (heaps of size m and n, m n): O(m log n) Algorithm Theory, WS 2017/18 Fabian Kuhn 3

Can We Do Better? Cost of Dijkstra with complete binary min-heap implementation: O E log V Binary heap: insert, delete-min, and decrease-key cost O(log n) merging two heaps is expensive One of the operations insert or delete-min must cost Ω(log n): Heap-Sort: Insert n elements into heap, then take out the minimum n times (Comparison-based) sorting costs at least Ω(n log n). But maybe we can improve merge, decrease-key, and one of the other two operations? Algorithm Theory, WS 2017/18 Fabian Kuhn 4

Fibonacci Heaps Structure: A Fibonacci heap H consists of a collection of trees satisfying the min-heap property. Min-Heap Property: Key of a node v keys of all nodes in any sub-tree of v Algorithm Theory, WS 2017/18 Fabian Kuhn 5

Fibonacci Heaps Structure: A Fibonacci heap H consists of a collection of trees satisfying the min-heap property. Variables: H. min: root of the tree containing the (a) minimum key H. rootlist: circular, doubly linked, unordered list containing the roots of all trees H. size: number of nodes currently in H Lazy Merging: To reduce the number of trees, sometimes, trees need to be merged Lazy merging: Do not merge as long as possible... Algorithm Theory, WS 2017/18 Fabian Kuhn 6

Trees in Fibonacci Heaps Structure of a single node v: parent left key degree right child mark v. child: points to circular, doubly linked and unordered list of the children of v v. left, v. right: pointers to siblings (in doubly linked list) v. mark: will be used later Advantages of circular, doubly linked lists: Deleting an element takes constant time Concatenating two lists takes constant time Algorithm Theory, WS 2017/18 Fabian Kuhn 7

Example Figure: Cormen et al., Introduction to Algorithms Algorithm Theory, WS 2017/18 Fabian Kuhn 8

Simple (Lazy) Operations Initialize-Heap H: H. rootlist H. min null Merge heaps H and H : concatenate root lists update H. min Insert element e into H: create new one-node tree containing e H mark of root node is set to false merge heaps H and H Get minimum element of H: return H. min Algorithm Theory, WS 2017/18 Fabian Kuhn 9

Operation Delete-Min Delete the node with minimum key from H and return its element: 1. m H. min; 2. if H. size > 0 then 3. remove H. min from H. rootlist; 4. add H. min. child (list) to H. rootlist 5. H. Consolidate(); // Repeatedly merge nodes with equal degree in the root list // until degrees of nodes in the root list are distinct. // Determine the element with minimum key 6. return m Algorithm Theory, WS 2017/18 Fabian Kuhn 10

Rank and Maximum Degree Ranks of nodes, trees, heap: Node v: rank(v): degree of v (number of children of v) Tree T: rank T : rank (degree) of root node of T Heap H: rank(h): maximum degree (#children) of any node in H Assumption (n: number of nodes in H): for a known function D(n) rank H D(n) Algorithm Theory, WS 2017/18 Fabian Kuhn 11

Merging Two Trees Given: Heap-ordered trees T, T with rank T = rank(t ) Assume: min-key of T < min-key of T Operation link(t, T ): Removes tree T from root list and adds T to child list of T T link T rank T rank T + 1 (T. mark = false) T T Algorithm Theory, WS 2017/18 Fabian Kuhn 12

Consolidation of Root List Array A pointing to find roots with the same rank: Consolidate: 1. for i 0 to D(n) do A i null; 2. while H. rootlist null do 3. T delete and return first element of H. rootlist 4. while A rank T null do 5. T A rank T ; 6. A rank T null; 7. T link(t, T ) 8. A rank T T 0 1 2 D(n) 9. Create new H. rootlist and H. min Time: O( H. rootlist +D n Algorithm Theory, WS 2017/18 Fabian Kuhn 13

Consolidate Example link 31 5 25 1 2 15 17 3 9 14 20 13 8 19 7 12 18 22 5 25 1 2 15 17 3 9 14 20 31 13 8 19 7 12 18 22 Algorithm Theory, WS 2017/18 Fabian Kuhn 14

Consolidate Example link 5 25 1 2 15 17 3 9 14 20 31 13 8 19 7 12 18 22 5 1 2 15 17 3 9 14 20 25 13 8 19 7 12 18 22 31 Algorithm Theory, WS 2017/18 Fabian Kuhn 15

Consolidate Example link 5 1 2 15 17 3 9 14 20 25 13 8 19 7 12 18 22 31 5 1 2 15 17 9 14 20 25 13 3 8 12 18 22 31 19 7 Algorithm Theory, WS 2017/18 Fabian Kuhn 16

Consolidate Example link 5 1 2 15 17 9 14 20 25 13 3 8 12 18 22 31 19 7 1 2 15 17 5 25 13 3 8 9 14 20 31 19 7 12 18 22 Algorithm Theory, WS 2017/18 Fabian Kuhn 17

Consolidate Example link 1 2 15 17 5 25 13 3 8 9 14 20 31 19 7 12 18 22 1 2 15 5 25 13 3 8 17 9 14 20 31 19 7 12 18 22 Algorithm Theory, WS 2017/18 Fabian Kuhn 18

Consolidate Example link 1 2 15 5 25 13 3 8 17 9 14 20 31 19 7 12 18 22 1 2 5 25 13 3 8 15 9 14 20 31 19 7 17 12 18 22 Algorithm Theory, WS 2017/18 Fabian Kuhn 19

Operation Decrease-Key Decrease-Key(v, x): (decrease key of node v to new value x) 1. if x v. key then return; 2. v. key x; update H. min; 3. if v H. rootlist x v. parent. key then return 4. repeat 5. parent v. parent; 6. H. cut v ; 7. v parent; 8. until v. mark v H. rootlist; 9. if v H. rootlist then v. mark true; Algorithm Theory, WS 2017/18 Fabian Kuhn 20

Operation Cut(v) Operation H. cut(v): Cuts v s sub-tree from its parent and adds v to rootlist 1. if v H. rootlist then 2. // cut the link between v and its parent 3. rank v. parent rank v. parent 1; 4. remove v from v. parent. child (list) 5. v. parent null; 6. add v to H. rootlist; v.mark false; 1 25 13 3 v 2 15 8 cut(v) 1 3 25 13 19 v 7 2 8 15 31 19 7 31 Algorithm Theory, WS 2017/18 Fabian Kuhn 21

Decrease-Key Example Green nodes are marked 5 1 2 15 17 9 14 20 25 13 3 8 12 18 v 31 19 7 22 Decrease-Key(v, 8) 5 8 9 1 2 15 17 14 20 22 12 25 13 3 8 31 19 7 Algorithm Theory, WS 2017/18 Fabian Kuhn 22

Fibonacci Heaps Marks Nodes in the root list (the tree roots) are always unmarked If a node is added to the root list (insert, decrease-key), the mark of the node is set to false. Nodes not in the root list can only get marked when a subtree is cut in a decrease-key operation A node v is marked if and only if v is not in the root list and v has lost a child since v was attached to its current parent a node can only change its parent by being moved to the root list Algorithm Theory, WS 2017/18 Fabian Kuhn 23

Fibonacci Heap Marks History of a node v: 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 H. cut v ; v. mark false Hence, the boolean value v. mark indicates whether node v has lost a child since the last time v was made the child of another node. Nodes v in the root list always have v. mark = false Algorithm Theory, WS 2017/18 Fabian Kuhn 24

Cost of Delete-Min & Decrease-Key Delete-Min: 1. Delete min. root r and add r. child to H. rootlist time: O 1 2. Consolidate H. rootlist time: O length of H. rootlist + D(n) Step 2 can potentially be linear in n (size of H) Decrease-Key (at node v): 1. If new key < parent key, cut sub-tree of node v time: O(1) 2. Cascading cuts up the tree as long as nodes are marked time: O(number of consecutive marked nodes) Step 2 can potentially be linear in n Exercises: Both operations can take Θ(n) time in the worst case! Algorithm Theory, WS 2017/18 Fabian Kuhn 25

Cost of Delete-Min & Decrease-Key Cost of delete-min and decrease-key can be Θ(n) Seems a large price to pay to get insert and merge in O(1) time Maybe, the operations are efficient most of the time? It seems to require a lot of operations to get a long rootlist and thus, an expensive consolidate operation In each decrease-key operation, at most one node gets marked: We need a lot of decrease-key operations to get an expensive decrease-key operation Can we show that the average cost per operation is small? We can requires amortized analysis Algorithm Theory, WS 2017/18 Fabian Kuhn 26

Fibonacci Heaps Complexity Worst-case cost of a single delete-min or decrease-key operation is Ω n Can we prove a small worst-case amortized cost for delete-min and decrease-key operations? Recall: Data structure that allows operations O 1,, O k We say that operation O p has amortized cost a p if for every execution the total time is k T p=1 n p a p, where n p is the number of operations of type O p Algorithm Theory, WS 2017/18 Fabian Kuhn 27

Amortized Cost of Fibonacci Heaps Initialize-heap, is-empty, get-min, insert, and merge have worst-case cost O(1) Delete-min has amortized cost O(log n) Decrease-key has amortized cost O(1) Starting with an empty heap, any sequence of n operations with at most n d delete-min operations has total cost (time) T = O n + n d log n. We will now need the marks Cost for Dijkstra: O E + V log V Algorithm Theory, WS 2017/18 Fabian Kuhn 28

Fibonacci Heaps: Marks Cycle of a node: 1. Node v is removed from root list and linked to a node v. mark = false 2. Child node u of v is cut and added to root list v. mark true 3. Second child of v is cut node v is cut as well and moved to root list v. mark false The boolean value v. mark indicates whether node v has lost a child since the last time v was made the child of another node. Algorithm Theory, WS 2017/18 Fabian Kuhn 29

Potential Function System state characterized by two parameters: R: number of trees (length of H. rootlist) M: number of marked nodes (not in the root list) Potential function: Φ R + 2M Example: 5 18 9 1 2 15 17 14 20 22 12 25 13 3 8 31 19 7 R = 7, M = 2 Φ = 11 Algorithm Theory, WS 2017/18 Fabian Kuhn 30

Actual Time of Operations Operations: initialize-heap, is-empty, insert, get-min, merge actual time: O(1) Normalize unit time such that t init, t is empty, t insert, t get min, t merge 1 Operation delete-min: Actual time: O length of H. rootlist + D n Normalize unit time such that t del min D n + length of H. rootlist Operation descrease-key: Actual time: O length ofpath to next unmarked ancestor Normalize unit time such that t decr key length of path to next unmarked ancestor Algorithm Theory, WS 2017/18 Fabian Kuhn 31

Amortized Times Assume operation i is of type: initialize-heap: actual time: t i 1, potential: Φ i 1 = Φ i = 0 amortized time: a i = t i + Φ i Φ i 1 1 is-empty, get-min: actual time: t i 1, potential: Φ i = Φ i 1 (heap doesn t change) amortized time: a i = t i + Φ i Φ i 1 1 merge: Actual time: t i 1 combined potential of both heaps: Φ i = Φ i 1 amortized time: a i = t i + Φ i Φ i 1 1 Algorithm Theory, WS 2017/18 Fabian Kuhn 32

Amortized Time of Insert Assume that operation i is an insert operation: Actual time: t i 1 Potential function: M remains unchanged (no nodes are marked or unmarked, no marked nodes are moved to the root list) R grows by 1 (one element is added to the root list) Amortized time: M i = M i 1, R i = R i 1 + 1 Φ i = Φ i 1 + 1 a i = t i + Φ i Φ i 1 2 Algorithm Theory, WS 2017/18 Fabian Kuhn 33

Amortized Time of Delete-Min Assume that operation i is a delete-min operation: Actual time: t i D n + H. rootlist Potential function Φ = R + 2M: R: changes from H. rootlist to at most D n + 1 M: (# of marked nodes that are not in the root list) Number of marks does not increase M i = M i 1, R i R i 1 + D n + 1 H. rootlist Φ i Φ i 1 + D n + 1 H. rootlist Amortized Time: a i = t i + Φ i Φ i 1 2D n + 1 Algorithm Theory, WS 2017/18 Fabian Kuhn 34

Amortized Time of Decrease-Key Assume that operation i is a decrease-key operation at node u: Actual time: t i length of path to next unmarked ancestor v Potential function Φ = R + 2M: Assume, node u and nodes u 1,, u k are moved to root list u 1,, u k are marked and moved to root list, v. mark is set to true Algorithm Theory, WS 2017/18 Fabian Kuhn 35

Amortized Time of Decrease-Key Assume that operation i is a decrease-key operation at node u: Actual time: t i length of path to next unmarked ancestor v Potential function Φ = R + 2M: Assume, node u and nodes u 1,, u k are moved to root list u 1,, u k are marked and moved to root list, v. mark is set to true k marked nodes go to root list, 1 node gets newly marked R grows by k + 1, M grows by 1 and is decreased by k R i R i 1 + k + 1, M i M i 1 + 1 k Φ i Φ i 1 + k + 1 2 k 1 = Φ i 1 + 3 k Amortized time: a i = t i + Φ i Φ i 1 k + 1 + 3 k = 4 Algorithm Theory, WS 2017/18 Fabian Kuhn 36

Complexities Fibonacci Heap Initialize-Heap: Is-Empty: O(1) O(1) Insert: O 1 Get-Min: O 1 Delete-Min: O D(n) Decrease-Key: O 1 amortized Merge (heaps of size m and n, m n): O(1) How large can D(n) get? Algorithm Theory, WS 2017/18 Fabian Kuhn 37

Rank of Children Lemma: Consider a node v of rank k and let u 1,, u k be the children of v in the order in which they were linked to v. Then, Proof: rank u i i 2. Algorithm Theory, WS 2017/18 Fabian Kuhn 38

Size of Trees Fibonacci Numbers: F 0 = 0, F 1 = 1, k 2: F k = F k 1 + F k 2 Lemma: In a Fibonacci heap, the size of the sub-tree of a node v with rank k is at least F k+2. Proof: S k : minimum size of the sub-tree of a node of rank k Algorithm Theory, WS 2017/18 Fabian Kuhn 39

Size of Trees S 0 = 1, S 1 = 2, k 2: S k 2 + Claim about Fibonacci numbers: k 0: F k+2 = 1 + k 2 i=0 k i=0 F i S i Algorithm Theory, WS 2017/18 Fabian Kuhn 40

Size of Trees k 2 k S 0 = 1, S 1 = 2, k 2: S k 2 + S i, F k+2 = 1 + F i i=0 i=0 Claim of lemma: S k F k+2 Algorithm Theory, WS 2017/18 Fabian Kuhn 41

Size of Trees Lemma: In a Fibonacci heap, the size of the sub-tree of a node v with rank k is at least F k+2. Theorem: The maximum rank of a node in a Fibonacci heap of size n is at most Proof: D n = O(log n). The Fibonacci numbers grow exponentially: F k = 1 5 1 + 5 2 For D n k, we need n F k+2 nodes. k 1 5 2 k Algorithm Theory, WS 2017/18 Fabian Kuhn 42

Summary: Binomial and Fibonacci Heaps Binary Heap Fibonacci Heap initialize O(1) O(1) insert O(log n) O(1) get-min O(1) O(1) delete-min O(log n) O(log n) * decrease-key O log n O(1) * merge O(m log n) O(1) is-empty O(1) O(1) amortized time Algorithm Theory, WS 2017/18 Fabian Kuhn 43