Skip to content
DeepForgeby svx
All problems
HardGraph Algorithmsgraph-063

Bipartite Minimum Path Cover

Problem

Compute the minimum number of vertex-disjoint paths needed to cover every node of a DAG.

Build a bipartite graph with a left and a right copy of each node and an edge from left u to right v for every DAG edge u to v. By Dilworth's theorem the answer is n minus the maximum matching. edges is a list of [u, v] pairs.

Hint

Each matched pair joins two path segments; unmatched right nodes are path starts.

Test cases

case 1
in: [4,[[0,1],[1,2],[2,3]]]
expected: 1
case 2
in: [4,[[0,1],[0,2],[1,3],[2,3]]]
expected: 2
case 3
in: [3,[]]
expected: 3
case 4
in: [2,[[0,1]]]
expected: 1
case 5
in: [5,[[0,1],[1,2],[3,4]]]
expected: 2
Starter codePython
def min_path_cover(n, edges):
    # Your code here
    pass

5 test cases · run your code against them in the editor