Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 213 – Large Scale Programming Lecture 14: Sequence-based Priority Queues.

Similar presentations


Presentation on theme: "CSC 213 – Large Scale Programming Lecture 14: Sequence-based Priority Queues."— Presentation transcript:

1 CSC 213 – Large Scale Programming Lecture 14: Sequence-based Priority Queues

2 Today’s Goal Discuss Sequence-based implementations Two different possible implementations How these algorithms would be performed Effects on big-Oh notation Discuss two new methods of sorting data Based upon the PriorityQueue implementations How they work, what the trade-offs are

3 Priority Queue ADT Priority queue another Collection Orders Entrys by their keys (lowest to highest) Within equal priorities, order is not required Can only access item with lowest priority Get entire Entry via min() or removeMin() Elements “inserted” at priorities’ location Only access single item, so these positions are theoretical How data actually stored is implementation-specific

4 Sequence-based Priority Queue Simplest implementation of a Priority Queue Entrys stored using a instance of Sequence Sequence has many possible implementations But that is an issue for CSC212, not here We simply assume O(1) access times Sequence holds data, but how to implement priorities? Maintain data in the Sequence in sorted order Leave data unsorted and instead find smallest priority whenever necessary

5 Implementation with a sorted list insert() takes ___ time removeMin() & min() take ____ time Using a Sorted Sequence Sequence > s; Comparator > c; private void addEntry(Entry e){ if (s.isEmpty()) { s.insertFirst(e); } else if (c.compare(e, s.last().element()>0){ s.insertLast(e); } else { Position > p; p = s.first(); while(c.compare(e, p) > 0){ p = s.next(p); } s.addBefore(e); } } 1 Sequence > s; Comparator > c; private Entry min(){ // Check for exception return s.first(); } private Entry removeMin(){ // Check for exception size -= 1; return s.remove(s.first()); } 5 5555 3 33 2 2 4 4

6 Implementation with a sorted list insert() takes ___ time removeMin() & min() take ____ time Using an Unsorted Sequence Sequence > s; Comparator > c; private void addEntry(Entry e){ s.insertLast(e); } But min & removeMin search through the Entrys in the Sequence to find the one with the lowest priority! 15234

7 Implementation Differences Both implementations are good… …best yet (until Wednesday’s lecture) But how will the Priority Queue be used? Calls to a Call Center & Reverse Auctions with lots of adding data, little removing or looking up use unsorted Sequence-based approach Lines to Board a Plane & CPU scheduling with little adding data, but lots of removals or look ups use sorted Sequence-based approach

8 Selection-Sort Uses unsorted Sequence-based priority queue Goal is to sort data, data are the priorities Just have null for all values since not used Running time of Selection-sort: n insert calls * O(1) per call = O( n ) time n removeMin calls * O( n ) per call = O( n 2 ) time Total time for this sort is in O( n 2 )

9 Selection-Sort Example Sequence S PriorityQueue P Input:(7,4,8,2,5,3,9)() Transfer Step(4,8,2,5,3,9)(7) (8,2,5,3,9)(7,4)...… ()(7,4,8,2,5,3,9) Sorting Step(2)(7,4,8,5,3,9) (2,3)(7,4,8,5,9) (2,3,4)(7,8,5,9)… (2,3,4,5,7,8)(9) (2,3,4,5,7,8,9)()

10 Insertion-Sort Uses sorted Sequence-based priority queue Otherwise identical to Selection-sort Running time of Insertion-sort: n insert operations * O( n ) per call = O( n 2 ) time n removeMin calls * O(1) per call = O( n ) time Total time for this sort is also O( n 2 ) Same as before, only ordering of steps is different

11 Insertion-Sort Example Sequence S PriorityQueue P Input:(7,4,8,2,5,3,9)() Sorting Step(4,8,2,5,3,9)(7) (8,2,5,3,9)(4,7) (2,5,3,9)(4,7,8) (5,3,9)(2,4,7,8)...… ()(2,3,4,5,7,8,9) Transfer Step(2)(3,4,5,7,8,9) (2,3)(4,5,7,8,9)… (2,3,4,5,7,8,9)()

12 In the Next Lecture Discuss first of two uses of the term Heap Alternate implementation of a Priority Queue Uses binary trees for possibly better performance Also defines a sort that is definitely better Gives you license to say “upheap” and “downheap” as much as you want


Download ppt "CSC 213 – Large Scale Programming Lecture 14: Sequence-based Priority Queues."

Similar presentations


Ads by Google