HINTS FOR HW 5


QUESTION 1. Correctness of Topological Sort on p.205.
Here is one method of proof. The first thing to do is to give a FORMAL statement of what it means to be CORRECT.

Here it is: let G be a DAG on n nodes. If there is an edge from node u to node v, we call v a "successor" of u, and call u a "predescessor" of v. Calling TopSort(G) will print a list of all the nodes in G.

	CORRECTNESS of TopSort(G) MEANS THIS:

If L = (u1, u2, ..., un) is the list of nodes printed by TopSort(G), then (P1) Every node in G appears in the list L (P2) If ui is a successor of uj, then i< j.
Now, property (P1) is obvious from the algorithm (why?). To prove property (P2), let us use induction on i to show that when ui is printed, then all its predecessors have been printed.

BASE CASE: Show that u1 is printed correctly.
INDUCTION: Show that if i>1, then ui is printed correctly.

We need some properties about TopHelp(G,v): Consider the moment just before v is printed. Since TopHelp(G,v) is a recursive algorithm, there is a recursive stack at this moment. Question: is it true that every node that is VISITED is already printed? HINT: think about this stack. Your argument should make use of the fact that the graph G is actually a DAG.

QUESTION 6: QUICKSORT
Basically, we want to make sure that after we partition the problem into two, we push the 2 subproblems onto the stack in such a way that the BIGGER subproblem is pushed first.

The property you need is this. Suppose that the list of subproblems on the stack has sizes

s1, s2, s3, ... sk.
I.e., there are k subproblems on the stack, with s1 at the bottom of the stack and with sk at the top of the stack. Then
(P3)         s1 >= s2 >= s3 >= ... >= sk,
and for i=2,...,k-1, we have
(P4)         si-1 + si-1 <= si-1.
Prove properties (P3) and (P4). Conclude from this that the stack has depth at most 2 lg n. [Note: actually, this implies a better bound of logc n where c=1.618... (the Golden Ratio).

PROGRAMMING QUESTION.