Priority Queues Chuan-Ming Liu

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,
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.
© 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.
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.
COMP20010: Algorithms and Imperative Programming Lecture 2 Data structures for binary trees Priority queues.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
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.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
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.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Advanced Sorting 7 2  9 4   2   4   7
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.
Chapter 11 Heap.
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,
COMP9024: Data Structures and Algorithms
Source: Muangsin / Weiss
Part-D1 Priority Queues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
ADT Heap data structure
7/23/2009 Many thanks to David Sun for some of the included slides!
Heaps and Priority Queues
CMSC 341: Data Structures Priority Queues – Binary Heaps
Priority Queues and Heaps
Part-D1 Priority Queues
Ch 6: Heapsort Ming-Te Chi
Heap Sort The idea: build a heap containing the elements to be sorted, then remove them in order. Let n be the size of the heap, and m be the number of.
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
Heaps and Priority Queues
Heap Sort CSE 2011 Winter January 2019.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Lecture 9 CS2013.
Heaps and Priority Queues
1 Lecture 10 CS2013.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heaps & Multi-way Search Trees
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

Priority Queues Chuan-Ming Liu Computer Science & Information Engineering National Taipei University of Technology Taiwan

Contents Priority Queue ADT Implementing a Priority Queue with a List Heaps

Example Consider that you are now a vendor who sells the service of a machine and each client pays a fixed amount per use. Your goal is to have the maximum profit. Solution: when the machine available, select the client with minimum time requirement

Knapsack Problem Given n objects Each object has a value (profit), pi and a weight wi. A knapsack can only carry weight m Maximize the value of the stuff in the knapsack. Here we assume that the objects can be split into pieces and carry a portion xi of object i. (i.e., 0≦ xi ≦1) max subject to and

Knapsack Example 3 objects: 1 knapsack: P:25 W:18 P:24 W:15 P:15 W:10 Capacity: 20 1 knapsack:

Using Largest Profit Solution = 1 * 25 + 2/15 * 24 = 28.2 P:25; W:18 Capacity: 20 P:25; W:18 P:24; W:15 Solution = 1 * 25 + 2/15 * 24 = 28.2

Using Least Weight Solution = 1 * 15 + 10/15 * 24 = 31 P:25; W:18 P:24; W:15 P:15; W:10 Capacity: 20 P:15; W:10 P:24; W:15 Solution = 1 * 15 + 10/15 * 24 = 31 Better Select() …?

Using Largest p/w p/w = 1.39 p/w = 1.6 p/w = 1.5 Capacity: 20 P:24; W:15 P:15; W:10 Solution = 1 * 24 + 5/10 * 15 = 31.5

Solutions to Knapsack Problem The framework for solving knapsack problem repeat Select the one which meets the criterion until the capacity of the knapsack is reached Three kinds of criteria: Largest profit Least weight Largest p/w ratio How to accomplish the repeated statement?

Introduction A priority queue is a data structure that manages objects with associated priority In particular, a priority queue will support insertion, deletion, and delete_highest_priority We can use a priority queue to help us accomplish the repeated statement in the knapsack problem. In general, we refer to the priority associated with the object as the key of that object.

Some Applications In a time-sharing system, a large number of tasks may be waiting for the CPU and some of the tasks have higher priority than the other. The set of tasks waiting for the CPU forms a priority queue The emergency room treats patients according to the urgency of their malady Selling stocks according to the capital gain

Keys A key is a “value” which can be used to identify, rank, or weight the managed objects. We focus on the key which has priority over another. In order to compare a pair of keys, it requires a total order relation among the keys

Total Order Relations total order relation () Such a relation Reflexive property: x  x Antisymmetric property: x  y  y  x  x = y Transitive property: x  y  y  z  x  z Such a relation defines a linear ordering relation among the keys provides a comparison rule For a finite collection of elements having a total order relation, a smallest (largest) key kmin (kmax)is well defined.

Entries An entry in a priority queue is simply a key-element pair Priority queues store entries to allow for efficient insertion and removal based on keys Methods: key(): returns the key for this entry element(): returns the element associated with this entry

Priority Queue ADT A priority queue is a container of objects (elements), each having an associated key that is provided at the time the object is insert Having the composition and comparator patterns, a priority queue P supports insert(k, x): inserts an entry with key k and object x removeMin() (deleteMin or extractMin): removes and returns the entry with smallest key min(): returns, but do not remove, an entry with smallest key size(), isEmpty()

Priority Queues Ways to represent a priority queue Unsorted lists insert:(1) removeMin and min: (n) Sorted lists insert:(n) removeMin and min : (1) Heaps insert, removeMin and min : (log n)

Sorting with a Priority Queue We can use a priority queue to sort a set 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

Contents Priority Queue ADT Implementing a Priority Queue with a List Heaps We use list to mean a collection of records stored in a certain order and each record having one or more fields.

Using an Unsorted List Using an unsorted list S to implement a priority queue P, where S is a doubly linked list Constant time insertion: simply add the new entry at the end of S Linear time search and removal: The operation min or removeMin on P needs to scan through all the elements in S

Using a Sorted List Representing the priority queue P by using a list S of entries sorted Constant time search and removal: the operation min or removeMin on P can be done by simply accessing the first element of S Linear time insertion: the insertion needs to scan through the list to find a place to insert the entry

Selection-sort Recall the sorting scheme using a priority queue Such a sorting using a priority queue with an unsorted list is better known as selection-sort The 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 time proportional to (1 + 2 + …+ n) total running time is O(n2).

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) () Insertion removeMin

Insertion-sort Insertion-sort is a sorting scheme using a priority queue with a sorted list The running time of insertion-sort: Inserting the elements into the priority queue with n insert operations takes time proportional to (1 + 2 + …+ n) Removing the elements in sorted order from the priority queue with n removeMin operations takes O(n) time total running time is O(n2)

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) () Insertion removeMin

Contents Priority Queue ADT Implementing a Priority Queue with a List Heaps

Heaps A min (max) heap is a complete binary tree with the property that the value at each node is at least as small as (as large as) the values at its children (if they exist). The above property is called as heap property Smallest (largest) element is at the root Min (max) heap is a complete binary tree and one can use an array to represent it

Example – Heaps 10 30 15 35 40 44 25 50 38 57 61

Complete Binary Trees A complete binary tree with height h is a binary tree where for i = 0, … , h - 2, 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 complete binary tree is the right-most node of depth h A heap T storing n entries has height

A Complete Binary Tree h = 3 last node Level 0 Level 1 Level 2 Level 3 10 30 15 35 40 44 25 50 38 57 61 h = 3 last node

Complete Binary Tree ADT A complete binary tree T supports all the methods of binary tree ADT, plus add(o): add a new node v which contains the element o to T such that the resulting tree is a complete binary tree with last node v remove(): remove the last node and return its element Operation add may increase the height of T by 1

Array List Representation A complete binary tree T can be implemented by a vector V where each node v is kept with rank equal to the level number p(v) If v is the root, then p(v)=1 If v is the left child of node u, then p(v)=2p(u) If v is the right child of node u, then p(v)=2p(u)+1 Methods add and remove can be done in O(1) time.

Priority Queue with a Heap Heap-based representation T for a priority queue P consists of the following: Heap, T Comparator Should support the following operations min (max) insert removeMin(removeMax)

 Insertion insert (v) // for Min_heap increase the size of the heap add v at the end of the vector while (v.key() < v.parent.key()) do exchange v and v.parent O(h), h is the height of the heap and generally is log n  up-heap bubbling

Example – Insertion (Min-Heap) 10 15 30 35 40 44 25 50 38 57 61 8

RemoveMin removeMin ( ) Downheap (p, T) O(log n) operations remove the item at the root r and output it replace the last element in the vector with r decrease the size of the heap downheap (r, T) Downheap (p, T) //rearrage the tree into a heap a in top-down fashion push the element at position p down to the right position in the heap O(log n) operations

Example – removeMin removeMin Downheap output Minimum 8 10 30 35 40 15 25 50 38 57 61 44

Heap Sort Recall the sorting scheme using a priority queue again The heap-sort algorithm sorts a sequence S of n elements in O(n log n) time

Constructing Heaps To form a heap for n elements Insert each element one by one Each insertion: O(log n) Total time: O(n log n) Build a heap by an input vector Work on the vector (a complete binary tree) Total time: O(n) Called heapify for a vector Bottom-up fashion

Heap Construction by Insertion Compute the time tightly, we count it level by level in worst case for inserting one by one log n i i-1 Note: A heap T storing n entries has height log n

Computing the Bound

Heapfy – An Example 1 35 2 3 20 40 4 5 6 7 30 25 15 28

Heapify – Analysis Compute the time tightly, we count it level by level The worst case number of iterations for a node at level i is h-i, where h is the height of the heap i log n log n-i ?

The Bound Changing variables i=log n - j

More Priority Queues There are more priority queues discussed in Chapter 9 of the textbook We so far look at some basic priority queues like, sorted lists, unsorted lists, and heap