π§© Python DSA Patterns β Topological Sorting ππ
Posted on: January 2, 2026
Description:
A Directed Acyclic Graph (DAG) is a graph with directed edges and no cycles. Topological Sorting produces a linear ordering of nodes such that every directed edge u β v means u comes before v. This pattern is fundamental for dependency resolution, task scheduling, and build systems.
β¨ When to Use
- When tasks have dependencies (one must finish before another starts).
- When you need a valid execution order.
- When the graph is directed and must be acyclic.
β‘ Two Common Approaches
1οΈβ£ Kahnβs Algorithm (BFS-based)
- Uses in-degree counting.
- Start with nodes having 0 in-degree.
- Remove them level by level.
2οΈβ£ DFS-based Topological Sort
- Uses postorder DFS.
- Push nodes to stack after visiting all neighbors.
π Example 1 β Kahnβs Algorithm (BFS / In-Degree Method)
Repeatedly process nodes with no incoming edges.
from collections import deque, defaultdict
def topo_sort_bfs(n, edges):
graph = defaultdict(list)
indegree = [0] * n
for u, v in edges:
graph[u].append(v)
indegree[v] += 1
queue = deque([i for i in range(n) if indegree[i] == 0])
order = []
while queue:
node = queue.popleft()
order.append(node)
for nei in graph[node]:
indegree[nei] -= 1
if indegree[nei] == 0:
queue.append(nei)
print("Topological Order (BFS):", order)
topo_sort_bfs(6, [(5,2),(5,0),(4,0),(4,1),(2,3),(3,1)])
π Example 2 β DFS-Based Topological Sort
Push nodes to stack after exploring all outgoing edges.
from collections import defaultdict
def topo_sort_dfs(n, edges):
graph = defaultdict(list)
visited = [False] * n
stack = []
for u, v in edges:
graph[u].append(v)
def dfs(node):
visited[node] = True
for nei in graph[node]:
if not visited[nei]:
dfs(nei)
stack.append(node)
for i in range(n):
if not visited[i]:
dfs(i)
print("Topological Order (DFS):", stack[::-1])
topo_sort_dfs(6, [(5,2),(5,0),(4,0),(4,1),(2,3),(3,1)])
π¨ Cycle Detection (Important)
- Topological sort exists ONLY if the graph is acyclic.
- In Kahnβs algorithm:
- If processed nodes < total nodes β cycle exists.
π§ Where It Helps
- ποΈ Build systems & compilation order
- π Course scheduling (prerequisites)
- π Task dependency resolution
- π Workflow orchestration
- π§© Deadlock detection (indirectly)
β Common Mistakes
- Applying topo sort to undirected graphs
- Forgetting to check for cycles
- Confusing BFS levels with valid ordering
β Takeaways
- Topological sorting works only on DAGs
- BFS method uses in-degree
- DFS method uses postorder traversal
- Essential for dependency-based problems
ποΈ Practice Problems
- Course Schedule
- Course Schedule II
- Alien Dictionary
- Find Eventual Safe States
- Build Order
Link copied!
Comments
Add Your Comment
Comment Added!
No comments yet. Be the first to comment!