deceptively simple implementation of topological sorting in python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Topological sorting is an essential concept in computer science, particularly when dealing with Directed Acyclic Graphs (DAGs). It involves ordering the vertices of the graph in a linear sequence such that for every directed edge UV from vertex U to vertex V, U appears before V in the ordering. Despite the seemingly complex definition, implementing topological sorting in Python can be deceptively simple, particularly with depth-first search (DFS).
Prerequisites
Before diving into the implementation, let's review a few essential terms:
- Graph: In this context, a set of vertices connected by edges.
- Directed Acyclic Graph (DAG): A directed graph with no cycles.
- Vertex: A node in the graph.
- Edge: A connection from one vertex to another.
Depth-First Search (DFS) Approach
The depth-first search (DFS) approach to topological sorting capitalizes on the idea of exploring each branch of a graph to its farthest extent before backtracking. Here’s the step-by-step breakdown:
- Start with an Empty Result Stack: The result stack will eventually contain all the nodes in topologically sorted order.
- Track Visited Nodes: Use a set or list to track which vertices have already been visited.
- Recursive DFS Function: Recurse into each node’s neighboring nodes if they haven't been visited:
- When recursion finishes for a node, push it onto the result stack.
- Reverse the Stack: Reverse the stack to get the topological order since nodes end up being pushed in a post-order fashion.
Implementation
Here is a simple Python implementation using DFS:
- Task Scheduling: Orders tasks given their dependencies.
- Dependency Resolution: Ensures all dependencies are resolved before a task starts.
- Compilation Order: Determines the order of compilation for files/modules.
- Finding nodes without incoming edges.
- Removing these nodes and the resulting edges, then appending them to the sorted array.
- Repeating until no nodes are left.

