CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of.
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)
Data Structures Lecture 7 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
The Priority Queue Abstract Data Type. Heaps. Adaptable Priority Queue. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich,
CSC401 – Analysis of Algorithms Lecture Notes 5 Heaps and Hash Tables Objectives: Introduce Heaps, Heap-sorting, and Heap- construction Analyze the performance.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
Chapter 8: Priority Queues
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Chapter 8: Priority Queues and Heaps Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided.
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
Adaptable Priority Queues1 3 a 5 g 4 e © 2010 Goodrich, Tamassia.
Chapter 21 Binary Heap.
Priority Queues & Heaps Chapter 9. Iterable Collection Abstract Collection Queue List Abstract Queue Priority Queue Array List Abstract List Vector Stack.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Priority Queues and Heaps. Outline and Reading PriorityQueue ADT (§8.1) Total order relation (§8.1.1) Comparator ADT (§8.1.2) Sorting with a Priority.
Chapter 2.4: Priority Queues and Heaps PriorityQueue ADT (§2.4.1) Total order relation (§2.4.1) Comparator ADT (§2.4.1) Sorting with a priority queue (§2.4.2)
HEAPS • Heaps • Properties of Heaps • HeapSort
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
CS 2468: Assignment 2 (Due Week 9, Tuesday. Drop a hard copy in Mail Box 75 or hand in during the lecture) Use array representation (double a[]) to implement.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Priority Queues Last Update: Oct 23, 2014 EECS2011: Priority Queues1.
Adaptable Priority Queues1 3 a 5 g 4 e © 2013 Goodrich, Tamassia, Goldwasser.
CSC 213 – Large Scale Programming. Priority Queue ADT  Prioritizes Entry s using their keys  For Entry s with equal priorities, order not specified.
1 COMP9024: Data Structures and Algorithms Week Seven: Priority Queues Hui Wu Session 1, 2016
Adaptable Priority Queues1 3 a 5 g 4 e © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java,
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Priority Queues Chuan-Ming Liu
COMP9024: Data Structures and Algorithms
Part-D1 Priority Queues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Chapter 2: Basic Data Structures
7/23/2009 Many thanks to David Sun for some of the included slides!
Heaps and Priority Queues
Priority Queues and Heaps
Part-D1 Priority Queues
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
Heaps A heap is a binary tree.
Heaps and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Copyright © Aiman Hanna All rights reserved
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Locators 3 a 1 g 4 e 12/29/2018 7:56 AM Locators Locators
Heaps and Priority Queues
Heaps II Implementation HeapSort Bottom-Up Heap Construction Locators.
Adaptable Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Lecture 9 CS2013.
Heaps and Priority Queues
1 Lecture 10 CS2013.
CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap Removing from Heap Heap Sort Heap Construction Top down Heap Construction Bottom up Heap Construction Adaptable PQ 7/28/2019 CS210-Summer 2005, Lecture 14

Heaps An efficient realization of a priority queue uses a data structure called a heap. Heap allows us to perform both insertions and removals in logarithmic time. Heap stores entries in a binary tree rather than in a list. 7/28/2019 CS210-Summer 2005, Lecture 14

Heaps and Priority Queues We can use a heap to implement a priority queue We store a (key, element) item at each node. We keep track of the position of the last node (2, Sue) (5, Pat) (6, Mark) (9, Jeff) (7, Anna) 7/28/2019 CS210-Summer 2005, Lecture 14

Insertion into a Heap (2, C) Method insert of the priority queue ADT corresponds to the insertion of an entry (k,x) to the heap The insertion algorithm consists of three steps Add a new node z (the new last node) Store e at z Restore the heap-order property (discussed next) (5, A) (6, Z) z (15,K) (9, F) insertion node (2, C) (5, A) (6, Z) z (15,K) (9, F) (1, D) 7/28/2019 CS210-Summer 2005, Lecture 14

Upheap After the insertion of a new entry e, the heap-order property may be violated Algorithm upheap restores the heap-order property by swapping e along an upward path from the insertion node Upheap terminates when the entry e reaches the root or a node whose parent has a key smaller than or equal to key k of e. Since a heap has height O(log n), upheap runs in O(log n) time (2, C) (1, D) (5, A) (1,D) (5, A) (2, C) z z (15,K) (9, F) (6, Z) (9, F) (6, Z) (15,K) 7/28/2019 CS210-Summer 2005, Lecture 14

Removal from a Heap Method removeMin of the priority queue ADT corresponds to the removal of the root entry from the heap The removal algorithm consists of three steps Replace the root entry with the entry of the last node w Remove w Restore the heap-order property (discussed next) (2, C) (5, A) (6, Z) w (15,K) (9, F) last node (9, F) (5, A) (6, Z) w (15,K) new last node 7/28/2019 CS210-Summer 2005, Lecture 14

Downheap After replacing the root entry with the entry e of the last node, the heap-order property may be violated Algorithm downheap restores the heap-order property by swapping entry e along a downward path from the root Upheap terminates when key k of entry e reaches a leaf or a node whose children have keys greater than or equal to k Since a heap has height O(log n), downheap runs in O(log n) time (9, F) (5, A) (5, A) (6, Z) (9, F) (6, Z) w w (15,K) (15,K) 7/28/2019 CS210-Summer 2005, Lecture 14

Heap-Sort Consider a priority queue with n items implemented by means of a heap methods insert and removeMin take O(log n) time methods size, isEmpty, and min take time O(1) time Using a heap-based priority queue, we can sort a sequence of n elements in O(n log n) time The resulting algorithm is called heap-sort Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort and selection-sort 7/28/2019 CS210-Summer 2005, Lecture 14

Heap Construction We can construct a heap storing n entries in O(nlogn) time, by means of n successive insert operations. However if all the n key-value pairs to be stored in the heap are given in advance, there is an alternative bottom up construction method that runs in O(n) time. 7/28/2019 CS210-Summer 2005, Lecture 14

Bottom up Heap Construction 16 15 4 12 6 7 23 20 25 5 11 27 9 8 14 16 4 15 12 6 7 23 20 25 5 11 27 9 8 14 7/28/2019 CS210-Summer 2005, Lecture 14

Bottom up Heap Construction 16 4 15 12 6 7 8 20 25 5 11 27 9 23 14 16 15 4 12 6 7 8 20 25 5 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14

Bottom up Heap Construction 16 4 15 12 6 7 8 20 25 5 11 27 9 23 14 16 15 4 12 6 7 8 20 25 5 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14

Bottom up Heap Construction 16 4 15 12 5 7 8 20 25 6 11 27 9 23 14 16 15 4 12 5 7 8 20 25 6 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14

Bottom up Heap Construction 16 4 15 12 5 7 8 20 25 6 11 27 9 23 14 16 15 4 12 5 7 8 20 25 6 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14

Bottom up Heap Construction 16 4 15 12 5 7 8 20 25 6 11 27 9 23 14 16 15 4 12 5 7 8 20 25 6 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14

Bottom up Heap Construction 16 4 5 12 6 7 8 20 25 15 11 27 9 23 14 16 5 4 12 6 7 8 20 25 15 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14

Bottom up Heap Construction 4 7 5 12 6 9 8 20 25 15 11 27 16 23 14 16 5 4 12 6 7 8 20 25 15 11 27 9 23 14 7/28/2019 CS210-Summer 2005, Lecture 14

Adaptable Priority Queues Suppose we have an online trading system where orders to purchase and sell a given stock are stored in two priority queues (one for sell orders and one for buy orders) as (p,s) entries: The key, p, of an order is the price The value, s, for an entry is the number of shares A buy order (p,s) is executed when a sell order (p’,s’) with price p’<p is added (the execution is complete if s’>s) A sell order (p,s) is executed when a buy order (p’,s’) with price p’>p is added What if someone wishes to cancel their order before it executes? What if someone wishes to update the price or number of shares for their order? 7/28/2019 CS210-Summer 2005, Lecture 14

Methods of the Adaptable PQ ADT remove(e): Remove and return entry e. replaceKey(e,k): Replace with k and return the key of entry e of P; an error condition occurs if k is invalid (that is, k cannot be compared with other keys). replaceValue(e,x): Replace with x and return the value of entry e of P. 7/28/2019 CS210-Summer 2005, Lecture 14

Example Operation Output P insert(5,A) e1 (5,A) insert(3,B) e2 (3,B),(5,A) insert(7,C) e3 (3,B),(5,A),(7,C) min() e2 (3,B),(5,A),(7,C) key(e2) 3 (3,B),(5,A),(7,C) remove(e1) e1 (3,B),(7,C) replaceKey(e2,9) 3 (7,C),(9,B) replaceValue(e3,D) C (7,D),(9,B) remove(e2) e2 (7,D) 7/28/2019 CS210-Summer 2005, Lecture 14

Locating Entries In order to implement the operations remove(k), replaceKey(e), and replaceValue(k), we need fast ways of locating an entry e in a priority queue. We can always just search the entire data structure to find an entry e, but there are better ways for locating entries. 7/28/2019 CS210-Summer 2005, Lecture 14

Location-Aware Entries A locator-aware entry identifies and tracks the location of its (key, value) object within a data structure Main idea: Since entries are created and returned from the data structure itself, it can return location-aware entries, thereby making future updates easier 7/28/2019 CS210-Summer 2005, Lecture 14

List Implementation A location-aware list entry is an object storing key value position (or rank) of the item in the list In turn, the position (or array cell) stores the entry trailer header nodes/positions 2 c 4 c 5 c 8 c entries 7/28/2019 CS210-Summer 2005, Lecture 14

Heap Implementation A location-aware heap entry is an object storing key value position of the entry in the underlying heap In turn, each heap position stores an entry Back pointers are updated during entry swaps 2 d 4 a 6 b 8 g 5 e 9 c 7/28/2019 CS210-Summer 2005, Lecture 14

Performance Using location-aware entries we can achieve the following running times: Method Unsorted List Sorted List Heap size, isEmpty O(1) O(1) O(1) insert O(1) O(n) O(log n) min O(n) O(1) O(1) removeMin O(n) O(1) O(log n) remove O(1) O(1) O(log n) replaceKey O(1) O(n) O(log n) replaceValue O(1) O(1) O(1) 7/28/2019 CS210-Summer 2005, Lecture 14