Exam 2 Review Problems:


Heap Problems:

RH1) The following elements are a min-heap except for the root-element, which violates the heap-condition:
s e b p o d t
Carryout siftdow() on the root, show the resulting help. Count how many key-comparisons are made.


RH2) Here are the elements of a max-heap, listed in the order they're stored in an array:
w t r o b c n k h
Change the value of the 2nd element from "t" to "c", and call siftdown() on the sub-tree rooted at that 2nd element. Show the result. Is it really a valid max-heap? How many comparisons were done when siftdown() was called?


RH3) Insert the element with value "s" into the max-heap you had at the end of the last problem. Show the result. How many comparisons were made?

RH4) Give an array with these values:
c o m p l e x i t y
Show how this is made into a max-heap using the method that calls siftdown() repeatedly. How many comparisons were made in total and by each call to siftdown()?

RH5) Now that you've created a heap for the array above, you could use heapsort to create an array sorted in ascending order. Start this process, but stop after two iterations, i.e. after the largest and 2nd largest elements are placed at the end of the array. Show the contents of the array at that point.


Graph Problems:

For the following problems, this the example graph to use:

RG1) Carry out Kruskal's MST algorithm on the above graph. List edges in the final spanning tree in the order they were selected, and give the total weight of the tree.

RG2) Carry out Prim's MST algorithm on the above graph. List the edges in the order they were selected. Also, when a candidate edge that's stored in the priority queue is replaced with a better candidate edge, list the replaced edge and what replaces it.

RG3) Carry out Dijkstra's shortest path algorithm on the above graph. List the edges in the order they were selected. Also, when a candidate edge that's stored in the priority queue is replaced with a better candidate edge, list the replaced edge and what replaces it.

RG4) In Prim's MST algorithm, give a list of what the priority queue operations are, what they do, when they occur.

RG5) For each of the above priority queue operations, how often can they occur in the worst-case for some graph G?


Greedy Algorithms:

RG6) State the greedy selection rule (aka the selection function) for these algorithms:

  1. The fractional knapsack algorithm that produces an optimal solution.
  2. The change-making algorithm.
  3. Kruskal's MST algorithm
  4. Prim's MST algorithm
  5. Dijkstra's shortest path algorithm.

RG7) What in Prim's MST algorithm prevents the algorithm from choosing an infeasible solution?