sec graph traversals ( (bfigure (aa graph (bthe breadth-first search tree for the graph when starting at vertex topological sort may be found by performing dfs on the graph when vertex is visitedno action is taken ( function previsit does nothingwhen the recursion pops back to that vertexfunction postvisit prints the vertex this yields topological sort in reverse order it does not matter where the sort startsas long as all vertices are visited in the end here is an implementation for the dfs-based algorithm static void topsort(graph /recursive topological sort for (int = < () ++/initialize mark array setmark(iunvisited)for (int = < () ++/process all vertices if ( getmark( =unvisitedtophelp(gi)/recursive helper function /topsort helper function static void tophelp(graph gint vg setmark(vvisited)for (int first( ) () next(vw)if ( getmark( =unvisitedtophelp(gw)printout( )/postvisit for vertex using this algorithm starting at and visiting adjacent neighbors in alphabetic ordervertices of the graph in figure are printed out in the order when reversedthis yields the legal topological sort