ZOMATO Coding Question – Solved

4 Live
Minimum Time to Complete Tasks with Dependencies You are given N tasks numbered from 0 to N-1. Each task takes 1 unit of time to complete. Some tasks depend on the completion of other tasks and can only start once all their prerequisite tasks are finished. You are given a list of dependency pairs (a, b), meaning task a must be completed before task b can start. You have unlimited resources and can run multiple tasks in parallel as long as their dependencies are satisfied. Your task is to find the minimum time required to complete all tasks. Function Description: Implement the function minimumTimeToCompleteTasks with parameters: - N: integer, total number of tasks - M: integer, number of dependency pairs - dependencies: list of pairs (a, b) where task a must be completed before task b Return: - int: minimum time to complete all tasks Constraints: - 1 ≤ N ≤ 10^5 - 0 ≤ M ≤ 10^5 - 0 ≤ a, b < N Sample Input: 5 4 0 1 1 2 0 3 3 4 Sample Output: 3 Explanation: - Task 0 starts at time 1 - Tasks 1 and 3 depend on task 0, so they start at time 2 - Task 2 depends on 1, and task 4 depends on 3, so they start at time 3 Thus, the minimum time to complete all tasks is 3.

Asked in: ZOMATO

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve the problem of finding the minimum time to complete all tasks with dependencies where tasks can run in parallel as soon as their prerequisites are done, you need to think carefully about the nature of the dependencies and how tasks can be scheduled respecting those constraints.

---

Step 1: Understand the Problem as a Directed Acyclic Graph (DAG)

- The tasks can be viewed as nodes in a graph.
- Dependencies represent directed edges from task a (prerequisite) to task b (dependent).
- The graph is a DAG because tasks with cyclic dependencies cannot be completed.
- The problem is essentially to find the minimum time needed to "topologically sort" the tasks while considering that multiple tasks can be executed simultaneously.

---

Step 2: Concept of Levels or Layers in the DAG

- Since tasks take one unit of time each, and tasks can start only after all dependencies are complete, the tasks can be arranged in layers.
- The tasks at the first layer (level 1) have no prerequisites and can start immediately.
- Tasks at layer 2 depend only on tasks from layer 1, and so on.
- The minimum time to complete all tasks is equal to the number of layers in this layered structure.

---

Step 3: Data Structures to Use

- Represent the graph using adjacency lists for efficient traversal.
- Maintain an array to track the in-degree (number of prerequisites) of each task.
- Tasks with in-degree 0 have no dependencies and can start immediately.

---

Step 4: Kahn’s Algorithm for Topological Sorting with Level Tracking

- Initialize a queue and add all tasks with in-degree 0 (no prerequisites).
- These tasks form the first level (can be completed at time 1).
- Use an array or map to record the time at which each task can be completed.
- For each task removed from the queue:
- For each of its dependent tasks, reduce the in-degree by 1.
- If the in-degree of a dependent task becomes 0, it means all prerequisites are done.
- Assign the dependent task a completion time one unit later than the current task's completion time (since tasks take one unit of time).
- Add the dependent task to the queue.
- Repeat until the queue is empty.

---

Step 5: Computing the Minimum Completion Time

- The minimum time to complete all tasks is the maximum completion time assigned to any task.
- Since tasks can run in parallel, tasks with the same earliest completion time belong to the same layer.
- Tracking the completion times ensures you know exactly when each task can finish.

---

Step 6: Handling Edge Cases

- If the graph has cycles (detected if some tasks never reach in-degree 0), it is impossible to complete all tasks.
- The problem constraints imply no cycles, but you should be prepared to detect such cases.
- If there are tasks with no dependencies, they start at time 1.
- If no dependencies exist at all (M=0), all tasks can run in parallel and complete in one unit of time.

---

Step 7: Complexity Considerations

- The approach visits each edge and node once, leading to O(N + M) time complexity.
- This is efficient given constraints where N and M can be up to 10^5.

---

Step 8: Intuitive Example

- For tasks 0,1,2,3,4 with dependencies:
- 0 → 1, 0 → 3, 1 → 2, 3 → 4
- Tasks with no prerequisites: task 0 at time 1.
- Tasks depending on 0: tasks 1 and 3 at time 2.
- Tasks depending on 1 and 3: tasks 2 and 4 at time 3.
- Hence, total minimum time = 3.

---

Step 9: Summary of Thought Process

- Model tasks and dependencies as a DAG.
- Use topological sorting with a queue, tracking in-degree of tasks.
- Assign completion times based on prerequisites.
- The maximum completion time among all tasks is the answer.
- This approach leverages BFS-like processing of layers in the dependency graph.
- Parallel execution means tasks in the same layer can be done simultaneously, so count only layers.

By conceptualizing the problem as a level-by-level topological sorting and carefully tracking task completion times, you can determine the minimum time needed to complete all dependent tasks efficiently and clearly.
```


Related Questions