Data Structures Fibonacci Heaps, Amortized Analysis



Similar documents
Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Full and Complete Binary Trees

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

CS711008Z Algorithm Design and Analysis

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Cpt S 223. School of EECS, WSU

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Converting a Number from Decimal to Binary

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

From Last Time: Remove (Delete) Operation

Ordered Lists and Binary Trees

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

PES Institute of Technology-BSC QUESTION BANK

Algorithms and Data Structures

Data Structures and Algorithms

Symbol Tables. Introduction

Data Structure [Question Bank]

Analysis of Algorithms I: Binary Search Trees

10CS35: Data Structures Using C

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Unordered Linked Lists

6 March Array Implementation of Binary Trees

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Section IV.1: Recursive Algorithms and Recursion Trees

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Binary Search Trees CMPSC 122

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Data Structures and Algorithms Written Examination

Analysis of a Search Algorithm

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Chapter 13: Query Processing. Basic Steps in Query Processing

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

DATA STRUCTURES USING C

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Sequential Data Structures

The Goldberg Rao Algorithm for the Maximum Flow Problem

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Guessing Game: NP-Complete?

K-Cover of Binary sequence

TREE BASIC TERMINOLOGIES

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

GRAPH THEORY LECTURE 4: TREES

Persistent Binary Search Trees

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

14.1 Rent-or-buy problem

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Row Echelon Form and Reduced Row Echelon Form

International Journal of Advanced Research in Computer Science and Software Engineering

Lecture Notes on Binary Search Trees

6.3 Conditional Probability and Independence

DATABASE DESIGN - 1DL400

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Scheduling Shop Scheduling. Tim Nieberg

Data Structures and Data Manipulation

Algorithms Chapter 12 Binary Search Trees

Complexity of Union-Split-Find Problems. Katherine Jane Lai

Efficient Data Structures for Decision Diagrams

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Sample Questions Csci 1112 A. Bellaachia

Randomized Binary Search Trees

Binary Search Trees (BST)

Binary Heap Algorithms

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Strategic Deployment in Graphs. 1 Introduction. v 1 = v s. v 2. v 4. e 1. e e 3. e 2. e 4

Solutions for Practice problems on proofs

Fundamental Algorithms

Data storage Tree indexes

M(0) = 1 M(1) = 2 M(h) = M(h 1) + M(h 2) + 1 (h > 1)

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Solutions to Math 51 First Exam January 29, 2015

Laboratory Module 6 Red-Black Trees

Lecture Notes on Binary Search Trees

Optimal Binary Search Trees Meet Object Oriented Programming

Chapter 8: Structures for Files. Truong Quynh Chi Spring- 2013

How To Create A Tree From A Tree In Runtime (For A Tree)

Introduction to Data Structures and Algorithms

APP INVENTOR. Test Review

Lecture 5 - CPA security, Pseudorandom functions

Binary Heaps. CSE 373 Data Structures

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

Midterm Practice Problems

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Satisfiability Checking

Persistent Data Structures and Planar Point Location

The Butterfly, Cube-Connected-Cycles and Benes Networks

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

Find-The-Number. 1 Find-The-Number With Comps

1 Definition of a Turing machine

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Exam study sheet for CS2711. List of topics

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Data Structures. Level 6 C Module Descriptor

An algorithmic classification of open surfaces

Practical Guide to the Simplex Method of Linear Programming

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Transcription:

Chapter 4 Data Structures Fibonacci Heaps, Amortized Analysis Algorithm Theory WS 2012/13 Fabian Kuhn

Fibonacci Heaps Lacy merge variant of binomial heaps: Do not merge trees as long as possible Structure: A Fibonacci heap consists of a collection of trees satisfying the min heap property. Variables:. : root of the tree containing the (a) minimum key. : circular, doubly linked, unordered list containing the roots of all trees. : number of nodes currently in Algorithm Theory, WS 2012/13 Fabian Kuhn 2

Trees in Fibonacci Heaps Structure of a single node : parent left key degree right child mark. : points to circular, doubly linked and unordered list of the children of.,. : pointers to siblings (in doubly linked list). : 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 2012/13 Fabian Kuhn 3

Example Figure: Cormen et al., Introduction to Algorithms Algorithm Theory, WS 2012/13 Fabian Kuhn 4

Simple (Lazy) Operations Initialize Heap :.. Merge heaps and : concatenate root lists update. Insert element into : create new one node tree containing H merge heaps and Get minimum element of : return. Algorithm Theory, WS 2012/13 Fabian Kuhn 5

Operation Delete Min Delete the node with minimum key from and return its element: 1.. ; 2. if. 0 then 3. remove. from. ; 4. add.. (list) to. 5.. ; // 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 Algorithm Theory, WS 2012/13 Fabian Kuhn 6

Rank and Maximum Degree Ranks of nodes, trees, heap: Node : : degree of Tree : : rank (degree) of root node of Heap : : maximum degree of any node in Assumption ( : number of nodes in ): for a known function Algorithm Theory, WS 2012/13 Fabian Kuhn 7

Merging Two Trees Given: Heap ordered trees, with Assume: min key of min key of Operation, : Removes tree from root list and adds to child list of 1. Algorithm Theory, WS 2012/13 Fabian Kuhn 8

Consolidation of Root List Array pointing to find roots with the same rank: 0 1 2 Consolidate: Time: 1. for 0to do null;. 2. while. null do 3. delete and return first element of. 4. while nulldo 5. ; 6. ; 7., 8. 9. Create new. and. Algorithm Theory, WS 2012/13 Fabian Kuhn 9

Operation Decrease Key Decrease Key, : (decrease key of node to new value ) 1. if. then return; 2.. ; update. ; 3. if... then return 4. repeat 5.. ; 6.. ; 7. ; 8. until.. ; 9. if. then. ; Algorithm Theory, WS 2012/13 Fabian Kuhn 10

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

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

Fibonacci Heap Marks History of a node : is being linked to a node. a child of is cut. a second child of is cut. Hence, the boolean value. indicates whether node has lost a child since the last time was made the child of another node. Algorithm Theory, WS 2012/13 Fabian Kuhn 13

Cost of Delete Min & Decrease Key Delete Min: 1. Delete min. root and add. to. time: 1 2. Consolidate. time: length of. Step 2 can potentially be linear in (size of ) Decrease Key (at node ): 1. If new key parent key, cut sub tree of node time: 1 2. Cascading cuts up the tree as long as nodes are marked time: number of consecutive marked nodes Step 2 can potentially be linear in Exercises: Both operations can take time in the worst case! Algorithm Theory, WS 2012/13 Fabian Kuhn 14

Cost of Delete Min & Decrease Key Cost of delete min and decrease key can be Θ Seems a large price to pay to get insert and merge in 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 2012/13 Fabian Kuhn 15

Amortization Consider sequence,,, of operations (typically performed on some data structure ) : execution time of operation : total execution time The execution time of a single operation might vary within a large range (e.g., 1, ) The worst case overall execution time might still be small average execution time per operation might be small in the worst case, even if single operations can be expensive Algorithm Theory, WS 2012/13 Fabian Kuhn 16

Analysis of Algorithms Best case Worst case Average case Amortized worst case What it the average cost of an operation in a worst case sequence of operations? Algorithm Theory, WS 2012/13 Fabian Kuhn 17

Example: Binary Counter Incrementing a binary counter: determine the bit flip cost: Operation Counter Value Cost 00000 1 00001 1 2 00010 2 3 00011 1 4 00100 3 5 00101 1 6 00110 2 7 00111 1 8 01000 4 9 01001 1 10 01010 2 11 01011 1 12 01100 3 13 01101 1 Algorithm Theory, WS 2012/13 Fabian Kuhn 18

Accounting Method Observation: Each increment flips exactly one 0 into a 1 00100 1111 00100 0000 Idea: Have a bank account (with initial amount 0) Paying to the bank account costs Take money from account to pay for expensive operations Applied to binary counter: Flip from 0 to 1: pay 1 to bank account (cost: 2) Flip from 1 to 0: take 1 from bank account (cost: 0) Amount on bank account = number of ones We always have enough money to pay! Algorithm Theory, WS 2012/13 Fabian Kuhn 19

Accounting Method Op. Counter Cost To Bank From Bank Net Cost Credit 0 0 0 0 0 1 00 0 0 1 1 2 00 0 1 0 2 3 00 0 1 1 1 4 00 1 0 0 3 5 00 1 0 1 1 6 00 1 1 0 2 7 00 1 1 1 1 8 01 0 0 0 4 9 01 0 0 1 1 10 0 1 0 1 0 2 Algorithm Theory, WS 2012/13 Fabian Kuhn 20

Potential Function Method Most generic and elegant way to do amortized analysis! But, also more abstract than the others State of data structure / system: (state space) Potential function : Operation : : actual cost of operation : state after execution of operation ( : initial state) Φ : potential after exec. of operation : amortized cost of operation : Algorithm Theory, WS 2012/13 Fabian Kuhn 21

Potential Function Method Operation : actual cost: amortized cost: Φ Φ Overall cost: Φ Φ Algorithm Theory, WS 2012/13 Fabian Kuhn 22

Binary Counter: Potential Method Potential function: : Clearly, Φ 0and Φ 0for all 0 Actual cost : 1 flip from 0 to 1 1flips from 1 to 0 Potential difference: Φ Φ 1 1 2 Amortized cost: Φ Φ 2 Algorithm Theory, WS 2012/13 Fabian Kuhn 23

Back to Fibonacci Heaps Worst case cost of a single delete min or decrease key operation is Ω Can we prove a small worst case amortized cost for delete min and decrease key operations? Remark: Data structure that allows operations,, We say that operation has amortized cost if for every execution the total time is where is the number of operations of type Algorithm Theory, WS 2012/13 Fabian Kuhn 24,

Amortized Cost of Fibonacci Heaps Initialize heap, is empty, get min, insert, and merge have worst case cost Delete min has amortized cost Decrease key has amortized cost Starting with an empty heap, any sequence of operations with at most delete min operations has total cost (time). We will now need the marks Cost for Dijkstra: log Algorithm Theory, WS 2012/13 Fabian Kuhn 25

Fibonacci Heaps: Marks Cycle of a node: 1. Node is removed from root list and linked to a node. 2. Child node of is cut and added to root list. 3. Second child of is cut node is cut as well and moved to root list The boolean value. indicates whether node has lost a child since the last time was made the child of another node. 26 Algorithm Theory, WS 2012/13 Fabian Kuhn Winter term 11/12 26

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

Actual Time of Operations Operations: initialize heap, is empty, insert, get min, merge actual time: 1 Normalize unit time such that,,,, 1 Operation delete min: Actual time: length of. Normalize unit time such that length of. Operation descrease key: Actual time: length of path to next unmarked ancestor Normalize unit time such that lengthof path to next unmarked ancestor Algorithm Theory, WS 2012/13 Fabian Kuhn 28

Amortized Times Assume operation is of type: initialize heap: actual time: 1, potential: Φ Φ 0 amortized time: Φ Φ 1 is empty, get min: actual time: 1, potential: Φ Φ (heap doesn t change) amortized time: Φ Φ 1 merge: Actual time: 1 combined potential of both heaps: Φ Φ amortized time: Φ Φ 1 Algorithm Theory, WS 2012/13 Fabian Kuhn 29

Amortized Time of Insert Assume that operation is an insert operation: Actual time: 1 Potential function: remains unchanged (no nodes are marked or unmarked, no marked nodes are moved to the root list) grows by 1 (one element is added to the root list), Φ Φ 1 1 Amortized time: Algorithm Theory, WS 2012/13 Fabian Kuhn 30

Amortized Time of Delete Min Assume that operation is a delete min operation: Actual time:. Potential function : : changes from. to at most : (# of marked nodes that are not in the root list) no new marks if node is moved away from root list,. is set to false value of does not change!,. Φ Φ. Amortized Time: Algorithm Theory, WS 2012/13 Fabian Kuhn 31

Amortized Time of Decrease Key Assume that operation is a decrease key operation at node : Actual time: length of path to next unmarked ancestor Potential function : Assume, node and nodes,, are moved to root list,, are marked and moved to root list,. mark is set to true marked nodes go to root list, 1node gets newly marked grows by 1, grows by 1 and is decreased by 1, 1 Φ Φ 1 2 1 Φ 3 Amortized time: Algorithm Theory, WS 2012/13 Fabian Kuhn 32

Complexities Fibonacci Heap Initialize Heap: Is Empty: Insert: Get Min: Delete Min: Decrease Key: amortized Merge (heaps of size and, ): How large can get? Algorithm Theory, WS 2012/13 Fabian Kuhn 33

Rank of Children Lemma: Consider a node of rank and let,, be the children of in the order in which they were linked to. Then, Proof:. Algorithm Theory, WS 2012/13 Fabian Kuhn 34

Size of Trees Fibonacci Numbers: 0, 1, 2: Lemma: In a Fibonacci heap, the size of the sub tree of a node with rank is at least. Proof: : minimum size of the sub tree of a node of rank Algorithm Theory, WS 2012/13 Fabian Kuhn 35

Size of Trees 1, 2, 2: 2 Claim about Fibonacci numbers: 0: 1 Algorithm Theory, WS 2012/13 Fabian Kuhn 36

Size of Trees 1, 2, 2: 2, 1 Claim of lemma: Algorithm Theory, WS 2012/13 Fabian Kuhn 37

Size of Trees Lemma: In a Fibonacci heap, the size of the sub tree of a node with rank is at least. Theorem: The maximum rank of a node in a Fibonacci heap of size is at most. Proof: The Fibonacci numbers grow exponentially: 1 5 1 5 2 For, we need nodes. 1 5 2 Algorithm Theory, WS 2012/13 Fabian Kuhn 38

Summary: Binomial and Fibonacci Heaps Binomial Heap Fibonacci Heap initialize insert get min delete min * decrease key * merge is empty amortized time Algorithm Theory, WS 2012/13 Fabian Kuhn 39