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: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.
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.
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.
The property you need is this. Suppose that the list of subproblems on the stack has sizes
HINT: I suggest that you DO NOT divide by N. Just use the TOTAL TIME for your time. All that this does is to change your constant C in T(n) = C ne by N. But when you compute the exponent, this factor is automatically removed! The advantage of this hint is that you will not get a time of 0 (which may happen if you take the average, and the number of bits is too small).