Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
17 Cards in this Set
- Front
- Back
what data type do you use when you need to extract things based on a score/weight for each item?
|
a priority queue
|
|
what is the api for a max priority queue?
|
create(), create(capacity), insert(key), max(), delete_max(), is_empty(), size()
|
|
what are some applications of a priority queue?
|
event-driven simulation (do the highest priorty event first), huffman codes, dijkstra's algorithm, prim's algorithm, A*, maintain largest M items,
|
|
how do you find the top M items in a list of N items where N is huge?
|
use a minimum priority queue and while iterating through each of the N items add it to the queue, but whenever the queue exceeds size M pop the smallest off the queue
|
|
what is a binary max heap?
|
a complete binary tree where every element is greater than or equal to its children
|
|
what is the height of a complete binary tree? How do you prove it?
|
1+floor(lg(n)), height only increases when n is a power of 2
|
|
how does the array representation of a binary max heap based priority queue work?
|
the largest element is in a[1], parent of node at k is at floor(k/2)
a variable is used to keep track of the index for the number of elements in the array children of node at k are at 2k and 2k+1 |
|
how is the position where a node can be inserted into a heap kept track of?
|
in the array implementation the location of the node the size of the binary heap so far +1. in a pointer based implementation a queue can be maintained of nodes who can have children added, whenever a node is added it gets added to the queue
|
|
how does insert(value) work?
|
a node is created in the last position of the heap containing the new value and then it is floated until it is smaller than its parent
|
|
how does float(node_id) work?
|
while the value at node_id is greater than its parent swap the values of the parent and node_id
|
|
how does delete_max() work?
|
replace the root of the heap with the last element in the heap and then let the new root value sink down the heap
|
|
how does sink(node_id) work?
|
while node_id's value is smaller than one of its children, swap it with the larger of the children. do this again with the swapped child
|
|
what is the advantage of heapsort over other asymptotically optimal sorts?
|
it can be done in-place i.e. using a small amount of extra memory O(log n)
|
|
what is the idea for an in-place heapsort of ascending items given an arbitrary array?
|
create a max-heap using a bottom-up construction
remove the elements from the heap in-place by swapping them with the last position in the heap (moving this down w/ each removal) |
|
how do you do turn an array into a heap in-place?
|
sink the interior nodes starting from the parent of the last position (N/2) go through the interior nodes bottom-up in level order by decrementing the the current node until the root is reached
|
|
how do you remove the items of a heap in-place so that the items in the array are in order?
|
exchange the root with the element in the last position decrement where the last position is then sink the root, repeat until the last position is the root
|
|
what is the complexity of heapsort?
|
nlogn to make the heap, nlogn to remove all the elements so O(nlogn)
with constant space |