Find All Downstream Dependents in a Dependency Graph
Trees & GraphsAuto-graded · 18 test casesLast updated
Tests graph traversal with edge reversal to find all nodes reachable through reversed dependency edges, handling cycles correctly.
You are analyzing a system where components have dependencies on one another. Each dependency is represented as a pair (component_x, component_y), meaning that component_x relies on component_y.
When component_y is modified, every component that relies on it—either directly or through a chain of dependencies—may need attention.
Write a function that accepts a list of dependency pairs and a specific component that has been changed, then returns all components that depend on the changed component, directly or indirectly.
Important: Do not include the changed component itself in the output, even if circular dependencies exist.
Input
dependencies: A list of pairs[component_x, component_y]wherecomponent_xdepends oncomponent_ychanged_component: The component that was modified (a string)
Output
Return a list of component names (strings) representing all components that depend on changed_component. The order of components in the output does not matter.
Constraints
- The dependency graph may contain cycles (circular dependencies between components)
- The total number of components and dependency relationships will not exceed 10,000
- Component names are non-empty strings
- The
changed_componentmay or may not appear in the dependency list - Self-dependencies (a component depending on itself) are possible
Examples
Example 1
Input: find_dependent_components([['A', 'B'], ['B', 'C']], 'C')
Output: ['B', 'A']
Example 2
Input: find_dependent_components([['A', 'B'], ['B', 'A'], ['C', 'B']], 'A')
Output: ['B', 'C']
Hints
Hint 1
The direction of edges in the input is opposite to the direction impact flows. If A depends on B, then changes to B impact A.
Hint 2
Build a reverse graph where each component points to all components that depend on it, then traverse from the changed component.
Hint 3
Use BFS or DFS with a visited set to handle cycles. Mark the starting component as visited immediately to exclude it from results.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is recognizing that the input edges point in the opposite direction from how impact propagates. When the input says (A, B) meaning "A depends on B," a change to B affects A. We need to traverse in the reverse direction.
Algorithm:
-
Reverse the graph: Build an adjacency list where each component points to all components that directly depend on it. For dependency pair
(dependent, dependency), adddependentto the list fordependency. -
Traverse from the changed component: Use breadth-first search starting from the changed component. Each neighbor in the reversed graph is a component that depends on the current one.
-
Track visited nodes: Maintain a set of visited components to handle cycles. Mark the starting component as visited immediately so it never appears in the output, even if cycles lead back to it.
-
Collect results: As we discover each new dependent component, add it to the result list.
Why this works:
- Every component we reach is connected to the changed component through a chain of dependencies in the original graph, meaning it truly depends on the changed component
- The visited set prevents infinite loops in circular dependencies and ensures each component appears at most once
- By marking the starting component visited before traversal, we correctly exclude it from results even when self-loops or cycles exist
Complexity
Time: O(V + E) where V is the number of components and E is the number of dependency pairs. We process each component at most once and examine each edge at most once during traversal.
Space: O(V + E) for the reverse adjacency list, plus O(V) for the visited set, queue, and result list.
Solution
from collections import defaultdict, deque
from typing import List
def find_dependent_components(dependencies: List[List[str]], changed_component: str) -> List[str]:
# Build reverse adjacency list: each component maps to components that depend on it
reverse_graph = defaultdict(list)
for dependent, dependency in dependencies:
reverse_graph[dependency].append(dependent)
# Mark the changed component as visited to exclude it from results
visited = {changed_component}
queue = deque([changed_component])
result = []
while queue:
current = queue.popleft()
for dependent in reverse_graph[current]:
if dependent not in visited:
visited.add(dependent)
result.append(dependent)
queue.append(dependent)
return result