steps and are repeated until all the vertices are gone at this pointthe list shows the vertices arranged in topological order you can see the process at work by using the graphd applet construct the graph of figure (or any other graphif you preferby double-clicking to make vertices and dragging to make edges then repeatedly click the topo button as each vertex is removedits label is placed at the beginning of the list below the graph deleting vertex may seem like drastic stepbut it' the heart of the algorithm the algorithm can' figure out the second vertex to remove until the first vertex is gone if you need toyou can save the graph' data (the vertex list and the adjacency matrixelsewhere and restore it when the sort is completedas we do in the graphd applet the algorithm works because if vertex has no successorsit must be the last one in the topological ordering as soon as it' removedone of the remaining vertices must have no successorsso it will be the next-to-last one in the orderingand so on the topological sorting algorithm works on unconnected graphs as well as connected graphs this models the situation in which you have two unrelated goalssuch as getting degree in mathematics and at the same time obtaining certificate in first aid cycles and trees one kind of graph the topological-sort algorithm cannot handle is graph with cycles what' cycleit' path that ends up where it started in figure the path - -db forms cycle (notice that - - - is not cycle because you can' go from to figure graph with cycle cycle models the catch- situation (which some students claim to have actually encountered at certain institutions)where course is prerequisite for course cc is prerequisite for dand is prerequisite for graph with no cycles is called tree the binary and multiway trees we saw earlier in this book are trees in this sense howeverthe trees that arise in graphs are more general than binary and multiway treeswhich have fixed maximum number of child nodes in grapha vertex in tree can be connected to any number of other verticesprovided that no cycles are created topological sort is carried out on directed graph with no cycles such graph is called directedacyclic graphoften abbreviated dag java code here' the java code for the topo(methodwhich carries out the topological sortpublic void topo(/toplogical sort