Runtime Review

Do your work in runtimeReview1 or runtimeReview2.
  1. Analyse the ideal worst-case run-time for largest_sum. You should estimate the running time based on n=len(L).
  2. 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
  3. How much time will largest_sum take to run on a list of size 1,000,000?
  4. Can you suggest a better algorithm? One that runs in time O(n log_2(n))?
  5. Can you suggest a better algorithm? One that runs in time O(n)?
  6. 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
  1. 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.
  2. 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.
  3. Build a max-heap containing the keys: 23,17,5,1,5,17,99,2,83,4
  4. Show the max-heap that results from the above, after 3 delete_max() operations
  5. 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.
    1. mh.get_max():
    2. mh.insert(key): add at end, then bubble up
    3. mh.delete_max(): remove top, put last into top, then push down
    4. 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)