(Latest Revision: Tue Oct 31 22:28:41 PST 2000 )
ALGORITHM FOR A DEPTH-FIRST TRAVERSAL PROCEDURE VisitComponentOf (startNode : node) ; BEGIN change the status of startNode to "ready" ; push startNode onto the ready stack ; WHILE the ready stack is not empty DO BEGIN Pop a node X from the ready stack ; process(X) ; FOR each neighbor Y of X DO BEGIN IF the status of Y is "waiting" THEN BEGIN change the status of Y to "ready" ; Push Y onto the ready stack END ELSE IF Y is in the ready stack THEN move Y to the top of the ready stack END END END ;(* Above, we could avoid the work of removing duplicate copies from the "stack" by marking a node "closed" after processing, and checking each node popped from the stack to see if it is "closed" before processing. If it is marked "closed", the algorithm would not process it, because having that mark means it was processed earlier at a greater or equal depth. The cost of this would be that we could have up to O(N^2) objects on the stack, where N is the number of nodes in the graph. *)
PROCEDURE DepthFirstTraversal (G : graph) ; BEGIN Set the status of each node of G to "waiting" ; FOR each node N of G DO IF the status of N is waiting THEN VisitComponentOf(N) END ;