VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-time-taken-by-each-job-to-be-completed-given-by-a-directed-acyclic-graph/

⇱ Minimum time taken by each job to be completed given by a Directed Acyclic Graph - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum time taken by each job to be completed given by a Directed Acyclic Graph

Last Updated : 15 Jul, 2025
Given a Directed Acyclic Graph having V vertices and E edges, where each edge {U, V} represents the Jobs U and V such that Job V can only be started only after completion of Job U. The task is to determine the minimum time taken by each job to be completed where each Job takes unit time to get completed.

Examples:

Input: N = 10, E = 13, Below is the given graph:

👁 Image

Output: 1 1 2 2 2 3 4 5 2 6  
Explanation:
Start the jobs 1 and 2 at the beginning and complete them at 1 unit of time. 
Since, jobs 3, 4, 5, and 9 have the only dependency on one job (i.e 1st job for jobs 3, 4, and 5 and 2nd job for job 9). So, we can start these jobs at 1st unit of time and complete these at 2nd unit of time after the completion of the dependent Job.
Similarly, 
Job 6 can only be done after 3rd and 4th jobs are done. So, start it at 2nd unit of time and complete it at 3rd unit of time.
Job 7 can only be done after job 6 is done. So, you can start it at 3rd unit of time and complete it at 4th unit of time.
Job 8 can only be done after 4th, 5th, and 7th jobs are done. So, start it at 4th unit of time and complete it at 5th unit of time.
Job 10 can only be done after the 8th job is done. So, start it at 5th unit of time and complete it at 6th unit of time.

Input: N = 7, E = 7, Below is the given graph:

👁 Image

Output: 1 2 3 3 3 4 4  
Explanation:
Start the Job 1 at the beginning and complete it at 1st unit of time.
The job 2 can only be done after 1st Job is done. So, start it at 1st unit of time and complete it at 2nd unit of time.
Since, Job 3, 4, and 5 have the only dependency on 2nd Job. So, start these jobs at 2nd unit of time and complete these at 3rd unit of time.
The Job 6 can only be done after the 3rd and 4th Job is done. So, start it at 3rd unit of time and complete it at 4th unit of time.
The Job 7 can only be done after the 5th Job is done. So, start it at 3rd hour and complete it at 4th unit of time.

Solve before going to solution: Solve

Approach: The job can be started only if all the jobs that are prerequisites of the job that are done. Therefore, the idea is to use Topological Sort for the given network. Below are the steps:

  1. Finish the jobs that are not dependent on any other job.
  2. Create an array inDegree[] to store the count of the dependent node for each node in the given network.
  3. Initialize a queue and push all the vertex whose inDegree[] is 0.
  4. Initialize the timer to 1 and store the current queue size(say size) and do the following:
    • Pop the node from the queue until the size is 0 and update the finishing time of this node to the timer.
    • While popping the node(say node U) from the queue decrement the inDegree of every node connected to it.
    • If inDegree of any node is 0 in the above step then insert that node in the queue.
    • Increment the timer after all the above steps.
  5. Print the finishing time of all the nodes after we traverse every node in the above step.

Below is the implementation of the above approach:


Output
1 1 2 2 2 3 4 5 2 6

Time Complexity: O(V+E), where V is the number of nodes and E is the number of edges. 
Auxiliary Space: O(V)

Comment