Topological Sort

A topological sort of an acyclic directed multigraph (V,E) is an ordering v1, v2, ..., vN of the N members of V such that for each e in E, src(e) = vi and snk(e) = vj for i < j. After a topological sort, the source vertex of each edge occurs earlier in the ordering than the sink vertex. That is, if you pick any two vertices vi and vj from the topological sort, then either vi and vj are independent or vi is a predecessor of vj.

The graph in Figure 3, which is the subgraph of consisting of {v1, v3, v4, v6} in Figure 2, has two distinct topological sorts given by {v1,v3, v4, v6} and {v1, v3, v6, v4}. These sorts are shown in Figure 4. In Figure 4, notice that in a topological sort, one can put the vertices in a row from left to right and connect the vertices with arcs.

 ----              ----              ---- 
| v  | ---------> | v  | ---------> | v  |
|  1 |            |  3 |            |  6 |
 ----              ----              ---- 
                    |                     
                    |                     
                    |                     
                    v                     
                   ----                   
                  | v  |                  
                  |  4 |                  
                   ----                   
Figure 3: An acyclic directed multigraph.
                  ---------------------------- 
                 |                            |
                 |                            v
 ----           ----           ----          ----
| v  | ------> | v  | ------> | v  |        | v  |
|  1 |         |  3 |         |  6 |        |  4 |
 ----           ----           ----          ----

                  ---------------------------- 
                 |                            |
                 |                            v
 ----           ----           ----          ----
| v  | ------> | v  | ------> | v  |        | v  |
|  1 |         |  3 |         |  4 |        |  6 |
 ----           ----           ----          ----

Figure 4: Two possible topological sorts for Figure 3.


Updated 02/25/99.