Topological Sort Algorithm

The topological sort algorithm is used to order the vertices of a directed graph in such a way that for every directed edge 𝑒 β†’ 𝑣, vertex 𝑒 comes before vertex 𝑣 in the ordering. This algorithm is commonly used in various applications like scheduling, task management, and dependency resolution.

Understanding Topological Sort

To understand the topological sort algorithm, let’s consider a directed graph represented as a set of vertices and edges. Each vertex represents a task, and the directed edges represent dependencies between tasks.

Directed Graph

The steps involved in the topological sort algorithm are as follows:

  1. Identify a vertex with no incoming edges (in-degree equals 0) and add it to the result.
  2. Remove the identified vertex and its outgoing edges from the graph.
  3. Repeat steps 1 and 2 until all vertices have been identified and added to the result.

Example of Topological Sort

Let’s consider a simple example to demonstrate the topological sort algorithm. Assume we have a set of tasks and their dependencies represented in a directed graph:

Tasks: A, B, C, D, E, F
Dependencies: A β†’ B, A β†’ C, B β†’ D, C β†’ E, D β†’ F, E β†’ F

The topological sort algorithm will produce the following ordering:

A β†’ C β†’ E β†’ B β†’ D β†’ F

In this ordering, each task is positioned after its dependencies. For example, task A is positioned before tasks B and C, as they directly depend on it.

Implementing Topological Sort Algorithm

To implement the topological sort algorithm in a programming language like Python, we can use a depth-first search (DFS) algorithm. Here is an example implementation using Python:

from collections import defaultdict

def topologicalSort(graph):
    visited = set()
    stack = []

    def dfs(node):
        nonlocal visited
        
        visited.add(node)

        for neighbor in graph[node]:
            if neighbor not in visited:
                dfs(neighbor)

        stack.append(node)

    for node in graph:
        if node not in visited:
            dfs(node)

    return stack[::-1]

In this implementation, we first create a graph using an adjacency list representation, where each vertex is mapped to its outgoing edges. Then, we perform a depth-first search starting from each unvisited vertex. The visited vertices are stored in a stack, and the final ordering is obtained by reversing the stack.

Conclusion

The topological sort algorithm is an important algorithm used in various applications where ordering based on dependencies is required. By following the steps of the algorithm, we can obtain a valid and consistent ordering of the vertices in a directed graph.