quotes “from each according to his ability, to each according to his needs” -- karl marx/queue...

31
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

Upload: christopher-russell-smith

Post on 18-Jan-2018

220 views

Category:

Documents


0 download

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

TRANSCRIPT

Page 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

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

Page 2: 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

Announcements

Homework #3 due today before labBlackboard is behaving oddly

Submit via Novell or e-mail me your filesSame goes for daily quizzes

Page 3: 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

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

Page 4: 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

Priorities

What types could we use for priorities?

Page 5: 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

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!

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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

}}

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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

Page 12: 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

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

Page 13: 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

What are these complexities?

Unsorted Sequence-based Insert –Remove –

Sorted Sequence-based Insert –Remove --

Page 14: 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

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

Page 15: 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

Sorting using Priority Queue

Insert each item into priority queueCall removeMin to retrieve all items in sorted

order

Page 16: 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

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 =

Page 17: 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

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 =

Page 18: 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

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

Page 19: 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

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

Page 20: 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

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

Page 21: 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

Heap Properties

Consider daily quiz due today

Teams Games Champ Wins Total Games124816n

Page 22: 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

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?

Page 23: 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

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

Page 24: 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

Problem

Algorithm assumes we know the first empty nodeHow can we find it before an insertion?

Page 25: 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

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

Page 26: 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

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

Page 27: 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

Removing a Node From Heap

Remove node with minimum keyWhere would we find this node?How can we be sure?

Page 28: 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

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

Page 29: 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

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

Page 30: 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

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

Page 31: 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

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?