Topological sorting in Data Structures Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Topological sorting arranges tasks in order when some must come before others.
We want to know how the time needed grows as the number of tasks and dependencies increase.
Analyze the time complexity of the following topological sort using Depth-First Search (DFS).
function topoSort(graph):
visited = set()
stack = []
for node in graph:
if node not in visited:
dfs(node, visited, stack, graph)
return reversed(stack)
function dfs(node, visited, stack, graph):
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
dfs(neighbor, visited, stack, graph)
stack.append(node)
This code visits each task and its dependencies once to produce a valid order.
- Primary operation: DFS visits each node and explores all its edges once.
- How many times: Each node and edge is processed exactly once during the traversal.
As the number of tasks (nodes) and dependencies (edges) grow, the work grows proportionally.
| Input Size (n nodes, e edges) | Approx. Operations |
|---|---|
| 10 nodes, 15 edges | About 25 operations |
| 100 nodes, 200 edges | About 300 operations |
| 1000 nodes, 5000 edges | About 6000 operations |
Pattern observation: The total steps increase roughly by adding nodes and edges together.
Time Complexity: O(n + e)
This means the time grows linearly with the number of tasks plus the number of dependencies.
[X] Wrong: "Topological sort takes quadratic time because it looks at all pairs of tasks."
[OK] Correct: The algorithm only visits each task and its direct dependencies once, not every pair, so it is much faster.
Understanding this time complexity helps you explain how efficiently you can order tasks with dependencies, a common real-world problem.
"What if the graph is represented using an adjacency matrix instead of adjacency lists? How would the time complexity change?"
Practice
topological sorting?Solution
Step 1: Understand the definition of topological sorting
Topological sorting orders nodes so that every directed edge from node A to node B means A comes before B in the order.Step 2: Identify the graph type needed
This ordering is only possible if the graph has no cycles, meaning it must be a Directed Acyclic Graph (DAG).Final Answer:
The graph must be a Directed Acyclic Graph (DAG) -> Option AQuick Check:
DAG = Required for topological sorting [OK]
- Thinking topological sort works on undirected graphs
- Assuming cycles are allowed
- Confusing DAG with any directed graph
Solution
Step 1: Recall the property of topological order
In topological sorting, every node appears before all nodes it has edges to (its dependencies come first).Step 2: Match this property to the options
A list where each node appears before all nodes it points to correctly states that each node appears before all nodes it points to, which is the definition of topological order.Final Answer:
A list where each node appears before all nodes it points to -> Option AQuick Check:
Node order respects edges direction = A list where each node appears before all nodes it points to [OK]
- Confusing order with numerical sorting
- Thinking nodes appear after their dependencies
- Assuming random order is valid
A -> B, B -> C, and A -> C, which of the following is a valid topological order?Solution
Step 1: Analyze the edges and dependencies
Node A points to B and C, so A must come before B and C. Node B points to C, so B must come before C.Step 2: Check each option against these rules
[A, B, C] respects A before B and C, and B before C. The other options violate these dependencies.Final Answer:
[A, B, C] -> Option DQuick Check:
Order respects edges = [A, B, C] [OK]
- Ignoring edge directions
- Placing dependent nodes before their prerequisites
- Choosing reverse order
1 -> 2, 2 -> 3, 3 -> 1. What is the problem when trying to perform topological sorting on this graph?Solution
Step 1: Identify the cycle in the graph
The edges form a cycle: 1 -> 2 -> 3 -> 1, which means the graph is not acyclic.Step 2: Understand topological sorting requirements
Topological sorting requires the graph to be acyclic. A cycle makes it impossible to order nodes without violating dependencies.Final Answer:
The graph contains a cycle, so topological sorting is impossible -> Option CQuick Check:
Cycle present = no topological sort [OK]
- Ignoring cycles and trying to sort anyway
- Confusing disconnected graphs with cycles
- Assuming undirected graphs can be topologically sorted
Solution
Step 1: List the dependencies clearly
Task 1 and Task 2 must come before Task 3, and Task 3 must come before Task 4.Step 2: Validate each option against dependencies
[1, 2, 3, 4] respects all dependencies. The other options violate at least one dependency.Final Answer:
[1, 2, 3, 4] -> Option BQuick Check:
Order respects all dependencies = [1, 2, 3, 4] [OK]
- Placing dependent tasks before prerequisites
- Ignoring order of multiple dependencies
- Assuming any order is valid
