Topological Sorting in Python (original) (raw)

Last Updated : 6 Nov, 2025

Given a Directed Acyclic Graph (DAG), the task is to find a linear order of its vertices such that for every directed edge u -> v, vertex u appears before v in the order. Topological Sorting is a technique used to arrange vertices of a DAG in a sequence that respects all dependencies. It ensures that no vertex appears before any of its prerequisites.

**For Example:

**Input Graph:

topologicalSorting

**Output: 5 4 2 3 1 0
**Explanation: Vertex 5 and 4 have no incoming edges, so they appear first. Each vertex appears only after all vertices pointing to it have been placed earlier in the order.

Using Kahn’s Algorithm (BFS-Based)

It works by repeatedly removing nodes with zero in-degree (no incoming edges) and adding them to the topological order until all nodes are processed. Here in this code, we use a queue to store vertices with zero in-degree and gradually build the order.

Python `

from collections import defaultdict, deque

class Graph: def init(self, vertices): self.graph = defaultdict(list) self.V = vertices

def addEdge(self, u, v):
    self.graph[u].append(v)

# Function to perform Kahn's Algorithm
def topologicalSort(self):
    in_degree = [0] * self.V  # Count of incoming edges for each vertex

    # Calculate in-degree for each vertex
    for u in self.graph:
        for v in self.graph[u]:
            in_degree[v] += 1

    # Queue for vertices with 0 in-degree
    queue = deque([i for i in range(self.V) if in_degree[i] == 0])
    topo_order = []

    while queue:
        u = queue.popleft()
        topo_order.append(u)

        # Decrease in-degree for adjacent vertices
        for v in self.graph[u]:
            in_degree[v] -= 1
            if in_degree[v] == 0:
                queue.append(v)

    print(topo_order)

g = Graph(6) g.addEdge(5, 2) g.addEdge(5, 0) g.addEdge(4, 0) g.addEdge(4, 1) g.addEdge(2, 3) g.addEdge(3, 1)

g.topologicalSort()

`

**Explanation:

Using Recursive DFS-Based Approach

This approach uses Depth-First Search (DFS) and recursion to find the topological order of a Directed Acyclic Graph (DAG). It explores all dependencies first and then adds the node to a stack, ensuring the correct order. The final sequence is obtained by reversing the stack.

Python `

from collections import defaultdict

class Graph: def init(self, vertices): self.graph = defaultdict(list) self.V = vertices

def addEdge(self, u, v):
    self.graph[u].append(v)

def dfsSort(self, v, visited, stack):
    visited[v] = True
    for i in self.graph[v]:
        if not visited[i]:
            self.dfsSort(i, visited, stack)
    stack.insert(0, v)  # Add to front after exploring

def topologicalSort(self):
    visited = [False] * self.V
    stack = []

    for i in range(self.V):
        if not visited[i]:
            self.dfsSort(i, visited, stack)

    print(stack)

g = Graph(6) g.addEdge(5, 2) g.addEdge(5, 0) g.addEdge(4, 0) g.addEdge(4, 1) g.addEdge(2, 3) g.addEdge(3, 1)

g.topologicalSort()

`

**Explanation:

Using Iterative DFS-Based Approach

This method eliminates recursion and uses an explicit stack to simulate DFS traversal. It employs generators for efficient neighbor iteration and manually manages backtracking. This approach is useful for handling large graphs without hitting recursion limits.

Python `

from collections import defaultdict

class Graph: def init(self, vertices): self.graph = defaultdict(list) self.V = vertices

def addEdge(self, u, v):
    self.graph[u].append(v)

def topologicalSort(self):
    visited = [False] * self.V
    stack = []
    working_stack = []

    for i in range(self.V):
        if not visited[i]:
            working_stack.append((i, iter(self.graph[i])))
            while working_stack:
                v, gen = working_stack[-1]
                visited[v] = True
                try:
                    next_v = next(gen)
                    if not visited[next_v]:
                        working_stack.append((next_v, iter(self.graph[next_v])))
                except StopIteration:
                    working_stack.pop()
                    stack.insert(0, v)

    print(stack)

g = Graph(6) g.addEdge(5, 2) g.addEdge(5, 0) g.addEdge(4, 0) g.addEdge(4, 1) g.addEdge(2, 3) g.addEdge(3, 1)

print("Topological Sort using Iterative DFS:") g.topologicalSort()

`

Output

Topological Sort using Iterative DFS: [5, 4, 2, 3, 1, 0]

**Explanation:

Please refer complete article on Topological Sorting for more details.