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?"
Practice
Solution
Step 1: Understand the purpose of CPM
The Critical Path Method is used to find the longest chain of tasks that must be completed on time to avoid delaying the project.Step 2: Identify what CPM does not measure
CPM does not calculate cost, team size, or quality standards; it focuses on task sequencing and timing.Final Answer:
The longest sequence of dependent tasks that sets the project duration -> Option BQuick Check:
CPM = longest task sequence [OK]
- Confusing CPM with cost estimation
- Thinking CPM calculates resources needed
- Assuming CPM sets quality standards
Solution
Step 1: Understand task dependency in CPM
In CPM, a task dependency means one task must finish before the next can start.Step 2: Evaluate each option
Only Task B starts only after Task A finishes correctly states that Task B starts after Task A finishes, showing a dependency.Final Answer:
Task B starts only after Task A finishes -> Option CQuick Check:
Dependency = task B after task A [OK]
- Assuming tasks can start simultaneously without dependency
- Thinking tasks are independent when they have dependencies
- Believing a task can start before its predecessor finishes
Task A: 3 days (start)
Task B: 4 days (after A)
Task C: 2 days (after A)
Task D: 5 days (after B and C)What is the total project duration using CPM?
Solution
Step 1: Calculate paths and durations
Path 1: A (3) -> B (4) -> D (5) = 3 + 4 + 5 = 12 days
Path 2: A (3) -> C (2) -> D (5) = 3 + 2 + 5 = 10 daysStep 2: Identify the critical path
The longest path is Path 1 with 12 days, so the project duration is 12 days.Final Answer:
12 days -> Option DQuick Check:
Longest path duration = 12 days [OK]
- Adding all tasks durations regardless of dependencies
- Choosing the shortest path instead of longest
- Ignoring that D depends on both B and C
A (2 days), B (3 days after A), C (4 days after B), D (1 day after A)Which task dependency is incorrect if the critical path is A -> B -> C?
Solution
Step 1: Analyze given dependencies
Tasks A, B, and C form the critical path with dependencies A->B->C.
Task D depends on A, so it starts after A.Step 2: Check if Task D depends on Task C
Since D depends on A only, not on C, Task D depends on Task C is incorrect because it states D depends on C.Final Answer:
Task D depends on Task C -> Option AQuick Check:
Incorrect dependency = D on C [OK]
- Assuming all tasks depend on the last task
- Mixing up which tasks depend on which
- Ignoring the critical path sequence
W: 2 days (start)
X: 3 days (after W)
Y: 4 days (after W)
Z: 1 day (after X and Y)If task Y is delayed by 2 days, what is the new project duration?
Solution
Step 1: Calculate original paths durations
Path 1: W (2) -> X (3) -> Z (1) = 6 days
Path 2: W (2) -> Y (4) -> Z (1) = 7 daysStep 2: Add delay to task Y and recalculate
Y delayed by 2 days: Y duration = 4 + 2 = 6 days
New Path 2 duration: 2 + 6 + 1 = 9 daysStep 3: Determine new project duration
Longest path is now Path 2 with 9 days, so project duration is 9 days.Final Answer:
9 days -> Option AQuick Check:
Delay added to longest path = 9 days [OK]
- Adding delay to shortest path
- Ignoring delay impact on critical path
- Calculating total duration without dependencies
Ready to prove it?
Pass the test and this pattern moves unknown
