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.

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

COL 106 Shweta Agrawal and Amit Kumar
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)
Problem of the Day  To get into club, must get past bouncer  Recorded correct responses of those before you  Guard said five & response was four  Guard.
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.
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.
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
CSC 213 – Large Scale Programming Lecture 14: Sequence-based Priority Queues.
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.
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.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max 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)
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.
Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank).  You only have one match; what.
CS 367 Introduction to Data Structures Lecture 8.
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 CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Priority Queues Last Update: Oct 23, 2014 EECS2011: Priority Queues1.
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
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:
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,
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.
CSCI2100 Data Structures Tutorial 7
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
CSCE 3110 Data Structures and Algorithm Analysis
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Heaps and Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
CSCE 3110 Data Structures and Algorithm Analysis
Heaps and Priority Queues
CSCE 3110 Data Structures and Algorithm Analysis
1 Lecture 10 CS2013.
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

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 power, then you get the women…” -- Homer Simpson/Priority Queue ADT

Announcements Homework #3 due today before lab  Blackboard is behaving oddly Submit via Novell or me your files  Same goes for daily quizzes

Queue v. Priority Queue Queues --- nice, simple, & fair  Represent first-come, first-served concept  Assume all elements are equal and patient  Have NO basis in reality When was the last time you were on a plane? Priority Queues -- biased & undemocratic  Entries are ranked in order  Prefer items with higher priority  Does not define order for similar priorities

Priorities What types could we use for priorities?

Priorities Comparing priorities can be very difficult: NFL Tie breaking procedure 1. Head-to-head (best won-lost-tied percentage in games between the clubs). 2. Best won-lost-tied percentage in games played within the division. 3. Best won-lost-tied percentage in common games. 4. Best won-lost-tied percentage in games played within the conference. 5. Strength of victory. 6. Strength of schedule. 7. Best combined ranking among conference teams in points scored and points allowed. 8. Best combined ranking among all teams in points scored and points allowed. 9. Best net points in common games. 10. Best net points in all games. 11. Best net touchdowns in all games.  Rules are different for wild-card ties!

Priorities May not be easy to state priority  Implementing the NFL tie-breaker  Comparing non-numeric keys Airlines use last digit and the letter on ticket to determine how receives standby tickets  May want to change weighting over time Banks usually prefer higher-balance customers, but prefer customers offering higher potential incomes during peak time  Hard to change definition of “ =”, …

Comparator ADT Comparator encapsulates comparing two objects (a & b) for a total order relation  a must either be less than, equal to, or greater than b Comparator is external to keys compared  Encapsulation principle says separate ideas should remain separate Changing comparator enables reweighting relative priorities

Comparator ADT Defines single method compare(x, y) : Returns integer 0 if a > b; throws exception when a and b cannot be compared public class StringComparator { public int compare(Object x, Object y){ return((String)x).compareTo((String)y); }

Entries Queue contains elements  Elements defined by only one data item  Position ADT defines only Object element() Position Queue contains entries  Entries defined by two data items Key specifying the priority of the entry Value the entry is storing

Entries Entry ADT defines key-value pair Makes adding data to Priority Queue and tracking data within this structure easier public interface Entry { public Object key(); public Object value(); }

PriorityQueue ADT Priority Queue ADT defines 5 different methods:  Entry insert(Object k, Object x) inserts entry with key k and value x  Entry removeMin() removes and returns entry with smallest key  Entry min() returns, but does not remove, entry with smallest key  int size() returns number of entries in priority queue  boolean isEmpty() returns true is priority queue is empty; false otherwise

Priority Queue Implementation Simplest implementations use Sequence 1. Use unsorted Sequence  Insert new items to front of Sequence  Scan Sequence for minimum key and remove it 2. Maintain sorted Sequence  Insert new item in location to maintain Entries in order of their key value  Remove item at front of Sequence

What are these complexities? Unsorted Sequence-based  Insert –  Remove – Sorted Sequence-based  Insert –  Remove --

Sorts We can use Priority Queues to sort data items  Insert each entry into the Priority Queue Key & Value of each entry are identical  Call removeMin to return entries in order Define two different sorts  Selection Sort  Insertion Sort

Sorting using Priority Queue Insert each item into priority queue  Call removeMin to retrieve all items in sorted order

Selection Sort Complexity Selection sort uses unordered Sequence- based implementation Time Complexity to sort n items by selection sort  n insertions * time = time  n removeMins * time = time  Total time =

Insertion Sort Complexity Selection sort uses unordered Sequence- based implementation Time Complexity to sort n items by selection sort  n insertions * time = time  n removeMins * time = time  Total time =

Heap Heap is binary tree which stores Entries Heap must satisfy the following properties:  For every node, v, other than the root: key(v)  key(parent(v))  Binary Tree must be complete

Heap Heap is binary tree which stores Entries Heap must satisfy the following properties:  For every node, v, other than the root: key(v)  key(parent(v))  Binary Tree must be complete Levels above lowest level must be full

Heap Heap is binary tree which stores Entries Heap must satisfy the following properties:  For every node, v, other than the root: key(v)  key(parent(v))  Binary Tree must be complete Lowest level must be filled from left to right

Heap Properties Consider daily quiz due today TeamsGames Champ WinsTotal Games n

Who Cares About Heaps? Can implement a priority queues with heap  Can implement heap like any binary tree Keep minimum entry at root  Makes min() implementation quick & easy Where must we insert new elements?

Upheap Insertion may violate heap-order property Upheap starts at the new node, travels up the tree restoring heap-order property  Swaps violating node’s Entry with its parent’s Entry Terminates when:  Reaches node with key smaller than that node’s parent’s key  Reaches tree root

Problem Algorithm assumes we know the first empty node  How can we find it before an insertion?

Last Node in Heap Start at the last Node  Go up until a left child or the root is reached  If a left child is reached, go to the right child  Keep taking left children until leaf is reached

Upheap Complexity For a tree of n nodes, what is the height of the tree? What is work finding last node & upheap could do? What is big-Oh complexity of insert()?

Removing a Node From Heap Remove node with minimum key  Where would we find this node?  How can we be sure?

Removing Node From Heap 1. Replace the root Entry with Entry of last node w Where would we find this node? 2. Remove node w 3. Perform downheap to restore heap-order property

Downheap Downheap starts at root, travels down the tree restoring heap-order property  Swaps violating node’s Entry with Entry at child node with smallest key Terminates when:  Reaches node with key larger than that node’s childrens’ keys  Reaches tree leaf

Downheap Complexity For a tree of n nodes, what is the height of the tree? What is maximum work downheap could do? What is big-Oh complexity of removeMin()?

Daily Quiz Consider another sort: heap sort  Like earlier sorts, but uses heap-based implementation of priority queue  Trace each step in heap as we sort following list: 6, 5, 4, 3, 2, 1  What is big-Oh complexity of inserting n items?  What is big-Oh complexity of removing n items?  What is big-Oh complexity of heap sort?