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