Problem of the Day  At low tide, a ship docks in the harbor  Ship is 9’ above the water line when it docks  Over the side hangs a rope ladder w/ rungs.

Slides:



Advertisements
Similar presentations
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)
Advertisements

CSC 212 – Data Structures.  Fri., Dec. 17 th from 8AM – 10AM in OM 200  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP 
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.
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.
Problem of the Day  At low tide, a ship docks in the harbor  Ship is 9’ above the water line when it docks  Over the side hangs a rope ladder w/ rungs.
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.
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.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
© 2004 Goodrich, Tamassia Priority Queues1. © 2004 Goodrich, Tamassia Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
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.
Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
CSC 213 – Large Scale Programming Lecture 14: Sequence-based Priority Queues.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
COMP20010: Algorithms and Imperative Programming Lecture 2 Data structures for binary trees Priority queues.
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.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
CSC 212 – Data Structures.  Fri., Dec. 14 th from 2:45 – 4:45PM in SH1028  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Problem of the Day  At low tide, a ship docks in the harbor  Ship is 9’ above the water line when it docks  Over the side hangs a rope ladder w/ rungs.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
Question of the Day  Two English words change their pronunciation when their first letter is capitalized. What are they?
Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start.
CSC 212 – Data Structures.  Tues., Dec. 10 th from 8:00 – 10:ooAM in OM220  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP.
Priority Queues. Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue.
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.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
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 and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
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.
CSC 213 – Large Scale Programming. Priority Queue ADT  Prioritizes Entry s using their keys  For Entry s with equal priorities, order not specified.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues and Heaps
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,
October 30th – Priority QUeues
Part-D1 Priority Queues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Priority Queue.
Priority Queues © 2014 Goodrich, Tamassia, Goldwasser Priority Queues
Part-D1 Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
B-Tree Insertions, Intro to Heaps
© 2013 Goodrich, Tamassia, Goldwasser
Ch. 8 Priority Queues And Heaps
Priority Queues Sell 100 IBM $ $120 Buy 500 $ $118
Hassan Khosravi / Geoffrey Tien
8/04/2009 Many thanks to David Sun for some of the included slides!
Priority Queues Sell 100 IBM $ $120 Buy 500 $ $118
© 2013 Goodrich, Tamassia, Goldwasser
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
CSE 12 – Basic Data Structures
1 Lecture 10 CS2013.
Priority Queues & Heaps
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

Problem of the Day  At low tide, a ship docks in the harbor  Ship is 9’ above the water line when it docks  Over the side hangs a rope ladder w/ rungs 1’ apart  Tide rises at a rate of 9” per hour After 6 hours what length of ladder will still be above water?

Problem of the Day  At low tide, a ship docks in the harbor  Ship is 9’ above the water line when it docks  Over the side hangs a rope ladder w/ rungs 1’ apart  Tide rises at a rate of 9” per hour After 6 hours what length of ladder will still be above water? 9’ – the ladder will rise with the boat!

CSC 212 – Data Structures

Queue ADT  Creates fair & equal system to handle data in order added  Structure for element processing in order added

Queue ADT  Creates fair & equal system to handle data in order added  Structure for element processing in order added From each according to his ability, to each according to his needs Karl Marx

Deque ADT  Limited inequality when processing elements process either end of structure  Add and remove to process either end of structure  Can’t use middle elements, however

Deque ADT  Limited inequality when processing elements process either end of structure  Add and remove to process either end of structure  Can’t use middle elements, however Four legs good, two legs better From Animal Farm George Orwell

Priority Queue ADT  Priority queue uses strict ordering of data  Values assigned priority when added to the queue completely biased order  Priorities used to process in completely biased order

Priority Queue ADT  Priority queue uses strict ordering of data  Values assigned priority when added to the queue completely biased order  Priorities used to process in completely biased order First you get the sugar, then you get the power, then you get the women

Priority Queue ADT  PriorityQueue yet another Collection  Prioritize each datum contained in the collection  PQ is organized from lowest to highest priority  Access smallest priority only sort of like Queue  min() & removeMin() return priority & value  Implementation not defined: this is still an ADT  Remember that organization & order is theoretical only

 PriorityQueue yet another Collection  Prioritize each datum contained in the collection  PQ is organized from lowest to highest priority  Access smallest priority only sort of like Queue  min() & removeMin() return priority & value  Implementation not defined: this is still an ADT  Remember that organization & order is theoretical only Priority Queue ADT

Entry s in a PriorityQueue  PriorityQueues use Entry to hold data  As with Position, implementations may differ  Entry has 2 items that define how it gets used  PQ will only use key – the priority given to the Entry  Value is important data to be processed by program

P RIORITY Q UEUE Operations D EQUE Q UEUE P RIORITY Q UEUE addFront() addLast() enqueue()insert() getFront() getLast() front()min() removeFront() removeLast() dequeue()removeMin()

Sequence -based Priority Queue

Using Sorted Sequence  Entry s stored in increasing order of priority  insert() checks each index to find where to add  Once found, Position s may shift to make space 5 Sequence

Using Sorted Sequence  Entry s stored in increasing order of priority  insert() checks each index to find where to add  Once found, Position s may shift to make space 5

Sequence Using Sorted Sequence  Entry s stored in increasing order of priority  insert() checks each index to find where to add  Once found, Position s may shift to make space 1 5

Sequence 1 Using Sorted Sequence  Entry s stored in increasing order of priority  insert() checks each index to find where to add  Once found, Position s may shift to make space 5

Sequence 1 Using Sorted Sequence  Entry s stored in increasing order of priority  insert() checks each index to find where to add  Once found, Position s may shift to make space 3 5

Using Sorted Sequence  Entry s stored in increasing order of priority  insert() checks each index to find where to add  Once found, Position s may shift to make space Sequence 1 35

Using Sorted Sequence  Entry s stored in increasing order of priority  insert() checks each index to find where to add  Once found, Position s may shift to make space 9 Sequence 135

Using Sorted Sequence  Entry s stored in increasing order of priority  insert() checks each index to find where to add  Once found, Position s may shift to make space Sequence 1 359

Using Sorted Sequence  Entry s stored in increasing order of priority  insert() checks each index to find where to add  Once found, Position s may shift to make space 0 Sequence 1359

Using Sorted Sequence  Entry s stored in increasing order of priority  insert() checks each index to find where to add  Once found, Position s may shift to make space

Using Sorted Sequence  Entry s stored in increasing order of priority  min() returns 0 th element in the Sequence  Planned well, removeMin() does not need to shift

Using Sorted Sequence  Entry s stored in increasing order of priority  min() returns 0 th element in the Sequence  Planned well, removeMin() does not need to shift

1 Using Sorted Sequence  Entry s stored in increasing order of priority  min() returns 0 th element in the Sequence  Planned well, removeMin() does not need to shift 359

Using Sorted Sequence  Entry s stored in increasing order of priority  min() returns 0 th element in the Sequence  Planned well, removeMin() does not need to shift 59 3

5 Using Sorted Sequence  Entry s stored in increasing order of priority  min() returns 0 th element in the Sequence  Planned well, removeMin() does not need to shift 9

Using Sorted Sequence  Entry s stored in increasing order of priority  min() returns 0 th element in the Sequence  Planned well, removeMin() does not need to shift Sequence 9

Using Sorted Sequence  Entry s stored in increasing order of priority  No shifting ever required if planned well. How?  What is big-Oh complexity of this approach? insert(): removeMin(): min():

Unsorted Sequence s ♥ Clutter  Entry s can be stored in any order  Add Entry to Sequence end during insert()  Guarantees that this is fast with no shifting needed 5 Sequence

Unsorted Sequence s ♥ Clutter  Entry s can be stored in any order  Add Entry to Sequence end during insert()  Guarantees that this is fast with no shifting needed Sequence 5

Unsorted Sequence s ♥ Clutter  Entry s can be stored in any order  Add Entry to Sequence end during insert()  Guarantees that this is fast with no shifting needed Sequence 1 5

Unsorted Sequence s ♥ Clutter  Entry s can be stored in any order  Add Entry to Sequence end during insert()  Guarantees that this is fast with no shifting needed Sequence 5 1

Unsorted Sequence s ♥ Clutter  Entry s can be stored in any order  Add Entry to Sequence end during insert()  Guarantees that this is fast with no shifting needed Sequence 5 3 1

Unsorted Sequence s ♥ Clutter  Entry s can be stored in any order  Add Entry to Sequence end during insert()  Guarantees that this is fast with no shifting needed Sequence 5 13

Unsorted Sequence s ♥ Clutter  Entry s can be stored in any order  Add Entry to Sequence end during insert()  Guarantees that this is fast with no shifting needed Sequence

Unsorted Sequence s ♥ Clutter  Entry s can be stored in any order  Add Entry to Sequence end during insert()  Guarantees that this is fast with no shifting needed Sequence 5 139

Unsorted Does It… Later  Put off work until min() & removeMin()  Scan Sequence to find Entry with smallest priority  Still should not shift when removing from Sequence Sequence 5 139

Unsorted Does It… Later  Put off work until min() & removeMin()  Scan Sequence to find Entry with smallest priority  Still should not shift when removing from Sequence Sequence 5 139

Unsorted Does It… Later  Put off work until min() & removeMin()  Scan Sequence to find Entry with smallest priority  Still should not shift when removing from Sequence Sequence 5 139

Unsorted Does It… Later  Put off work until min() & removeMin()  Scan Sequence to find Entry with smallest priority  Still should not shift when removing from Sequence Sequence 5 139

Unsorted Does It… Later  Put off work until min() & removeMin()  Scan Sequence to find Entry with smallest priority  Still should not shift when removing from Sequence Sequence 5 139

Unsorted Does It… Later  Put off work until min() & removeMin()  Scan Sequence to find Entry with smallest priority  Still should not shift when removing from Sequence Sequence 5 139

Unsorted Does It… Later  Put off work until min() & removeMin()  Scan Sequence to find Entry with smallest priority  Still should not shift when removing from Sequence Sequence 5 39

Unsorted Sequence Complexity  What is big-Oh complexity of this approach? insert(): removeMin(): min():

Why Use Sorted Sequence ?  Cheap to check & remove data with this approach  But it is relatively expensive to add data  When insert() rare relative to calling min()  Good trade-off here, so sorted Sequence worth it

Why Use Sorted Sequence ?  Cheap to check & remove data with this approach  But it is relatively expensive to add data  When insert() rare relative to calling min()  Good trade-off here, so sorted Sequence worth it

Why Use Unsorted Sequence ?  Cheap to add data with this approach  But it is relatively expensive to remove or check it  When would this trade-off work?

Why Use Unsorted Sequence ?  Cheap to add data with this approach  But it is relatively expensive to remove or check it  When would this trade-off work? Examples?

For Next Lecture  Complete your (last) weekly assignment  Read GT 8.3 before next Monday's lecture  Why did we discuss binary trees before this?  Does order really matter?  Reasons using this over Sequence-based approaches?  Programming project #3 due soon