Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 10 CS2013.

Similar presentations


Presentation on theme: "1 Lecture 10 CS2013."— Presentation transcript:

1 1 Lecture 10 CS2013

2 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

3 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

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

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

6 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());

7 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; }

8 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()); }

9 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

10 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

11 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

12 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

13 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 …+ 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

14 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

15 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

16 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

17 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  … + 2h 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 Java Implementation © 2014 Goodrich, Tamassia, Goldwasser Heaps 25

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

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


Download ppt "1 Lecture 10 CS2013."

Similar presentations


Ads by Google