forked from jgrapht/jgrapht
/
EdmondsBlossomShrinking.java
212 lines (181 loc) · 5.86 KB
/
EdmondsBlossomShrinking.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* (C) Copyright 2012-2016, by Alejandro Ramon Lopez del Huerto and Contributors.
*
* JGraphT : a free Java graph-theory library
*
* This program and the accompanying materials are dual-licensed under
* either
*
* (a) the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation, or (at your option) any
* later version.
*
* or (per the licensee's choosing)
*
* (b) the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation.
*/
package org.jgrapht.alg;
import java.util.*;
import org.jgrapht.*;
import org.jgrapht.alg.interfaces.*;
import org.jgrapht.util.*;
/**
* An implementation of Edmonds Blossom Shrinking algorithm for constructing maximum matchings on
* graphs. The algorithm runs in time O(V^4).
*
* @param <V> the graph vertex type
* @param <E> the graph edge type
*
* @author Alejandro R. Lopez del Huerto
* @since Jan 24, 2012
* @deprecated Use {@link org.jgrapht.alg.matching.EdmondsBlossomShrinking} instead.
*/
@Deprecated
public class EdmondsBlossomShrinking<V, E>
implements MatchingAlgorithm<V, E>
{
private UndirectedGraph<V, E> graph;
private Set<E> matching;
private Map<V, V> match;
private Map<V, V> path;
private Map<V, V> contracted;
/**
* Construct an instance of the Edmonds blossom shrinking algorithm.
*
* @param G the input graph
*/
public EdmondsBlossomShrinking(final UndirectedGraph<V, E> G)
{
this.graph = G;
}
/**
* {@inheritDoc}
*/
@Override
public Set<E> getMatching()
{
if (matching == null) {
matching = findMatch();
}
return Collections.unmodifiableSet(matching);
}
/**
* Runs the algorithm on the input graph and returns the match edge set.
*
* @return set of Edges
*/
private Set<E> findMatch()
{
Set<E> result = new ArrayUnenforcedSet<>();
match = new HashMap<>();
path = new HashMap<>();
contracted = new HashMap<>();
for (V i : graph.vertexSet()) {
// Any augmenting path should start with _exposed_ vertex
// (vertex may not escape match-set being added once)
if (!match.containsKey(i)) {
// Match is maximal iff graph G contains no more augmenting paths
V v = findPath(i);
while (v != null) {
V pv = path.get(v);
V ppv = match.get(pv);
match.put(v, pv);
match.put(pv, v);
v = ppv;
}
}
}
Set<V> seen = new HashSet<>();
graph.vertexSet().stream().filter(v -> !seen.contains(v) && match.containsKey(v)).forEach(
v -> {
seen.add(v);
seen.add(match.get(v));
result.add(graph.getEdge(v, match.get(v)));
});
return result;
}
private V findPath(V root)
{
Set<V> used = new HashSet<>();
Queue<V> q = new ArrayDeque<>();
// Expand graph back from its contracted state
path.clear();
contracted.clear();
graph.vertexSet().forEach(vertex -> contracted.put(vertex, vertex));
used.add(root);
q.add(root);
while (!q.isEmpty()) {
V v = q.remove();
for (E e : graph.edgesOf(v)) {
V to = graph.getEdgeSource(e);
if (to.equals(v)) {
to = graph.getEdgeTarget(e);
}
if ((contracted.get(v).equals(contracted.get(to))) || to.equals(match.get(v))) {
continue;
}
// Check whether we've hit a 'blossom'
if ((to.equals(root))
|| ((match.containsKey(to)) && (path.containsKey(match.get(to)))))
{
V stem = lowestCommonAncestor(v, to);
Set<V> blossom = new HashSet<>();
markPath(v, to, stem, blossom);
markPath(to, v, stem, blossom);
graph
.vertexSet().stream()
.filter(
i -> contracted.containsKey(i) && blossom.contains(contracted.get(i)))
.forEach(i -> {
contracted.put(i, stem);
if (!used.contains(i)) {
used.add(i);
q.add(i);
}
});
// Check whether we've had hit a loop (of even length (!) presumably)
} else if (!path.containsKey(to)) {
path.put(to, v);
if (!match.containsKey(to)) {
return to;
}
to = match.get(to);
used.add(to);
q.add(to);
}
}
}
return null;
}
private void markPath(V v, V child, V stem, Set<V> blossom)
{
while (!contracted.get(v).equals(stem)) {
blossom.add(contracted.get(v));
blossom.add(contracted.get(match.get(v)));
path.put(v, child);
child = match.get(v);
v = path.get(match.get(v));
}
}
private V lowestCommonAncestor(V a, V b)
{
Set<V> seen = new HashSet<>();
for (;;) {
a = contracted.get(a);
seen.add(a);
if (!match.containsKey(a)) {
break;
}
a = path.get(match.get(a));
}
for (;;) {
b = contracted.get(b);
if (seen.contains(b)) {
return b;
}
b = path.get(match.get(b));
}
}
}
// End EdmondsBlossomShrinking.java