Computer Algorithms: Topological Sort Revisited




Topological Sort



Topological Sort - step 1

We put them in our sorted list in random order. Since they don’t depend on each other we can assume they are equally sorted already. Indeed thinking of a task schedule if there are tasks that don’t have predecessors (they don’t depend on other tasks before them) and that don’t depend on each other we can put them in random order (and execute them in random order).

Topological Sort - Order

Once we have the vertices with no predecessors we must remove the edges starting from them. Then – go again with the vertices with no predecessors.

Topological Sort - step 2

It’s as simple as that, so why do we need a revision of this algorithm? Well, basically because of its efficiency.


As we know most of the graph algorithms depend on the way the graph is represented in our application. We consider as the two main representations the adjacency matrix …

Adjacency Matrix

… and adjacency lists.

Adjacency Lists

Let’s first take a look of some of the main approaches to get the topologically sorted list at the end of the algorithm.

What can we do in order to find the vertices with no predecessors? We can only scan the entire list of vertices.

Adjacency Matrix

In case we’re using adjacency matrix we need|V|^2 space to store the graph. To find the vertices with no predecessors we have to scan the entire graph, which will cost us O(|V|^2) time. And we’ll have to do that |V| times. This will be |V|^3 time consuming algorithm and for dense graphs this will be quite an ineffective algorithm.

Adjacency Lists

What about the adjacency list? There we need |E| space to store a directed graph. How fast can we find a node with no predecessor? Practically we’ll need O(|E|) time. Thus in the worst case we have again O(|V|^2) time consuming programs.

So what can be done in order to optimize this algorithm?

Practically we can start by picking up a random vertex and “go back” until we get a node with no predecessors. This approach can be very effective yet also very ineffective. First of all if we have to scan all the way back to a node with no predecessors this will cost us |V| time, but if we stuck on a node that don’t have a preceding node then we’ll have a constant speed.

This means that we can modify the algorithm a bit in order to improve a lot the algorithm. We just need to store both incoming and outgoing edges and slightly modify the adjacency lists.

Modified Adjacency Lists

What’s the algorithm now?

First we easily find the nodes with no predecessors. Then, using a queue, we can keep the nodes with no predecessors and on each dequeue we can remove the edges from the node to all other nodes.

Pseudo Code

1. Represent the graph with two lists on each vertex (incoming edges and outgoing edges)
2. Make an empty queue Q;
3. Make an empty topologically sorted list T;
4. Push all items with no predecessors in Q;
5. While Q is not empty
   a. Dequeue from Q into u;
   b. Push u in T;
   c. Remove all outgoing edges from u;
6. Return T;

This approach will give us a better performance than the “brute force” approach. The running time complexity is O(|V| + |E|). The problem is that we need additional space and an operational queue, but this approach is a perfect example of how by using additional space you can get a better performing algorithm.

One thought on “Computer Algorithms: Topological Sort Revisited

  1. Hi Stoimen – I ran across your article on the Boyer-Moore algorithm, and thought that you may be interested in my version of the pathology of it, in Java. I programme a Java instant messenger and voip application, and this Boyer-Moore was something I was just looking at recently. I am pasting the source code here, but it is also available on my website. If you had any comments, I’d be interested to hear. Thanks, George.

    public class Boyer_Moore_Bonfield {

    static String result = null;


    static String needle = “”;

    public static void main(String[] args){

    java.util.Scanner input = new java.util.Scanner(;


    System.out.println(“\nEnter a word or string to search for . . . \n”);

    needle = input.nextLine().toUpperCase();

    System.out.println(search_for(needle, haystack)+”\n”);


    static String search_for(String needl, String haystac){

    int lenNeedle = needl.length();
    int lenHaystack = haystac.length();
    int needlePointer;
    int haystackPointer = lenNeedle-1;
    int flexHaystackPointer = haystackPointer;
    boolean matched = false;

    while (haystackPointer < lenHaystack){

    needlePointer = lenNeedle-1;
    flexHaystackPointer = haystackPointer;


    //System.out.println("needlePointer is "+needlePointer+" flexHaystackPointer is "+flexHaystackPointer);//DEBUG




    if( (needl.lastIndexOf(haystac.charAt(flexHaystackPointer)) == -1) ){

    //the char is not in the pattern at all
    // so move the pattern right by the length of the pattern
    haystackPointer += needl.length();

    haystackPointer += needl.length()-needl.lastIndexOf(haystac.charAt(flexHaystackPointer))-1;


    return("Found : "+haystac.substring(flexHaystackPointer,flexHaystackPointer+lenNeedle));
    return("No match");

Leave a Reply
