I have N
different jobs. Some jobs can be done in succession.
It is necessary to arrange consecutive jobs to form a sequence of jobs so that the number of job sequences M
is minimum.
The problem is in the form of Maximum cardinality matching.
But is it sure that when Maximum cardinality matching, the number of job sequences is the minimum?
I'm looking for an algorithm to solve it.
Example
N=6
The following jobs can be followed:
Job 1 can then go to jobs 2, 5.
Job 2 can then go to job 3.
Job 4 can then go to jobs 2, 5.
Job 5 can then go to job 6.
Performing job assignments, we get the following 2 jobs sequences:
1-2-3
4-5-6
Then M=2.
This can be seen as a problem to find the minimum number of crew to make all flights (jobs).
This problem is related to Topological Sorting.
Think about every job as a node of a graph. The rest of the approach is same as top sort.
Procedure to solve the problem:
Let's consider your given input.
Note: Here it processed nodes could be
1, 5, 6
based on the relation1->5->6
. It'll also give the right answer. Which nodes will be processed first, it depends on the way you make the adjacency list.So you've your answer, which is
2
.Note: After processing a path, new node can be found which is left unprocessed and whose in-degree is 0. For example, consider the relation
1->2, 2->3
,2->4
.If you first process the path
1->2->3
,4
will be left unprocessed.If you first process the path
1->2->4
,3
will be left unprocessed.Just following the basic Topological Sorting procedure will give you the answer. You'll have to count, from how many unprocessed nodes with in-degree 0, you're running the DFS.
Helpful resources to learn about topological sorting:
Topological Sorting CP-Algorithms
Topological Sorting GFG
Topological Sort Tutorials & Notes | HackerEarth