Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is topological sorting?
Topological sorting is a way to arrange the nodes of a directed graph in a linear order so that for every directed edge from node A to node B, A comes before B in the order.
Click to reveal answer
beginner
Which type of graph can have a topological sort?
Only directed acyclic graphs (DAGs) can have a topological sort because cycles would make it impossible to order nodes without conflicts.
Click to reveal answer
intermediate
Why can't graphs with cycles be topologically sorted?
Because in a cycle, nodes depend on each other in a loop, so there is no way to order them linearly without breaking the dependency.
Click to reveal answer
intermediate
Name two common algorithms used for topological sorting.
The two common algorithms are Kahn's algorithm and Depth-First Search (DFS) based algorithm.
Click to reveal answer
beginner
Give a real-life example where topological sorting is useful.
Topological sorting is useful in scheduling tasks where some tasks must be done before others, like building a house where the foundation must be done before walls.
Click to reveal answer
Which graph property is required for topological sorting?
AThe graph must be complete
BThe graph must be undirected
CThe graph must contain cycles
DThe graph must be directed and acyclic
✗ Incorrect
Topological sorting only works on directed acyclic graphs (DAGs).
What does a topological sort of a graph represent?
AA shortest path between two nodes
BA random order of nodes
CA linear order of nodes respecting dependencies
DA cycle detection method
✗ Incorrect
It arranges nodes so that all dependencies come before dependent nodes.
Which algorithm is NOT used for topological sorting?
ADijkstra's algorithm
BKahn's algorithm
CDepth-First Search (DFS)
DNone of the above
✗ Incorrect
Dijkstra's algorithm is for shortest paths, not topological sorting.
What happens if you try to topologically sort a graph with a cycle?
AIt is impossible to find a valid order
BThe cycle is ignored
CThe graph becomes undirected
DThe sorting completes normally
✗ Incorrect
Cycles prevent a linear ordering that respects dependencies.
In a task scheduling scenario, what does topological sorting help with?
AIgnoring task dependencies
BOrdering tasks so prerequisites come first
CRandomizing task order
DFinding the longest task
✗ Incorrect
It ensures tasks are done only after their prerequisites.
Explain what topological sorting is and why it requires a directed acyclic graph.
Think about ordering tasks with dependencies and why loops cause problems.
You got /4 concepts.
Describe two algorithms used for topological sorting and how they generally work.
One uses removing nodes step-by-step, the other uses depth-first search.
You got /4 concepts.
Practice
(1/5)
1. What is the main requirement for a graph to have a valid topological sorting?
easy
A. The graph must be a Directed Acyclic Graph (DAG)
B. The graph must be undirected
C. The graph must contain cycles
D. The graph must be complete
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 A
Quick Check:
DAG = Required for topological sorting [OK]
Hint: Topological sort needs no cycles, so graph must be DAG [OK]
Common Mistakes:
Thinking topological sort works on undirected graphs
Assuming cycles are allowed
Confusing DAG with any directed graph
2. Which of the following is the correct way to represent the order of nodes after a topological sort?
easy
A. A list where each node appears before all nodes it points to
B. A list where nodes appear in random order
C. A list where each node appears after all nodes it points to
D. A list sorted by node values numerically
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 A
Quick Check:
Node order respects edges direction = A list where each node appears before all nodes it points to [OK]
Hint: Topological order means dependencies come first [OK]
Common Mistakes:
Confusing order with numerical sorting
Thinking nodes appear after their dependencies
Assuming random order is valid
3. Given the directed edges: A -> B, B -> C, and A -> C, which of the following is a valid topological order?
medium
A. [C, A, B]
B. [B, A, C]
C. [C, B, A]
D. [A, B, C]
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 D
Quick Check:
Order respects edges = [A, B, C] [OK]
Hint: Place nodes before their dependents in order [OK]
Common Mistakes:
Ignoring edge directions
Placing dependent nodes before their prerequisites
Choosing reverse order
4. Consider the following graph edges: 1 -> 2, 2 -> 3, 3 -> 1. What is the problem when trying to perform topological sorting on this graph?
medium
A. The graph has too many nodes
B. The graph is disconnected
C. The graph contains a cycle, so topological sorting is impossible
D. The graph is undirected
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.
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 C
Quick Check:
Cycle present = no topological sort [OK]
Hint: Cycles block topological sorting; check for cycles first [OK]
Common Mistakes:
Ignoring cycles and trying to sort anyway
Confusing disconnected graphs with cycles
Assuming undirected graphs can be topologically sorted
5. You have tasks with dependencies: Task 1 before Task 3, Task 2 before Task 3, and Task 3 before Task 4. Which of the following is a valid topological order for these tasks?
hard
A. [3, 1, 2, 4]
B. [1, 2, 3, 4]
C. [4, 3, 2, 1]
D. [2, 1, 4, 3]
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 B
Quick Check:
Order respects all dependencies = [1, 2, 3, 4] [OK]
Hint: Place all prerequisites before dependent tasks [OK]