-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathBipartiteMatching.java
51 lines (38 loc) · 1.42 KB
/
BipartiteMatching.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class BipartiteMatching {
private Graph G;
private int maxMatching;
public BipartiteMatching(Graph G){
BipartitionDetection bd = new BipartitionDetection(G);
if(!bd.isBipartite())
throw new IllegalArgumentException("BipartiteMatching only works for bipartite graph.");
this.G = G;
int[] colors = bd.colors();
// 源点为 V, 汇点为 V + 1
WeightedGraph network = new WeightedGraph(G.V() + 2, true);
for(int v = 0; v < G.V(); v ++) {
if(colors[v] == 0)network.addEdge(G.V(), v, 1);
else network.addEdge(v, G.V() + 1, 1);
for (int w : G.adj(v))
if(v < w){
if (colors[v] == 0) network.addEdge(v, w, 1);
else network.addEdge(w, v, 1);
}
}
MaxFlow maxFlow = new MaxFlow(network, G.V(), G.V() + 1);
maxMatching = maxFlow.result();
}
public int maxMatching(){
return maxMatching;
}
public boolean isPerfectMatching(){
return maxMatching * 2 == G.V();
}
public static void main(String[] args){
Graph g = new Graph("g.txt");
BipartiteMatching bm = new BipartiteMatching(g);
System.out.println(bm.maxMatching());
Graph g2 = new Graph("g2.txt");
BipartiteMatching bm2 = new BipartiteMatching(g2);
System.out.println(bm2.maxMatching());
}
}