20-CS-4003-001 Organization of Programming Languages Fall 2017

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures           Code     Demo

Topological Sort


A factory which produces complex machinery on a single assembly line may need to take some care in planning which subassemblies or components are completed at what point on the line1. For, if a component type is scheduled for completion after it is supposed to be installed, then a problem will certainly exist. The problem is compounded by the fact that duplicates of the same component type may be used many times in assembling other components which themselves may be used many times and so on.

We wish to find an order of assembly of components on a single production line so that no component requires a subassembly that has not been built earlier in the line. We again turn to graphs to help devise a solution. Use a vertex (a circle on a piece of paper) to represent a component type. If one component type, call it A, requires another, say B, then draw a line between the two circles representing type A and B with an arrowhead pointing in the direction of the B vertex. The arrow tells us that we cannot build A components before we build B components. For example, in the following picture

no component can be built before E components are, A components must wait until D and G components, and so on. If the component assemblies are scheduled in the following order

     E, G, H, B, F, D, A, C, I, J
then all the components can be assembled successfully. By the way, a graph with arrows as above is referred to as a partial ordering of elements (in this case component types A through J) and an ordering such as the one above, where all dependencies are satisfied, is called a topological sort (in this case, of components A through J). If the graph were such that for some vertex you could visit one or more other vertices by moving in the direction of the arrows, and then return to the vertex at which you started, then the graph is not a partial ordering and a topological sort is impossible.

Topological Sort Problem:

Given a collection of n items and dependency lists for each item, determine whether the dependencies support a partial ordering. If not, report this fact. Otherwise, find a topological sort of the vertices with respect to the given dependency lists.


input: A graph of vertices and lines with arrowheads in one direction
output: "Impossible" or a topological sort of the vertices

    0. Create objects for each vertex
    1. Set the state of each object to START
    2. Install all the dependency lists for all vertices
    3. For all vertices v execute topo(v) given below
       (imagine there is one copy of that code for each vertex)

input: A vertex v - invoked as topo(v)

    1. Check whether v is in the DONE state
    2. If so, return
    3. Check whether v is in the ERROR state
    4. If so, print "Impossible" and exit
    5. Change v's state to ERROR
    6. For all the vertices w in the dependency list of v do the following:
    6a.   Invoke topo(w)
    7. Change v's state to DONE
    8. Print "v"
    9. Return