jacint@1077
|
1 |
/* -*- C++ -*-
|
jacint@1077
|
2 |
* src/lemon/max_matching.h - Part of LEMON, a generic C++ optimization library
|
jacint@1077
|
3 |
*
|
alpar@1164
|
4 |
* Copyright (C) 2005 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@1093
|
21 |
#include <lemon/invalid.h>
|
jacint@1093
|
22 |
#include <lemon/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@1090
|
50 |
///writePos after running the algorithm.
|
jacint@1077
|
51 |
///
|
jacint@1077
|
52 |
///\param Graph The undirected graph type the algorithm runs on.
|
jacint@1077
|
53 |
///
|
jacint@1077
|
54 |
///\author Jacint Szabo
|
jacint@1077
|
55 |
template <typename Graph>
|
jacint@1077
|
56 |
class MaxMatching {
|
jacint@1165
|
57 |
|
jacint@1165
|
58 |
protected:
|
jacint@1165
|
59 |
|
jacint@1077
|
60 |
typedef typename Graph::Node Node;
|
jacint@1077
|
61 |
typedef typename Graph::Edge Edge;
|
jacint@1077
|
62 |
typedef typename Graph::UndirEdgeIt UndirEdgeIt;
|
jacint@1077
|
63 |
typedef typename Graph::NodeIt NodeIt;
|
jacint@1077
|
64 |
typedef typename Graph::IncEdgeIt IncEdgeIt;
|
jacint@1077
|
65 |
|
jacint@1077
|
66 |
typedef UnionFindEnum<Node, Graph::template NodeMap> UFE;
|
jacint@1077
|
67 |
|
jacint@1077
|
68 |
public:
|
jacint@1077
|
69 |
|
jacint@1077
|
70 |
///Indicates the Gallai-Edmonds decomposition of the graph.
|
jacint@1077
|
71 |
|
jacint@1077
|
72 |
///Indicates the Gallai-Edmonds decomposition of the graph, which
|
jacint@1077
|
73 |
///shows an upper bound on the size of a maximum matching. The
|
jacint@1077
|
74 |
///nodes with pos_enum \c D induce a graph with factor-critical
|
jacint@1077
|
75 |
///components, the nodes in \c A form the canonical barrier, and the
|
jacint@1077
|
76 |
///nodes in \c C induce a graph having a perfect matching.
|
jacint@1077
|
77 |
enum pos_enum {
|
jacint@1077
|
78 |
D=0,
|
jacint@1077
|
79 |
A=1,
|
jacint@1077
|
80 |
C=2
|
jacint@1077
|
81 |
};
|
jacint@1077
|
82 |
|
jacint@1165
|
83 |
protected:
|
jacint@1077
|
84 |
|
jacint@1077
|
85 |
static const int HEUR_density=2;
|
jacint@1077
|
86 |
const Graph& g;
|
jacint@1093
|
87 |
typename Graph::template NodeMap<Node> _mate;
|
jacint@1077
|
88 |
typename Graph::template NodeMap<pos_enum> position;
|
jacint@1077
|
89 |
|
jacint@1077
|
90 |
public:
|
jacint@1077
|
91 |
|
jacint@1093
|
92 |
MaxMatching(const Graph& _g) : g(_g), _mate(_g,INVALID), position(_g) {}
|
jacint@1077
|
93 |
|
jacint@1077
|
94 |
///Runs Edmonds' algorithm.
|
jacint@1077
|
95 |
|
jacint@1077
|
96 |
///Runs Edmonds' algorithm for sparse graphs (number of edges <
|
jacint@1077
|
97 |
///2*number of nodes), and a heuristical Edmonds' algorithm with a
|
jacint@1090
|
98 |
///heuristic of postponing shrinks for dense graphs.
|
jacint@1077
|
99 |
inline void run();
|
jacint@1077
|
100 |
|
jacint@1077
|
101 |
///Runs Edmonds' algorithm.
|
jacint@1077
|
102 |
|
jacint@1077
|
103 |
///If heur=0 it runs Edmonds' algorithm. If heur=1 it runs
|
jacint@1077
|
104 |
///Edmonds' algorithm with a heuristic of postponing shrinks,
|
jacint@1090
|
105 |
///giving a faster algorithm for dense graphs.
|
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 actual matching to the empty matching.
|
jacint@1077
|
121 |
|
jacint@1077
|
122 |
///Resets the actual matching to the empty matching.
|
jacint@1077
|
123 |
///
|
jacint@1077
|
124 |
void resetMatching();
|
jacint@1077
|
125 |
|
jacint@1093
|
126 |
///Returns the mate of a node in the actual matching.
|
jacint@1093
|
127 |
|
jacint@1093
|
128 |
///Returns the mate of a \c node in the actual matching.
|
jacint@1093
|
129 |
///Returns INVALID if the \c node is not covered by the actual matching.
|
jacint@1093
|
130 |
Node mate(Node& node) const {
|
jacint@1093
|
131 |
return _mate[node];
|
jacint@1093
|
132 |
}
|
jacint@1093
|
133 |
|
jacint@1165
|
134 |
///Reads a matching from a \c Node valued \c Node map.
|
jacint@1077
|
135 |
|
jacint@1165
|
136 |
///Reads a matching from a \c Node valued \c Node map. This map
|
jacint@1165
|
137 |
///must be \e symmetric, i.e. if \c map[u]==v then \c map[v]==u
|
jacint@1165
|
138 |
///must hold, and \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@1093
|
142 |
_mate.set(v,map[v]);
|
jacint@1077
|
143 |
}
|
jacint@1077
|
144 |
}
|
jacint@1077
|
145 |
|
jacint@1165
|
146 |
///Writes the stored matching to a \c Node valued \c Node map.
|
jacint@1077
|
147 |
|
jacint@1165
|
148 |
///Writes the stored matching to a \c Node valued \c Node map. 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@1093
|
154 |
map.set(v,_mate[v]);
|
jacint@1077
|
155 |
}
|
jacint@1077
|
156 |
}
|
jacint@1077
|
157 |
|
jacint@1165
|
158 |
///Reads a matching from an \c UndirEdge valued \c Node map.
|
jacint@1077
|
159 |
|
jacint@1165
|
160 |
///Reads a matching from an \c UndirEdge valued \c Node map. \c
|
jacint@1165
|
161 |
///map[v] must be an \c UndirEdge incident to \c v. This map must
|
jacint@1165
|
162 |
///have the property that if \c g.oppositeNode(u,map[u])==v then
|
jacint@1165
|
163 |
///\c \c g.oppositeNode(v,map[v])==u holds, and now some edge
|
marci@1172
|
164 |
///joining \c u to \c v will be an edge of the matching.
|
jacint@1077
|
165 |
template<typename NMapE>
|
jacint@1077
|
166 |
void readNMapEdge(NMapE& map) {
|
jacint@1077
|
167 |
for(NodeIt v(g); v!=INVALID; ++v) {
|
jacint@1165
|
168 |
UndirEdge e=map[v];
|
jacint@1165
|
169 |
if ( e!=INVALID )
|
jacint@1166
|
170 |
_mate.set(v,g.oppositeNode(v,e));
|
jacint@1077
|
171 |
}
|
jacint@1077
|
172 |
}
|
jacint@1077
|
173 |
|
jacint@1165
|
174 |
///Writes the matching stored to an \c UndirEdge valued \c Node map.
|
jacint@1077
|
175 |
|
jacint@1165
|
176 |
///Writes the stored matching to an \c UndirEdge valued \c Node
|
jacint@1165
|
177 |
///map. \c map[v] will be an \c UndirEdge incident to \c v. This
|
jacint@1165
|
178 |
///map will have the property that if \c g.oppositeNode(u,map[u])
|
jacint@1165
|
179 |
///== v then \c map[u]==map[v] holds, and now this edge is an edge
|
jacint@1165
|
180 |
///of the matching.
|
jacint@1077
|
181 |
template<typename NMapE>
|
jacint@1077
|
182 |
void writeNMapEdge (NMapE& map) const {
|
jacint@1077
|
183 |
typename Graph::template NodeMap<bool> todo(g,true);
|
jacint@1077
|
184 |
for(NodeIt v(g); v!=INVALID; ++v) {
|
jacint@1093
|
185 |
if ( todo[v] && _mate[v]!=INVALID ) {
|
jacint@1093
|
186 |
Node u=_mate[v];
|
jacint@1077
|
187 |
for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
|
klao@1158
|
188 |
if ( g.runningNode(e) == u ) {
|
jacint@1077
|
189 |
map.set(u,e);
|
jacint@1077
|
190 |
map.set(v,e);
|
jacint@1077
|
191 |
todo.set(u,false);
|
jacint@1077
|
192 |
todo.set(v,false);
|
jacint@1077
|
193 |
break;
|
jacint@1077
|
194 |
}
|
jacint@1077
|
195 |
}
|
jacint@1077
|
196 |
}
|
jacint@1077
|
197 |
}
|
jacint@1077
|
198 |
}
|
jacint@1077
|
199 |
|
jacint@1077
|
200 |
|
jacint@1165
|
201 |
///Reads a matching from a \c bool valued \c Edge map.
|
jacint@1077
|
202 |
|
jacint@1165
|
203 |
///Reads a matching from a \c bool valued \c Edge map. This map
|
jacint@1165
|
204 |
///must have the property that there are no two incident edges \c
|
jacint@1165
|
205 |
///e, \c f with \c map[e]==map[f]==true. The edges \c e with \c
|
jacint@1077
|
206 |
///map[e]==true form the matching.
|
jacint@1077
|
207 |
template<typename EMapB>
|
jacint@1077
|
208 |
void readEMapBool(EMapB& map) {
|
jacint@1077
|
209 |
for(UndirEdgeIt e(g); e!=INVALID; ++e) {
|
jacint@1077
|
210 |
if ( map[e] ) {
|
jacint@1077
|
211 |
Node u=g.source(e);
|
jacint@1077
|
212 |
Node v=g.target(e);
|
jacint@1093
|
213 |
_mate.set(u,v);
|
jacint@1093
|
214 |
_mate.set(v,u);
|
jacint@1077
|
215 |
}
|
jacint@1077
|
216 |
}
|
jacint@1077
|
217 |
}
|
jacint@1077
|
218 |
|
jacint@1077
|
219 |
|
jacint@1165
|
220 |
///Writes the matching stored to a \c bool valued \c Edge map.
|
jacint@1077
|
221 |
|
jacint@1165
|
222 |
///Writes the matching stored to a \c bool valued \c Edge
|
jacint@1165
|
223 |
///map. This map will have the property that there are no two
|
jacint@1165
|
224 |
///incident edges \c e, \c f with \c map[e]==map[f]==true. The
|
jacint@1165
|
225 |
///edges \c e with \c map[e]==true form the matching.
|
jacint@1077
|
226 |
template<typename EMapB>
|
jacint@1077
|
227 |
void writeEMapBool (EMapB& map) const {
|
jacint@1077
|
228 |
for(UndirEdgeIt e(g); e!=INVALID; ++e) map.set(e,false);
|
jacint@1077
|
229 |
|
jacint@1077
|
230 |
typename Graph::template NodeMap<bool> todo(g,true);
|
jacint@1077
|
231 |
for(NodeIt v(g); v!=INVALID; ++v) {
|
jacint@1093
|
232 |
if ( todo[v] && _mate[v]!=INVALID ) {
|
jacint@1093
|
233 |
Node u=_mate[v];
|
jacint@1077
|
234 |
for(IncEdgeIt e(g,v); e!=INVALID; ++e) {
|
klao@1158
|
235 |
if ( g.runningNode(e) == u ) {
|
jacint@1077
|
236 |
map.set(e,true);
|
jacint@1077
|
237 |
todo.set(u,false);
|
jacint@1077
|
238 |
todo.set(v,false);
|
jacint@1077
|
239 |
break;
|
jacint@1077
|
240 |
}
|
jacint@1077
|
241 |
}
|
jacint@1077
|
242 |
}
|
jacint@1077
|
243 |
}
|
jacint@1077
|
244 |
}
|
jacint@1077
|
245 |
|
jacint@1077
|
246 |
|
jacint@1077
|
247 |
///Writes the canonical decomposition of the graph after running
|
jacint@1077
|
248 |
///the algorithm.
|
jacint@1077
|
249 |
|
jacint@1090
|
250 |
///After calling any run methods of the class, it writes the
|
jacint@1090
|
251 |
///Gallai-Edmonds canonical decomposition of the graph. \c map
|
jacint@1090
|
252 |
///must be a node map of \ref pos_enum 's.
|
jacint@1077
|
253 |
template<typename NMapEnum>
|
jacint@1077
|
254 |
void writePos (NMapEnum& map) const {
|
jacint@1077
|
255 |
for(NodeIt v(g); v!=INVALID; ++v) map.set(v,position[v]);
|
jacint@1077
|
256 |
}
|
jacint@1077
|
257 |
|
jacint@1077
|
258 |
private:
|
jacint@1077
|
259 |
|
jacint@1165
|
260 |
|
jacint@1077
|
261 |
void lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
|
jacint@1077
|
262 |
UFE& blossom, UFE& tree);
|
jacint@1077
|
263 |
|
jacint@1077
|
264 |
void normShrink(Node v, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
265 |
UFE& blossom, UFE& tree);
|
jacint@1077
|
266 |
|
jacint@1077
|
267 |
bool noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
268 |
UFE& blossom, UFE& tree, std::queue<Node>& Q);
|
jacint@1077
|
269 |
|
jacint@1077
|
270 |
void shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
271 |
UFE& blossom, UFE& tree, std::queue<Node>& Q);
|
jacint@1077
|
272 |
|
jacint@1077
|
273 |
void augment(Node x, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
274 |
UFE& blossom, UFE& tree);
|
jacint@1077
|
275 |
|
jacint@1077
|
276 |
};
|
jacint@1077
|
277 |
|
jacint@1077
|
278 |
|
jacint@1077
|
279 |
// **********************************************************************
|
jacint@1077
|
280 |
// IMPLEMENTATIONS
|
jacint@1077
|
281 |
// **********************************************************************
|
jacint@1077
|
282 |
|
jacint@1077
|
283 |
|
jacint@1077
|
284 |
template <typename Graph>
|
jacint@1077
|
285 |
void MaxMatching<Graph>::run() {
|
jacint@1077
|
286 |
if ( countUndirEdges(g) < HEUR_density*countNodes(g) ) {
|
jacint@1077
|
287 |
greedyMatching();
|
jacint@1077
|
288 |
runEdmonds(0);
|
jacint@1077
|
289 |
} else runEdmonds(1);
|
jacint@1077
|
290 |
}
|
jacint@1077
|
291 |
|
jacint@1077
|
292 |
|
jacint@1077
|
293 |
template <typename Graph>
|
jacint@1077
|
294 |
void MaxMatching<Graph>::runEdmonds( int heur=1 ) {
|
jacint@1090
|
295 |
|
jacint@1090
|
296 |
for(NodeIt v(g); v!=INVALID; ++v)
|
jacint@1090
|
297 |
position.set(v,C);
|
jacint@1090
|
298 |
|
jacint@1077
|
299 |
typename Graph::template NodeMap<Node> ear(g,INVALID);
|
jacint@1077
|
300 |
//undefined for the base nodes of the blossoms (i.e. for the
|
jacint@1165
|
301 |
//representative elements of UFE blossom) and for the nodes in C
|
jacint@1165
|
302 |
|
jacint@1077
|
303 |
typename UFE::MapType blossom_base(g);
|
jacint@1077
|
304 |
UFE blossom(blossom_base);
|
jacint@1077
|
305 |
typename UFE::MapType tree_base(g);
|
jacint@1077
|
306 |
UFE tree(tree_base);
|
jacint@1165
|
307 |
//If these UFE's would be members of the class then also
|
jacint@1165
|
308 |
//blossom_base and tree_base should be a member.
|
jacint@1077
|
309 |
|
jacint@1077
|
310 |
for(NodeIt v(g); v!=INVALID; ++v) {
|
jacint@1093
|
311 |
if ( position[v]==C && _mate[v]==INVALID ) {
|
jacint@1077
|
312 |
blossom.insert(v);
|
jacint@1077
|
313 |
tree.insert(v);
|
jacint@1077
|
314 |
position.set(v,D);
|
jacint@1077
|
315 |
if ( heur == 1 ) lateShrink( v, ear, blossom, tree );
|
jacint@1077
|
316 |
else normShrink( v, ear, blossom, tree );
|
jacint@1077
|
317 |
}
|
jacint@1077
|
318 |
}
|
jacint@1077
|
319 |
}
|
jacint@1077
|
320 |
|
jacint@1077
|
321 |
|
jacint@1077
|
322 |
template <typename Graph>
|
jacint@1077
|
323 |
void MaxMatching<Graph>::lateShrink(Node v, typename Graph::template NodeMap<Node>& ear,
|
jacint@1077
|
324 |
UFE& blossom, UFE& tree) {
|
jacint@1077
|
325 |
|
jacint@1077
|
326 |
std::queue<Node> Q; //queue of the totally unscanned nodes
|
jacint@1077
|
327 |
Q.push(v);
|
jacint@1077
|
328 |
std::queue<Node> R;
|
jacint@1077
|
329 |
//queue of the nodes which must be scanned for a possible shrink
|
jacint@1077
|
330 |
|
jacint@1077
|
331 |
while ( !Q.empty() ) {
|
jacint@1077
|
332 |
Node x=Q.front();
|
jacint@1077
|
333 |
Q.pop();
|
jacint@1077
|
334 |
if ( noShrinkStep( x, ear, blossom, tree, Q ) ) return;
|
jacint@1077
|
335 |
else R.push(x);
|
jacint@1077
|
336 |
}
|
jacint@1077
|
337 |
|
jacint@1077
|
338 |
while ( !R.empty() ) {
|
jacint@1077
|
339 |
Node x=R.front();
|
jacint@1077
|
340 |
R.pop();
|
jacint@1077
|
341 |
|
jacint@1077
|
342 |
for( IncEdgeIt e(g,x); e!=INVALID ; ++e ) {
|
klao@1158
|
343 |
Node y=g.runningNode(e);
|
jacint@1077
|
344 |
|
jacint@1077
|
345 |
if ( position[y] == D && blossom.find(x) != blossom.find(y) ) {
|
jacint@1077
|
346 |
//x and y must be in the same tree
|
jacint@1077
|
347 |
|
jacint@1077
|
348 |
typename Graph::template NodeMap<bool> path(g,false);
|
jacint@1077
|
349 |
|
jacint@1077
|
350 |
Node b=blossom.find(x);
|
jacint@1077
|
351 |
path.set(b,true);
|
jacint@1093
|
352 |
b=_mate[b];
|
jacint@1077
|
353 |
while ( b!=INVALID ) {
|
jacint@1077
|
354 |
b=blossom.find(ear[b]);
|
jacint@1077
|
355 |
path.set(b,true);
|
jacint@1093
|
356 |
b=_mate[b];
|
jacint@1077
|
357 |
} //going till the root
|
jacint@1077
|
358 |
|
jacint@1077
|
359 |
Node top=y;
|
jacint@1077
|
360 |
Node middle=blossom.find(top);
|
jacint@1077
|
361 |
Node bottom=x;
|
jacint@1077
|
362 |
while ( !path[middle] )
|
jacint@1077
|
363 |
shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
|
jacint@1077
|
364 |
|
jacint@1077
|
365 |
Node base=middle;
|
jacint@1077
|
366 |
top=x;
|
jacint@1077
|
367 |
middle=blossom.find(top);
|
jacint@1077
|
368 |
bottom=y;
|
jacint@1077
|
369 |
Node blossom_base=blossom.find(base);
|
jacint@1077
|
370 |
while ( middle!=blossom_base )
|
jacint@1077
|
371 |
shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
|
jacint@1077
|
372 |
|
jacint@1077
|
373 |
blossom.makeRep(base);
|
jacint@1077
|
374 |
} // if shrink is needed
|
jacint@1077
|
375 |
|
jacint@1077
|
376 |
while ( !Q.empty() ) {
|
jacint@1077
|
377 |
Node x=Q.front();
|
jacint@1077
|
378 |
Q.pop();
|
jacint@1077
|
379 |
if ( noShrinkStep(x, ear, blossom, tree, Q) ) return;
|
jacint@1077
|
380 |
else R.push(x);
|
jacint@1077
|
381 |
}
|
jacint@1077
|
382 |
} //for e
|
jacint@1077
|
383 |
} // while ( !R.empty() )
|
jacint@1077
|
384 |
}
|
jacint@1077
|
385 |
|
jacint@1077
|
386 |
|
jacint@1077
|
387 |
template <typename Graph>
|
jacint@1077
|
388 |
void MaxMatching<Graph>::normShrink(Node v, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
389 |
UFE& blossom, UFE& tree) {
|
jacint@1077
|
390 |
|
jacint@1077
|
391 |
std::queue<Node> Q; //queue of the unscanned nodes
|
jacint@1077
|
392 |
Q.push(v);
|
jacint@1077
|
393 |
while ( !Q.empty() ) {
|
jacint@1077
|
394 |
|
jacint@1077
|
395 |
Node x=Q.front();
|
jacint@1077
|
396 |
Q.pop();
|
jacint@1077
|
397 |
|
jacint@1077
|
398 |
for( IncEdgeIt e(g,x); e!=INVALID; ++e ) {
|
klao@1158
|
399 |
Node y=g.runningNode(e);
|
jacint@1077
|
400 |
|
jacint@1077
|
401 |
switch ( position[y] ) {
|
jacint@1077
|
402 |
case D: //x and y must be in the same tree
|
jacint@1077
|
403 |
|
jacint@1077
|
404 |
if ( blossom.find(x) != blossom.find(y) ) { //shrink
|
jacint@1077
|
405 |
typename Graph::template NodeMap<bool> path(g,false);
|
jacint@1077
|
406 |
|
jacint@1077
|
407 |
Node b=blossom.find(x);
|
jacint@1077
|
408 |
path.set(b,true);
|
jacint@1093
|
409 |
b=_mate[b];
|
jacint@1077
|
410 |
while ( b!=INVALID ) {
|
jacint@1077
|
411 |
b=blossom.find(ear[b]);
|
jacint@1077
|
412 |
path.set(b,true);
|
jacint@1093
|
413 |
b=_mate[b];
|
jacint@1077
|
414 |
} //going till the root
|
jacint@1077
|
415 |
|
jacint@1077
|
416 |
Node top=y;
|
jacint@1077
|
417 |
Node middle=blossom.find(top);
|
jacint@1077
|
418 |
Node bottom=x;
|
jacint@1077
|
419 |
while ( !path[middle] )
|
jacint@1077
|
420 |
shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
|
jacint@1077
|
421 |
|
jacint@1077
|
422 |
Node base=middle;
|
jacint@1077
|
423 |
top=x;
|
jacint@1077
|
424 |
middle=blossom.find(top);
|
jacint@1077
|
425 |
bottom=y;
|
jacint@1077
|
426 |
Node blossom_base=blossom.find(base);
|
jacint@1077
|
427 |
while ( middle!=blossom_base )
|
jacint@1077
|
428 |
shrinkStep(top, middle, bottom, ear, blossom, tree, Q);
|
jacint@1077
|
429 |
|
jacint@1077
|
430 |
blossom.makeRep(base);
|
jacint@1077
|
431 |
}
|
jacint@1077
|
432 |
break;
|
jacint@1077
|
433 |
case C:
|
jacint@1093
|
434 |
if ( _mate[y]!=INVALID ) { //grow
|
jacint@1077
|
435 |
|
jacint@1077
|
436 |
ear.set(y,x);
|
jacint@1093
|
437 |
Node w=_mate[y];
|
jacint@1077
|
438 |
blossom.insert(w);
|
jacint@1077
|
439 |
position.set(y,A);
|
jacint@1077
|
440 |
position.set(w,D);
|
jacint@1077
|
441 |
tree.insert(y);
|
jacint@1077
|
442 |
tree.insert(w);
|
jacint@1077
|
443 |
tree.join(y,blossom.find(x));
|
jacint@1077
|
444 |
tree.join(w,y);
|
jacint@1077
|
445 |
Q.push(w);
|
jacint@1077
|
446 |
} else { //augment
|
jacint@1077
|
447 |
augment(x, ear, blossom, tree);
|
jacint@1093
|
448 |
_mate.set(x,y);
|
jacint@1093
|
449 |
_mate.set(y,x);
|
jacint@1077
|
450 |
return;
|
jacint@1077
|
451 |
} //if
|
jacint@1077
|
452 |
break;
|
jacint@1077
|
453 |
default: break;
|
jacint@1077
|
454 |
}
|
jacint@1077
|
455 |
}
|
jacint@1077
|
456 |
}
|
jacint@1077
|
457 |
}
|
jacint@1077
|
458 |
|
jacint@1077
|
459 |
template <typename Graph>
|
jacint@1077
|
460 |
void MaxMatching<Graph>::greedyMatching() {
|
jacint@1077
|
461 |
for(NodeIt v(g); v!=INVALID; ++v)
|
jacint@1093
|
462 |
if ( _mate[v]==INVALID ) {
|
jacint@1077
|
463 |
for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) {
|
klao@1158
|
464 |
Node y=g.runningNode(e);
|
jacint@1093
|
465 |
if ( _mate[y]==INVALID && y!=v ) {
|
jacint@1093
|
466 |
_mate.set(v,y);
|
jacint@1093
|
467 |
_mate.set(y,v);
|
jacint@1077
|
468 |
break;
|
jacint@1077
|
469 |
}
|
jacint@1077
|
470 |
}
|
jacint@1077
|
471 |
}
|
jacint@1077
|
472 |
}
|
jacint@1077
|
473 |
|
jacint@1077
|
474 |
template <typename Graph>
|
jacint@1077
|
475 |
int MaxMatching<Graph>::size() const {
|
jacint@1077
|
476 |
int s=0;
|
jacint@1077
|
477 |
for(NodeIt v(g); v!=INVALID; ++v) {
|
jacint@1093
|
478 |
if ( _mate[v]!=INVALID ) {
|
jacint@1077
|
479 |
++s;
|
jacint@1077
|
480 |
}
|
jacint@1077
|
481 |
}
|
jacint@1165
|
482 |
return s/2;
|
jacint@1077
|
483 |
}
|
jacint@1077
|
484 |
|
jacint@1077
|
485 |
template <typename Graph>
|
jacint@1077
|
486 |
void MaxMatching<Graph>::resetMatching() {
|
jacint@1077
|
487 |
for(NodeIt v(g); v!=INVALID; ++v)
|
jacint@1093
|
488 |
_mate.set(v,INVALID);
|
jacint@1077
|
489 |
}
|
jacint@1077
|
490 |
|
jacint@1077
|
491 |
template <typename Graph>
|
jacint@1077
|
492 |
bool MaxMatching<Graph>::noShrinkStep(Node x, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
493 |
UFE& blossom, UFE& tree, std::queue<Node>& Q) {
|
jacint@1077
|
494 |
for( IncEdgeIt e(g,x); e!= INVALID; ++e ) {
|
klao@1158
|
495 |
Node y=g.runningNode(e);
|
jacint@1077
|
496 |
|
jacint@1077
|
497 |
if ( position[y]==C ) {
|
jacint@1093
|
498 |
if ( _mate[y]!=INVALID ) { //grow
|
jacint@1077
|
499 |
ear.set(y,x);
|
jacint@1093
|
500 |
Node w=_mate[y];
|
jacint@1077
|
501 |
blossom.insert(w);
|
jacint@1077
|
502 |
position.set(y,A);
|
jacint@1077
|
503 |
position.set(w,D);
|
jacint@1077
|
504 |
tree.insert(y);
|
jacint@1077
|
505 |
tree.insert(w);
|
jacint@1077
|
506 |
tree.join(y,blossom.find(x));
|
jacint@1077
|
507 |
tree.join(w,y);
|
jacint@1077
|
508 |
Q.push(w);
|
jacint@1077
|
509 |
} else { //augment
|
jacint@1077
|
510 |
augment(x, ear, blossom, tree);
|
jacint@1093
|
511 |
_mate.set(x,y);
|
jacint@1093
|
512 |
_mate.set(y,x);
|
jacint@1077
|
513 |
return true;
|
jacint@1077
|
514 |
}
|
jacint@1077
|
515 |
}
|
jacint@1077
|
516 |
}
|
jacint@1077
|
517 |
return false;
|
jacint@1077
|
518 |
}
|
jacint@1077
|
519 |
|
jacint@1077
|
520 |
template <typename Graph>
|
jacint@1077
|
521 |
void MaxMatching<Graph>::shrinkStep(Node& top, Node& middle, Node& bottom, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
522 |
UFE& blossom, UFE& tree, std::queue<Node>& Q) {
|
jacint@1077
|
523 |
ear.set(top,bottom);
|
jacint@1077
|
524 |
Node t=top;
|
jacint@1077
|
525 |
while ( t!=middle ) {
|
jacint@1093
|
526 |
Node u=_mate[t];
|
jacint@1077
|
527 |
t=ear[u];
|
jacint@1077
|
528 |
ear.set(t,u);
|
jacint@1077
|
529 |
}
|
jacint@1093
|
530 |
bottom=_mate[middle];
|
jacint@1077
|
531 |
position.set(bottom,D);
|
jacint@1077
|
532 |
Q.push(bottom);
|
jacint@1077
|
533 |
top=ear[bottom];
|
jacint@1077
|
534 |
Node oldmiddle=middle;
|
jacint@1077
|
535 |
middle=blossom.find(top);
|
jacint@1077
|
536 |
tree.erase(bottom);
|
jacint@1077
|
537 |
tree.erase(oldmiddle);
|
jacint@1077
|
538 |
blossom.insert(bottom);
|
jacint@1077
|
539 |
blossom.join(bottom, oldmiddle);
|
jacint@1077
|
540 |
blossom.join(top, oldmiddle);
|
jacint@1077
|
541 |
}
|
jacint@1077
|
542 |
|
jacint@1077
|
543 |
template <typename Graph>
|
jacint@1077
|
544 |
void MaxMatching<Graph>::augment(Node x, typename Graph::NodeMap<Node>& ear,
|
jacint@1077
|
545 |
UFE& blossom, UFE& tree) {
|
jacint@1093
|
546 |
Node v=_mate[x];
|
jacint@1077
|
547 |
while ( v!=INVALID ) {
|
jacint@1077
|
548 |
|
jacint@1077
|
549 |
Node u=ear[v];
|
jacint@1093
|
550 |
_mate.set(v,u);
|
jacint@1077
|
551 |
Node tmp=v;
|
jacint@1093
|
552 |
v=_mate[u];
|
jacint@1093
|
553 |
_mate.set(u,tmp);
|
jacint@1077
|
554 |
}
|
jacint@1077
|
555 |
typename UFE::ItemIt it;
|
jacint@1077
|
556 |
for (tree.first(it,blossom.find(x)); tree.valid(it); tree.next(it)) {
|
jacint@1077
|
557 |
if ( position[it] == D ) {
|
jacint@1077
|
558 |
typename UFE::ItemIt b_it;
|
jacint@1077
|
559 |
for (blossom.first(b_it,it); blossom.valid(b_it); blossom.next(b_it)) {
|
jacint@1077
|
560 |
position.set( b_it ,C);
|
jacint@1077
|
561 |
}
|
jacint@1077
|
562 |
blossom.eraseClass(it);
|
jacint@1077
|
563 |
} else position.set( it ,C);
|
jacint@1077
|
564 |
}
|
jacint@1077
|
565 |
tree.eraseClass(x);
|
jacint@1077
|
566 |
|
jacint@1077
|
567 |
}
|
jacint@1077
|
568 |
|
jacint@1077
|
569 |
/// @}
|
jacint@1077
|
570 |
|
jacint@1077
|
571 |
} //END OF NAMESPACE LEMON
|
jacint@1077
|
572 |
|
jacint@1165
|
573 |
#endif //LEMON_MAX_MATCHING_H
|