1 Lecture 10 CS2013.

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.
© 2004 Goodrich, Tamassia Priority Queues1. © 2004 Goodrich, Tamassia Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection.
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.
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.
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)
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.
Quotes “From each according to his ability, to each according to his needs” -- Karl Marx/Queue ADT “In America, first you get the sugar, then you get the.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
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.
1 COMP9024: Data Structures and Algorithms Week Seven: Priority Queues Hui Wu Session 1, 2016
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
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Chapter 2: Basic Data Structures
Priority Queues © 2014 Goodrich, Tamassia, Goldwasser Priority Queues
Heaps and Priority Queues
Chapter 2, Sections 4 and 5 Priority Queues Heaps Dictionaries
Priority Queues and Heaps
Part-D1 Priority Queues
Heaps and Priority Queues
Priority Queues 4/6/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
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
Heaps and Priority Queues
Copyright © Aiman Hanna All rights reserved
Priority Queues Sell 100 IBM $ $120 Buy 500 $ $118
© 2013 Goodrich, Tamassia, Goldwasser
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Lecture 9 CS2013.
Heaps and Priority Queues
Priority Queues © 2010 Goodrich, Tamassia Priority Queues
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

1 Lecture 10 CS2013

Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue ADT insert(k, v) inserts an entry with key k and value v removeMin() removes and returns the entry with smallest key, or null if the the priority queue is empty Additional methods min() returns, but does not remove, an entry with smallest key, or null if the the priority queue is empty size(), isEmpty() Applications: Standby flyers Auctions Stock market © 2014 Goodrich, Tamassia, Goldwasser Priority Queues 2

Entry ADT As a Java interface: /** * Interface for a key-value An entry in a priority queue is simply a key-value pair Priority queues store entries to allow for efficient insertion and removal based on keys Methods: getKey: returns the key for this entry getValue: returns the value associated with this entry As a Java interface: /** * Interface for a key-value * pair entry **/ public interface Entry<K,V> { K getKey(); V getValue(); } © 2014 Goodrich, Tamassia, Goldwasser Priority Queues 3

Example A sequence of priority queue methods The numbers are keys (priorities), letters are values (data). © 2014 Goodrich, Tamassia, Goldwasser Priority Queues 4

5 Comparator The Comparator interface specifies method compare(obj1, obj2). It is parameterized by the type of the objects that will be compared. It is typically implemented in a class which exists only for this purpose. Notice the contrast with the Comparable interface, which requires an instance method that compares the current object to another object passed in as a parameter. Here are some common scenarios for Comparator: We have a collection of objects of a class that we didn't write and can’t change. It may implement Comparable, but we want to sort using a different order than the one that will be produced by Comparable. We need to sort the same kinds of objects in different ways at different times We need to be able to compare two objects of different classes without changing the class code. This requires parameterizing with a superclass or interface both types share.

Priority Queue 6 package priorityqueue; public class Product implements Comparable <Product>{ String name; double priceInDollars; public String getName() { return name; } public double getPriceInDollars() { return priceInDollars; public Product(String nameIn, double priceInDollarsIn) { name = nameIn; priceInDollars = priceInDollarsIn; public String toString(){ return name + ": $ " + priceInDollars; @Override public int compareTo(Product otherProduct){ return name.compareTo(otherProduct.getName());

Priority Queue 7 package priorityqueue; import java.util.Comparator; public class ProductComparatorUsingPrice implements Comparator<Product>{ @Override public int compare(Product p1, Product p2) { double diff = p1.getPriceInDollars() - p2.getPriceInDollars(); if(diff > 0) return -1; else if (diff < 0) return 1; else return 0; }

Priority Queue 8 package priorityqueue; import java.util.Comparator; import java.util.PriorityQueue; public class PriorityQueueDemo { public static void main(String[] args) throws InterruptedException { PriorityQueue<Product> queue = new PriorityQueue<Product>(); //Comparator<Product> comp = new ProductComparatorUsingPrice(); //PriorityQueue<Product> queue = new PriorityQueue<Product>(comp); queue.offer(new Product("Socks", 7.99)); queue.offer(new Product("Shoes", 59.99)); queue.offer(new Product("Laces", 1.99)); queue.offer(new Product("Hat", 23.99)); queue.offer(new Product("Shirt", 41.99)); while (!queue.isEmpty()) { System.out.println(queue.remove()); }

Sequence-based Priority Queue Implementation with an unsorted list Performance: insert takes O(1) time since we can insert the item at the beginning or end of the sequence removeMin and min take O(n) time since we have to traverse the entire sequence to find the smallest key Implementation with a sorted list Performance: insert takes O(n) time since we have to find the place where to insert the item removeMin and min take O(1) time, since the smallest key is at the beginning 4 5 2 3 1 1 2 3 4 5 © 2014 Goodrich, Tamassia, Goldwasser Priority Queues 9

Priority Queue Sorting We can use a priority queue to sort a list of comparable elements Insert the elements one by one with a series of insert operations Remove the elements in sorted order with a series of removeMin operations The running time of this sorting method depends on the priority queue implementation Algorithm PQ-Sort(S, C) Input list S, comparator C for the elements of S Output list S sorted in increasing order according to C P  priority queue with comparator C while S.isEmpty () e  S.remove(S.first ()) P.insert (e, ) while P.isEmpty() e  P.removeMin().getKey() S.addLast(e) © 2014 Goodrich, Tamassia, Goldwasser Priority Queues 10

Selection-Sort Selection-sort is the variation of PQ-sort in which the priority queue is implemented with an unsorted sequence Running time of Selection-sort: Inserting the elements into the priority queue with n insert operations takes O(n) time Removing the elements in sorted order from the priority queue with n removeMin operations takes n O(n) operations O(n2) + O(n) = O(n2) Like other variants of selection sort you may already know, this selection-sort runs in O(n2) time © 2014 Goodrich, Tamassia, Goldwasser Priority Queues 11

Selection-Sort Example Sequence S Priority Queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (7,4) .. .. .. (g) () (7,4,8,2,5,3,9) Phase 2 (a) (2) (7,4,8,5,3,9) (b) (2,3) (7,4,8,5,9) (c) (2,3,4) (7,8,5,9) (d) (2,3,4,5) (7,8,9) (e) (2,3,4,5,7) (8,9) (f) (2,3,4,5,7,8) (9) (g) (2,3,4,5,7,8,9) () © 2014 Goodrich, Tamassia, Goldwasser Priority Queues 12

Insertion-Sort Insertion-sort is the variation of PQ-sort in which the priority queue is implemented with a sorted sequence Running time of Insertion-sort: Inserting the elements into the priority queue with n insert operations takes time proportional to 1 + 2 + …+ n = O(n2) Removing the elements in sorted order from the priority queue with a series of n removeMin operations takes O(n) time Insertion-sort runs in O(n2) time © 2014 Goodrich, Tamassia, Goldwasser Priority Queues 13

Insertion-Sort Example Sequence S Priority queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (4,7) (c) (2,5,3,9) (4,7,8) (d) (5,3,9) (2,4,7,8) (e) (3,9) (2,4,5,7,8) (f) (9) (2,3,4,5,7,8) (g) () (2,3,4,5,7,8,9) Phase 2 (a) (2) (3,4,5,7,8,9) (b) (2,3) (4,5,7,8,9) .. .. .. (g) (2,3,4,5,7,8,9) () © 2014 Goodrich, Tamassia, Goldwasser Priority Queues 14

Heap The term "heap" is used in several different ways in programming. Priority queues are often implemented using heaps that adhere to this definition: A heap is a binary tree (one in which each node has at most two children) with these two properties: Order property: If node A is the parent of node B, then the key of node A is ordered with respect to the key of node B with the same ordering applying across the heap. Either the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node (max heap) or the keys of parent nodes are less than or equal to those of the children and the lowest key is in the root node (min heap). Shape property: 1- All leaves are either at depth d or d-1 (for some value d). 2- All of the leaves at depth d-1 are to the right of the leaves at depth d. 3- (a) There is at most 1 node with just 1 child. (b) Any such child is the left child of its parent, and (c) it is the rightmost leaf at depth d. 15

Heaps The last node of a heap is the rightmost node of maximum depth 2 4/23/2019 Heaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: Heap-Order: for every internal node v other than the root, key(v)  key(parent(v)) Complete Binary Tree: let h be the height of the heap for i = 0, … , h - 1, there are 2i nodes of depth i at depth h - 1, the internal nodes are to the left of the external nodes The last node of a heap is the rightmost node of maximum depth 2 5 6 9 7 last node © 2014 Goodrich, Tamassia, Goldwasser Heaps 16 16

Height of a Heap Theorem: A heap storing n keys has height O(log n) Proof: (we apply the complete binary tree property) Let h be the height of a heap storing n keys Since there are 2i keys at depth i = 0, … , h - 1 and at least one key at depth h, we have n  1 + 2 + 4 + … + 2h-1 + 1 Thus, n  2h , i.e., h  log n depth keys 1 1 2 h-1 2h-1 h 1 © 2014 Goodrich, Tamassia, Goldwasser Heaps 17

Heaps and Priority Queues We can use a heap to implement a priority queue We store a (key, element) item at each internal node We keep track of the position of the last node (2, Sue) (5, Pat) (6, Mark) (9, Jeff) (7, Anna) © 2014 Goodrich, Tamassia, Goldwasser Heaps 18

Insertion into a Heap Method insertItem of the priority queue ADT corresponds to the insertion of a key k to the heap The insertion algorithm consists of three steps Find the insertion node z (the new last node) Store k at z Restore the heap-order property (discussed next) 2 5 6 z 9 7 insertion node 2 5 6 z 9 7 1 © 2014 Goodrich, Tamassia, Goldwasser Heaps 19

Upheap After the insertion of a new key k, the heap-order property may be violated Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k Since a heap has height O(log n), upheap runs in O(log n) time 2 1 5 1 5 2 z z 9 7 6 9 7 6 © 2014 Goodrich, Tamassia, Goldwasser Heaps 20

Removal from a Heap Method removeMin of the priority queue ADT corresponds to the removal of the root key from the heap The removal algorithm consists of three steps Replace the root key with the key of the last node w Remove w Restore the heap-order property (discussed next) 2 5 6 w 9 7 last node 7 5 6 w 9 new last node © 2014 Goodrich, Tamassia, Goldwasser Heaps 21

Downheap After replacing the root key with the key k of the last node, the heap-order property may be violated Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root Downheap terminates when key k 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 7 5 5 6 7 6 w w 9 9 © 2014 Goodrich, Tamassia, Goldwasser Heaps 22

Heap-Sort Consider a priority queue with n items implemented by means of a heap the space used is O(n) 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, using n insert operations, each O(log n)) The resulting algorithm is called heap- sort Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort and selection-sort © 2014 Goodrich, Tamassia, Goldwasser Heaps 23

Array-based Heap Implementation We can represent a heap with n keys by means of an array of length n For the node at rank i the left child is at rank 2i + 1 the right child is at rank 2i + 2 Links between nodes are not explicitly stored Operation add corresponds to inserting at rank n + 1 Operation remove_min corresponds to removing at rank n Yields in-place heap-sort 2 5 6 9 7 2 5 6 9 7 1 3 4 © 2014 Goodrich, Tamassia, Goldwasser Heaps 24

Java Implementation © 2014 Goodrich, Tamassia, Goldwasser Heaps 25

Java Implementation, 2 © 2014 Goodrich, Tamassia, Goldwasser Heaps 26

Java Implementation, 3 © 2014 Goodrich, Tamassia, Goldwasser Heaps 27