Upload
christopher-russell-smith
View
220
Download
0
Embed Size (px)
DESCRIPTION
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
Citation preview
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 labBlackboard is behaving oddly
Submit via Novell or e-mail me your filesSame goes for daily quizzes
Queue v. Priority Queue
Queues --- nice, simple, & fairRepresent first-come, first-served conceptAssume all elements are equal and patientHave NO basis in reality
When was the last time you were on a plane? Priority Queues -- biased & undemocratic
Entries are ranked in orderPrefer items with higher priorityDoes not define order for similar priorities
Priorities
What types could we use for priorities?
Priorities
Comparing priorities can be very difficult:NFL Tie breaking procedure1. 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-breakerComparing 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 timeHard to change definition of “<”, “>=”, …
Comparator ADT
Comparator encapsulates comparing two objects (a & b) for a total order relationa must either be less than, equal to, or greater than b
Comparator is external to keys comparedEncapsulation principle says separate ideas should
remain separate Changing comparator enables reweighting
relative priorities
Comparator ADT
Defines single methodcompare(x, y): Returns integer < 0 if a < b,
0 if a = b, and 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 elementsElements defined by only one data itemPosition ADT defines only Object element()
Position Queue contains entriesEntries 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 Sequence1. 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 identicalCall removeMin to return entries in order
Define two different sortsSelection Sort Insertion Sort
Sorting using Priority Queue
Insert each item into priority queueCall 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 sortn insertions * time = timen removeMins * time = timeTotal time =
Insertion Sort Complexity
Selection sort uses unordered Sequence-based implementation
Time Complexity to sort n items by selection sortn insertions * time = timen removeMins * time = timeTotal 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 complete2
65
79
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 complete2
65
79
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 complete2
65
79Lowest level must befilled from left to right
Heap Properties
Consider daily quiz due today
Teams Games Champ Wins Total Games124816n
Who Cares About Heaps?
Can implement a priority queues with heapCan implement heap like any binary tree
Keep minimum entry at rootMakes 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 propertySwaps 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 nodeHow can we find it before an insertion?
Last Node in Heap
Start at the last NodeGo up until a left child or the root is reached If a left child is reached, go to the right childKeep 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 keyWhere 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 sortLike earlier sorts, but uses heap-based
implementation of priority queueTrace 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?