Solutions to 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 siftdown() on the root, show the resulting help. Count how many key-comparisons are made.

Solution: b e d p o s t
4 comparisons were 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?

Solution: w o r k b e n c h
Yes, it is a valid max-heap. 4 comparisons were made.


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?

Solution: w s r k o e n c h b
3 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()?

Solution: Calls to siftdown() are made on the 5th element, then the 4th,..., then finally the root (the 1st element in the array). For these calls, the number of comparisons made was: 1, 2, 2, 3, and 6, or a total of 14 comparisons. The result max-heap is:
y t x p o e m i c l


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.

Solution: t p m i o e l c x y
Note: x and y at the end of the array are sorted, and the first 8 elements are a valid max-heap.


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.

Solution: If the edges are sorted as follows:
wt = 1: AB BC DF EG
wt = 2: AF BD DE EF
wt = 4: BE CE
wt = 6: EG
then the order the edges are selected is: AB BC DF EG AF DE
The MST has weight 8 and is shown below:


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.

Solution: The edges are added to the tree in this order:
AB, BC, BD (or AF could be chosen here), DF, DE, EG
Replacements: DF replaces AF; DE replaces BE; EG replaces AE.
The MST is shown below:


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.

Solution: The edges are added in this order: AB, BC, BD, AF, FE, EG
Replacements: FE replaces DE; EG replaces AG
The tree showing the shortest path to each node is show below:


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

Solution: They are:

  1. Insert a new item, the new fringe node when it is first encountered. The key-value stored is the weight of the candidate edge connecting the node to the tree.
  2. Decrease-Key: if a node is stored in the priority queue and we later find a 2nd candidate edge with a better weight, replace the old edge with the new one and its lower edge-weight.
  3. Extract-Min: Remove the lowest-value candidate edge to make the tree grow one edge larger.


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

Solution:

  1. This always happen exactly n times, where n is the number of nodes.
  2. If the graph is a complete graph, then the ith node added to the tree will have be adjacent to the n-i nodes not yet in the tree. It is possible to construct a worst-case input where each of these nodes leads to a candidate edge replacement. For the first node selected, its n-1 neighbors are inserted in the priority queue. Then for the second node, its n-2 neighbors could lead to replacements of candidate edges. Then the 3rd node leads to n-3 replacements, etc. So the total is the sum (n-2)+(n-3)+...+1 =
    sum{i=2 to n}(n-i) =
    n(n-1) - 0.5(n)(n+1) + 1 =
    (0.5)n^2 - (1.5)n + 1
  3. This always happens exactly n times (assuming the graph is connected).



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.

Solution:

  1. From the items that have not been chosen yet, choose as much of possible of the item that has the largest value/weight ratio.
  2. If the remaining amount of change is X, consider the largest remaining coin-value and take as many of those as possible (that don't sum to more than X).
  3. After sorting edges in ascending order by weight, choose the next edge unless it connects two existing trees in the forest that is being created.
  4. From the fringe nodes, choose the one with the smallest candidate-edge weight, i.e. the weight of the one edge that connects it to the tree.
  5. From the fringe nodes, choose the one with the smallest total distance back to the starting node.


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

Solution: Once a node is selected to be added the tree, we can never choose a candidate edge to that node because it has been added to the tree and will never be added back into the priority queue. (Only fringe nodes can be added to the priority queue.)