Priority Queues CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
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)
The Priority Queue ADT A priority queue is a restricted form of a list, where items are arranged according to their priorities (or keys). The key assigned.
Priority Queue and Heap 1. 2 Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the.
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.
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.
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.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
D ESIGN & A NALYSIS OF A LGORITHM 08 – P RIORITY Q UEUE Informatics Department Parahyangan Catholic University.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Binary Trees Priority Queues, and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer.
Maps and Dictionaries Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Review and Prepare for Test 3 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
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.
Bubble Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Priority Queues, Trees, and Huffman Encoding CS 244 This presentation requires Audio Enabled Brent M. Dingle, Ph.D. Game Design and Development Program.
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)
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
Priority Queues and Sorting CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
12/23/2015 2:18 AMPriority Queues1 Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Heap Sorting and Building
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Binary Trees Priority Queues, and Heaps CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer.
Queues Chapter 4.
Design & Analysis of Algorithm Priority Queue
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Priority Queues Chuan-Ming Liu
Queues Chapter 4.
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
More on Merge Sort CS 244 This presentation is not given in class
Priority Queues © 2014 Goodrich, Tamassia, Goldwasser Priority Queues
Heaps and Priority Queues
Priority Queues and Heaps
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 and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Ch. 8 Priority Queues And Heaps
Priority Queues Sell 100 IBM $ $120 Buy 500 $ $118
Heaps and Priority Queues
Priority Queues Sell 100 IBM $ $120 Buy 500 $ $118
© 2013 Goodrich, Tamassia, Goldwasser
Data Structures and Analysis (COMP 410)
CSE 12 – Basic Data Structures
Heaps and Priority Queues
Priority Queues © 2010 Goodrich, Tamassia Priority Queues
CSE 373 Priority queue implementation; Intro to heaps
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
The Priority Queue ADT A priority queue is a restricted form of a list, where items are arranged according to their priorities (or keys). The key assigned.
Stacks and Linked Lists
Presentation transcript:

Priority Queues CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Content derived from: Data Structures Using C++ (D.S. Malik) and Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount)

Previously Chapters 7 and 8 Stacks and Queues Chapter 9 Also mentions Priority Queues (PQs) This is an Intro They show up again in Sorting (Chapter 10)

Things to Note Homework 6 is Due Nov 4 Today Homework 7 is Due Nov 11 Test 2 is Nov 13

For Today Priority Queues finish chapter 8 Followed by Searching and Hashing begin chapter 9

Where are we headed? Have seen Stacks (LIFO) Implemented via statically allocated arrays Implemented via dynamically allocated arrays Implemented via linked lists Queues (FIFO) Implemented via arrays (linear and circular) Implemented via lists Deques Implemented via doubly linked list Add or Remove from either end Will soon see Priority Queues nodes have keys and data Remove based on priority

Marker Slide General Questions? Next: Priority Queues General Implementation

Problems of Priority Print Jobs Printers get print requests from all over a building. Suppose we want them to print staff jobs before faculty jobs before student jobs, and grad students before undergraduate students, and so on. How do we set this behavior up? Emergency Room scheduling Say you are in charge of scheduling patients for treatment in the ER. A gunshot victim would likely get treatment sooner than someone with a sore neck – regardless of arrival time. How do we always choose the most urgent case when new patients continue to arrive?

The Priority Queue ADT A priority queue (PQ) is a restricted form of a list items are arranged according to their priorities (or keys) keys may or may not be unique Unlike a queue which is FIFO A priority queue removes items based on priority

PQ – Total Order Relation A priority queue must hold a total order relation (example <= ) A Total Order Relation has the following properties: Reflexive k <= k Antisymmetric if x <= y and y <= x then x = y Transitive if a <= b and b <= c then a <= c

2 ‘types’ of PQ Priority Queues may be viewed as: min PQ minimum key value is the highest priority max PQ maximum key value is the highest priority

Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority Queue ADT insertItem(k, o) inserts an item with key k and element o removeMin() removes the item with the smallest key Additional methods minKey() returns, but does not remove, the smallest key of an item minElement() returns, but does not remove, the element of an item with smallest key size(), isEmpty() Applications: Standby flyers Auctions

Priority Queue ADT Operations insertItem(key, data) aka enqueue(key, data) inserts data into the PQ according to key removeItem() removes the item with the smallest (largest) key depending if using a min PQ or max PQ size() returns number of elements in PQ empty() returns true if zero elements in PQ minItem() / maxItem() returns item with smallest/largest key minKey() / maxKey() returns smallest/largest key Main Methods of PQs Additional Methods of PQs removeItem() may also be named removeMin() or removeMax() or dequeue()

Priority Queues and Sorting Priority Queues are useful for Sorting This is covered in more detail in Chapter 10 Briefly mention now preview of things to come

Sorting Using a Priority Queue Given a sequence of N items a PQ can be used to sort the sequence: Step 1 Insert N items into the PQ via insertItem(key, data) Step 2 Remove the items by calling removeItem() N times The first remove removes the largest item, the second call the second largest, … the last removes the smallest item

Sorting Using a Priority Queue Given a sequence of N items a PQ can be used to sort the sequence: Step 1 Insert N items into the PQ via insertItem(key, data) Step 2 Remove the items by calling removeItem() N times The first remove removes the largest item, the second call the second largest, … the last removes the smallest item S

Sorting Using a Priority Queue Given a sequence of N items a PQ can be used to sort the sequence: Step 1 Insert N items into the PQ via insertItem(key, data) Step 2 Remove the items by calling removeItem() N times The first remove removes the largest item, the second call the second largest, … the last removes the smallest item

Sorting Using a PQ Given a sequence of N items a PQ can be used to sort the sequence: Step 1 Insert N items into the PQ via insertItem(key, data) Step 2 Remove the items by calling removeItem() N times The first remove removes the largest item, the second call the second largest, … the last removes the smallest item Algorithm PriorityQueueSort (S, PQ): Input: Sequence, S, of n items, and empty priority queue, PQ. Output: Sequence S sorted by the total order relation. while ! S.empty() do item := S.removeFirst() PQ.insertItem(item.key, item.data) while ! PQ.empty do item := PQ.removeItem() S.insertLast(item)

Marker Slide Questions on: Priority Queues General Next: Priority Queues Implementations Unordered List Ordered List Binary Tree (Heap)

PQ Implementation Priority Queues can be implemented using Unordered List Ordered List Binary Tree (Heap) This last one we will see later The card example just shown was using an unordered list

Implementing PQ as an Unordered List Each item in a PQ is a composition of 2 objects the key (priority) and the data Thus we have the pair (key, data) So we can implement this using a linked list details next slide, look at the list’s node structure

Recall Doubly Linked List But for our priority queue we split elem into TWO things key (of type int) data (of type T) template class Item { public: T elem; Item *prev, *next; }; next elem node prev

Recall Doubly Linked List But for our priority queue we split elem into TWO things key (of type int) data (of type T) template class Item { public: int key; T data; Item *prev, *next; }; next key data node prev

More Code – For Ref If Needed class Item { private int key, data; private Item next, prev; public Item () { key = 0; data = 0; next = null; prev = null; } public Item (int newKey, int newData, Item newNext, Item newPrev) { key = newKey; data = newData; next = newNext; prev = newPrev; }

Code Continues – If needed public int getKey () { return key; } public int getData () { return data; } public Item getNext () { return next; } public Item getPrev () { return prev; } public void setKey (int newKey) { key = newKey; } public void setData (int newData) { data = newData; } public void setNext (Item newNext) { next = newNext; } public void setPrev (Item newPrev) { prev = newPrev; } public void displayItem () { System.out.println ("key: " + key + "; data: " + data); }

Implementing a PQ as Unordered list So if we have a list of unordered stuff To make it a PQ we need the functions insertItem(key, data) removeItem() assume a Min PQ so this removes the item with the smallest key

PQ as Unordered list PQ::insertItem(key, data) This is just an insertFront() call for the list like m_list.insertFront(…) Recall this is an O(1) operation Review of this follows

Inserting at the Front 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node  12, Leonard7, Sheldon98, Howard54, Raj headtail Review context of PQ::insertItem(key, data) PQ is implemented using Unordered list

Inserting at the Front 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node  12, Leonard7, Sheldon98, Howard54, Raj head  19, Penny tail Review context of PQ::insertItem(key, data) PQ is implemented using Unordered list

Inserting at the Front 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node  12, Leonard7, Sheldon98, Howard54, Raj head 19, Penny tail Review context of PQ::insertItem(key, data) PQ is implemented using Unordered list

Inserting at the Front 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node  12, Leonard7, Sheldon98, Howard54, Raj head 19, Penny tail Review context of PQ::insertItem(key, data) PQ is implemented using Unordered list

PQ as Unordered list So if we have a list of unordered stuff To make it a PQ we need the functions insertItem(key, data) Can be done in O(1) -- as just seen What about… removeItem() assume a Min PQ so this removes the item with the smallest key

PQ as Unordered list removeItem() - remove item with minimum key Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the item This is O(n)…  MinSoFar curPtr First node is assumed to be minimum until something lower is found

PQ as Unordered list removeItem() - remove item with minimum key Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the item This is O(n)…  MinSoFar curPtr

PQ as Unordered list removeItem() - remove item with minimum key Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the item This is O(n)…  MinSoFar curPtr Found something < 56 So adjust MinSoFar

PQ as Unordered list removeItem() - remove item with minimum key Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the item This is O(n)…  MinSoFar curPtr

PQ as Unordered list removeItem() - remove item with minimum key Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the item This is O(n)…  MinSoFar curPtr

PQ as Unordered list removeItem() - remove item with minimum key Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the item This is O(n)…  MinSoFar curPtr curPtr reaches the end of the list so wherever MinSoFar is pointing at must be the minimum so remove its node and adjust pointers as needed

PQ as Unordered list removeItem() - remove item with minimum key Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the item This is O(n)…  curPtr curPtr reaches the end of the list so wherever MinSoFar is pointing at must be the minimum so remove its node and adjust pointers as needed

PQ as Unordered list So if we have a list of unordered stuff To make it a PQ we need the functions insertItem(key, data) Can be done in O(1) removeItem() Requires O(n) This is no good… we want faster Maybe if we use an ORDERED list instead…

PQ as Unordered list So if we have a list of unordered stuff To make it a PQ we need the functions insertItem(key, data) Can be done in O(1) removeItem() Requires O(n) This is no good… we want faster Maybe if we use an ORDERED list instead… What does the class think? Will an ordered list help?

Marker Slide Questions on: Priority Queues General Implementations Unordered List Next: Priority Queues Implementations Ordered List

Implementing a PQ as an Ordered list Let’s say we have a list of Ordered stuff To make it a PQ we still need the functions insertItem(key, data) removeItem() assume a Min PQ so this removes the item with the smallest key

PQ as an Ordered list PQ::removeItem() is now easy This is just a removeFront() call for the list like m_list.removeFront(…) Recall this is an O(1) operation Review of this follows

Removing at the Front 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  headtail Review context of PQ::removeItem() PQ is implemented using Ordered list

Removing at the Front 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  headtail Review context of PQ::removeItem() PQ is implemented using Ordered list

Removing at the Front 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  headtail Review context of PQ::removeItem() PQ is implemented using Ordered list

Removing at the Front 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  headtail Review context of PQ::removeItem() PQ is implemented using Ordered list

Removing at the Front 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  headtail Review context of PQ::removeItem() PQ is implemented using Ordered list

PQ as an Ordered list Let’s say we have a list of Ordered stuff To make it a PQ we need the functions removeItem() Can be done in O(1) What about… insertItem(key, data) note this must maintain the ordering so we have to find the correct place to insert it

PQ as an Ordered list insertItem(key, data) Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the correct place for the item This is O(n)…  prevPtr curPtr  56 

PQ as an Ordered list insertItem(key, data) Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the correct place for the item This is O(n)…  prevPtr curPtr 56 

PQ as an Ordered list insertItem(key, data) Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the correct place for the item This is O(n)…  prevPtr curPtr 56 

PQ as an Ordered list insertItem(key, data) Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the correct place for the item This is O(n)…  prevPtr curPtr prevPtr’s key < 56 curPtr’s key > 56 So the place for key = 56 is between them Adjust pointers to make it so 56 

PQ as an Ordered list insertItem(key, data) Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the correct place for the item This is O(n)…  prevPtr curPtr prevPtr’s key < 56 curPtr’s key > 56 So the place for key = 56 is between them Adjust pointers to make it so

PQ as an Ordered list insertItem(key, data) Whether using a singly or doubly linked list we must start either at the front or the back of the list and walk through the list to find the correct place for the item This is O(n)…  prevPtr curPtr prevPtr’s key < 56 curPtr’s key > 56 So the place for key = 56 is between them Adjust pointers to make it so

PQ as an Ordered list Let’s say we have a list of Ordered stuff To make it a PQ we need the functions removeItem() Can be done in O(1) insertItem(key, data) Requires O(n) So either way, ordered or unordered list implementations of Priority Queues have at least one operation that is O(n) There must be something better… … else why would I be saying O(n) is bad =) But you have to wait until chapter 10

Marker Slide Questions on: Priority Queues General Implementations Unordered List Ordered List Next Summary

Summary: List-based Priority Queue Unsorted list implementation Store the items of the priority queue in a list-based sequence, in arbitrary order Performance: insertItem takes O(1) time since we can insert the item at the beginning or end of the sequence removeMin, minKey and minElement take O(n) time since we have to traverse the entire sequence to find the smallest key Sorted list implementation Store the items of the priority queue in a sequence, sorted by key Performance: insertItem takes O(n) time since we have to find the place where to insert the item removeMin, minKey and minElement take O(1) time since the smallest key is at the beginning of the sequence

The End of This Part End