Data Structures & Algorithms / Editorial solution guide
A dependency order violates prerequisites or omits tasks
Check edge direction, vertex coverage, and cycles before accepting a topological order.
Read-only knowledge base ยท No public comments
Recognize the symptoms
A build starts a task before its prerequisite, or an ordering omits tasks while appearing successful. The same graph may have several valid orders, so a different order alone is not proof of failure. Capture a small synthetic graph and the exact returned sequence.
Understand the likely causes
- The implementation may reverse edge meaning, fail to track indegrees correctly, or return a partial sequence after encountering a directed cycle. A test that compares one expected order can also reject a different valid sequence.
Reproduce the dependency contract
Write down what an edge means: in this guide source must precede destination. Construct a chain with a known order, then add independent vertices and a cycle. Confirm that the input includes every vertex, including those without outgoing edges. Trace the initial indegrees and the queue of ready vertices; check whether each removed edge decrements the intended neighbor exactly once.
Check the returned order
The validation function first checks coverage and then verifies that every source appears before its destination. It assumes every neighbor is a graph key; validate that input contract separately. This check accepts any valid order rather than a single hard-coded sequence. For a cycle, no complete order can satisfy all constraints; a topological sorting implementation should report that failure rather than return its partial work as success.
def valid_order(graph, order):
if len(order) != len(graph) or set(order) != set(graph):
return False
position = {node: index for index, node in enumerate(order)}
return all(position[node] < position[neighbor]
for node, neighbors in graph.items()
for neighbor in neighbors)
graph = {"design": ["build"], "build": ["test"], "test": []}
assert valid_order(graph, ["design", "build", "test"])
assert not valid_order(graph, ["test", "build", "design"])
The checker accepts a complete order only when every represented edge points forward.
Repair the algorithm and error path
Correct the edge direction consistently at graph construction and processing. Initialize indegrees for all vertices, enqueue zero-indegree vertices, and report a cycle if fewer vertices are processed than exist. Define how missing vertices and duplicate edges are represented; count each represented edge consistently. Do not start dependent side effects from a partial order unless the application explicitly supports and reports partial execution.
Verify against small graphs
Test an empty graph, chain, disconnected components, independent vertices, self-loop, multi-vertex cycle, and missing neighbor. For successful orders assert exact vertex coverage and every edge constraint with a position map. For cycles assert the documented failure, not a particular partial sequence. Compare small cases with a simple reference, and verify the application does not run tasks after an invalid dependency graph is rejected.
Verify the fix
Verify the chain succeeds, reversed order fails, and cyclic inputs are rejected by the ordering algorithm. Check missing vertices according to the input contract.
Keep it from recurring
- Document edge direction and cycle behavior, validate graph construction, and test constraints rather than assuming a unique order.