Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

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

3 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

4 Priorities What types could we use for priorities?

5 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!

6 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 “ =”, …

7 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

8 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); }

9 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

10 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(); }

11 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

12 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

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

14 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

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

16 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 =

17 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 =

18 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 2 65 79

19 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 2 65 79 Levels above lowest level must be full

20 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 2 65 79 Lowest level must be filled from left to right

21 Heap Properties Consider daily quiz due today TeamsGames Champ WinsTotal Games 1 2 4 8 16 n

22 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?

23 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

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

25 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

26 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()?

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

28 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

29 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

30 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()?

31 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?


Download ppt "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."

Similar presentations


Ads by Google