Priority Queues (Chapter 9) Flashcards
What is an entry in terms of priority queue? Name the two methods associated with the entries.
An entry is a key-value pair that allows for efficient insertion and removal based on keys.
key() : returns a key for this entry
value(): returns a value for this entry
What is an entry in terms of priority queue? Name the two methods associated with the entries.
An entry is a key-value pair that allows for efficient insertion and removal based on keys.
key() : returns a key for this entry
value(): returns a value for this entry
Describe what total order relations are within the context of PQs
Criteria that a key must meet/comply with in order to be comparable
Name and give a formula for the 3 different total order relations
- Reflexivity: x <= x
- Antisymmetric: if x <=y and y<= x then x = y
3, Transitivity: if x<= y and y <= z then x<= z
Name 3 applications of PQs to the real world
- Stand by flyers (if you were to miss a flight you get attended to based on how important you are to the flight company )
- Auctions (you are prioritized based on the amount of money you are willing to pay for an item)
- Stock Market (the more money you have, the more valuable you become)
What does the comparator ADT do?
It encapsulates the comparison of two objects according to a given total order relation
Describe how the comparator main method compare(x,y) works
it works by returning an integer based on how 2 objects relate to one another. it will return i>0 if x > y, it will return i = 0 if x =y, it will return i < 0 if x < y otherwise give an error if x and y cannot be compared.
What two main methods are associated with PQs?
insert(k,v) and removeMin()
What two main methods are associated with PQs?
insert(k,v) and removeMin()
Discuss the difference in the performance of the two main methods of PQ when implemented with an unsorted list vs a sorted list
Usorted List:
- insert() will be O(1), this is because it is simply inserted without being specifically arranged
- removeMin() will be O(n) in worst time since the whole collection will have to be traversed through in order to be selected as the next minimum in the collection
Discuss the difference in the performance of the two main methods of PQ when implemented with a sorted list
- insert() will be O(n) as the list will need to be traversed through in order to find the correct place to insert the element
- removeMin() will be O(1) since the minimum will be at the beginning
Name and give a brief explanation of the 2 variations of PQ sort. Also, state the runtime of their different phases and the overall runtime
- Selection sort, implemented with an unsorted list
phase 1: O(1)n = O(n)
phase 2: O(n)n = O(n^2)
OVERALL: O(n^2) - Insertion sort implemented with a sorted list
phase 1: O(n)n = O(n^2)
phase 1: O(1)n = O(n)
OVERALL: O(n^2)
What is in-place insertion-sort and how does it work?
It is a way of sorting a PQ without the use of an external data structure like a LL in the case of selection and insertion sort.
It works by keeping a portion of the queue sorted and swapping out of order elements