UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters.

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

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.
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
Analysis of Algorithms
Advanced Data structure
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Fibonacci Heaps CS 252: Algorithms Geetika Tewari 252a-al Smith College December, 2000.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2009 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677
Comp 122, Spring 2004 Heapsort. heapsort - 2 Lin / Devi Comp 122 Heapsort  Combines the better attributes of merge sort and insertion sort. »Like merge.
Heapsort Chapter 6. Heaps A data structure with  Nearly complete binary tree  Heap property: A[parent(i)] ≥ A[i] eg. Parent(i) { return } Left(i) {
Fibonacci Heap.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2007 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Dr. Andrew Wallace PhD BEng(hons) EurIng
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Heap Lecture 2 Chapter 7 Wed. 10/10/01 Use NOTES feature to.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
1 Binomial heaps, Fibonacci heaps, and applications.
Sorting Algorithms (Part II) Slightly modified definition of the sorting problem: input: A collection of n data items where data item a i has a key, k.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Chapter 21 Binary Heap.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Lecture X Fibonacci Heaps
CS 473Lecture X1 CS473-Algorithms Lecture BINOMIAL HEAPS.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
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.
CS 2133: Data Structures Quicksort. Review: Heaps l A heap is a “complete” binary tree, usually represented as an array:
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
1 Analysis of Algorithms Chapter - 03 Sorting Algorithms.
1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort (speed) Based on a data structure called a “binary heap”
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
David Luebke 1 2/5/2016 CS 332: Algorithms Introduction to heapsort.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Fibonacci Heaps.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
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.
Fibonacci Heap. p2. Procedure Binary heap (worst-case) Fibonacci heap (amortized) MAKE-HEAP  (1) INSERT  (lg n)  (1) MINIMUM  (1) EXTRACT-MIN  (lg.
1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort.
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))
Heaps, Heapsort, and Priority Queues
Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick
Introduction to Algorithms
CS 583 Analysis of Algorithms
CS200: Algorithm Analysis
Ch 6: Heapsort Ming-Te Chi
ערמות בינומיות ופיבונצ'י
CS 583 Analysis of Algorithms
Fibonacci Heaps.
Presentation transcript:

UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters 20-21

Relevant Sections of Chapters Ch20 Binomial Heaps You’re responsible for material in this chapter that we discuss in lecture. (Note that this includes all sections.) Ch21 Fibonacci Heaps You’re responsible for material in this chapter that we discuss in lecture. (Note that this includes all sections.) Note that Chapter 22 has been removed. Ch7 HeapSort

Overview ä Chapter 7: Heap Review ä Chapter 20: Binomial Heaps ä Chapter 21: Fibonacci Heaps

Chapter 7 Heap Review

Review of Heap Basics ä Structure: ä Nearly complete binary tree ä Convenient array representation ä HEAP Property: (for MAX HEAP) ä Parent’s label not less than that of each child

Operations on a Heap ä HEAPIFY: ä for a given node that is the root of a subtree, if both subtrees of that node are already HEAPs, HEAPIFY enforces the HEAP PROPERTY via “downward swaps” so that the node together with its subtrees form a HEAP ä BUILD-HEAP: ä builds a HEAP from scratch using HEAPIFY ä HEAPSORT: ä sorts an array by first using BUILD-HEAP then repeatedly swapping out root and calling HEAPIFY assuming array representation

Operations on a Heap ä HEAPIFY: ä BUILD-HEAP: ä HEAPSORT: assuming array representation Asymptotic worst-case running time is in O(lg n). Asymptotic worst- case running time is in O(n lg n). However this is a loose bound! Time is also in O(n). Asymptotic worst-case running time is in O(n lg n). For a node at height h, time is in O(h). O(n) + O(lgn) nO(lgn) T(n) = T(2n/3) +  (1) is in O( lgn ) using Master Theorem

Operations on a Heap ä PRIORITY QUEUE SUPPORT: ä HEAP-INSERT ä adds new leaf to the tree and then “swaps up” to restore HEAP PROPERTY ä HEAP- MAXIMUM ä HEAP PROPERTY guarantees that maximum is at the root of a MAX HEAP ä HEAP- EXTRACT-MAX ä removes the maximum value from the root by swapping it out ä restores HEAP PROPERTY using HEAPIFY assuming array representation Applications: Job Scheduling, Event Scheduling

Operations on a Heap ä PRIORITY QUEUE SUPPORT: ä HEAP-INSERT ä HEAP- MAXIMUM ä HEAP- EXTRACT-MAX assuming array representation Asymptotic worst-case running time is in O(lg n). For a node at height h, time is in O(h). Asymptotic worst-case running time is in O(1). Asymptotic worst-case running time is in O(lg n). For a node at height h, time is in O(h). O(1) + O(lgn)

Building a Heap using HEAPIFY vs. HEAP-INSERT ä HEAPIFY ä swaps down ä compares parent with both children before each swap Asymptotic worst-case running time of BUILD-HEAP using HEAPIFY is in O(n). However, using HEAP-INSERT the time would only be in O(n lg n). ä HEAP-INSERT ä swaps up ä compares parent with one child before each swap number of levels number of nodes in this level maximum number of swaps = length of path from this level down to leaf number of levels number of nodes in this level maximum number of swaps = length of path from this level up to root O(n) as in HEAPIFY

Chapter 20 Binomial Heaps

Mergeable Heap Operations source: textbook Cormen et al. If UNION not needed, binary heap (Ch7) suffices Mergable Heaps supporting fast UNION

Mergeable Heap Operations source: textbook Cormen et al. Mergable Heaps supporting fast UNION (inefficient SEARCH) search root list use min pointer UNION add to root list; update min pointer orphan, reverse, UNION orphan, consolidate = degree roots swap up if heap property violation swap with root; swap down if heap property violation swap up if heap property violation DECREASE-KEY, EXTRACT-MIN add to root list; update min pointer cascading cut if heap property violation

Binomial Tree Definition source: textbook Cormen et al. Binomial Heap is a collection of Binomial Trees Binomial Tree B k is an ordered tree defined recursively (children are ordered)

Binomial Tree Properties source: textbook Cormen et al.

Binomial Tree Properties source: textbook Cormen et al.

Binomial Tree Properties source: textbook Cormen et al. Proof: (continued)

Binomial Heap Definition source: textbook Cormen et al. root degrees increase along root list

Binomial Heap Operations: MAKE-HEAP, MINIMUM source: textbook Cormen et al. MAKE-HEAP worst-case running time is in  (1) HEAP-MINIMUM worst-case running time is in  (lgn) ( assume no keys with value) Minimum key must be in a root node due to heap-ordering. There are at most roots to check. search root list

Binomial Heap Operations: UNION source: textbook Cormen et al. Links 2 binomial heaps whose roots have same degree Merge root lists into single linked list sorted by nondecreasing degree Link roots of equal degree until at most one root remains of each degree Pointers into root list: HEAP-UNION worst-case running time is in  (lgn) orphan, consolidate = degree roots

Binomial Heap Operations: UNION (continued) source: textbook Cormen et al. 3 roots of same degree

Binomial Heap Operations: UNION (continued) source: textbook Cormen et al.

Binomial Heap Operations: UNION (continued) source: textbook Cormen et al.

Binomial Heap Operations: INSERT source: textbook Cormen et al. HEAP-INSERT worst-case running time is in  (lgn) UNION

Binomial Heap Operations: EXTRACT-MIN source: textbook Cormen et al. HEAP-EXTRACT-MIN worst-case running time is in  (lgn) orphan, reverse, UNION

Binomial Heap Operations: DECREASE-KEY source: textbook Cormen et al. HEAP-DECREASE-KEY worst-case running time is in  (lgn) If violate heap property, swap up. No change in structure. swap up if heap property violation

Binomial Heap Operations: DELETE source: textbook Cormen et al. HEAP-DELETE worst-case running time is in  (lgn) DECREASE-KEY, EXTRACT-MIN

Chapter 21 Fibonacci Heaps

Mergeable Heap Operations source: textbook Cormen et al. Mergable Heaps supporting fast UNION (inefficient SEARCH) search root list UNION orphan, reverse, UNION orphan, consolidate = degree roots swap up if heap property violation swap with root; swap down if heap property violation swap up if heap property violation DECREASE-KEY, EXTRACT-MIN use min pointer add to root list; update min pointer orphan, consolidate = degree roots DECREASE-KEY, EXTRACT-MIN add to root list; update min pointer cascading cut if heap property violation O(D(n)) O(D(n))

Fibonacci Heap Basics source: textbook Cormen et al. For asymptotically fast MST, shortest paths Collection of trees Relaxed structure Lazy: delay work Amortized (potential) cost Circular linked lists Minimum root t(H) = #trees in root list m(H) = #marked nodes Heap Potential Function

Potential Method (review) ä Potential Method ä amortized cost can differ across operations (as in accounting method) ä overcharge some operations early in sequence (as in accounting method) ä store overcharge as “potential energy” of data structure as a whole ä (unlike accounting method) ä Let c i be actual cost of ith operation ä Let D i be data structure after applying ith operation  Let  (D i ) be potential associated with D i ä Amortized cost of ith operation: ä Total amortized cost of n operations: ä Must have:to “pay in advance” terms telescope

Unordered Binomial Tree Properties source: textbook Cormen et al. DIFFERENCE DECREASE-KEY Fibonacci Heap operation may violate Unordered Binomial Tree properties.

Fibonacci Heap Operations: INSERT source: textbook Cormen et al. Increase in potential = Amortized cost is in O(1) Actual cost is in O(1) add to root list; update min pointer Heap Potential Function t (H) = #trees in root list m(H) = #marked nodes

Fibonacci Heap Operations: UNION source: textbook Cormen et al. add to root list; update min pointer Heap Potential Function t (H) = #trees in root list m(H) = #marked nodes

Fibonacci Heap Operations: EXTRACT-MIN source: textbook Cormen et al. disassemble Consolidate wherever possible: Link roots of = degree until at most one root remains of each degree Process one tree at a time, starting with new “min”. Next, update “min” First, disassemble old min tree. orphan, consolidate = degree roots

Fibonacci Heap Operations: EXTRACT-MIN Consolidation source: textbook Cormen et al. Degree 0 merge: Combine 7 with 23 Degree 1 merge: (keep going) Combine 7/23 with 17/30 Degree 2 merge: (keep going) Combine 7… with 24…

Fibonacci Heap Operations: EXTRACT-MIN Consolidation source: textbook Cormen et al.

Fibonacci Heap Operations: EXTRACT-MIN Pseudocode source: textbook Cormen et al.

Fibonacci Heap Operations: EXTRACT-MIN Pseudocode source: textbook Cormen et al. may do multiple degree merges

Fibonacci Heap Operations: EXTRACT-MIN Analysis source: textbook Cormen et al. Amortized cost is in O(lgn) D(n) = upper bound on maximum degree of any node in an n-node Fibonacci heap (shown in Section 21.3 to be in O(lgn)) O(D(n)) Heap Potential Function t (H) = #trees in root list m(H) = #marked nodes Actual Work: >= O(D(n)) since at most D(n) children of minimum node When CONSOLIDATE is called, size of root list <= D(n) + t(H) - 1 Work in CONSOLIDATE’s for loop in O(D(n) + t(H)) due to tree linking in each iteration Total Actual Cost is in O(D(n) + t(H))

Fibonacci Heap Operations: DECREASE-KEY PseudoCode source: textbook Cormen et al. cascading cut if heap property violation

Fibonacci Heap Operations: DECREASE-KEY Analysis source: textbook Cormen et al. Heap Potential Function t (H) = #trees in root list m(H) = #marked nodes As soon as 2nd child of x is lost, cut x from parent, making it a new root. Actual Work: Dominated by cost of CASCADING-CUT Assume CASCADING-CUT called recursively c times Total Actual Cost is in O(c) Amortized cost is in  (1) Change in potential is at most:: t(H) to start with, c-1 added from cuts, + tree rooted at x c-1 unmarked by cascading cuts + at most 1 from last call to CASCADING-CUT

Fibonacci Heap Operations: DELETE source: textbook Cormen et al. Amortized cost is in  (lgn) DECREASE-KEY, EXTRACT-MIN O(D(n))

Bounding the Maximum Degree source: textbook Cormen et al. EXTRACT-MIN (& DELETE) O(lgn) bounds require D(n) in O(lgn) Show: Cutting node when it loses 2nd child Link only occurs in EXTRACT-MIN, DELETE

Bounding the Maximum Degree source: textbook Cormen et al.

Bounding the Maximum Degree source: textbook Cormen et al. Exercise 2.2-8

Bounding the Maximum Degree source: textbook Cormen et al. Thus, EXTRACT-MIN (& DELETE) have O(lgn) time bounds.