Minimum time taken by each job to be completed given by a Directed Acyclic Graph
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:
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:
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.
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:
- Finish the jobs that are not dependent on any other job.
- Create an array inDegree[] to store the count of the dependent node for each node in the given network.
- Initialize a queue and push all the vertex whose inDegree[] is 0.
- 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.
- 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:
C++
#include <bits/stdc++.h>
using namespace std;
#define maxN 100000
vector< int > graph[maxN];
int indegree[maxN];
int job[maxN];
void addEdge( int u, int v)
{
graph[u].push_back(v);
indegree[v]++;
}
void printOrder( int n, int m)
{
queue< int > q;
for ( int i = 1; i <= n; i++) {
if (indegree[i] == 0) {
q.push(i);
job[i] = 1;
}
}
while (!q.empty()) {
int cur = q.front();
q.pop();
for ( int adj : graph[cur]) {
indegree[adj]--;
if (indegree[adj] == 0) {
job[adj] = job[cur] + 1;
q.push(adj);
}
}
}
for ( int i = 1; i <= n; i++)
cout << job[i] << " " ;
cout << "\n" ;
}
int main()
{
int n, m;
n = 10;
m = 13;
addEdge(1, 3);
addEdge(1, 4);
addEdge(1, 5);
addEdge(2, 3);
addEdge(2, 8);
addEdge(2, 9);
addEdge(3, 6);
addEdge(4, 6);
addEdge(4, 8);
addEdge(5, 8);
addEdge(6, 7);
addEdge(7, 8);
addEdge(8, 10);
printOrder(n, m);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int maxN = 100000 ;
@SuppressWarnings ( "unchecked" )
static Vector<Integer> []graph = new Vector[maxN];
static int []indegree = new int [maxN];
static int []job = new int [maxN];
static void addEdge( int u, int v)
{
graph[u].add(v);
indegree[v]++;
}
static void printOrder( int n, int m)
{
Queue<Integer> q = new LinkedList<>();
for ( int i = 1 ; i <= n; i++)
{
if (indegree[i] == 0 )
{
q.add(i);
job[i] = 1 ;
}
}
while (!q.isEmpty())
{
int cur = q.peek();
q.remove();
for ( int adj : graph[cur])
{
indegree[adj]--;
if (indegree[adj] == 0 ){
job[adj] = 1 + job[cur];
q.add(adj);
}
}
}
for ( int i = 1 ; i <= n; i++)
System.out.print(job[i] + " " );
System.out.print( "\n" );
}
public static void main(String[] args)
{
int n, m;
n = 10 ;
m = 13 ;
for ( int i = 0 ; i < graph.length; i++)
graph[i] = new Vector<Integer>();
addEdge( 1 , 3 );
addEdge( 1 , 4 );
addEdge( 1 , 5 );
addEdge( 2 , 3 );
addEdge( 2 , 8 );
addEdge( 2 , 9 );
addEdge( 3 , 6 );
addEdge( 4 , 6 );
addEdge( 4 , 8 );
addEdge( 5 , 8 );
addEdge( 6 , 7 );
addEdge( 7 , 8 );
addEdge( 8 , 10 );
printOrder(n, m);
}
}
|
Python3
from collections import defaultdict
class Graph:
def __init__( self , vertices, edges):
self .graph = defaultdict( list )
self .n = vertices
self .m = edges
def addEdge( self , u, v):
self .graph[u].append(v)
def printOrder( self , n, m):
indegree = [ 0 ] * ( self .n + 1 )
for i in self .graph:
for j in self .graph[i]:
indegree[j] + = 1
job = [ 0 ] * ( self .n + 1 )
q = []
for i in range ( 1 , self .n + 1 ):
if indegree[i] = = 0 :
q.append(i)
job[i] = 1
while q:
cur = q.pop( 0 )
for adj in self .graph[cur]:
indegree[adj] - = 1
if (indegree[adj] = = 0 ):
job[adj] = 1 + job[cur]
q.append(adj)
for i in range ( 1 , n + 1 ):
print (job[i], end = " " )
print ()
n = 10
m = 13
g = Graph(n, m)
g.addEdge( 1 , 3 )
g.addEdge( 1 , 4 )
g.addEdge( 1 , 5 )
g.addEdge( 2 , 3 )
g.addEdge( 2 , 8 )
g.addEdge( 2 , 9 )
g.addEdge( 3 , 6 )
g.addEdge( 4 , 6 )
g.addEdge( 4 , 8 )
g.addEdge( 5 , 8 )
g.addEdge( 6 , 7 )
g.addEdge( 7 , 8 )
g.addEdge( 8 , 10 )
g.printOrder(n, m)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static readonly int maxN = 100000;
static List< int > []graph = new List< int >[maxN];
static int []indegree = new int [maxN];
static int []job = new int [maxN];
static void addEdge( int u, int v)
{
graph[u].Add(v);
indegree[v]++;
}
static void printOrder( int n, int m)
{
Queue< int > q = new Queue< int >();
for ( int i = 1; i <= n; i++)
{
if (indegree[i] == 0)
{
q.Enqueue(i);
job[i] = 1;
}
}
while (q.Count != 0)
{
int cur = q.Peek();
q.Dequeue();
foreach ( int adj in graph[cur])
{
indegree[adj]--;
if (indegree[adj] == 0){
job[adj] = 1 + job[cur];
q.Enqueue(adj);
}
}
}
for ( int i = 1; i <= n; i++)
Console.Write(job[i] + " " );
Console.Write( "\n" );
}
public static void Main(String[] args)
{
int n, m;
n = 10;
m = 13;
for ( int i = 0; i < graph.Length; i++)
graph[i] = new List< int >();
addEdge(1, 3);
addEdge(1, 4);
addEdge(1, 5);
addEdge(2, 3);
addEdge(2, 8);
addEdge(2, 9);
addEdge(3, 6);
addEdge(4, 6);
addEdge(4, 8);
addEdge(5, 8);
addEdge(6, 7);
addEdge(7, 8);
addEdge(8, 10);
printOrder(n, m);
}
}
|
Javascript
const maxN = 100000;
const graph = new Array(maxN);
for (let i = 0; i < maxN; i++) {
graph[i] = [];
}
const indegree = new Array(maxN).fill(0);
const job = new Array(maxN).fill(0);
function addEdge(u, v) {
graph[u].push(v);
indegree[v]++;
}
function printOrder(n, m) {
const q = [];
for (let i = 1; i <= n; i++) {
if (indegree[i] === 0) {
q.push(i);
job[i] = 1;
}
}
while (q.length > 0) {
const cur = q.shift();
for (const adj of graph[cur]) {
indegree[adj]--;
if (indegree[adj]== 0) {
job[adj] = job[cur] + 1;
q.push(adj);
}
}
}
for (let i = 1; i <= n; i++) {
console.log(job[i]);
}
}
function main() {
const n = 10;
const m = 13;
addEdge(1, 3);
addEdge(1, 4);
addEdge(1, 5);
addEdge(2, 3);
addEdge(2, 8);
addEdge(2, 9);
addEdge(3, 6);
addEdge(4, 6);
addEdge(4, 8);
addEdge(5, 8);
addEdge(6, 7);
addEdge(7, 8);
addEdge(8, 10);
printOrder(n, m);
}
main();
|
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)
Last Updated :
06 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...