deceptively simple implementation of topological sorting in python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- Deciding on an algorithm for 'Jumping Jack
- Decimal to binary algorithm in C
- Decision boundaries for nearest centroid
- Decision tree implementation for returning the next feature to split the tree
- Decision tree implementation issue in apache spark with java
- Decision Tree in Matlab
- deciding among subprocess, multiprocessing, and thread in Python?
- Decode HTML entities in Python string?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.