-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMaxMatchingEdmonds.java
128 lines (120 loc) · 3.22 KB
/
MaxMatchingEdmonds.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package Matching;
import java.util.*;
import java.util.stream.*;
// https://en.wikipedia.org/wiki/Blossom_algorithm in O(V^3)
public class MaxMatchingEdmonds {
public static int maxMatching(List<Integer>[] graph) {
int n = graph.length;
int[] match = new int[n];
Arrays.fill(match, -1);
int[] p = new int[n];
for (int i = 0; i < n; ++i) {
if (match[i] == -1) {
int v = findPath(graph, match, p, i);
while (v != -1) {
int pv = p[v];
int ppv = match[pv];
match[v] = pv;
match[pv] = v;
v = ppv;
}
}
}
return (int) Arrays.stream(match).filter(x -> x != -1).count() / 2;
}
static int findPath(List<Integer>[] graph, int[] match, int[] p, int root) {
Arrays.fill(p, -1);
int n = graph.length;
int[] base = IntStream.range(0, n).toArray();
boolean[] used = new boolean[n];
int[] q = new int[n];
int qt = 0;
used[root] = true;
q[qt++] = root;
for (int qh = 0; qh < qt; qh++) {
int u = q[qh];
for (int v : graph[u]) {
if (base[u] == base[v] || match[u] == v) continue;
if (v == root || match[v] != -1 && p[match[v]] != -1) {
int curbase = lca(match, base, p, u, v);
boolean[] blossom = new boolean[n];
markPath(match, base, blossom, p, u, curbase, v);
markPath(match, base, blossom, p, v, curbase, u);
for (int i = 0; i < n; ++i)
if (blossom[base[i]]) {
base[i] = curbase;
if (!used[i]) {
used[i] = true;
q[qt++] = i;
}
}
} else if (p[v] == -1) {
p[v] = u;
if (match[v] == -1)
return v;
v = match[v];
used[v] = true;
q[qt++] = v;
}
}
}
return -1;
}
static void markPath(int[] match, int[] base, boolean[] blossom, int[] p, int u, int curbase, int child) {
for (; base[u] != curbase; u = p[match[u]]) {
blossom[base[u]] = blossom[base[match[u]]] = true;
p[u] = child;
child = match[u];
}
}
static int lca(int[] match, int[] base, int[] p, int a, int b) {
boolean[] used = new boolean[match.length];
while (true) {
a = base[a];
used[a] = true;
if (match[a] == -1) break;
a = p[match[a]];
}
while (true) {
b = base[b];
if (used[b]) return b;
b = p[match[b]];
}
}
// random test
public static void main(String[] args) {
Random rnd = new Random(1);
for (int step = 0; step < 1000; step++) {
int n = rnd.nextInt(10) + 1;
boolean[][] g = new boolean[n][n];
List<Integer>[] graph = Stream.generate(ArrayList::new).limit(n).toArray(List[]::new);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
g[i][j] = g[j][i] = rnd.nextBoolean();
if (g[i][j]) {
graph[i].add(j);
graph[j].add(i);
}
}
}
int res1 = maxMatching(graph);
int res2 = maxMatchingSlow(g);
if (res1 != res2) {
System.err.println(res1 + " " + res2);
}
}
}
static int maxMatchingSlow(boolean[][] g) {
int n = g.length;
int[] dp = new int[1 << n];
for (int mask = 0; mask < dp.length; mask++)
for (int i = 0; i < n; i++)
if ((mask & (1 << i)) == 0) {
for (int j = i + 1; j < n; j++)
if ((mask & (1 << j)) == 0 && g[i][j])
dp[mask | (1 << i) | (1 << j)] = Math.max(dp[mask | (1 << i) | (1 << j)], dp[mask] + 1);
break;
}
return dp[dp.length - 1];
}
}