Critical path method in Software Engineering - Time & Space Complexity
When using the Critical Path Method, we want to know how the time to find the longest path grows as the project size increases.
We ask: How does the work needed change when there are more tasks and dependencies?
Analyze the time complexity of the following code snippet.
function findCriticalPath(tasks) {
let graph = buildGraph(tasks);
let visited = new Set();
let memo = new Map();
function dfs(task) {
if (memo.has(task)) return memo.get(task);
let maxLength = 0;
for (let next of graph.get(task) || []) {
maxLength = Math.max(maxLength, dfs(next));
}
memo.set(task, maxLength + 1);
return maxLength + 1;
}
let maxPath = 0;
for (let task of tasks) {
maxPath = Math.max(maxPath, dfs(task));
}
return maxPath;
}
This code finds the longest sequence of dependent tasks in a project, which is the critical path.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Depth-first search (DFS) traversal of tasks to find longest path.
- How many times: Each task is visited once due to memoization, but edges are checked during traversal.
As the number of tasks and dependencies grows, the work grows roughly in proportion to the number of tasks plus their connections.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 tasks | About 10 to 20 checks |
| 100 tasks | About 100 to 200 checks |
| 1000 tasks | About 1000 to 2000 checks |
Pattern observation: The work grows roughly in a straight line with the number of tasks and their links.
Time Complexity: O(n + e)
This means the time to find the critical path grows linearly with the number of tasks and their dependencies.
[X] Wrong: "The time grows exponentially because we check all possible paths."
[OK] Correct: Memoization ensures each task is processed once, so we do not repeat work for the same task multiple times.
Understanding how the critical path method scales helps you explain project scheduling and dependency analysis clearly in interviews.
"What if we removed memoization? How would the time complexity change?"