jacint@1077
|
1 |
/* -*- C++ -*-
|
jacint@1077
|
2 |
* src/lemon/max_matching.h - Part of LEMON, a generic C++ optimization library
|
jacint@1077
|
3 |
*
|
jacint@1077
|
4 |
* Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
jacint@1077
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
jacint@1077
|
6 |
*
|
jacint@1077
|
7 |
* Permission to use, modify and distribute this software is granted
|
jacint@1077
|
8 |
* provided that this copyright notice appears in all copies. For
|
jacint@1077
|
9 |
* precise terms see the accompanying LICENSE file.
|
jacint@1077
|
10 |
*
|
jacint@1077
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
jacint@1077
|
12 |
* express or implied, and with no claim as to its suitability for any
|
jacint@1077
|
13 |
* purpose.
|
jacint@1077
|
14 |
*
|
jacint@1077
|
15 |
*/
|
jacint@1077
|
16 |
|
jacint@1077
|
17 |
#ifndef LEMON_MAX_MATCHING_H
|
jacint@1077
|
18 |
#define LEMON_MAX_MATCHING_H
|
jacint@1077
|
19 |
|
jacint@1077
|
20 |
#include <queue>
|
jacint@1077
|
21 |
#include <invalid.h>
|
jacint@1077
|
22 |
#include <unionfind.h>
|
jacint@1077
|
23 |
#include <lemon/graph_utils.h>
|
jacint@1077
|
24 |
|
jacint@1077
|
25 |
///\ingroup galgs
|
jacint@1077
|
26 |
///\file
|
jacint@1077
|
27 |
///\brief Maximum matching algorithm.
|
jacint@1077
|
28 |
|
jacint@1077
|
29 |
namespace lemon {
|
jacint@1077
|
30 |
|
jacint@1077
|
31 |
/// \addtogroup galgs
|
jacint@1077
|
32 |
/// @{
|
jacint@1077
|
33 |
|
jacint@1077
|
34 |
///Edmonds' alternating forest maximum matching algorithm.
|
jacint@1077
|
35 |
|
jacint@1077
|
36 |
///This class provides Edmonds' alternating forest matching
|
jacint@1077
|
37 |
///algorithm. The starting matching (if any) can be passed to the
|
jacint@1077
|
38 |
///algorithm using read-in functions \ref readNMapNode, \ref
|
jacint@1077
|
39 |
///readNMapEdge or \ref readEMapBool depending on the container. The
|
jacint@1077
|
40 |
///resulting maximum matching can be attained by write-out functions
|
jacint@1077
|
41 |
///\ref writeNMapNode, \ref writeNMapEdge or \ref writeEMapBool
|
jacint@1077
|
42 |
///depending on the preferred container.
|
jacint@1077
|
43 |
///
|
jacint@1077
|
44 |
///The dual side of a matching is a map of the nodes to
|
jacint@1077
|
45 |
///MaxMatching::pos_enum, having values D, A and C showing the
|
jacint@1077
|
46 |
///Gallai-Edmonds decomposition of the graph. The nodes in D induce
|
jacint@1077
|
47 |
///a graph with factor-critical components, the nodes in A form the
|
jacint@1077
|
48 |
///barrier, and the nodes in C induce a graph having a perfect
|
jacint@1077
|
49 |
///matching. This decomposition can be attained by calling \ref
|
jacint@1077
|
50 |
///writePos after running the algorithm. Before subsequent runs,
|
jacint@1077
|
51 |
///the function \ref resetPos() must be called.
|
jacint@1077
|
52 |
///
|
jacint@1077
|
53 |
///\param Graph The undirected graph type the algorithm runs on.
|
jacint@1077
|
54 |
///
|
jacint@1077
|
55 |
///\author Jacint Szabo
|
jacint@1077
|
56 |
template <typename Graph>
|
jacint@1077
|
57 |
class MaxMatching {
|
jacint@1077
|
58 |
typedef typename Graph::Node Node;
|
jacint@1077
|
59 |
typedef typename Graph::Edge Edge;
|
jacint@1077
|
60 |
typedef typename Graph::UndirEdgeIt UndirEdgeIt;
|
jacint@1077
|
61 |
typedef typename Graph::NodeIt NodeIt;
|
jacint@1077
|
62 |
typedef typename Graph::IncEdgeIt IncEdgeIt;
|
jacint@1077
|
63 |
|
jacint@1077
|
64 |
typedef UnionFindEnum<Node, Graph::template NodeMap> UFE;
|
jacint@1077
|
65 |
|
jacint@1077
|
66 |
public:
|
jacint@1077
|
67 |
|
jacint@1077
|
68 |
///Indicates the Gallai-Edmonds decomposition of the graph.
|
jacint@1077
|
69 |
|
jacint@1077
|
70 |
///Indicates the Gallai-Edmonds decomposition of the graph, which
|
jacint@1077
|
71 |
///shows an upper bound on the size of a maximum matching. The
|
jacint@1077
|
72 |
///nodes with pos_enum \c D induce a graph with factor-critical
|
jacint@1077
|
73 |
///components, the nodes in \c A form the canonical barrier, and the
|
jacint@1077
|
74 |
///nodes in \c C induce a graph having a perfect matching.
|
jacint@1077
|
75 |
enum pos_enum {
|
jacint@1077
|
76 |
D=0,
|
jacint@1077
|
77 |
A=1,
|
jacint@1077
|
78 |
C=2
|
jacint@1077
|
79 |
};
|
jacint@1077
|
80 |
|
jacint@1077
|
81 |
private:
|
jacint@1077
|
82 |
|
jacint@1077
|
83 |
static const int HEUR_density=2;
|
jacint@1077
|
84 |
const Graph& g;
|
jacint@1077
|
85 |
typename Graph::template NodeMap<Node> mate;
|
jacint@1077
|
86 |
typename Graph::template NodeMap<pos_enum> position;
|
jacint@1077
|
87 |
|
jacint@1077
|
88 |
public:
|
jacint@1077
|
89 |
|
jacint@1077
|
90 |
MaxMatching(const Graph& _g) : g(_g), mate(_g,INVALID), position(_g,C) {}
|
jacint@1077
|
91 |
|
jacint@1077
|
92 |
///Runs Edmonds' algorithm.
|
jacint@1077
|
93 |
|
jacint@1077
|
94 |
///Runs Edmonds' algorithm for sparse graphs (number of edges <
|
jacint@1077
|
95 |
///2*number of nodes), and a heuristical Edmonds' algorithm with a
|
jacint@1077
|
96 |
///heuristic of postponing shrinks for dense graphs. \pre Before
|
jacint@1077
|
97 |
///the subsequent calls \ref resetPos must be called.
|
jacint@1077
|
98 |
inline void run();
|
jacint@1077
|
99 |
|
jacint@1077
|
100 |
///Runs Edmonds' algorithm.
|
jacint@1077
|
101 |
|
jacint@1077
|
102 |
///If heur=0 it runs Edmonds' algorithm. If heur=1 it runs
|
jacint@1077
|
103 |
///Edmonds' algorithm with a heuristic of postponing shrinks,
|
jacint@1077
|
104 |
///giving a faster algorithm for dense graphs. \pre Before the
|
jacint@1077
|
105 |
///subsequent calls \ref resetPos must be called.
|
jacint@1077
|
106 |
void runEdmonds( int heur );
|
jacint@1077
|
107 |
|
jacint@1077
|
108 |
///Finds a greedy matching starting from the actual matching.
|
jacint@1077
|
109 |
|
jacint@1077
|
110 |
///Starting form the actual matching stored, it finds a maximal
|
jacint@1077
|
111 |
///greedy matching.
|
jacint@1077
|
112 |
void greedyMatching();
|
jacint@1077
|
113 |
|
jacint@1077
|
114 |
///Returns the size of the actual matching stored.
|
jacint@1077
|
115 |
|
jacint@1077
|
116 |
///Returns the size of the actual matching stored. After \ref
|
jacint@1077
|
117 |
///run() it returns the size of a maximum matching in the graph.
|
jacint@1077
|
118 |
int size() const;
|
jacint@1077
|
119 |
|
jacint@1077
|
120 |
///Resets the map storing the Gallai-Edmonds decomposition.
|
jacint@1077
|
121 |
|
jacint@1077
|
122 |
///Resets the map storing the Gallai-Edmonds decomposition of the
|
jacint@1077
|
123 |
///graph, making it possible to run the algorithm. Must be called
|
jacint@1077
|
124 |
///before all runs of the Edmonds algorithm, except for the first
|
jacint@1077
|
125 |
///run.
|
jacint@1077
|
126 |
void resetPos();
|
jacint@1077
|
127 |
|
jacint@1077
|
128 |
///Resets the actual matching to the empty matching.
|
jacint@1077
|
129 |
|
jacint@1077
|
130 |
///Resets the actual matching to the empty matching.
|
jacint@1077
|
131 |
///
|
jacint@1077
|
132 |
void resetMatching();
|
jacint@1077
|
133 |
|
jacint@1077
|
134 |
///Reads a matching from a \c Node map of \c Nodes.
|
jacint@1077
|
135 |
|
jacint@1077
|
136 |
///Reads a matching from a \c Node map of \c Nodes. This map must be \e
|
jacint@1077
|
137 |
///symmetric, i.e. if \c map[u]==v then \c map[v]==u must hold, and
|
jacint@1077
|
138 |
///\c uv will be an edge of the matching.
|
jacint@1077
|
139 |
template<typename NMapN>
|
jacint@1077
|
140 |
void readNMapNode(NMapN& map) {
|
jacint@1077
|
141 |
for(NodeIt v(g); v!=INVALID; ++v) {
|
jacint@1077
|
142 |
mate.set(v,map[v]);
|
jacint@1077
|
143 |
}
|
jacint@1077
|
144 |
}
|
jacint@1077
|
145 |
|
jacint@1077
|
146 |
///Writes the stored matching to a \c Node map of \c Nodes.
|
jacint@1077
|
147 |
|
jacint@1077
|
148 |
///Writes the stored matching to a \c Node map of \c Nodes. The
|
jacint@1077
|
149 |
///resulting map will be \e symmetric, i.e. if \c map[u]==v then \c
|
jacint@1077
|
150 |
///map[v]==u will hold, and now \c uv is an edge of the matching.
|
jacint@1077
|
151 |
template<typename NMapN>
|
jacint@1077
|
152 |
void writeNMapNode (NMapN& map) const {
|
jacint@1077
|
153 |
for(NodeIt v(g); v!=INVALID; ++v) {
|
jacint@1077
|
154 |
map.set(v,mate[v]);
|
jacint@1077
|
155 |
}
|
jacint@1077
|
156 |
}
|
jacint@1077
|
157 |
|
jacint@1077
|
158 |
///Reads a matching from a \c Node map of \c Edges.
|
jacint@1077
|
159 |
|
jacint@1077
|
160 |
///Reads a matching from a \c Node map of incident \c Edges. This
|
jacint@1077
|
161 |
///map must have the property that if \c G.target(map[u])==v then \c
|
jacint@1077
|
162 |
///G.target(map[v])==u must hold, and now this edge is an edge of
|
jacint@1077
|
163 |
///the matching.
|
jacint@1077
|
164 |
template<typename NMapE>
|
jacint@1077
|
165 |
void readNMapEdge(NMapE& map) {
|
jacint@1077
|
166 |
for(NodeIt v(g); v!=INVALID; ++v) {
|
jacint@1077
|
167 |
Edge e=map[v];
|
jacint@1077
|
168 |
if ( g.valid(e) )
|
jacint@1077
|
169 |
g.source(e) == v ? mate.set(v,g.target(e)) : mate.set(v,g.source(e));
|
jacint@1077
|
170 |
}
|
jacint@1077
|
171 |
}
|
jacint@1077
|
172 |
|
jacint@1077
|
173 |
///Writes the matching stored to a \c Node map of \c Edges.
|
jacint@1077
|
174 |
|
jacint@1077
|
175 |
///Writes the stored matching to a \c Node map of incident \c
|
jacint@1077
|
176 |
///Edges. This map will have the property that if \c
|
jacint@1077
|
177 |
///g.target(map[u])==v then \c g.target(map[v])==u holds, and now this
|
jacint@1077
|
178 |
///edge is an edge of the matching.
|
jacint@1077
|
179 |
template<typename NMapE>
|
jacint@1077
|
180 |
void writeNMapEdge (NMapE& map) const {
|
jacint@1077
|
181 |
typename Graph::template NodeMap<bool> todo(g,true);
|
jacint@1077
|
182 |
for(NodeIt v(g); v!=INVALID; ++v) {
|
jacint@1077
|
183 |
if ( todo[v] && mate[v]!=INVALID ) {
|
jacint@1077
|
184 |
Node u=mate[v];
|
jacint@1077
|
185 |
for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
|
jacint@1077
|
186 |
if ( g.target(e) == u ) {
|
jacint@1077
|
187 |
map.set(u,e);
|
jacint@1077
|
188 |
map.set(v,e);
|
jacint@1077
|
189 |
todo.set(u,false);
|
jacint@1077
|
190 |
todo.set(v,false);
|
jacint@1077
|
191 |
break;
|
jacint@1077
|
192 |
}
|
jacint@1077
|
193 |
}
|
jacint@1077
|
194 |
}
|
jacint@1077
|
195 |
}
|
jacint@1077
|
196 |
}
|
jacint@1077
|
197 |
|
jacint@1077
|
198 |
|
jacint@1077
|
199 |
///Reads a matching from an \c Edge map of \c bools.
|
jacint@1077
|
200 |
|
jacint@1077
|
201 |
///Reads a matching from an \c Edge map of \c bools. This map must
|
jacint@1077
|
202 |
///have the property that there are no two adjacent edges \c e, \c
|
jacint@1077
|
203 |
///f with \c map[e]==map[f]==true. The edges \c e with \c
|
jacint@1077
|
204 |
///map[e]==true form the matching.
|
jacint@1077
|
205 |
template<typename EMapB>
|
jacint@1077
|
206 |
void readEMapBool(EMapB& map) {
|
jacint@1077
|
207 |
for(UndirEdgeIt e(g); e!=INVALID; ++e) {
|
jacint@1077
|
208 |
if ( map[e] ) {
|
jacint@1077
|
209 |
Node u=g.source(e);
|
jacint@1077
|
210 |
Node v=g.target(e);
|
jacint@1077
|
211 |
mate.set(u,v);
|
jacint@1077
|
212 |
mate.set(v,u);
|
jacint@1077
|
213 |
}
|
jacint@1077
|
214 |
}
|
jacint@1077
|
215 |
}
|
jacint@1077
|
216 |
|
jacint@1077
|
217 |
|
jacint@1077
|
218 |
///Writes the matching stored to an \c Edge map of \c bools.
|
jacint@1077
|
219 |
|
jacint@1077
|
220 |
///Writes the matching stored to an \c Edge map of \c bools. This
|
jacint@1077
|
221 |
///map will have the property that there are no two adjacent edges
|
jacint@1077
|
222 |
///\c e, \c f with \c map[e]==map[f]==true. The edges \c e with \c
|
jacint@1077
|
223 |
///map[e]==true form the matching.
|
jacint@1077
|
224 |
template<typename EMapB>
|
jacint@1077
|
225 |
void writeEMapBool (EMapB& map) const {
|
jacint@1077
|
226 |
for(UndirEdgeIt e(g); e!=INVALID; ++e) map.set(e,false);
|
jacint@1077
|
227 |
|
jacint@1077
|
228 |
typename Graph::template NodeMap<bool> todo(g,true);
|
jacint@1077
|
229 |
for(NodeIt v(g); v!=INVALID; ++v) {
|
jacint@1077
|
230 |
if ( todo[v] && mate[v]!=INVALID ) {
|
jacint@1077
|
231 |
Node u=mate[v];
|
jacint@1077
|
232 |
for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
|
jacint@1077
|
233 |
if ( g.target(e) == u ) {
|
jacint@1077
|
234 |
map.set(e,true);
|
jacint@1077
|
235 |
todo.set(u,false);
|
jacint@1077
|
236 |
todo.set(v,false);
|
jacint@1077
|
237 |
break;
|
jacint@1077
|
238 |
}
|
jacint@1077
|
239 |
}
|
jacint@1077
|
240 |
}
|
jacint@1077
|
241 |
}
|
jacint@1077
|
242 |
}
|
jacint@1077
|
243 |
|
jacint@1077
|
244 |
|
jacint@1077
|
245 |
///Writes the canonical decomposition of the graph after running
|
jacint@1077
|
246 |
///the algorithm.
|
jacint@1077
|
247 |
|
jacint@1077
|
248 |
///After calling any run methods of the class, and before calling
|
jacint@1077
|
249 |
///\ref resetPos(), it writes the Gallai-Edmonds canonical
|
jacint@1077
|
250 |
///decomposition of the graph. \c map must be a node map
|
jacint@1077
|
251 |
///of \ref pos_enum 's.
|
jacint@1077
|
252 |
template<typename NMapEnum>
|
jacint@1077
|
253 |
void writePos (NMapEnum& map) const {
|
jacint@1077
|
254 |
for(NodeIt v(g); v!=INVALID; ++v) map.set(v,position[v]);
|
jacint@1077
|
255 |
}
|
jacint@1077
|
256 |
|
jacint@1077
|
257 |
private:
|
jacint@1077
|
258 |
|
jacint@1077
|
259 |
void lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
|
jacint@1077
|
260 |
UFE& blossom, UFE& tree);
|
jacint@1077
|
261 |
|
jacint@1077
|
262 |
void normShrink(Node v, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
263 |
UFE& blossom, UFE& tree);
|
jacint@1077
|
264 |
|
jacint@1077
|
265 |
bool noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
266 |
UFE& blossom, UFE& tree, std::queue<Node>& Q);
|
jacint@1077
|
267 |
|
jacint@1077
|
268 |
void shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
269 |
UFE& blossom, UFE& tree, std::queue<Node>& Q);
|
jacint@1077
|
270 |
|
jacint@1077
|
271 |
void augment(Node x, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
272 |
UFE& blossom, UFE& tree);
|
jacint@1077
|
273 |
|
jacint@1077
|
274 |
};
|
jacint@1077
|
275 |
|
jacint@1077
|
276 |
|
jacint@1077
|
277 |
// **********************************************************************
|
jacint@1077
|
278 |
// IMPLEMENTATIONS
|
jacint@1077
|
279 |
// **********************************************************************
|
jacint@1077
|
280 |
|
jacint@1077
|
281 |
|
jacint@1077
|
282 |
template <typename Graph>
|
jacint@1077
|
283 |
void MaxMatching<Graph>::run() {
|
jacint@1077
|
284 |
if ( countUndirEdges(g) < HEUR_density*countNodes(g) ) {
|
jacint@1077
|
285 |
greedyMatching();
|
jacint@1077
|
286 |
runEdmonds(0);
|
jacint@1077
|
287 |
} else runEdmonds(1);
|
jacint@1077
|
288 |
}
|
jacint@1077
|
289 |
|
jacint@1077
|
290 |
|
jacint@1077
|
291 |
template <typename Graph>
|
jacint@1077
|
292 |
void MaxMatching<Graph>::runEdmonds( int heur=1 ) {
|
jacint@1077
|
293 |
|
jacint@1077
|
294 |
typename Graph::template NodeMap<Node> ear(g,INVALID);
|
jacint@1077
|
295 |
//undefined for the base nodes of the blossoms (i.e. for the
|
jacint@1077
|
296 |
//representative elements of UFE blossom) and for the nodes in C
|
jacint@1077
|
297 |
|
jacint@1077
|
298 |
typename UFE::MapType blossom_base(g);
|
jacint@1077
|
299 |
UFE blossom(blossom_base);
|
jacint@1077
|
300 |
typename UFE::MapType tree_base(g);
|
jacint@1077
|
301 |
UFE tree(tree_base);
|
jacint@1077
|
302 |
|
jacint@1077
|
303 |
for(NodeIt v(g); v!=INVALID; ++v) {
|
jacint@1077
|
304 |
if ( position[v]==C && mate[v]==INVALID ) {
|
jacint@1077
|
305 |
blossom.insert(v);
|
jacint@1077
|
306 |
tree.insert(v);
|
jacint@1077
|
307 |
position.set(v,D);
|
jacint@1077
|
308 |
if ( heur == 1 ) lateShrink( v, ear, blossom, tree );
|
jacint@1077
|
309 |
else normShrink( v, ear, blossom, tree );
|
jacint@1077
|
310 |
}
|
jacint@1077
|
311 |
}
|
jacint@1077
|
312 |
}
|
jacint@1077
|
313 |
|
jacint@1077
|
314 |
|
jacint@1077
|
315 |
template <typename Graph>
|
jacint@1077
|
316 |
void MaxMatching<Graph>::lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
|
jacint@1077
|
317 |
UFE& blossom, UFE& tree) {
|
jacint@1077
|
318 |
|
jacint@1077
|
319 |
std::queue<Node> Q; //queue of the totally unscanned nodes
|
jacint@1077
|
320 |
Q.push(v);
|
jacint@1077
|
321 |
std::queue<Node> R;
|
jacint@1077
|
322 |
//queue of the nodes which must be scanned for a possible shrink
|
jacint@1077
|
323 |
|
jacint@1077
|
324 |
while ( !Q.empty() ) {
|
jacint@1077
|
325 |
Node x=Q.front();
|
jacint@1077
|
326 |
Q.pop();
|
jacint@1077
|
327 |
if ( noShrinkStep( x, ear, blossom, tree, Q ) ) return;
|
jacint@1077
|
328 |
else R.push(x);
|
jacint@1077
|
329 |
}
|
jacint@1077
|
330 |
|
jacint@1077
|
331 |
while ( !R.empty() ) {
|
jacint@1077
|
332 |
Node x=R.front();
|
jacint@1077
|
333 |
R.pop();
|
jacint@1077
|
334 |
|
jacint@1077
|
335 |
for( IncEdgeIt e(g,x); e!=INVALID ; ++e ) {
|
jacint@1077
|
336 |
Node y=g.target(e);
|
jacint@1077
|
337 |
|
jacint@1077
|
338 |
if ( position[y] == D && blossom.find(x) != blossom.find(y) ) {
|
jacint@1077
|
339 |
//x and y must be in the same tree
|
jacint@1077
|
340 |
|
jacint@1077
|
341 |
typename Graph::template NodeMap<bool> path(g,false);
|
jacint@1077
|
342 |
|
jacint@1077
|
343 |
Node b=blossom.find(x);
|
jacint@1077
|
344 |
path.set(b,true);
|
jacint@1077
|
345 |
b=mate[b];
|
jacint@1077
|
346 |
while ( b!=INVALID ) {
|
jacint@1077
|
347 |
b=blossom.find(ear[b]);
|
jacint@1077
|
348 |
path.set(b,true);
|
jacint@1077
|
349 |
b=mate[b];
|
jacint@1077
|
350 |
} //going till the root
|
jacint@1077
|
351 |
|
jacint@1077
|
352 |
Node top=y;
|
jacint@1077
|
353 |
Node middle=blossom.find(top);
|
jacint@1077
|
354 |
Node bottom=x;
|
jacint@1077
|
355 |
while ( !path[middle] )
|
jacint@1077
|
356 |
shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
|
jacint@1077
|
357 |
|
jacint@1077
|
358 |
Node base=middle;
|
jacint@1077
|
359 |
top=x;
|
jacint@1077
|
360 |
middle=blossom.find(top);
|
jacint@1077
|
361 |
bottom=y;
|
jacint@1077
|
362 |
Node blossom_base=blossom.find(base);
|
jacint@1077
|
363 |
while ( middle!=blossom_base )
|
jacint@1077
|
364 |
shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
|
jacint@1077
|
365 |
|
jacint@1077
|
366 |
blossom.makeRep(base);
|
jacint@1077
|
367 |
} // if shrink is needed
|
jacint@1077
|
368 |
|
jacint@1077
|
369 |
while ( !Q.empty() ) {
|
jacint@1077
|
370 |
Node x=Q.front();
|
jacint@1077
|
371 |
Q.pop();
|
jacint@1077
|
372 |
if ( noShrinkStep(x, ear, blossom, tree, Q) ) return;
|
jacint@1077
|
373 |
else R.push(x);
|
jacint@1077
|
374 |
}
|
jacint@1077
|
375 |
} //for e
|
jacint@1077
|
376 |
} // while ( !R.empty() )
|
jacint@1077
|
377 |
}
|
jacint@1077
|
378 |
|
jacint@1077
|
379 |
|
jacint@1077
|
380 |
template <typename Graph>
|
jacint@1077
|
381 |
void MaxMatching<Graph>::normShrink(Node v, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
382 |
UFE& blossom, UFE& tree) {
|
jacint@1077
|
383 |
|
jacint@1077
|
384 |
std::queue<Node> Q; //queue of the unscanned nodes
|
jacint@1077
|
385 |
Q.push(v);
|
jacint@1077
|
386 |
while ( !Q.empty() ) {
|
jacint@1077
|
387 |
|
jacint@1077
|
388 |
Node x=Q.front();
|
jacint@1077
|
389 |
Q.pop();
|
jacint@1077
|
390 |
|
jacint@1077
|
391 |
for( IncEdgeIt e(g,x); e!=INVALID; ++e ) {
|
jacint@1077
|
392 |
Node y=g.target(e);
|
jacint@1077
|
393 |
|
jacint@1077
|
394 |
switch ( position[y] ) {
|
jacint@1077
|
395 |
case D: //x and y must be in the same tree
|
jacint@1077
|
396 |
|
jacint@1077
|
397 |
if ( blossom.find(x) != blossom.find(y) ) { //shrink
|
jacint@1077
|
398 |
typename Graph::template NodeMap<bool> path(g,false);
|
jacint@1077
|
399 |
|
jacint@1077
|
400 |
Node b=blossom.find(x);
|
jacint@1077
|
401 |
path.set(b,true);
|
jacint@1077
|
402 |
b=mate[b];
|
jacint@1077
|
403 |
while ( b!=INVALID ) {
|
jacint@1077
|
404 |
b=blossom.find(ear[b]);
|
jacint@1077
|
405 |
path.set(b,true);
|
jacint@1077
|
406 |
b=mate[b];
|
jacint@1077
|
407 |
} //going till the root
|
jacint@1077
|
408 |
|
jacint@1077
|
409 |
Node top=y;
|
jacint@1077
|
410 |
Node middle=blossom.find(top);
|
jacint@1077
|
411 |
Node bottom=x;
|
jacint@1077
|
412 |
while ( !path[middle] )
|
jacint@1077
|
413 |
shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
|
jacint@1077
|
414 |
|
jacint@1077
|
415 |
Node base=middle;
|
jacint@1077
|
416 |
top=x;
|
jacint@1077
|
417 |
middle=blossom.find(top);
|
jacint@1077
|
418 |
bottom=y;
|
jacint@1077
|
419 |
Node blossom_base=blossom.find(base);
|
jacint@1077
|
420 |
while ( middle!=blossom_base )
|
jacint@1077
|
421 |
shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
|
jacint@1077
|
422 |
|
jacint@1077
|
423 |
blossom.makeRep(base);
|
jacint@1077
|
424 |
}
|
jacint@1077
|
425 |
break;
|
jacint@1077
|
426 |
case C:
|
jacint@1077
|
427 |
if ( mate[y]!=INVALID ) { //grow
|
jacint@1077
|
428 |
|
jacint@1077
|
429 |
ear.set(y,x);
|
jacint@1077
|
430 |
Node w=mate[y];
|
jacint@1077
|
431 |
blossom.insert(w);
|
jacint@1077
|
432 |
position.set(y,A);
|
jacint@1077
|
433 |
position.set(w,D);
|
jacint@1077
|
434 |
tree.insert(y);
|
jacint@1077
|
435 |
tree.insert(w);
|
jacint@1077
|
436 |
tree.join(y,blossom.find(x));
|
jacint@1077
|
437 |
tree.join(w,y);
|
jacint@1077
|
438 |
Q.push(w);
|
jacint@1077
|
439 |
} else { //augment
|
jacint@1077
|
440 |
augment(x, ear, blossom, tree);
|
jacint@1077
|
441 |
mate.set(x,y);
|
jacint@1077
|
442 |
mate.set(y,x);
|
jacint@1077
|
443 |
return;
|
jacint@1077
|
444 |
} //if
|
jacint@1077
|
445 |
break;
|
jacint@1077
|
446 |
default: break;
|
jacint@1077
|
447 |
}
|
jacint@1077
|
448 |
}
|
jacint@1077
|
449 |
}
|
jacint@1077
|
450 |
}
|
jacint@1077
|
451 |
|
jacint@1077
|
452 |
template <typename Graph>
|
jacint@1077
|
453 |
void MaxMatching<Graph>::greedyMatching() {
|
jacint@1077
|
454 |
for(NodeIt v(g); v!=INVALID; ++v)
|
jacint@1077
|
455 |
if ( mate[v]==INVALID ) {
|
jacint@1077
|
456 |
for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) {
|
jacint@1077
|
457 |
Node y=g.target(e);
|
jacint@1077
|
458 |
if ( mate[y]==INVALID && y!=v ) {
|
jacint@1077
|
459 |
mate.set(v,y);
|
jacint@1077
|
460 |
mate.set(y,v);
|
jacint@1077
|
461 |
break;
|
jacint@1077
|
462 |
}
|
jacint@1077
|
463 |
}
|
jacint@1077
|
464 |
}
|
jacint@1077
|
465 |
}
|
jacint@1077
|
466 |
|
jacint@1077
|
467 |
template <typename Graph>
|
jacint@1077
|
468 |
int MaxMatching<Graph>::size() const {
|
jacint@1077
|
469 |
int s=0;
|
jacint@1077
|
470 |
for(NodeIt v(g); v!=INVALID; ++v) {
|
jacint@1077
|
471 |
if ( mate[v]!=INVALID ) {
|
jacint@1077
|
472 |
++s;
|
jacint@1077
|
473 |
}
|
jacint@1077
|
474 |
}
|
jacint@1077
|
475 |
return (int)s/2;
|
jacint@1077
|
476 |
}
|
jacint@1077
|
477 |
|
jacint@1077
|
478 |
template <typename Graph>
|
jacint@1077
|
479 |
void MaxMatching<Graph>::resetPos() {
|
jacint@1077
|
480 |
for(NodeIt v(g); v!=INVALID; ++v)
|
jacint@1077
|
481 |
position.set(v,C);
|
jacint@1077
|
482 |
}
|
jacint@1077
|
483 |
|
jacint@1077
|
484 |
template <typename Graph>
|
jacint@1077
|
485 |
void MaxMatching<Graph>::resetMatching() {
|
jacint@1077
|
486 |
for(NodeIt v(g); v!=INVALID; ++v)
|
jacint@1077
|
487 |
mate.set(v,INVALID);
|
jacint@1077
|
488 |
}
|
jacint@1077
|
489 |
|
jacint@1077
|
490 |
template <typename Graph>
|
jacint@1077
|
491 |
bool MaxMatching<Graph>::noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
492 |
UFE& blossom, UFE& tree, std::queue<Node>& Q) {
|
jacint@1077
|
493 |
for( IncEdgeIt e(g,x); e!= INVALID; ++e ) {
|
jacint@1077
|
494 |
Node y=g.target(e);
|
jacint@1077
|
495 |
|
jacint@1077
|
496 |
if ( position[y]==C ) {
|
jacint@1077
|
497 |
if ( mate[y]!=INVALID ) { //grow
|
jacint@1077
|
498 |
ear.set(y,x);
|
jacint@1077
|
499 |
Node w=mate[y];
|
jacint@1077
|
500 |
blossom.insert(w);
|
jacint@1077
|
501 |
position.set(y,A);
|
jacint@1077
|
502 |
position.set(w,D);
|
jacint@1077
|
503 |
tree.insert(y);
|
jacint@1077
|
504 |
tree.insert(w);
|
jacint@1077
|
505 |
tree.join(y,blossom.find(x));
|
jacint@1077
|
506 |
tree.join(w,y);
|
jacint@1077
|
507 |
Q.push(w);
|
jacint@1077
|
508 |
} else { //augment
|
jacint@1077
|
509 |
augment(x, ear, blossom, tree);
|
jacint@1077
|
510 |
mate.set(x,y);
|
jacint@1077
|
511 |
mate.set(y,x);
|
jacint@1077
|
512 |
return true;
|
jacint@1077
|
513 |
}
|
jacint@1077
|
514 |
}
|
jacint@1077
|
515 |
}
|
jacint@1077
|
516 |
return false;
|
jacint@1077
|
517 |
}
|
jacint@1077
|
518 |
|
jacint@1077
|
519 |
template <typename Graph>
|
jacint@1077
|
520 |
void MaxMatching<Graph>::shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
521 |
UFE& blossom, UFE& tree, std::queue<Node>& Q) {
|
jacint@1077
|
522 |
ear.set(top,bottom);
|
jacint@1077
|
523 |
Node t=top;
|
jacint@1077
|
524 |
while ( t!=middle ) {
|
jacint@1077
|
525 |
Node u=mate[t];
|
jacint@1077
|
526 |
t=ear[u];
|
jacint@1077
|
527 |
ear.set(t,u);
|
jacint@1077
|
528 |
}
|
jacint@1077
|
529 |
bottom=mate[middle];
|
jacint@1077
|
530 |
position.set(bottom,D);
|
jacint@1077
|
531 |
Q.push(bottom);
|
jacint@1077
|
532 |
top=ear[bottom];
|
jacint@1077
|
533 |
Node oldmiddle=middle;
|
jacint@1077
|
534 |
middle=blossom.find(top);
|
jacint@1077
|
535 |
tree.erase(bottom);
|
jacint@1077
|
536 |
tree.erase(oldmiddle);
|
jacint@1077
|
537 |
blossom.insert(bottom);
|
jacint@1077
|
538 |
blossom.join(bottom, oldmiddle);
|
jacint@1077
|
539 |
blossom.join(top, oldmiddle);
|
jacint@1077
|
540 |
}
|
jacint@1077
|
541 |
|
jacint@1077
|
542 |
template <typename Graph>
|
jacint@1077
|
543 |
void MaxMatching<Graph>::augment(Node x, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
544 |
UFE& blossom, UFE& tree) {
|
jacint@1077
|
545 |
Node v=mate[x];
|
jacint@1077
|
546 |
while ( v!=INVALID ) {
|
jacint@1077
|
547 |
|
jacint@1077
|
548 |
Node u=ear[v];
|
jacint@1077
|
549 |
mate.set(v,u);
|
jacint@1077
|
550 |
Node tmp=v;
|
jacint@1077
|
551 |
v=mate[u];
|
jacint@1077
|
552 |
mate.set(u,tmp);
|
jacint@1077
|
553 |
}
|
jacint@1077
|
554 |
typename UFE::ItemIt it;
|
jacint@1077
|
555 |
for (tree.first(it,blossom.find(x)); tree.valid(it); tree.next(it)) {
|
jacint@1077
|
556 |
if ( position[it] == D ) {
|
jacint@1077
|
557 |
typename UFE::ItemIt b_it;
|
jacint@1077
|
558 |
for (blossom.first(b_it,it); blossom.valid(b_it); blossom.next(b_it)) {
|
jacint@1077
|
559 |
position.set( b_it ,C);
|
jacint@1077
|
560 |
}
|
jacint@1077
|
561 |
blossom.eraseClass(it);
|
jacint@1077
|
562 |
} else position.set( it ,C);
|
jacint@1077
|
563 |
}
|
jacint@1077
|
564 |
tree.eraseClass(x);
|
jacint@1077
|
565 |
|
jacint@1077
|
566 |
}
|
jacint@1077
|
567 |
|
jacint@1077
|
568 |
/// @}
|
jacint@1077
|
569 |
|
jacint@1077
|
570 |
} //END OF NAMESPACE LEMON
|
jacint@1077
|
571 |
|
jacint@1077
|
572 |
#endif //EDMONDS_H
|