Back to Data Structures & Algorithms
2026-03-046 min read

topological sort (Data Structures & Algorithms)

Learn topological sort (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

Topological sorting is a fundamental concept in data structures and algorithms that plays a significant role in solving problems involving dependencies or prerequisites between multiple tasks. It is used extensively in various fields such as computer science, engineering, and business management. Understanding topological sorting can help you tackle real-world scenarios more effectively and improve your problem-solving skills.

Prerequisites

Before delving into topological sorting, it's crucial to have a strong foundation in the following areas:

  1. Graph Theory: Familiarize yourself with graph terminology like vertices (nodes), edges, adjacency lists, and directed graphs.
  2. Graph Traversal Algorithms: Understand Depth-First Search (DFS) and Breadth-First Search (BFS). These algorithms are essential for implementing topological sorting.
  3. Data Structures: Be comfortable with Python data structures such as lists, dictionaries, sets, and tuples.
  4. Basic Python Concepts: Ensure you have a solid understanding of control flow statements (if-else, loops), functions, and error handling.

Core Concept

Topological sorting is an algorithm that arranges the vertices of a Directed Acyclic Graph (DAG) in such a way that for every directed edge u v, vertex u comes before v. This can be achieved using Depth-First Search (DFS).

  1. Initialize two lists: one to store the sorted order and another to keep track of incoming edges for each vertex.
  2. Perform a DFS on the graph starting from an arbitrary vertex. During the DFS, mark each visited vertex as visited and add it to the sorted_order. If a cycle is detected during the DFS (a vertex is encountered that has already been marked as visited), the graph cannot be topologically sorted.
  3. After the DFS, the sorted_order list will contain the topological sort of the DAG.

Here's a Python implementation:

def topological_sort(graph):
visited, sorted_order = set(), []

def dfs(vertex):
if vertex not in visited:
visited.add(vertex)
for neighbor in graph[vertex]:
dfs(neighbor)
sorted_order.insert(0, vertex)

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

return sorted_order if len(sorted_order) == len(graph) else None

Worked Example

Let's consider a simple Directed Acyclic Graph (DAG):

graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': [],
'D': [],
'E': []
}

Using the topological_sort function, we can obtain:

topological_sort(graph) # Output: ['A', 'C', 'D', 'B', 'E']

Common Mistakes

  1. Incorrect graph representation: Ensure that your graph is represented as a dictionary where keys are vertices and values are lists of adjacent vertices (edges).
  2. Inconsistent DFS implementation: Make sure that the DFS function correctly marks visited vertices, handles cycles, and adds vertices to the sorted order in the correct order.
  3. Returning an empty list instead of None: If the graph contains a cycle, it cannot be topologically sorted. In such cases, return None instead of an empty list.
  4. Not handling disconnected components: If the graph has multiple connected components (subgraphs that are not reachable from each other), the topological sort will only work for one component at a time. You can handle this by recursively calling the topological_sort function on each connected component.
  5. Ignoring parallel edges: Topological sorting handles parallel edges correctly, as it only considers the presence of an edge, not its multiplicity. However, if you need to consider the weight of edges, you may need to adapt the algorithm accordingly.
  6. Not considering self-loops: If the graph contains self-loops (edges from a vertex to itself), they should be handled carefully during DFS to avoid infinite loops or incorrect results.
  7. Incorrect handling of weighted graphs: When dealing with weighted graphs, you may need to modify the algorithm to consider edge weights while sorting the vertices.
  8. Not considering directed cycles: If a cycle is detected during DFS, it should be handled correctly to avoid an infinite loop or incorrect results.

Practice Questions

  1. Implement topological sort for a weighted DAG (where edges have weights).
  2. Given a list of prerequisite courses for a university program, write a Python function to determine the minimum number of semesters required to complete all courses.
  3. Write a Python script to find the longest path in a Directed Acyclic Graph using topological sorting.
  4. Implement topological sort for a directed graph with self-loops (edges from a vertex to itself).
  5. Given a list of tasks with dependencies and a fixed number of workers, write a Python function to schedule tasks such that the maximum time taken to complete all tasks is minimized.
  6. Write a Python program to find strongly connected components in a directed graph using topological sorting.
  7. Implement topological sort for a weighted directed graph with self-loops (edges from a vertex to itself and edges have weights).
  8. Given a list of activities where some activities are prerequisites for others, write a Python function to find the maximum number of activities that can be performed in a day.
  9. Write a Python script to find the shortest path between two vertices in a Directed Acyclic Graph using topological sorting and Breadth-First Search (BFS).
  10. Implement a Python program to find the minimum number of semesters required to complete all courses when there are multiple sections for each course with different prerequisites.

FAQ

  1. What if the graph contains cycles?: If the graph contains a cycle, it cannot be topologically sorted, and the algorithm will return None.
  2. Can we use Breadth-First Search (BFS) for topological sorting?: While both DFS and BFS can be used for topological sorting, DFS is generally preferred due to its simpler implementation and better performance in practice. However, BFS can be used when the graph has weighted edges and we want to find a minimum-weight topological order.
  3. What is the time complexity of topological sorting using DFS?: The time complexity of topological sorting using DFS is O(V + E), where V is the number of vertices (nodes) and E is the number of edges.
  4. Can we use topological sorting for undirected graphs?: Topological sorting can only be applied to directed acyclic graphs, as it relies on the direction of the edges to determine the ordering. However, you can convert an undirected graph into a directed one by replacing each edge with two opposite edges (a and b -> a <- b and a -> b).
  5. How does topological sorting handle parallel edges?: Topological sorting handles parallel edges correctly, as it only considers the presence of an edge, not its multiplicity. However, if you need to consider the weight of edges, you may need to adapt the algorithm accordingly.
  6. How does topological sorting handle self-loops?: If the graph contains self-loops, they should be handled carefully during DFS to avoid infinite loops or incorrect results. One approach is to mark visited vertices with a special value (e.g., -1) when encountering a self-loop and ignore it during the sorting process.
  7. Can we use topological sorting for weighted graphs?: Yes, topological sorting can be extended to handle weighted graphs by modifying the algorithm to consider edge weights while sorting the vertices.
  8. What is the difference between topological sorting and BFS?: Topological sorting and BFS are both graph traversal algorithms with different purposes. Topological sorting arranges the vertices of a directed acyclic graph in a specific order, while BFS finds the shortest path from a source vertex to all other reachable vertices.
  9. What is the difference between topological sorting and Dijkstra's algorithm?: Topological sorting and Dijkstra's algorithm are both used for finding paths in graphs but have different focuses. Topological sorting arranges the vertices of a directed acyclic graph, while Dijkstra's algorithm finds the shortest path between a source vertex and all other reachable vertices in a weighted graph.
  10. What is the difference between topological sorting and Bellman-Ford algorithm?: Topological sorting and Bellman-Ford algorithm are both used for finding paths in graphs but have different focuses. Topological sorting arranges the vertices of a directed acyclic graph, while Bellman-Ford algorithm finds the shortest path between a source vertex and all other reachable vertices in a weighted graph, including handling negative edge weights and cycles with more than one negative-weight cycle.
topological sort (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn