tsort

UN*Xcommand for performing a topological sort on inputdata. Reads pairs of items from standard input, separated by whitespace. As a special case, the pair X X indicates merely that X is present (but does not specify a cycle of length 1!). Other pairs X Y indicate that X must come before Y. Outputs a topologically sorted list of the items, or gives an error message in case of a cycle. When a cycle exists in the input, most implementations seem to "break" it by removing one of its edges and continuing (they still flag this as an error condition, of course).