Runtime Review
Do your work in runtimeReview1 or
runtimeReview2.
- Analyse the ideal worst-case run-time for largest_sum. You should
estimate the running time based on n=len(L).
- Using the approach in the notes,
come up with an expression which captures the actual clock time taken to run largest_sum on lists of size n on my computer.
As a result of running the script, I have the following data...
n | milliseconds
-------+--------------
1000 | 116
10000 | 11745
20000 | 46602
- How much time will largest_sum take to run on a list of size 1,000,000?
- Can you suggest a better algorithm? One that runs in time O(n log_2(n))?
- Can you suggest a better algorithm? One that runs in time O(n)?
- Explain why there is no algorithm that runs in time O(1) and solves this problem.
Heap Review
Do your work in heapReview1 or
heapReview2
- What is the max-heap property. That is, how does the key at a node compare with the keys
at its subtrees? Hint: For a Binary Search tree, left<key<right.
- I want to build a heap to contain 1,000,000 keys. How many levels will that heap have? If you don't
remember the formulae, you can draw a few pictures and figure it out.
- Build a max-heap containing the keys: 23,17,5,1,5,17,99,2,83,4
- Show the max-heap that results from the above, after 3 delete_max() operations
- Turn to your neighbour and explain the algorithms behind each of the following operations on a max-heap. Based on your explanation, write down the running time fo each.
- mh.get_max():
- mh.insert(key): add at end, then bubble up
- mh.delete_max(): remove top, put last into top, then push down
- heap-sort(L):
mh=MaxHeap()
while len(L)>0:
key=L.pop()
mh.insert(key)
while mh._size>0:
key=mh.delete_max()
L.append(key)