0
13
0
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
namespace lemon { |
20 | 20 |
/*! |
21 | 21 |
|
22 | 22 |
|
23 | 23 |
|
24 | 24 |
\page lgf-format LEMON Graph Format (LGF) |
25 | 25 |
|
26 | 26 |
The \e LGF is a <em>column oriented</em> |
27 | 27 |
file format for storing graphs and associated data like |
28 | 28 |
node and edge maps. |
29 | 29 |
|
30 | 30 |
Each line with \c '#' first non-whitespace |
31 | 31 |
character is considered as a comment line. |
32 | 32 |
|
33 | 33 |
Otherwise the file consists of sections starting with |
34 | 34 |
a header line. The header lines starts with an \c '@' character followed by the |
35 | 35 |
type of section. The standard section types are \c \@nodes, \c |
36 | 36 |
\@arcs and \c \@edges |
37 | 37 |
and \@attributes. Each header line may also have an optional |
38 | 38 |
\e name, which can be use to distinguish the sections of the same |
39 | 39 |
type. |
40 | 40 |
|
41 | 41 |
The standard sections are column oriented, each line consists of |
42 | 42 |
<em>token</em>s separated by whitespaces. A token can be \e plain or |
43 | 43 |
\e quoted. A plain token is just a sequence of non-whitespace characters, |
44 | 44 |
while a quoted token is a |
45 | 45 |
character sequence surrounded by double quotes, and it can also |
46 | 46 |
contain whitespaces and escape sequences. |
47 | 47 |
|
48 | 48 |
The \c \@nodes section describes a set of nodes and associated |
49 | 49 |
maps. The first is a header line, its columns are the names of the |
50 | 50 |
maps appearing in the following lines. |
51 | 51 |
One of the maps must be called \c |
52 | 52 |
"label", which plays special role in the file. |
53 | 53 |
The following |
54 | 54 |
non-empty lines until the next section describes nodes of the |
55 | 55 |
graph. Each line contains the values of the node maps |
56 | 56 |
associated to the current node. |
57 | 57 |
|
58 | 58 |
\code |
59 | 59 |
@nodes |
60 | 60 |
label coordinates size title |
61 | 61 |
1 (10,20) 10 "First node" |
62 | 62 |
2 (80,80) 8 "Second node" |
63 | 63 |
3 (40,10) 10 "Third node" |
64 | 64 |
\endcode |
65 | 65 |
|
66 | 66 |
The \c \@arcs section is very similar to the \c \@nodes section, it |
67 | 67 |
again starts with a header line describing the names of the maps, but |
68 | 68 |
the \c "label" map is not obligatory here. The following lines |
69 | 69 |
describe the arcs. The first two tokens of each line are the source |
70 | 70 |
and the target node of the arc, respectively, then come the map |
71 | 71 |
values. The source and target tokens must be node labels. |
72 | 72 |
|
73 | 73 |
\code |
74 | 74 |
@arcs |
75 | 75 |
capacity |
76 | 76 |
1 2 16 |
77 | 77 |
1 3 12 |
78 | 78 |
2 3 18 |
79 | 79 |
\endcode |
80 | 80 |
|
81 | 81 |
If there is no map in the \c \@arcs section at all, then it must be |
82 | 82 |
indicated by a sole '-' sign in the first line. |
83 | 83 |
|
84 | 84 |
\code |
85 | 85 |
@arcs |
86 | 86 |
- |
87 | 87 |
1 2 |
88 | 88 |
1 3 |
89 | 89 |
2 3 |
90 | 90 |
\endcode |
91 | 91 |
|
92 | 92 |
The \c \@edges is just a synonym of \c \@arcs. The \@arcs section can |
93 | 93 |
also store the edge set of an undirected graph. In such case there is |
94 | 94 |
a conventional method for store arc maps in the file, if two columns |
95 | 95 |
have the same caption with \c '+' and \c '-' prefix, then these columns |
96 | 96 |
can be regarded as the values of an arc map. |
97 | 97 |
|
98 | 98 |
The \c \@attributes section contains key-value pairs, each line |
99 | 99 |
consists of two tokens, an attribute name, and then an attribute |
100 | 100 |
value. The value of the attribute could be also a label value of a |
101 | 101 |
node or an edge, or even an edge label prefixed with \c '+' or \c '-', |
102 | 102 |
which regards to the forward or backward directed arc of the |
103 | 103 |
corresponding edge. |
104 | 104 |
|
105 | 105 |
\code |
106 | 106 |
@attributes |
107 | 107 |
source 1 |
108 | 108 |
target 3 |
109 | 109 |
caption "LEMON test digraph" |
110 | 110 |
\endcode |
111 | 111 |
|
112 | 112 |
The \e LGF can contain extra sections, but there is no restriction on |
113 | 113 |
the format of such sections. |
114 | 114 |
|
115 | 115 |
*/ |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
// LocalWords: whitespace whitespaces |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/error.h> |
24 | 24 |
|
25 | 25 |
namespace lemon { |
26 | 26 |
|
27 | 27 |
template <typename _Digraph> |
28 | 28 |
class DigraphAdaptorExtender : public _Digraph { |
29 | 29 |
typedef _Digraph Parent; |
30 | 30 |
|
31 | 31 |
public: |
32 | 32 |
|
33 | 33 |
typedef _Digraph Digraph; |
34 | 34 |
typedef DigraphAdaptorExtender Adaptor; |
35 | 35 |
|
36 | 36 |
// Base extensions |
37 | 37 |
|
38 | 38 |
typedef typename Parent::Node Node; |
39 | 39 |
typedef typename Parent::Arc Arc; |
40 | 40 |
|
41 | 41 |
int maxId(Node) const { |
42 | 42 |
return Parent::maxNodeId(); |
43 | 43 |
} |
44 | 44 |
|
45 | 45 |
int maxId(Arc) const { |
46 | 46 |
return Parent::maxArcId(); |
47 | 47 |
} |
48 | 48 |
|
49 | 49 |
Node fromId(int id, Node) const { |
50 | 50 |
return Parent::nodeFromId(id); |
51 | 51 |
} |
52 | 52 |
|
53 | 53 |
Arc fromId(int id, Arc) const { |
54 | 54 |
return Parent::arcFromId(id); |
55 | 55 |
} |
56 | 56 |
|
57 | 57 |
Node oppositeNode(const Node &n, const Arc &e) const { |
58 | 58 |
if (n == Parent::source(e)) |
59 | 59 |
return Parent::target(e); |
60 | 60 |
else if(n==Parent::target(e)) |
61 | 61 |
return Parent::source(e); |
62 | 62 |
else |
63 | 63 |
return INVALID; |
64 | 64 |
} |
65 | 65 |
|
66 | 66 |
class NodeIt : public Node { |
67 | 67 |
const Adaptor* _adaptor; |
68 | 68 |
public: |
69 | 69 |
|
70 | 70 |
NodeIt() {} |
71 | 71 |
|
72 | 72 |
NodeIt(Invalid i) : Node(i) { } |
73 | 73 |
|
74 | 74 |
explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
75 | 75 |
_adaptor->first(static_cast<Node&>(*this)); |
76 | 76 |
} |
77 | 77 |
|
78 | 78 |
NodeIt(const Adaptor& adaptor, const Node& node) |
79 | 79 |
: Node(node), _adaptor(&adaptor) {} |
80 | 80 |
|
81 | 81 |
NodeIt& operator++() { |
82 | 82 |
_adaptor->next(*this); |
83 | 83 |
return *this; |
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
}; |
87 | 87 |
|
88 | 88 |
|
89 | 89 |
class ArcIt : public Arc { |
90 | 90 |
const Adaptor* _adaptor; |
91 | 91 |
public: |
92 | 92 |
|
93 | 93 |
ArcIt() { } |
94 | 94 |
|
95 | 95 |
ArcIt(Invalid i) : Arc(i) { } |
96 | 96 |
|
97 | 97 |
explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
98 | 98 |
_adaptor->first(static_cast<Arc&>(*this)); |
99 | 99 |
} |
100 | 100 |
|
101 | 101 |
ArcIt(const Adaptor& adaptor, const Arc& e) : |
102 | 102 |
Arc(e), _adaptor(&adaptor) { } |
103 | 103 |
|
104 | 104 |
ArcIt& operator++() { |
105 | 105 |
_adaptor->next(*this); |
106 | 106 |
return *this; |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
}; |
110 | 110 |
|
111 | 111 |
|
112 | 112 |
class OutArcIt : public Arc { |
113 | 113 |
const Adaptor* _adaptor; |
114 | 114 |
public: |
115 | 115 |
|
116 | 116 |
OutArcIt() { } |
117 | 117 |
|
118 | 118 |
OutArcIt(Invalid i) : Arc(i) { } |
119 | 119 |
|
120 | 120 |
OutArcIt(const Adaptor& adaptor, const Node& node) |
121 | 121 |
: _adaptor(&adaptor) { |
122 | 122 |
_adaptor->firstOut(*this, node); |
123 | 123 |
} |
124 | 124 |
|
125 | 125 |
OutArcIt(const Adaptor& adaptor, const Arc& arc) |
126 | 126 |
: Arc(arc), _adaptor(&adaptor) {} |
127 | 127 |
|
128 | 128 |
OutArcIt& operator++() { |
129 | 129 |
_adaptor->nextOut(*this); |
130 | 130 |
return *this; |
131 | 131 |
} |
132 | 132 |
|
133 | 133 |
}; |
134 | 134 |
|
135 | 135 |
|
136 | 136 |
class InArcIt : public Arc { |
137 | 137 |
const Adaptor* _adaptor; |
138 | 138 |
public: |
139 | 139 |
|
140 | 140 |
InArcIt() { } |
141 | 141 |
|
142 | 142 |
InArcIt(Invalid i) : Arc(i) { } |
143 | 143 |
|
144 | 144 |
InArcIt(const Adaptor& adaptor, const Node& node) |
145 | 145 |
: _adaptor(&adaptor) { |
146 | 146 |
_adaptor->firstIn(*this, node); |
147 | 147 |
} |
148 | 148 |
|
149 | 149 |
InArcIt(const Adaptor& adaptor, const Arc& arc) : |
150 | 150 |
Arc(arc), _adaptor(&adaptor) {} |
151 | 151 |
|
152 | 152 |
InArcIt& operator++() { |
153 | 153 |
_adaptor->nextIn(*this); |
154 | 154 |
return *this; |
155 | 155 |
} |
156 | 156 |
|
157 | 157 |
}; |
158 | 158 |
|
159 | 159 |
Node baseNode(const OutArcIt &e) const { |
160 | 160 |
return Parent::source(e); |
161 | 161 |
} |
162 | 162 |
Node runningNode(const OutArcIt &e) const { |
163 | 163 |
return Parent::target(e); |
164 | 164 |
} |
165 | 165 |
|
166 | 166 |
Node baseNode(const InArcIt &e) const { |
167 | 167 |
return Parent::target(e); |
168 | 168 |
} |
169 | 169 |
Node runningNode(const InArcIt &e) const { |
170 | 170 |
return Parent::source(e); |
171 | 171 |
} |
172 | 172 |
|
173 | 173 |
}; |
174 | 174 |
|
175 | 175 |
template <typename _Graph> |
176 | 176 |
class GraphAdaptorExtender : public _Graph { |
177 | 177 |
typedef _Graph Parent; |
178 | 178 |
|
179 | 179 |
public: |
180 | 180 |
|
181 | 181 |
typedef _Graph Graph; |
182 | 182 |
typedef GraphAdaptorExtender Adaptor; |
183 | 183 |
|
184 | 184 |
typedef True UndirectedTag; |
185 | 185 |
|
186 | 186 |
typedef typename Parent::Node Node; |
187 | 187 |
typedef typename Parent::Arc Arc; |
188 | 188 |
typedef typename Parent::Edge Edge; |
189 | 189 |
|
190 | 190 |
// Graph extension |
191 | 191 |
|
192 | 192 |
int maxId(Node) const { |
193 | 193 |
return Parent::maxNodeId(); |
194 | 194 |
} |
195 | 195 |
|
196 | 196 |
int maxId(Arc) const { |
197 | 197 |
return Parent::maxArcId(); |
198 | 198 |
} |
199 | 199 |
|
200 | 200 |
int maxId(Edge) const { |
201 | 201 |
return Parent::maxEdgeId(); |
202 | 202 |
} |
203 | 203 |
|
204 | 204 |
Node fromId(int id, Node) const { |
205 | 205 |
return Parent::nodeFromId(id); |
206 | 206 |
} |
207 | 207 |
|
208 | 208 |
Arc fromId(int id, Arc) const { |
209 | 209 |
return Parent::arcFromId(id); |
210 | 210 |
} |
211 | 211 |
|
212 | 212 |
Edge fromId(int id, Edge) const { |
213 | 213 |
return Parent::edgeFromId(id); |
214 | 214 |
} |
215 | 215 |
|
216 | 216 |
Node oppositeNode(const Node &n, const Edge &e) const { |
217 | 217 |
if( n == Parent::u(e)) |
218 | 218 |
return Parent::v(e); |
219 | 219 |
else if( n == Parent::v(e)) |
220 | 220 |
return Parent::u(e); |
221 | 221 |
else |
222 | 222 |
return INVALID; |
223 | 223 |
} |
224 | 224 |
|
225 | 225 |
Arc oppositeArc(const Arc &a) const { |
226 | 226 |
return Parent::direct(a, !Parent::direction(a)); |
227 | 227 |
} |
228 | 228 |
|
229 | 229 |
using Parent::direct; |
230 | 230 |
Arc direct(const Edge &e, const Node &s) const { |
231 | 231 |
return Parent::direct(e, Parent::u(e) == s); |
232 | 232 |
} |
233 | 233 |
|
234 | 234 |
|
235 | 235 |
class NodeIt : public Node { |
236 | 236 |
const Adaptor* _adaptor; |
237 | 237 |
public: |
238 | 238 |
|
239 | 239 |
NodeIt() {} |
240 | 240 |
|
241 | 241 |
NodeIt(Invalid i) : Node(i) { } |
242 | 242 |
|
243 | 243 |
explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
244 | 244 |
_adaptor->first(static_cast<Node&>(*this)); |
245 | 245 |
} |
246 | 246 |
|
247 | 247 |
NodeIt(const Adaptor& adaptor, const Node& node) |
248 | 248 |
: Node(node), _adaptor(&adaptor) {} |
249 | 249 |
|
250 | 250 |
NodeIt& operator++() { |
251 | 251 |
_adaptor->next(*this); |
252 | 252 |
return *this; |
253 | 253 |
} |
254 | 254 |
|
255 | 255 |
}; |
256 | 256 |
|
257 | 257 |
|
258 | 258 |
class ArcIt : public Arc { |
259 | 259 |
const Adaptor* _adaptor; |
260 | 260 |
public: |
261 | 261 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_PATH_DUMP_H |
20 | 20 |
#define LEMON_BITS_PATH_DUMP_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/concept_check.h> |
24 | 24 |
|
25 | 25 |
namespace lemon { |
26 | 26 |
|
27 | 27 |
template <typename _Digraph, typename _PredMap> |
28 | 28 |
class PredMapPath { |
29 | 29 |
public: |
30 | 30 |
typedef True RevPathTag; |
31 | 31 |
|
32 | 32 |
typedef _Digraph Digraph; |
33 | 33 |
typedef typename Digraph::Arc Arc; |
34 | 34 |
typedef _PredMap PredMap; |
35 | 35 |
|
36 | 36 |
PredMapPath(const Digraph& _digraph, const PredMap& _predMap, |
37 | 37 |
typename Digraph::Node _target) |
38 | 38 |
: digraph(_digraph), predMap(_predMap), target(_target) {} |
39 | 39 |
|
40 | 40 |
int length() const { |
41 | 41 |
int len = 0; |
42 | 42 |
typename Digraph::Node node = target; |
43 | 43 |
typename Digraph::Arc arc; |
44 | 44 |
while ((arc = predMap[node]) != INVALID) { |
45 | 45 |
node = digraph.source(arc); |
46 | 46 |
++len; |
47 | 47 |
} |
48 | 48 |
return len; |
49 | 49 |
} |
50 | 50 |
|
51 | 51 |
bool empty() const { |
52 | 52 |
return predMap[target] == INVALID; |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
class RevArcIt { |
56 | 56 |
public: |
57 | 57 |
RevArcIt() {} |
58 | 58 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
59 | 59 |
RevArcIt(const PredMapPath& _path) |
60 | 60 |
: path(&_path), current(_path.target) { |
61 | 61 |
if (path->predMap[current] == INVALID) current = INVALID; |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
operator const typename Digraph::Arc() const { |
65 | 65 |
return path->predMap[current]; |
66 | 66 |
} |
67 | 67 |
|
68 | 68 |
RevArcIt& operator++() { |
69 | 69 |
current = path->digraph.source(path->predMap[current]); |
70 | 70 |
if (path->predMap[current] == INVALID) current = INVALID; |
71 | 71 |
return *this; |
72 | 72 |
} |
73 | 73 |
|
74 | 74 |
bool operator==(const RevArcIt& e) const { |
75 | 75 |
return current == e.current; |
76 | 76 |
} |
77 | 77 |
|
78 | 78 |
bool operator!=(const RevArcIt& e) const { |
79 | 79 |
return current != e.current; |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
bool operator<(const RevArcIt& e) const { |
83 | 83 |
return current < e.current; |
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
private: |
87 | 87 |
const PredMapPath* path; |
88 | 88 |
typename Digraph::Node current; |
89 | 89 |
}; |
90 | 90 |
|
91 | 91 |
private: |
92 | 92 |
const Digraph& digraph; |
93 | 93 |
const PredMap& predMap; |
94 | 94 |
typename Digraph::Node target; |
95 | 95 |
}; |
96 | 96 |
|
97 | 97 |
|
98 | 98 |
template <typename _Digraph, typename _PredMatrixMap> |
99 | 99 |
class PredMatrixMapPath { |
100 | 100 |
public: |
101 | 101 |
typedef True RevPathTag; |
102 | 102 |
|
103 | 103 |
typedef _Digraph Digraph; |
104 | 104 |
typedef typename Digraph::Arc Arc; |
105 | 105 |
typedef _PredMatrixMap PredMatrixMap; |
106 | 106 |
|
107 | 107 |
PredMatrixMapPath(const Digraph& _digraph, |
108 | 108 |
const PredMatrixMap& _predMatrixMap, |
109 | 109 |
typename Digraph::Node _source, |
110 | 110 |
typename Digraph::Node _target) |
111 | 111 |
: digraph(_digraph), predMatrixMap(_predMatrixMap), |
112 | 112 |
source(_source), target(_target) {} |
113 | 113 |
|
114 | 114 |
int length() const { |
115 | 115 |
int len = 0; |
116 | 116 |
typename Digraph::Node node = target; |
117 | 117 |
typename Digraph::Arc arc; |
118 | 118 |
while ((arc = predMatrixMap(source, node)) != INVALID) { |
119 | 119 |
node = digraph.source(arc); |
120 | 120 |
++len; |
121 | 121 |
} |
122 | 122 |
return len; |
123 | 123 |
} |
124 | 124 |
|
125 | 125 |
bool empty() const { |
126 | 126 |
return predMatrixMap(source, target) == INVALID; |
127 | 127 |
} |
128 | 128 |
|
129 | 129 |
class RevArcIt { |
130 | 130 |
public: |
131 | 131 |
RevArcIt() {} |
132 | 132 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
133 | 133 |
RevArcIt(const PredMatrixMapPath& _path) |
134 | 134 |
: path(&_path), current(_path.target) { |
135 | 135 |
if (path->predMatrixMap(path->source, current) == INVALID) |
136 | 136 |
current = INVALID; |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
operator const typename Digraph::Arc() const { |
140 | 140 |
return path->predMatrixMap(path->source, current); |
141 | 141 |
} |
142 | 142 |
|
143 | 143 |
RevArcIt& operator++() { |
144 | 144 |
current = |
145 | 145 |
path->digraph.source(path->predMatrixMap(path->source, current)); |
146 | 146 |
if (path->predMatrixMap(path->source, current) == INVALID) |
147 | 147 |
current = INVALID; |
148 | 148 |
return *this; |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
bool operator==(const RevArcIt& e) const { |
152 | 152 |
return current == e.current; |
153 | 153 |
} |
154 | 154 |
|
155 | 155 |
bool operator!=(const RevArcIt& e) const { |
156 | 156 |
return current != e.current; |
157 | 157 |
} |
158 | 158 |
|
159 | 159 |
bool operator<(const RevArcIt& e) const { |
160 | 160 |
return current < e.current; |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
private: |
164 | 164 |
const PredMatrixMapPath* path; |
165 | 165 |
typename Digraph::Node current; |
166 | 166 |
}; |
167 | 167 |
|
168 | 168 |
private: |
169 | 169 |
const Digraph& digraph; |
170 | 170 |
const PredMatrixMap& predMatrixMap; |
171 | 171 |
typename Digraph::Node source; |
172 | 172 |
typename Digraph::Node target; |
173 | 173 |
}; |
174 | 174 |
|
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\file |
20 | 20 |
///\brief Some basic non-inline functions and static global data. |
21 | 21 |
|
22 | 22 |
#include<lemon/bits/windows.h> |
23 | 23 |
|
24 | 24 |
#ifdef WIN32 |
25 | 25 |
#ifndef WIN32_LEAN_AND_MEAN |
26 | 26 |
#define WIN32_LEAN_AND_MEAN |
27 | 27 |
#endif |
28 | 28 |
#ifndef NOMINMAX |
29 | 29 |
#define NOMINMAX |
30 | 30 |
#endif |
31 | 31 |
#ifdef UNICODE |
32 | 32 |
#undef UNICODE |
33 | 33 |
#endif |
34 | 34 |
#include <windows.h> |
35 | 35 |
#ifdef LOCALE_INVARIANT |
36 | 36 |
#define MY_LOCALE LOCALE_INVARIANT |
37 | 37 |
#else |
38 | 38 |
#define MY_LOCALE LOCALE_NEUTRAL |
39 | 39 |
#endif |
40 | 40 |
#else |
41 | 41 |
#include <unistd.h> |
42 | 42 |
#include <ctime> |
43 | 43 |
#ifndef WIN32 |
44 | 44 |
#include <sys/times.h> |
45 | 45 |
#endif |
46 | 46 |
#include <sys/time.h> |
47 | 47 |
#endif |
48 | 48 |
|
49 | 49 |
#include <cmath> |
50 | 50 |
#include <sstream> |
51 | 51 |
|
52 | 52 |
namespace lemon { |
53 | 53 |
namespace bits { |
54 | 54 |
void getWinProcTimes(double &rtime, |
55 | 55 |
double &utime, double &stime, |
56 | 56 |
double &cutime, double &cstime) |
57 | 57 |
{ |
58 | 58 |
#ifdef WIN32 |
59 | 59 |
static const double ch = 4294967296.0e-7; |
60 | 60 |
static const double cl = 1.0e-7; |
61 | 61 |
|
62 | 62 |
FILETIME system; |
63 | 63 |
GetSystemTimeAsFileTime(&system); |
64 | 64 |
rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime; |
65 | 65 |
|
66 | 66 |
FILETIME create, exit, kernel, user; |
67 | 67 |
if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) { |
68 | 68 |
utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime; |
69 | 69 |
stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime; |
70 | 70 |
cutime = 0; |
71 | 71 |
cstime = 0; |
72 | 72 |
} else { |
73 | 73 |
rtime = 0; |
74 | 74 |
utime = 0; |
75 | 75 |
stime = 0; |
76 | 76 |
cutime = 0; |
77 | 77 |
cstime = 0; |
78 | 78 |
} |
79 | 79 |
#else |
80 | 80 |
timeval tv; |
81 | 81 |
gettimeofday(&tv, 0); |
82 | 82 |
rtime=tv.tv_sec+double(tv.tv_usec)/1e6; |
83 | 83 |
|
84 | 84 |
tms ts; |
85 | 85 |
double tck=sysconf(_SC_CLK_TCK); |
86 | 86 |
times(&ts); |
87 | 87 |
utime=ts.tms_utime/tck; |
88 | 88 |
stime=ts.tms_stime/tck; |
89 | 89 |
cutime=ts.tms_cutime/tck; |
90 | 90 |
cstime=ts.tms_cstime/tck; |
91 | 91 |
#endif |
92 | 92 |
} |
93 | 93 |
|
94 | 94 |
std::string getWinFormattedDate() |
95 | 95 |
{ |
96 | 96 |
std::ostringstream os; |
97 | 97 |
#ifdef WIN32 |
98 | 98 |
SYSTEMTIME time; |
99 | 99 |
GetSystemTime(&time); |
100 | 100 |
char buf1[11], buf2[9], buf3[5]; |
101 | 101 |
if (GetDateFormat(MY_LOCALE, 0, &time, |
102 | 102 |
("ddd MMM dd"), buf1, 11) && |
103 | 103 |
GetTimeFormat(MY_LOCALE, 0, &time, |
104 | 104 |
("HH':'mm':'ss"), buf2, 9) && |
105 | 105 |
GetDateFormat(MY_LOCALE, 0, &time, |
106 | 106 |
("yyyy"), buf3, 5)) { |
107 | 107 |
os << buf1 << ' ' << buf2 << ' ' << buf3; |
108 | 108 |
} |
109 | 109 |
else os << "unknown"; |
110 | 110 |
#else |
111 | 111 |
timeval tv; |
112 | 112 |
gettimeofday(&tv, 0); |
113 | 113 |
|
114 | 114 |
char cbuf[26]; |
115 | 115 |
ctime_r(&tv.tv_sec,cbuf); |
116 | 116 |
os << cbuf; |
117 | 117 |
#endif |
118 | 118 |
return os.str(); |
119 | 119 |
} |
120 | 120 |
|
121 | 121 |
int getWinRndSeed() |
122 | 122 |
{ |
123 | 123 |
#ifdef WIN32 |
124 | 124 |
FILETIME time; |
125 | 125 |
GetSystemTimeAsFileTime(&time); |
126 | 126 |
return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime; |
127 | 127 |
#else |
128 | 128 |
timeval tv; |
129 | 129 |
gettimeofday(&tv, 0); |
130 | 130 |
return getpid() + tv.tv_sec + tv.tv_usec; |
131 | 131 |
#endif |
132 | 132 |
} |
133 | 133 |
} |
134 | 134 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_COST_SCALING_H |
20 | 20 |
#define LEMON_COST_SCALING_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_cost_flow_algs |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Cost scaling algorithm for finding a minimum cost flow. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <deque> |
28 | 28 |
#include <limits> |
29 | 29 |
|
30 | 30 |
#include <lemon/core.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
#include <lemon/math.h> |
33 | 33 |
#include <lemon/static_graph.h> |
34 | 34 |
#include <lemon/circulation.h> |
35 | 35 |
#include <lemon/bellman_ford.h> |
36 | 36 |
|
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
/// \brief Default traits class of CostScaling algorithm. |
40 | 40 |
/// |
41 | 41 |
/// Default traits class of CostScaling algorithm. |
42 | 42 |
/// \tparam GR Digraph type. |
43 | 43 |
/// \tparam V The number type used for flow amounts, capacity bounds |
44 | 44 |
/// and supply values. By default it is \c int. |
45 | 45 |
/// \tparam C The number type used for costs and potentials. |
46 | 46 |
/// By default it is the same as \c V. |
47 | 47 |
#ifdef DOXYGEN |
48 | 48 |
template <typename GR, typename V = int, typename C = V> |
49 | 49 |
#else |
50 | 50 |
template < typename GR, typename V = int, typename C = V, |
51 | 51 |
bool integer = std::numeric_limits<C>::is_integer > |
52 | 52 |
#endif |
53 | 53 |
struct CostScalingDefaultTraits |
54 | 54 |
{ |
55 | 55 |
/// The type of the digraph |
56 | 56 |
typedef GR Digraph; |
57 | 57 |
/// The type of the flow amounts, capacity bounds and supply values |
58 | 58 |
typedef V Value; |
59 | 59 |
/// The type of the arc costs |
60 | 60 |
typedef C Cost; |
61 | 61 |
|
62 | 62 |
/// \brief The large cost type used for internal computations |
63 | 63 |
/// |
64 | 64 |
/// The large cost type used for internal computations. |
65 | 65 |
/// It is \c long \c long if the \c Cost type is integer, |
66 | 66 |
/// otherwise it is \c double. |
67 | 67 |
/// \c Cost must be convertible to \c LargeCost. |
68 | 68 |
typedef double LargeCost; |
69 | 69 |
}; |
70 | 70 |
|
71 | 71 |
// Default traits class for integer cost types |
72 | 72 |
template <typename GR, typename V, typename C> |
73 | 73 |
struct CostScalingDefaultTraits<GR, V, C, true> |
74 | 74 |
{ |
75 | 75 |
typedef GR Digraph; |
76 | 76 |
typedef V Value; |
77 | 77 |
typedef C Cost; |
78 | 78 |
#ifdef LEMON_HAVE_LONG_LONG |
79 | 79 |
typedef long long LargeCost; |
80 | 80 |
#else |
81 | 81 |
typedef long LargeCost; |
82 | 82 |
#endif |
83 | 83 |
}; |
84 | 84 |
|
85 | 85 |
|
86 | 86 |
/// \addtogroup min_cost_flow_algs |
87 | 87 |
/// @{ |
88 | 88 |
|
89 | 89 |
/// \brief Implementation of the Cost Scaling algorithm for |
90 | 90 |
/// finding a \ref min_cost_flow "minimum cost flow". |
91 | 91 |
/// |
92 | 92 |
/// \ref CostScaling implements a cost scaling algorithm that performs |
93 | 93 |
/// push/augment and relabel operations for finding a \ref min_cost_flow |
94 | 94 |
/// "minimum cost flow" \ref amo93networkflows, \ref goldberg90approximation, |
95 | 95 |
/// \ref goldberg97efficient, \ref bunnagel98efficient. |
96 | 96 |
/// It is a highly efficient primal-dual solution method, which |
97 | 97 |
/// can be viewed as the generalization of the \ref Preflow |
98 | 98 |
/// "preflow push-relabel" algorithm for the maximum flow problem. |
99 | 99 |
/// |
100 | 100 |
/// Most of the parameters of the problem (except for the digraph) |
101 | 101 |
/// can be given using separate functions, and the algorithm can be |
102 | 102 |
/// executed using the \ref run() function. If some parameters are not |
103 | 103 |
/// specified, then default values will be used. |
104 | 104 |
/// |
105 | 105 |
/// \tparam GR The digraph type the algorithm runs on. |
106 | 106 |
/// \tparam V The number type used for flow amounts, capacity bounds |
107 | 107 |
/// and supply values in the algorithm. By default, it is \c int. |
108 | 108 |
/// \tparam C The number type used for costs and potentials in the |
109 | 109 |
/// algorithm. By default, it is the same as \c V. |
110 | 110 |
/// \tparam TR The traits class that defines various types used by the |
111 | 111 |
/// algorithm. By default, it is \ref CostScalingDefaultTraits |
112 | 112 |
/// "CostScalingDefaultTraits<GR, V, C>". |
113 | 113 |
/// In most cases, this parameter should not be set directly, |
114 | 114 |
/// consider to use the named template parameters instead. |
115 | 115 |
/// |
116 | 116 |
/// \warning Both number types must be signed and all input data must |
117 | 117 |
/// be integer. |
118 | 118 |
/// \warning This algorithm does not support negative costs for such |
119 | 119 |
/// arcs that have infinite upper bound. |
120 | 120 |
/// |
121 | 121 |
/// \note %CostScaling provides three different internal methods, |
122 | 122 |
/// from which the most efficient one is used by default. |
123 | 123 |
/// For more information, see \ref Method. |
124 | 124 |
#ifdef DOXYGEN |
125 | 125 |
template <typename GR, typename V, typename C, typename TR> |
126 | 126 |
#else |
127 | 127 |
template < typename GR, typename V = int, typename C = V, |
128 | 128 |
typename TR = CostScalingDefaultTraits<GR, V, C> > |
129 | 129 |
#endif |
130 | 130 |
class CostScaling |
131 | 131 |
{ |
132 | 132 |
public: |
133 | 133 |
|
134 | 134 |
/// The type of the digraph |
135 | 135 |
typedef typename TR::Digraph Digraph; |
136 | 136 |
/// The type of the flow amounts, capacity bounds and supply values |
137 | 137 |
typedef typename TR::Value Value; |
138 | 138 |
/// The type of the arc costs |
139 | 139 |
typedef typename TR::Cost Cost; |
140 | 140 |
|
141 | 141 |
/// \brief The large cost type |
142 | 142 |
/// |
143 | 143 |
/// The large cost type used for internal computations. |
144 | 144 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
145 | 145 |
/// otherwise it is \c double. |
146 | 146 |
typedef typename TR::LargeCost LargeCost; |
147 | 147 |
|
148 | 148 |
/// The \ref CostScalingDefaultTraits "traits class" of the algorithm |
149 | 149 |
typedef TR Traits; |
150 | 150 |
|
151 | 151 |
public: |
152 | 152 |
|
153 | 153 |
/// \brief Problem type constants for the \c run() function. |
154 | 154 |
/// |
155 | 155 |
/// Enum type containing the problem type constants that can be |
156 | 156 |
/// returned by the \ref run() function of the algorithm. |
157 | 157 |
enum ProblemType { |
158 | 158 |
/// The problem has no feasible solution (flow). |
159 | 159 |
INFEASIBLE, |
160 | 160 |
/// The problem has optimal solution (i.e. it is feasible and |
161 | 161 |
/// bounded), and the algorithm has found optimal flow and node |
162 | 162 |
/// potentials (primal and dual solutions). |
163 | 163 |
OPTIMAL, |
164 | 164 |
/// The digraph contains an arc of negative cost and infinite |
165 | 165 |
/// upper bound. It means that the objective function is unbounded |
166 | 166 |
/// on that arc, however, note that it could actually be bounded |
167 | 167 |
/// over the feasible flows, but this algroithm cannot handle |
168 | 168 |
/// these cases. |
169 | 169 |
UNBOUNDED |
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
/// \brief Constants for selecting the internal method. |
173 | 173 |
/// |
174 | 174 |
/// Enum type containing constants for selecting the internal method |
175 | 175 |
/// for the \ref run() function. |
176 | 176 |
/// |
177 | 177 |
/// \ref CostScaling provides three internal methods that differ mainly |
178 | 178 |
/// in their base operations, which are used in conjunction with the |
179 | 179 |
/// relabel operation. |
180 | 180 |
/// By default, the so called \ref PARTIAL_AUGMENT |
181 | 181 |
/// "Partial Augment-Relabel" method is used, which proved to be |
182 | 182 |
/// the most efficient and the most robust on various test inputs. |
183 | 183 |
/// However, the other methods can be selected using the \ref run() |
184 | 184 |
/// function with the proper parameter. |
185 | 185 |
enum Method { |
186 | 186 |
/// Local push operations are used, i.e. flow is moved only on one |
187 | 187 |
/// admissible arc at once. |
188 | 188 |
PUSH, |
189 | 189 |
/// Augment operations are used, i.e. flow is moved on admissible |
190 | 190 |
/// paths from a node with excess to a node with deficit. |
191 | 191 |
AUGMENT, |
192 | 192 |
/// Partial augment operations are used, i.e. flow is moved on |
193 | 193 |
/// admissible paths started from a node with excess, but the |
194 | 194 |
/// lengths of these paths are limited. This method can be viewed |
195 | 195 |
/// as a combined version of the previous two operations. |
196 | 196 |
PARTIAL_AUGMENT |
197 | 197 |
}; |
198 | 198 |
|
199 | 199 |
private: |
200 | 200 |
|
201 | 201 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
202 | 202 |
|
203 | 203 |
typedef std::vector<int> IntVector; |
204 | 204 |
typedef std::vector<Value> ValueVector; |
205 | 205 |
typedef std::vector<Cost> CostVector; |
206 | 206 |
typedef std::vector<LargeCost> LargeCostVector; |
207 | 207 |
typedef std::vector<char> BoolVector; |
208 | 208 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
209 | 209 |
|
210 | 210 |
private: |
211 | 211 |
|
212 | 212 |
template <typename KT, typename VT> |
213 | 213 |
class StaticVectorMap { |
214 | 214 |
public: |
215 | 215 |
typedef KT Key; |
216 | 216 |
typedef VT Value; |
217 | 217 |
|
218 | 218 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {} |
219 | 219 |
|
220 | 220 |
const Value& operator[](const Key& key) const { |
221 | 221 |
return _v[StaticDigraph::id(key)]; |
222 | 222 |
} |
223 | 223 |
|
224 | 224 |
Value& operator[](const Key& key) { |
225 | 225 |
return _v[StaticDigraph::id(key)]; |
226 | 226 |
} |
227 | 227 |
|
228 | 228 |
void set(const Key& key, const Value& val) { |
229 | 229 |
_v[StaticDigraph::id(key)] = val; |
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
private: |
233 | 233 |
std::vector<Value>& _v; |
234 | 234 |
}; |
235 | 235 |
|
236 | 236 |
typedef StaticVectorMap<StaticDigraph::Node, LargeCost> LargeCostNodeMap; |
237 | 237 |
typedef StaticVectorMap<StaticDigraph::Arc, LargeCost> LargeCostArcMap; |
238 | 238 |
|
239 | 239 |
private: |
240 | 240 |
|
241 | 241 |
// Data related to the underlying digraph |
242 | 242 |
const GR &_graph; |
243 | 243 |
int _node_num; |
244 | 244 |
int _arc_num; |
245 | 245 |
int _res_node_num; |
246 | 246 |
int _res_arc_num; |
247 | 247 |
int _root; |
248 | 248 |
|
249 | 249 |
// Parameters of the problem |
250 | 250 |
bool _have_lower; |
251 | 251 |
Value _sum_supply; |
252 | 252 |
int _sup_node_num; |
253 | 253 |
|
254 | 254 |
// Data structures for storing the digraph |
255 | 255 |
IntNodeMap _node_id; |
256 | 256 |
IntArcMap _arc_idf; |
257 | 257 |
IntArcMap _arc_idb; |
258 | 258 |
IntVector _first_out; |
259 | 259 |
BoolVector _forward; |
260 | 260 |
IntVector _source; |
261 | 261 |
IntVector _target; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_MAPS_H |
20 | 20 |
#define LEMON_MAPS_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
#include <functional> |
24 | 24 |
#include <vector> |
25 | 25 |
#include <map> |
26 | 26 |
|
27 | 27 |
#include <lemon/core.h> |
28 | 28 |
|
29 | 29 |
///\file |
30 | 30 |
///\ingroup maps |
31 | 31 |
///\brief Miscellaneous property maps |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
/// \addtogroup maps |
36 | 36 |
/// @{ |
37 | 37 |
|
38 | 38 |
/// Base class of maps. |
39 | 39 |
|
40 | 40 |
/// Base class of maps. It provides the necessary type definitions |
41 | 41 |
/// required by the map %concepts. |
42 | 42 |
template<typename K, typename V> |
43 | 43 |
class MapBase { |
44 | 44 |
public: |
45 | 45 |
/// \brief The key type of the map. |
46 | 46 |
typedef K Key; |
47 | 47 |
/// \brief The value type of the map. |
48 | 48 |
/// (The type of objects associated with the keys). |
49 | 49 |
typedef V Value; |
50 | 50 |
}; |
51 | 51 |
|
52 | 52 |
|
53 | 53 |
/// Null map. (a.k.a. DoNothingMap) |
54 | 54 |
|
55 | 55 |
/// This map can be used if you have to provide a map only for |
56 | 56 |
/// its type definitions, or if you have to provide a writable map, |
57 | 57 |
/// but data written to it is not required (i.e. it will be sent to |
58 | 58 |
/// <tt>/dev/null</tt>). |
59 | 59 |
/// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
60 | 60 |
/// |
61 | 61 |
/// \sa ConstMap |
62 | 62 |
template<typename K, typename V> |
63 | 63 |
class NullMap : public MapBase<K, V> { |
64 | 64 |
public: |
65 | 65 |
///\e |
66 | 66 |
typedef K Key; |
67 | 67 |
///\e |
68 | 68 |
typedef V Value; |
69 | 69 |
|
70 | 70 |
/// Gives back a default constructed element. |
71 | 71 |
Value operator[](const Key&) const { return Value(); } |
72 | 72 |
/// Absorbs the value. |
73 | 73 |
void set(const Key&, const Value&) {} |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
/// Returns a \c NullMap class |
77 | 77 |
|
78 | 78 |
/// This function just returns a \c NullMap class. |
79 | 79 |
/// \relates NullMap |
80 | 80 |
template <typename K, typename V> |
81 | 81 |
NullMap<K, V> nullMap() { |
82 | 82 |
return NullMap<K, V>(); |
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
|
86 | 86 |
/// Constant map. |
87 | 87 |
|
88 | 88 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
89 | 89 |
/// value to each key. |
90 | 90 |
/// |
91 | 91 |
/// In other aspects it is equivalent to \c NullMap. |
92 | 92 |
/// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" |
93 | 93 |
/// concept, but it absorbs the data written to it. |
94 | 94 |
/// |
95 | 95 |
/// The simplest way of using this map is through the constMap() |
96 | 96 |
/// function. |
97 | 97 |
/// |
98 | 98 |
/// \sa NullMap |
99 | 99 |
/// \sa IdentityMap |
100 | 100 |
template<typename K, typename V> |
101 | 101 |
class ConstMap : public MapBase<K, V> { |
102 | 102 |
private: |
103 | 103 |
V _value; |
104 | 104 |
public: |
105 | 105 |
///\e |
106 | 106 |
typedef K Key; |
107 | 107 |
///\e |
108 | 108 |
typedef V Value; |
109 | 109 |
|
110 | 110 |
/// Default constructor |
111 | 111 |
|
112 | 112 |
/// Default constructor. |
113 | 113 |
/// The value of the map will be default constructed. |
114 | 114 |
ConstMap() {} |
115 | 115 |
|
116 | 116 |
/// Constructor with specified initial value |
117 | 117 |
|
118 | 118 |
/// Constructor with specified initial value. |
119 | 119 |
/// \param v The initial value of the map. |
120 | 120 |
ConstMap(const Value &v) : _value(v) {} |
121 | 121 |
|
122 | 122 |
/// Gives back the specified value. |
123 | 123 |
Value operator[](const Key&) const { return _value; } |
124 | 124 |
|
125 | 125 |
/// Absorbs the value. |
126 | 126 |
void set(const Key&, const Value&) {} |
127 | 127 |
|
128 | 128 |
/// Sets the value that is assigned to each key. |
129 | 129 |
void setAll(const Value &v) { |
130 | 130 |
_value = v; |
131 | 131 |
} |
132 | 132 |
|
133 | 133 |
template<typename V1> |
134 | 134 |
ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {} |
135 | 135 |
}; |
136 | 136 |
|
137 | 137 |
/// Returns a \c ConstMap class |
138 | 138 |
|
139 | 139 |
/// This function just returns a \c ConstMap class. |
140 | 140 |
/// \relates ConstMap |
141 | 141 |
template<typename K, typename V> |
142 | 142 |
inline ConstMap<K, V> constMap(const V &v) { |
143 | 143 |
return ConstMap<K, V>(v); |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
template<typename K, typename V> |
147 | 147 |
inline ConstMap<K, V> constMap() { |
148 | 148 |
return ConstMap<K, V>(); |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
|
152 | 152 |
template<typename T, T v> |
153 | 153 |
struct Const {}; |
154 | 154 |
|
155 | 155 |
/// Constant map with inlined constant value. |
156 | 156 |
|
157 | 157 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
158 | 158 |
/// value to each key. |
159 | 159 |
/// |
160 | 160 |
/// In other aspects it is equivalent to \c NullMap. |
161 | 161 |
/// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" |
162 | 162 |
/// concept, but it absorbs the data written to it. |
163 | 163 |
/// |
164 | 164 |
/// The simplest way of using this map is through the constMap() |
165 | 165 |
/// function. |
166 | 166 |
/// |
167 | 167 |
/// \sa NullMap |
168 | 168 |
/// \sa IdentityMap |
169 | 169 |
template<typename K, typename V, V v> |
170 | 170 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> { |
171 | 171 |
public: |
172 | 172 |
///\e |
173 | 173 |
typedef K Key; |
174 | 174 |
///\e |
175 | 175 |
typedef V Value; |
176 | 176 |
|
177 | 177 |
/// Constructor. |
178 | 178 |
ConstMap() {} |
179 | 179 |
|
180 | 180 |
/// Gives back the specified value. |
181 | 181 |
Value operator[](const Key&) const { return v; } |
182 | 182 |
|
183 | 183 |
/// Absorbs the value. |
184 | 184 |
void set(const Key&, const Value&) {} |
185 | 185 |
}; |
186 | 186 |
|
187 | 187 |
/// Returns a \c ConstMap class with inlined constant value |
188 | 188 |
|
189 | 189 |
/// This function just returns a \c ConstMap class with inlined |
190 | 190 |
/// constant value. |
191 | 191 |
/// \relates ConstMap |
192 | 192 |
template<typename K, typename V, V v> |
193 | 193 |
inline ConstMap<K, Const<V, v> > constMap() { |
194 | 194 |
return ConstMap<K, Const<V, v> >(); |
195 | 195 |
} |
196 | 196 |
|
197 | 197 |
|
198 | 198 |
/// Identity map. |
199 | 199 |
|
200 | 200 |
/// This \ref concepts::ReadMap "read-only map" gives back the given |
201 | 201 |
/// key as value without any modification. |
202 | 202 |
/// |
203 | 203 |
/// \sa ConstMap |
204 | 204 |
template <typename T> |
205 | 205 |
class IdentityMap : public MapBase<T, T> { |
206 | 206 |
public: |
207 | 207 |
///\e |
208 | 208 |
typedef T Key; |
209 | 209 |
///\e |
210 | 210 |
typedef T Value; |
211 | 211 |
|
212 | 212 |
/// Gives back the given value without any modification. |
213 | 213 |
Value operator[](const Key &k) const { |
214 | 214 |
return k; |
215 | 215 |
} |
216 | 216 |
}; |
217 | 217 |
|
218 | 218 |
/// Returns an \c IdentityMap class |
219 | 219 |
|
220 | 220 |
/// This function just returns an \c IdentityMap class. |
221 | 221 |
/// \relates IdentityMap |
222 | 222 |
template<typename T> |
223 | 223 |
inline IdentityMap<T> identityMap() { |
224 | 224 |
return IdentityMap<T>(); |
225 | 225 |
} |
226 | 226 |
|
227 | 227 |
|
228 | 228 |
/// \brief Map for storing values for integer keys from the range |
229 | 229 |
/// <tt>[0..size-1]</tt>. |
230 | 230 |
/// |
231 | 231 |
/// This map is essentially a wrapper for \c std::vector. It assigns |
232 | 232 |
/// values to integer keys from the range <tt>[0..size-1]</tt>. |
233 | 233 |
/// It can be used together with some data structures, e.g. |
234 | 234 |
/// heap types and \c UnionFind, when the used items are small |
235 | 235 |
/// integers. This map conforms to the \ref concepts::ReferenceMap |
236 | 236 |
/// "ReferenceMap" concept. |
237 | 237 |
/// |
238 | 238 |
/// The simplest way of using this map is through the rangeMap() |
239 | 239 |
/// function. |
240 | 240 |
template <typename V> |
241 | 241 |
class RangeMap : public MapBase<int, V> { |
242 | 242 |
template <typename V1> |
243 | 243 |
friend class RangeMap; |
244 | 244 |
private: |
245 | 245 |
|
246 | 246 |
typedef std::vector<V> Vector; |
247 | 247 |
Vector _vector; |
248 | 248 |
|
249 | 249 |
public: |
250 | 250 |
|
251 | 251 |
/// Key type |
252 | 252 |
typedef int Key; |
253 | 253 |
/// Value type |
254 | 254 |
typedef V Value; |
255 | 255 |
/// Reference type |
256 | 256 |
typedef typename Vector::reference Reference; |
257 | 257 |
/// Const reference type |
258 | 258 |
typedef typename Vector::const_reference ConstReference; |
259 | 259 |
|
260 | 260 |
typedef True ReferenceMapTag; |
261 | 261 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_PREFLOW_H |
20 | 20 |
#define LEMON_PREFLOW_H |
21 | 21 |
|
22 | 22 |
#include <lemon/tolerance.h> |
23 | 23 |
#include <lemon/elevator.h> |
24 | 24 |
|
25 | 25 |
/// \file |
26 | 26 |
/// \ingroup max_flow |
27 | 27 |
/// \brief Implementation of the preflow algorithm. |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
/// \brief Default traits class of Preflow class. |
32 | 32 |
/// |
33 | 33 |
/// Default traits class of Preflow class. |
34 | 34 |
/// \tparam GR Digraph type. |
35 | 35 |
/// \tparam CAP Capacity map type. |
36 | 36 |
template <typename GR, typename CAP> |
37 | 37 |
struct PreflowDefaultTraits { |
38 | 38 |
|
39 | 39 |
/// \brief The type of the digraph the algorithm runs on. |
40 | 40 |
typedef GR Digraph; |
41 | 41 |
|
42 | 42 |
/// \brief The type of the map that stores the arc capacities. |
43 | 43 |
/// |
44 | 44 |
/// The type of the map that stores the arc capacities. |
45 | 45 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
46 | 46 |
typedef CAP CapacityMap; |
47 | 47 |
|
48 | 48 |
/// \brief The type of the flow values. |
49 | 49 |
typedef typename CapacityMap::Value Value; |
50 | 50 |
|
51 | 51 |
/// \brief The type of the map that stores the flow values. |
52 | 52 |
/// |
53 | 53 |
/// The type of the map that stores the flow values. |
54 | 54 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
55 | 55 |
#ifdef DOXYGEN |
56 | 56 |
typedef GR::ArcMap<Value> FlowMap; |
57 | 57 |
#else |
58 | 58 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
59 | 59 |
#endif |
60 | 60 |
|
61 | 61 |
/// \brief Instantiates a FlowMap. |
62 | 62 |
/// |
63 | 63 |
/// This function instantiates a \ref FlowMap. |
64 | 64 |
/// \param digraph The digraph for which we would like to define |
65 | 65 |
/// the flow map. |
66 | 66 |
static FlowMap* createFlowMap(const Digraph& digraph) { |
67 | 67 |
return new FlowMap(digraph); |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
/// \brief The elevator type used by Preflow algorithm. |
71 | 71 |
/// |
72 | 72 |
/// The elevator type used by Preflow algorithm. |
73 | 73 |
/// |
74 | 74 |
/// \sa Elevator, LinkedElevator |
75 | 75 |
#ifdef DOXYGEN |
76 | 76 |
typedef lemon::Elevator<GR, GR::Node> Elevator; |
77 | 77 |
#else |
78 | 78 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
79 | 79 |
#endif |
80 | 80 |
|
81 | 81 |
/// \brief Instantiates an Elevator. |
82 | 82 |
/// |
83 | 83 |
/// This function instantiates an \ref Elevator. |
84 | 84 |
/// \param digraph The digraph for which we would like to define |
85 | 85 |
/// the elevator. |
86 | 86 |
/// \param max_level The maximum level of the elevator. |
87 | 87 |
static Elevator* createElevator(const Digraph& digraph, int max_level) { |
88 | 88 |
return new Elevator(digraph, max_level); |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
/// \brief The tolerance used by the algorithm |
92 | 92 |
/// |
93 | 93 |
/// The tolerance used by the algorithm to handle inexact computation. |
94 | 94 |
typedef lemon::Tolerance<Value> Tolerance; |
95 | 95 |
|
96 | 96 |
}; |
97 | 97 |
|
98 | 98 |
|
99 | 99 |
/// \ingroup max_flow |
100 | 100 |
/// |
101 | 101 |
/// \brief %Preflow algorithm class. |
102 | 102 |
/// |
103 | 103 |
/// This class provides an implementation of Goldberg-Tarjan's \e preflow |
104 | 104 |
/// \e push-relabel algorithm producing a \ref max_flow |
105 | 105 |
/// "flow of maximum value" in a digraph \ref clrs01algorithms, |
106 | 106 |
/// \ref amo93networkflows, \ref goldberg88newapproach. |
107 | 107 |
/// The preflow algorithms are the fastest known maximum |
108 | 108 |
/// flow algorithms. The current implementation uses a mixture of the |
109 | 109 |
/// \e "highest label" and the \e "bound decrease" heuristics. |
110 | 110 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$. |
111 | 111 |
/// |
112 | 112 |
/// The algorithm consists of two phases. After the first phase |
113 | 113 |
/// the maximum flow value and the minimum cut is obtained. The |
114 | 114 |
/// second phase constructs a feasible maximum flow on each arc. |
115 | 115 |
/// |
116 | 116 |
/// \warning This implementation cannot handle infinite or very large |
117 | 117 |
/// capacities (e.g. the maximum value of \c CAP::Value). |
118 | 118 |
/// |
119 | 119 |
/// \tparam GR The type of the digraph the algorithm runs on. |
120 | 120 |
/// \tparam CAP The type of the capacity map. The default map |
121 | 121 |
/// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
122 | 122 |
/// \tparam TR The traits class that defines various types used by the |
123 | 123 |
/// algorithm. By default, it is \ref PreflowDefaultTraits |
124 | 124 |
/// "PreflowDefaultTraits<GR, CAP>". |
125 | 125 |
/// In most cases, this parameter should not be set directly, |
126 | 126 |
/// consider to use the named template parameters instead. |
127 | 127 |
#ifdef DOXYGEN |
128 | 128 |
template <typename GR, typename CAP, typename TR> |
129 | 129 |
#else |
130 | 130 |
template <typename GR, |
131 | 131 |
typename CAP = typename GR::template ArcMap<int>, |
132 | 132 |
typename TR = PreflowDefaultTraits<GR, CAP> > |
133 | 133 |
#endif |
134 | 134 |
class Preflow { |
135 | 135 |
public: |
136 | 136 |
|
137 | 137 |
///The \ref PreflowDefaultTraits "traits class" of the algorithm. |
138 | 138 |
typedef TR Traits; |
139 | 139 |
///The type of the digraph the algorithm runs on. |
140 | 140 |
typedef typename Traits::Digraph Digraph; |
141 | 141 |
///The type of the capacity map. |
142 | 142 |
typedef typename Traits::CapacityMap CapacityMap; |
143 | 143 |
///The type of the flow values. |
144 | 144 |
typedef typename Traits::Value Value; |
145 | 145 |
|
146 | 146 |
///The type of the flow map. |
147 | 147 |
typedef typename Traits::FlowMap FlowMap; |
148 | 148 |
///The type of the elevator. |
149 | 149 |
typedef typename Traits::Elevator Elevator; |
150 | 150 |
///The type of the tolerance. |
151 | 151 |
typedef typename Traits::Tolerance Tolerance; |
152 | 152 |
|
153 | 153 |
private: |
154 | 154 |
|
155 | 155 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
156 | 156 |
|
157 | 157 |
const Digraph& _graph; |
158 | 158 |
const CapacityMap* _capacity; |
159 | 159 |
|
160 | 160 |
int _node_num; |
161 | 161 |
|
162 | 162 |
Node _source, _target; |
163 | 163 |
|
164 | 164 |
FlowMap* _flow; |
165 | 165 |
bool _local_flow; |
166 | 166 |
|
167 | 167 |
Elevator* _level; |
168 | 168 |
bool _local_level; |
169 | 169 |
|
170 | 170 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
171 | 171 |
ExcessMap* _excess; |
172 | 172 |
|
173 | 173 |
Tolerance _tolerance; |
174 | 174 |
|
175 | 175 |
bool _phase; |
176 | 176 |
|
177 | 177 |
|
178 | 178 |
void createStructures() { |
179 | 179 |
_node_num = countNodes(_graph); |
180 | 180 |
|
181 | 181 |
if (!_flow) { |
182 | 182 |
_flow = Traits::createFlowMap(_graph); |
183 | 183 |
_local_flow = true; |
184 | 184 |
} |
185 | 185 |
if (!_level) { |
186 | 186 |
_level = Traits::createElevator(_graph, _node_num); |
187 | 187 |
_local_level = true; |
188 | 188 |
} |
189 | 189 |
if (!_excess) { |
190 | 190 |
_excess = new ExcessMap(_graph); |
191 | 191 |
} |
192 | 192 |
} |
193 | 193 |
|
194 | 194 |
void destroyStructures() { |
195 | 195 |
if (_local_flow) { |
196 | 196 |
delete _flow; |
197 | 197 |
} |
198 | 198 |
if (_local_level) { |
199 | 199 |
delete _level; |
200 | 200 |
} |
201 | 201 |
if (_excess) { |
202 | 202 |
delete _excess; |
203 | 203 |
} |
204 | 204 |
} |
205 | 205 |
|
206 | 206 |
public: |
207 | 207 |
|
208 | 208 |
typedef Preflow Create; |
209 | 209 |
|
210 | 210 |
///\name Named Template Parameters |
211 | 211 |
|
212 | 212 |
///@{ |
213 | 213 |
|
214 | 214 |
template <typename T> |
215 | 215 |
struct SetFlowMapTraits : public Traits { |
216 | 216 |
typedef T FlowMap; |
217 | 217 |
static FlowMap *createFlowMap(const Digraph&) { |
218 | 218 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
219 | 219 |
return 0; // ignore warnings |
220 | 220 |
} |
221 | 221 |
}; |
222 | 222 |
|
223 | 223 |
/// \brief \ref named-templ-param "Named parameter" for setting |
224 | 224 |
/// FlowMap type |
225 | 225 |
/// |
226 | 226 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
227 | 227 |
/// type. |
228 | 228 |
template <typename T> |
229 | 229 |
struct SetFlowMap |
230 | 230 |
: public Preflow<Digraph, CapacityMap, SetFlowMapTraits<T> > { |
231 | 231 |
typedef Preflow<Digraph, CapacityMap, |
232 | 232 |
SetFlowMapTraits<T> > Create; |
233 | 233 |
}; |
234 | 234 |
|
235 | 235 |
template <typename T> |
236 | 236 |
struct SetElevatorTraits : public Traits { |
237 | 237 |
typedef T Elevator; |
238 | 238 |
static Elevator *createElevator(const Digraph&, int) { |
239 | 239 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
240 | 240 |
return 0; // ignore warnings |
241 | 241 |
} |
242 | 242 |
}; |
243 | 243 |
|
244 | 244 |
/// \brief \ref named-templ-param "Named parameter" for setting |
245 | 245 |
/// Elevator type |
246 | 246 |
/// |
247 | 247 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
248 | 248 |
/// type. If this named parameter is used, then an external |
249 | 249 |
/// elevator object must be passed to the algorithm using the |
250 | 250 |
/// \ref elevator(Elevator&) "elevator()" function before calling |
251 | 251 |
/// \ref run() or \ref init(). |
252 | 252 |
/// \sa SetStandardElevator |
253 | 253 |
template <typename T> |
254 | 254 |
struct SetElevator |
255 | 255 |
: public Preflow<Digraph, CapacityMap, SetElevatorTraits<T> > { |
256 | 256 |
typedef Preflow<Digraph, CapacityMap, |
257 | 257 |
SetElevatorTraits<T> > Create; |
258 | 258 |
}; |
259 | 259 |
|
260 | 260 |
template <typename T> |
261 | 261 |
struct SetStandardElevatorTraits : public Traits { |
... | ... |
@@ -301,544 +301,544 @@ |
301 | 301 |
/// \param capacity The capacity of the arcs. |
302 | 302 |
/// \param source The source node. |
303 | 303 |
/// \param target The target node. |
304 | 304 |
Preflow(const Digraph& digraph, const CapacityMap& capacity, |
305 | 305 |
Node source, Node target) |
306 | 306 |
: _graph(digraph), _capacity(&capacity), |
307 | 307 |
_node_num(0), _source(source), _target(target), |
308 | 308 |
_flow(0), _local_flow(false), |
309 | 309 |
_level(0), _local_level(false), |
310 | 310 |
_excess(0), _tolerance(), _phase() {} |
311 | 311 |
|
312 | 312 |
/// \brief Destructor. |
313 | 313 |
/// |
314 | 314 |
/// Destructor. |
315 | 315 |
~Preflow() { |
316 | 316 |
destroyStructures(); |
317 | 317 |
} |
318 | 318 |
|
319 | 319 |
/// \brief Sets the capacity map. |
320 | 320 |
/// |
321 | 321 |
/// Sets the capacity map. |
322 | 322 |
/// \return <tt>(*this)</tt> |
323 | 323 |
Preflow& capacityMap(const CapacityMap& map) { |
324 | 324 |
_capacity = ↦ |
325 | 325 |
return *this; |
326 | 326 |
} |
327 | 327 |
|
328 | 328 |
/// \brief Sets the flow map. |
329 | 329 |
/// |
330 | 330 |
/// Sets the flow map. |
331 | 331 |
/// If you don't use this function before calling \ref run() or |
332 | 332 |
/// \ref init(), an instance will be allocated automatically. |
333 | 333 |
/// The destructor deallocates this automatically allocated map, |
334 | 334 |
/// of course. |
335 | 335 |
/// \return <tt>(*this)</tt> |
336 | 336 |
Preflow& flowMap(FlowMap& map) { |
337 | 337 |
if (_local_flow) { |
338 | 338 |
delete _flow; |
339 | 339 |
_local_flow = false; |
340 | 340 |
} |
341 | 341 |
_flow = ↦ |
342 | 342 |
return *this; |
343 | 343 |
} |
344 | 344 |
|
345 | 345 |
/// \brief Sets the source node. |
346 | 346 |
/// |
347 | 347 |
/// Sets the source node. |
348 | 348 |
/// \return <tt>(*this)</tt> |
349 | 349 |
Preflow& source(const Node& node) { |
350 | 350 |
_source = node; |
351 | 351 |
return *this; |
352 | 352 |
} |
353 | 353 |
|
354 | 354 |
/// \brief Sets the target node. |
355 | 355 |
/// |
356 | 356 |
/// Sets the target node. |
357 | 357 |
/// \return <tt>(*this)</tt> |
358 | 358 |
Preflow& target(const Node& node) { |
359 | 359 |
_target = node; |
360 | 360 |
return *this; |
361 | 361 |
} |
362 | 362 |
|
363 | 363 |
/// \brief Sets the elevator used by algorithm. |
364 | 364 |
/// |
365 | 365 |
/// Sets the elevator used by algorithm. |
366 | 366 |
/// If you don't use this function before calling \ref run() or |
367 | 367 |
/// \ref init(), an instance will be allocated automatically. |
368 | 368 |
/// The destructor deallocates this automatically allocated elevator, |
369 | 369 |
/// of course. |
370 | 370 |
/// \return <tt>(*this)</tt> |
371 | 371 |
Preflow& elevator(Elevator& elevator) { |
372 | 372 |
if (_local_level) { |
373 | 373 |
delete _level; |
374 | 374 |
_local_level = false; |
375 | 375 |
} |
376 | 376 |
_level = &elevator; |
377 | 377 |
return *this; |
378 | 378 |
} |
379 | 379 |
|
380 | 380 |
/// \brief Returns a const reference to the elevator. |
381 | 381 |
/// |
382 | 382 |
/// Returns a const reference to the elevator. |
383 | 383 |
/// |
384 | 384 |
/// \pre Either \ref run() or \ref init() must be called before |
385 | 385 |
/// using this function. |
386 | 386 |
const Elevator& elevator() const { |
387 | 387 |
return *_level; |
388 | 388 |
} |
389 | 389 |
|
390 | 390 |
/// \brief Sets the tolerance used by the algorithm. |
391 | 391 |
/// |
392 | 392 |
/// Sets the tolerance object used by the algorithm. |
393 | 393 |
/// \return <tt>(*this)</tt> |
394 | 394 |
Preflow& tolerance(const Tolerance& tolerance) { |
395 | 395 |
_tolerance = tolerance; |
396 | 396 |
return *this; |
397 | 397 |
} |
398 | 398 |
|
399 | 399 |
/// \brief Returns a const reference to the tolerance. |
400 | 400 |
/// |
401 | 401 |
/// Returns a const reference to the tolerance object used by |
402 | 402 |
/// the algorithm. |
403 | 403 |
const Tolerance& tolerance() const { |
404 | 404 |
return _tolerance; |
405 | 405 |
} |
406 | 406 |
|
407 | 407 |
/// \name Execution Control |
408 | 408 |
/// The simplest way to execute the preflow algorithm is to use |
409 | 409 |
/// \ref run() or \ref runMinCut().\n |
410 | 410 |
/// If you need better control on the initial solution or the execution, |
411 | 411 |
/// you have to call one of the \ref init() functions first, then |
412 | 412 |
/// \ref startFirstPhase() and if you need it \ref startSecondPhase(). |
413 | 413 |
|
414 | 414 |
///@{ |
415 | 415 |
|
416 | 416 |
/// \brief Initializes the internal data structures. |
417 | 417 |
/// |
418 | 418 |
/// Initializes the internal data structures and sets the initial |
419 | 419 |
/// flow to zero on each arc. |
420 | 420 |
void init() { |
421 | 421 |
createStructures(); |
422 | 422 |
|
423 | 423 |
_phase = true; |
424 | 424 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
425 | 425 |
(*_excess)[n] = 0; |
426 | 426 |
} |
427 | 427 |
|
428 | 428 |
for (ArcIt e(_graph); e != INVALID; ++e) { |
429 | 429 |
_flow->set(e, 0); |
430 | 430 |
} |
431 | 431 |
|
432 | 432 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
433 | 433 |
|
434 | 434 |
_level->initStart(); |
435 | 435 |
_level->initAddItem(_target); |
436 | 436 |
|
437 | 437 |
std::vector<Node> queue; |
438 | 438 |
reached[_source] = true; |
439 | 439 |
|
440 | 440 |
queue.push_back(_target); |
441 | 441 |
reached[_target] = true; |
442 | 442 |
while (!queue.empty()) { |
443 | 443 |
_level->initNewLevel(); |
444 | 444 |
std::vector<Node> nqueue; |
445 | 445 |
for (int i = 0; i < int(queue.size()); ++i) { |
446 | 446 |
Node n = queue[i]; |
447 | 447 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
448 | 448 |
Node u = _graph.source(e); |
449 | 449 |
if (!reached[u] && _tolerance.positive((*_capacity)[e])) { |
450 | 450 |
reached[u] = true; |
451 | 451 |
_level->initAddItem(u); |
452 | 452 |
nqueue.push_back(u); |
453 | 453 |
} |
454 | 454 |
} |
455 | 455 |
} |
456 | 456 |
queue.swap(nqueue); |
457 | 457 |
} |
458 | 458 |
_level->initFinish(); |
459 | 459 |
|
460 | 460 |
for (OutArcIt e(_graph, _source); e != INVALID; ++e) { |
461 | 461 |
if (_tolerance.positive((*_capacity)[e])) { |
462 | 462 |
Node u = _graph.target(e); |
463 | 463 |
if ((*_level)[u] == _level->maxLevel()) continue; |
464 | 464 |
_flow->set(e, (*_capacity)[e]); |
465 | 465 |
(*_excess)[u] += (*_capacity)[e]; |
466 | 466 |
if (u != _target && !_level->active(u)) { |
467 | 467 |
_level->activate(u); |
468 | 468 |
} |
469 | 469 |
} |
470 | 470 |
} |
471 | 471 |
} |
472 | 472 |
|
473 | 473 |
/// \brief Initializes the internal data structures using the |
474 | 474 |
/// given flow map. |
475 | 475 |
/// |
476 | 476 |
/// Initializes the internal data structures and sets the initial |
477 | 477 |
/// flow to the given \c flowMap. The \c flowMap should contain a |
478 | 478 |
/// flow or at least a preflow, i.e. at each node excluding the |
479 | 479 |
/// source node the incoming flow should greater or equal to the |
480 | 480 |
/// outgoing flow. |
481 | 481 |
/// \return \c false if the given \c flowMap is not a preflow. |
482 | 482 |
template <typename FlowMap> |
483 | 483 |
bool init(const FlowMap& flowMap) { |
484 | 484 |
createStructures(); |
485 | 485 |
|
486 | 486 |
for (ArcIt e(_graph); e != INVALID; ++e) { |
487 | 487 |
_flow->set(e, flowMap[e]); |
488 | 488 |
} |
489 | 489 |
|
490 | 490 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
491 | 491 |
Value excess = 0; |
492 | 492 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
493 | 493 |
excess += (*_flow)[e]; |
494 | 494 |
} |
495 | 495 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
496 | 496 |
excess -= (*_flow)[e]; |
497 | 497 |
} |
498 | 498 |
if (excess < 0 && n != _source) return false; |
499 | 499 |
(*_excess)[n] = excess; |
500 | 500 |
} |
501 | 501 |
|
502 | 502 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
503 | 503 |
|
504 | 504 |
_level->initStart(); |
505 | 505 |
_level->initAddItem(_target); |
506 | 506 |
|
507 | 507 |
std::vector<Node> queue; |
508 | 508 |
reached[_source] = true; |
509 | 509 |
|
510 | 510 |
queue.push_back(_target); |
511 | 511 |
reached[_target] = true; |
512 | 512 |
while (!queue.empty()) { |
513 | 513 |
_level->initNewLevel(); |
514 | 514 |
std::vector<Node> nqueue; |
515 | 515 |
for (int i = 0; i < int(queue.size()); ++i) { |
516 | 516 |
Node n = queue[i]; |
517 | 517 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
518 | 518 |
Node u = _graph.source(e); |
519 | 519 |
if (!reached[u] && |
520 | 520 |
_tolerance.positive((*_capacity)[e] - (*_flow)[e])) { |
521 | 521 |
reached[u] = true; |
522 | 522 |
_level->initAddItem(u); |
523 | 523 |
nqueue.push_back(u); |
524 | 524 |
} |
525 | 525 |
} |
526 | 526 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
527 | 527 |
Node v = _graph.target(e); |
528 | 528 |
if (!reached[v] && _tolerance.positive((*_flow)[e])) { |
529 | 529 |
reached[v] = true; |
530 | 530 |
_level->initAddItem(v); |
531 | 531 |
nqueue.push_back(v); |
532 | 532 |
} |
533 | 533 |
} |
534 | 534 |
} |
535 | 535 |
queue.swap(nqueue); |
536 | 536 |
} |
537 | 537 |
_level->initFinish(); |
538 | 538 |
|
539 | 539 |
for (OutArcIt e(_graph, _source); e != INVALID; ++e) { |
540 | 540 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
541 | 541 |
if (_tolerance.positive(rem)) { |
542 | 542 |
Node u = _graph.target(e); |
543 | 543 |
if ((*_level)[u] == _level->maxLevel()) continue; |
544 | 544 |
_flow->set(e, (*_capacity)[e]); |
545 | 545 |
(*_excess)[u] += rem; |
546 | 546 |
} |
547 | 547 |
} |
548 | 548 |
for (InArcIt e(_graph, _source); e != INVALID; ++e) { |
549 | 549 |
Value rem = (*_flow)[e]; |
550 | 550 |
if (_tolerance.positive(rem)) { |
551 | 551 |
Node v = _graph.source(e); |
552 | 552 |
if ((*_level)[v] == _level->maxLevel()) continue; |
553 | 553 |
_flow->set(e, 0); |
554 | 554 |
(*_excess)[v] += rem; |
555 | 555 |
} |
556 | 556 |
} |
557 |
for (NodeIt n(_graph); n != INVALID; ++n) |
|
557 |
for (NodeIt n(_graph); n != INVALID; ++n) |
|
558 | 558 |
if(n!=_source && n!=_target && _tolerance.positive((*_excess)[n])) |
559 | 559 |
_level->activate(n); |
560 |
|
|
560 |
|
|
561 | 561 |
return true; |
562 | 562 |
} |
563 | 563 |
|
564 | 564 |
/// \brief Starts the first phase of the preflow algorithm. |
565 | 565 |
/// |
566 | 566 |
/// The preflow algorithm consists of two phases, this method runs |
567 | 567 |
/// the first phase. After the first phase the maximum flow value |
568 | 568 |
/// and a minimum value cut can already be computed, although a |
569 | 569 |
/// maximum flow is not yet obtained. So after calling this method |
570 | 570 |
/// \ref flowValue() returns the value of a maximum flow and \ref |
571 | 571 |
/// minCut() returns a minimum cut. |
572 | 572 |
/// \pre One of the \ref init() functions must be called before |
573 | 573 |
/// using this function. |
574 | 574 |
void startFirstPhase() { |
575 | 575 |
_phase = true; |
576 | 576 |
|
577 | 577 |
while (true) { |
578 | 578 |
int num = _node_num; |
579 | 579 |
|
580 | 580 |
Node n = INVALID; |
581 | 581 |
int level = -1; |
582 | 582 |
|
583 | 583 |
while (num > 0) { |
584 | 584 |
n = _level->highestActive(); |
585 | 585 |
if (n == INVALID) goto first_phase_done; |
586 | 586 |
level = _level->highestActiveLevel(); |
587 | 587 |
--num; |
588 |
|
|
588 |
|
|
589 | 589 |
Value excess = (*_excess)[n]; |
590 | 590 |
int new_level = _level->maxLevel(); |
591 | 591 |
|
592 | 592 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
593 | 593 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
594 | 594 |
if (!_tolerance.positive(rem)) continue; |
595 | 595 |
Node v = _graph.target(e); |
596 | 596 |
if ((*_level)[v] < level) { |
597 | 597 |
if (!_level->active(v) && v != _target) { |
598 | 598 |
_level->activate(v); |
599 | 599 |
} |
600 | 600 |
if (!_tolerance.less(rem, excess)) { |
601 | 601 |
_flow->set(e, (*_flow)[e] + excess); |
602 | 602 |
(*_excess)[v] += excess; |
603 | 603 |
excess = 0; |
604 | 604 |
goto no_more_push_1; |
605 | 605 |
} else { |
606 | 606 |
excess -= rem; |
607 | 607 |
(*_excess)[v] += rem; |
608 | 608 |
_flow->set(e, (*_capacity)[e]); |
609 | 609 |
} |
610 | 610 |
} else if (new_level > (*_level)[v]) { |
611 | 611 |
new_level = (*_level)[v]; |
612 | 612 |
} |
613 | 613 |
} |
614 | 614 |
|
615 | 615 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
616 | 616 |
Value rem = (*_flow)[e]; |
617 | 617 |
if (!_tolerance.positive(rem)) continue; |
618 | 618 |
Node v = _graph.source(e); |
619 | 619 |
if ((*_level)[v] < level) { |
620 | 620 |
if (!_level->active(v) && v != _target) { |
621 | 621 |
_level->activate(v); |
622 | 622 |
} |
623 | 623 |
if (!_tolerance.less(rem, excess)) { |
624 | 624 |
_flow->set(e, (*_flow)[e] - excess); |
625 | 625 |
(*_excess)[v] += excess; |
626 | 626 |
excess = 0; |
627 | 627 |
goto no_more_push_1; |
628 | 628 |
} else { |
629 | 629 |
excess -= rem; |
630 | 630 |
(*_excess)[v] += rem; |
631 | 631 |
_flow->set(e, 0); |
632 | 632 |
} |
633 | 633 |
} else if (new_level > (*_level)[v]) { |
634 | 634 |
new_level = (*_level)[v]; |
635 | 635 |
} |
636 | 636 |
} |
637 | 637 |
|
638 | 638 |
no_more_push_1: |
639 | 639 |
|
640 | 640 |
(*_excess)[n] = excess; |
641 | 641 |
|
642 | 642 |
if (excess != 0) { |
643 | 643 |
if (new_level + 1 < _level->maxLevel()) { |
644 | 644 |
_level->liftHighestActive(new_level + 1); |
645 | 645 |
} else { |
646 | 646 |
_level->liftHighestActiveToTop(); |
647 | 647 |
} |
648 | 648 |
if (_level->emptyLevel(level)) { |
649 | 649 |
_level->liftToTop(level); |
650 | 650 |
} |
651 | 651 |
} else { |
652 | 652 |
_level->deactivate(n); |
653 | 653 |
} |
654 | 654 |
} |
655 | 655 |
|
656 | 656 |
num = _node_num * 20; |
657 | 657 |
while (num > 0) { |
658 | 658 |
while (level >= 0 && _level->activeFree(level)) { |
659 | 659 |
--level; |
660 | 660 |
} |
661 | 661 |
if (level == -1) { |
662 | 662 |
n = _level->highestActive(); |
663 | 663 |
level = _level->highestActiveLevel(); |
664 | 664 |
if (n == INVALID) goto first_phase_done; |
665 | 665 |
} else { |
666 | 666 |
n = _level->activeOn(level); |
667 | 667 |
} |
668 | 668 |
--num; |
669 | 669 |
|
670 | 670 |
Value excess = (*_excess)[n]; |
671 | 671 |
int new_level = _level->maxLevel(); |
672 | 672 |
|
673 | 673 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
674 | 674 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
675 | 675 |
if (!_tolerance.positive(rem)) continue; |
676 | 676 |
Node v = _graph.target(e); |
677 | 677 |
if ((*_level)[v] < level) { |
678 | 678 |
if (!_level->active(v) && v != _target) { |
679 | 679 |
_level->activate(v); |
680 | 680 |
} |
681 | 681 |
if (!_tolerance.less(rem, excess)) { |
682 | 682 |
_flow->set(e, (*_flow)[e] + excess); |
683 | 683 |
(*_excess)[v] += excess; |
684 | 684 |
excess = 0; |
685 | 685 |
goto no_more_push_2; |
686 | 686 |
} else { |
687 | 687 |
excess -= rem; |
688 | 688 |
(*_excess)[v] += rem; |
689 | 689 |
_flow->set(e, (*_capacity)[e]); |
690 | 690 |
} |
691 | 691 |
} else if (new_level > (*_level)[v]) { |
692 | 692 |
new_level = (*_level)[v]; |
693 | 693 |
} |
694 | 694 |
} |
695 | 695 |
|
696 | 696 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
697 | 697 |
Value rem = (*_flow)[e]; |
698 | 698 |
if (!_tolerance.positive(rem)) continue; |
699 | 699 |
Node v = _graph.source(e); |
700 | 700 |
if ((*_level)[v] < level) { |
701 | 701 |
if (!_level->active(v) && v != _target) { |
702 | 702 |
_level->activate(v); |
703 | 703 |
} |
704 | 704 |
if (!_tolerance.less(rem, excess)) { |
705 | 705 |
_flow->set(e, (*_flow)[e] - excess); |
706 | 706 |
(*_excess)[v] += excess; |
707 | 707 |
excess = 0; |
708 | 708 |
goto no_more_push_2; |
709 | 709 |
} else { |
710 | 710 |
excess -= rem; |
711 | 711 |
(*_excess)[v] += rem; |
712 | 712 |
_flow->set(e, 0); |
713 | 713 |
} |
714 | 714 |
} else if (new_level > (*_level)[v]) { |
715 | 715 |
new_level = (*_level)[v]; |
716 | 716 |
} |
717 | 717 |
} |
718 | 718 |
|
719 | 719 |
no_more_push_2: |
720 | 720 |
|
721 | 721 |
(*_excess)[n] = excess; |
722 | 722 |
|
723 | 723 |
if (excess != 0) { |
724 | 724 |
if (new_level + 1 < _level->maxLevel()) { |
725 | 725 |
_level->liftActiveOn(level, new_level + 1); |
726 | 726 |
} else { |
727 | 727 |
_level->liftActiveToTop(level); |
728 | 728 |
} |
729 | 729 |
if (_level->emptyLevel(level)) { |
730 | 730 |
_level->liftToTop(level); |
731 | 731 |
} |
732 | 732 |
} else { |
733 | 733 |
_level->deactivate(n); |
734 | 734 |
} |
735 | 735 |
} |
736 | 736 |
} |
737 | 737 |
first_phase_done:; |
738 | 738 |
} |
739 | 739 |
|
740 | 740 |
/// \brief Starts the second phase of the preflow algorithm. |
741 | 741 |
/// |
742 | 742 |
/// The preflow algorithm consists of two phases, this method runs |
743 | 743 |
/// the second phase. After calling one of the \ref init() functions |
744 | 744 |
/// and \ref startFirstPhase() and then \ref startSecondPhase(), |
745 | 745 |
/// \ref flowMap() returns a maximum flow, \ref flowValue() returns the |
746 | 746 |
/// value of a maximum flow, \ref minCut() returns a minimum cut |
747 | 747 |
/// \pre One of the \ref init() functions and \ref startFirstPhase() |
748 | 748 |
/// must be called before using this function. |
749 | 749 |
void startSecondPhase() { |
750 | 750 |
_phase = false; |
751 | 751 |
|
752 | 752 |
typename Digraph::template NodeMap<bool> reached(_graph); |
753 | 753 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
754 | 754 |
reached[n] = (*_level)[n] < _level->maxLevel(); |
755 | 755 |
} |
756 | 756 |
|
757 | 757 |
_level->initStart(); |
758 | 758 |
_level->initAddItem(_source); |
759 | 759 |
|
760 | 760 |
std::vector<Node> queue; |
761 | 761 |
queue.push_back(_source); |
762 | 762 |
reached[_source] = true; |
763 | 763 |
|
764 | 764 |
while (!queue.empty()) { |
765 | 765 |
_level->initNewLevel(); |
766 | 766 |
std::vector<Node> nqueue; |
767 | 767 |
for (int i = 0; i < int(queue.size()); ++i) { |
768 | 768 |
Node n = queue[i]; |
769 | 769 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
770 | 770 |
Node v = _graph.target(e); |
771 | 771 |
if (!reached[v] && _tolerance.positive((*_flow)[e])) { |
772 | 772 |
reached[v] = true; |
773 | 773 |
_level->initAddItem(v); |
774 | 774 |
nqueue.push_back(v); |
775 | 775 |
} |
776 | 776 |
} |
777 | 777 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
778 | 778 |
Node u = _graph.source(e); |
779 | 779 |
if (!reached[u] && |
780 | 780 |
_tolerance.positive((*_capacity)[e] - (*_flow)[e])) { |
781 | 781 |
reached[u] = true; |
782 | 782 |
_level->initAddItem(u); |
783 | 783 |
nqueue.push_back(u); |
784 | 784 |
} |
785 | 785 |
} |
786 | 786 |
} |
787 | 787 |
queue.swap(nqueue); |
788 | 788 |
} |
789 | 789 |
_level->initFinish(); |
790 | 790 |
|
791 | 791 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
792 | 792 |
if (!reached[n]) { |
793 | 793 |
_level->dirtyTopButOne(n); |
794 | 794 |
} else if ((*_excess)[n] > 0 && _target != n) { |
795 | 795 |
_level->activate(n); |
796 | 796 |
} |
797 | 797 |
} |
798 | 798 |
|
799 | 799 |
Node n; |
800 | 800 |
while ((n = _level->highestActive()) != INVALID) { |
801 | 801 |
Value excess = (*_excess)[n]; |
802 | 802 |
int level = _level->highestActiveLevel(); |
803 | 803 |
int new_level = _level->maxLevel(); |
804 | 804 |
|
805 | 805 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
806 | 806 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
807 | 807 |
if (!_tolerance.positive(rem)) continue; |
808 | 808 |
Node v = _graph.target(e); |
809 | 809 |
if ((*_level)[v] < level) { |
810 | 810 |
if (!_level->active(v) && v != _source) { |
811 | 811 |
_level->activate(v); |
812 | 812 |
} |
813 | 813 |
if (!_tolerance.less(rem, excess)) { |
814 | 814 |
_flow->set(e, (*_flow)[e] + excess); |
815 | 815 |
(*_excess)[v] += excess; |
816 | 816 |
excess = 0; |
817 | 817 |
goto no_more_push; |
818 | 818 |
} else { |
819 | 819 |
excess -= rem; |
820 | 820 |
(*_excess)[v] += rem; |
821 | 821 |
_flow->set(e, (*_capacity)[e]); |
822 | 822 |
} |
823 | 823 |
} else if (new_level > (*_level)[v]) { |
824 | 824 |
new_level = (*_level)[v]; |
825 | 825 |
} |
826 | 826 |
} |
827 | 827 |
|
828 | 828 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
829 | 829 |
Value rem = (*_flow)[e]; |
830 | 830 |
if (!_tolerance.positive(rem)) continue; |
831 | 831 |
Node v = _graph.source(e); |
832 | 832 |
if ((*_level)[v] < level) { |
833 | 833 |
if (!_level->active(v) && v != _source) { |
834 | 834 |
_level->activate(v); |
835 | 835 |
} |
836 | 836 |
if (!_tolerance.less(rem, excess)) { |
837 | 837 |
_flow->set(e, (*_flow)[e] - excess); |
838 | 838 |
(*_excess)[v] += excess; |
839 | 839 |
excess = 0; |
840 | 840 |
goto no_more_push; |
841 | 841 |
} else { |
842 | 842 |
excess -= rem; |
843 | 843 |
(*_excess)[v] += rem; |
844 | 844 |
_flow->set(e, 0); |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/concepts/digraph.h> |
20 | 20 |
#include <lemon/smart_graph.h> |
21 | 21 |
#include <lemon/list_graph.h> |
22 | 22 |
#include <lemon/lgf_reader.h> |
23 | 23 |
#include <lemon/dfs.h> |
24 | 24 |
#include <lemon/path.h> |
25 | 25 |
|
26 | 26 |
#include "graph_test.h" |
27 | 27 |
#include "test_tools.h" |
28 | 28 |
|
29 | 29 |
using namespace lemon; |
30 | 30 |
|
31 | 31 |
char test_lgf[] = |
32 | 32 |
"@nodes\n" |
33 | 33 |
"label\n" |
34 | 34 |
"0\n" |
35 | 35 |
"1\n" |
36 | 36 |
"2\n" |
37 | 37 |
"3\n" |
38 | 38 |
"4\n" |
39 | 39 |
"5\n" |
40 | 40 |
"6\n" |
41 | 41 |
"@arcs\n" |
42 | 42 |
" label\n" |
43 | 43 |
"0 1 0\n" |
44 | 44 |
"1 2 1\n" |
45 | 45 |
"2 3 2\n" |
46 | 46 |
"1 4 3\n" |
47 | 47 |
"4 2 4\n" |
48 | 48 |
"4 5 5\n" |
49 | 49 |
"5 0 6\n" |
50 | 50 |
"6 3 7\n" |
51 | 51 |
"@attributes\n" |
52 | 52 |
"source 0\n" |
53 | 53 |
"target 5\n" |
54 | 54 |
"source1 6\n" |
55 | 55 |
"target1 3\n"; |
56 | 56 |
|
57 | 57 |
|
58 | 58 |
void checkDfsCompile() |
59 | 59 |
{ |
60 | 60 |
typedef concepts::Digraph Digraph; |
61 | 61 |
typedef Dfs<Digraph> DType; |
62 | 62 |
typedef Digraph::Node Node; |
63 | 63 |
typedef Digraph::Arc Arc; |
64 | 64 |
|
65 | 65 |
Digraph G; |
66 | 66 |
Node s, t; |
67 | 67 |
Arc e; |
68 | 68 |
int l, i; |
69 | 69 |
bool b; |
70 | 70 |
DType::DistMap d(G); |
71 | 71 |
DType::PredMap p(G); |
72 | 72 |
Path<Digraph> pp; |
73 | 73 |
concepts::ReadMap<Arc,bool> am; |
74 | 74 |
|
75 | 75 |
{ |
76 | 76 |
DType dfs_test(G); |
77 | 77 |
const DType& const_dfs_test = dfs_test; |
78 | 78 |
|
79 | 79 |
dfs_test.run(s); |
80 | 80 |
dfs_test.run(s,t); |
81 | 81 |
dfs_test.run(); |
82 | 82 |
|
83 | 83 |
dfs_test.init(); |
84 | 84 |
dfs_test.addSource(s); |
85 | 85 |
e = dfs_test.processNextArc(); |
86 | 86 |
e = const_dfs_test.nextArc(); |
87 | 87 |
b = const_dfs_test.emptyQueue(); |
88 | 88 |
i = const_dfs_test.queueSize(); |
89 | 89 |
|
90 | 90 |
dfs_test.start(); |
91 | 91 |
dfs_test.start(t); |
92 | 92 |
dfs_test.start(am); |
93 | 93 |
|
94 | 94 |
l = const_dfs_test.dist(t); |
95 | 95 |
e = const_dfs_test.predArc(t); |
96 | 96 |
s = const_dfs_test.predNode(t); |
97 | 97 |
b = const_dfs_test.reached(t); |
98 | 98 |
d = const_dfs_test.distMap(); |
99 | 99 |
p = const_dfs_test.predMap(); |
100 | 100 |
pp = const_dfs_test.path(t); |
101 | 101 |
} |
102 | 102 |
{ |
103 | 103 |
DType |
104 | 104 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
105 | 105 |
::SetDistMap<concepts::ReadWriteMap<Node,int> > |
106 | 106 |
::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
107 | 107 |
::SetStandardProcessedMap |
108 | 108 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
109 | 109 |
::Create dfs_test(G); |
110 | 110 |
|
111 | 111 |
concepts::ReadWriteMap<Node,Arc> pred_map; |
112 | 112 |
concepts::ReadWriteMap<Node,int> dist_map; |
113 | 113 |
concepts::ReadWriteMap<Node,bool> reached_map; |
114 | 114 |
concepts::WriteMap<Node,bool> processed_map; |
115 | 115 |
|
116 | 116 |
dfs_test |
117 | 117 |
.predMap(pred_map) |
118 | 118 |
.distMap(dist_map) |
119 | 119 |
.reachedMap(reached_map) |
120 | 120 |
.processedMap(processed_map); |
121 | 121 |
|
122 | 122 |
dfs_test.run(s); |
123 | 123 |
dfs_test.run(s,t); |
124 | 124 |
dfs_test.run(); |
125 | 125 |
dfs_test.init(); |
126 | 126 |
|
127 | 127 |
dfs_test.addSource(s); |
128 | 128 |
e = dfs_test.processNextArc(); |
129 | 129 |
e = dfs_test.nextArc(); |
130 | 130 |
b = dfs_test.emptyQueue(); |
131 | 131 |
i = dfs_test.queueSize(); |
132 | 132 |
|
133 | 133 |
dfs_test.start(); |
134 | 134 |
dfs_test.start(t); |
135 | 135 |
dfs_test.start(am); |
136 | 136 |
|
137 | 137 |
l = dfs_test.dist(t); |
138 | 138 |
e = dfs_test.predArc(t); |
139 | 139 |
s = dfs_test.predNode(t); |
140 | 140 |
b = dfs_test.reached(t); |
141 | 141 |
pp = dfs_test.path(t); |
142 | 142 |
} |
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
void checkDfsFunctionCompile() |
146 | 146 |
{ |
147 | 147 |
typedef int VType; |
148 | 148 |
typedef concepts::Digraph Digraph; |
149 | 149 |
typedef Digraph::Arc Arc; |
150 | 150 |
typedef Digraph::Node Node; |
151 | 151 |
|
152 | 152 |
Digraph g; |
153 | 153 |
bool b; |
154 | 154 |
dfs(g).run(Node()); |
155 | 155 |
b=dfs(g).run(Node(),Node()); |
156 | 156 |
dfs(g).run(); |
157 | 157 |
dfs(g) |
158 | 158 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
159 | 159 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
160 | 160 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
161 | 161 |
.processedMap(concepts::WriteMap<Node,bool>()) |
162 | 162 |
.run(Node()); |
163 | 163 |
b=dfs(g) |
164 | 164 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
165 | 165 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
166 | 166 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
167 | 167 |
.processedMap(concepts::WriteMap<Node,bool>()) |
168 | 168 |
.path(concepts::Path<Digraph>()) |
169 | 169 |
.dist(VType()) |
170 | 170 |
.run(Node(),Node()); |
171 | 171 |
dfs(g) |
172 | 172 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
173 | 173 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
174 | 174 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
175 | 175 |
.processedMap(concepts::WriteMap<Node,bool>()) |
176 | 176 |
.run(); |
177 | 177 |
} |
178 | 178 |
|
179 | 179 |
template <class Digraph> |
180 | 180 |
void checkDfs() { |
181 | 181 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
182 | 182 |
|
183 | 183 |
Digraph G; |
184 | 184 |
Node s, t; |
185 | 185 |
Node s1, t1; |
186 | 186 |
|
187 | 187 |
std::istringstream input(test_lgf); |
188 | 188 |
digraphReader(G, input). |
189 | 189 |
node("source", s). |
190 | 190 |
node("target", t). |
191 | 191 |
node("source1", s1). |
192 | 192 |
node("target1", t1). |
193 | 193 |
run(); |
194 | 194 |
|
195 | 195 |
Dfs<Digraph> dfs_test(G); |
196 | 196 |
dfs_test.run(s); |
197 | 197 |
|
198 | 198 |
Path<Digraph> p = dfs_test.path(t); |
199 | 199 |
check(p.length() == dfs_test.dist(t),"path() found a wrong path."); |
200 | 200 |
check(checkPath(G, p),"path() found a wrong path."); |
201 | 201 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
202 | 202 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
203 | 203 |
|
204 | 204 |
for(NodeIt v(G); v!=INVALID; ++v) { |
205 | 205 |
if (dfs_test.reached(v)) { |
206 | 206 |
check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree."); |
207 | 207 |
if (dfs_test.predArc(v)!=INVALID ) { |
208 | 208 |
Arc e=dfs_test.predArc(v); |
209 | 209 |
Node u=G.source(e); |
210 | 210 |
check(u==dfs_test.predNode(v),"Wrong tree."); |
211 | 211 |
check(dfs_test.dist(v) - dfs_test.dist(u) == 1, |
212 | 212 |
"Wrong distance. (" << dfs_test.dist(u) << "->" |
213 | 213 |
<< dfs_test.dist(v) << ")"); |
214 | 214 |
} |
215 | 215 |
} |
216 | 216 |
} |
217 | 217 |
|
218 | 218 |
{ |
219 | 219 |
Dfs<Digraph> dfs(G); |
220 | 220 |
check(dfs.run(s1,t1) && dfs.reached(t1),"Node 3 is reachable from Node 6."); |
221 | 221 |
} |
222 |
|
|
222 |
|
|
223 | 223 |
{ |
224 | 224 |
NullMap<Node,Arc> myPredMap; |
225 | 225 |
dfs(G).predMap(myPredMap).run(s); |
226 | 226 |
} |
227 | 227 |
} |
228 | 228 |
|
229 | 229 |
int main() |
230 | 230 |
{ |
231 | 231 |
checkDfs<ListDigraph>(); |
232 | 232 |
checkDfs<SmartDigraph>(); |
233 | 233 |
return 0; |
234 | 234 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/smart_graph.h> |
20 | 20 |
#include <lemon/list_graph.h> |
21 | 21 |
#include <lemon/lgf_reader.h> |
22 | 22 |
#include <lemon/error.h> |
23 | 23 |
|
24 | 24 |
#include "test_tools.h" |
25 | 25 |
|
26 | 26 |
using namespace std; |
27 | 27 |
using namespace lemon; |
28 | 28 |
|
29 | 29 |
void digraph_copy_test() { |
30 | 30 |
const int nn = 10; |
31 | 31 |
|
32 | 32 |
// Build a digraph |
33 | 33 |
SmartDigraph from; |
34 | 34 |
SmartDigraph::NodeMap<int> fnm(from); |
35 | 35 |
SmartDigraph::ArcMap<int> fam(from); |
36 | 36 |
SmartDigraph::Node fn = INVALID; |
37 | 37 |
SmartDigraph::Arc fa = INVALID; |
38 | 38 |
|
39 | 39 |
std::vector<SmartDigraph::Node> fnv; |
40 | 40 |
for (int i = 0; i < nn; ++i) { |
41 | 41 |
SmartDigraph::Node node = from.addNode(); |
42 | 42 |
fnv.push_back(node); |
43 | 43 |
fnm[node] = i * i; |
44 | 44 |
if (i == 0) fn = node; |
45 | 45 |
} |
46 | 46 |
|
47 | 47 |
for (int i = 0; i < nn; ++i) { |
48 | 48 |
for (int j = 0; j < nn; ++j) { |
49 | 49 |
SmartDigraph::Arc arc = from.addArc(fnv[i], fnv[j]); |
50 | 50 |
fam[arc] = i + j * j; |
51 | 51 |
if (i == 0 && j == 0) fa = arc; |
52 | 52 |
} |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
// Test digraph copy |
56 | 56 |
ListDigraph to; |
57 | 57 |
ListDigraph::NodeMap<int> tnm(to); |
58 | 58 |
ListDigraph::ArcMap<int> tam(to); |
59 | 59 |
ListDigraph::Node tn; |
60 | 60 |
ListDigraph::Arc ta; |
61 | 61 |
|
62 | 62 |
SmartDigraph::NodeMap<ListDigraph::Node> nr(from); |
63 | 63 |
SmartDigraph::ArcMap<ListDigraph::Arc> er(from); |
64 | 64 |
|
65 | 65 |
ListDigraph::NodeMap<SmartDigraph::Node> ncr(to); |
66 | 66 |
ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to); |
67 | 67 |
|
68 | 68 |
digraphCopy(from, to). |
69 | 69 |
nodeMap(fnm, tnm).arcMap(fam, tam). |
70 | 70 |
nodeRef(nr).arcRef(er). |
71 | 71 |
nodeCrossRef(ncr).arcCrossRef(ecr). |
72 | 72 |
node(fn, tn).arc(fa, ta).run(); |
73 |
|
|
73 |
|
|
74 | 74 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
75 | 75 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
76 | 76 |
|
77 | 77 |
for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) { |
78 | 78 |
check(ncr[nr[it]] == it, "Wrong copy."); |
79 | 79 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
for (SmartDigraph::ArcIt it(from); it != INVALID; ++it) { |
83 | 83 |
check(ecr[er[it]] == it, "Wrong copy."); |
84 | 84 |
check(fam[it] == tam[er[it]], "Wrong copy."); |
85 | 85 |
check(nr[from.source(it)] == to.source(er[it]), "Wrong copy."); |
86 | 86 |
check(nr[from.target(it)] == to.target(er[it]), "Wrong copy."); |
87 | 87 |
} |
88 | 88 |
|
89 | 89 |
for (ListDigraph::NodeIt it(to); it != INVALID; ++it) { |
90 | 90 |
check(nr[ncr[it]] == it, "Wrong copy."); |
91 | 91 |
} |
92 | 92 |
|
93 | 93 |
for (ListDigraph::ArcIt it(to); it != INVALID; ++it) { |
94 | 94 |
check(er[ecr[it]] == it, "Wrong copy."); |
95 | 95 |
} |
96 | 96 |
check(tn == nr[fn], "Wrong copy."); |
97 | 97 |
check(ta == er[fa], "Wrong copy."); |
98 | 98 |
|
99 | 99 |
// Test repeated copy |
100 | 100 |
digraphCopy(from, to).run(); |
101 |
|
|
101 |
|
|
102 | 102 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
103 | 103 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
104 | 104 |
} |
105 | 105 |
|
106 | 106 |
void graph_copy_test() { |
107 | 107 |
const int nn = 10; |
108 | 108 |
|
109 | 109 |
// Build a graph |
110 | 110 |
SmartGraph from; |
111 | 111 |
SmartGraph::NodeMap<int> fnm(from); |
112 | 112 |
SmartGraph::ArcMap<int> fam(from); |
113 | 113 |
SmartGraph::EdgeMap<int> fem(from); |
114 | 114 |
SmartGraph::Node fn = INVALID; |
115 | 115 |
SmartGraph::Arc fa = INVALID; |
116 | 116 |
SmartGraph::Edge fe = INVALID; |
117 | 117 |
|
118 | 118 |
std::vector<SmartGraph::Node> fnv; |
119 | 119 |
for (int i = 0; i < nn; ++i) { |
120 | 120 |
SmartGraph::Node node = from.addNode(); |
121 | 121 |
fnv.push_back(node); |
122 | 122 |
fnm[node] = i * i; |
123 | 123 |
if (i == 0) fn = node; |
124 | 124 |
} |
125 | 125 |
|
126 | 126 |
for (int i = 0; i < nn; ++i) { |
127 | 127 |
for (int j = 0; j < nn; ++j) { |
128 | 128 |
SmartGraph::Edge edge = from.addEdge(fnv[i], fnv[j]); |
129 | 129 |
fem[edge] = i * i + j * j; |
130 | 130 |
fam[from.direct(edge, true)] = i + j * j; |
131 | 131 |
fam[from.direct(edge, false)] = i * i + j; |
132 | 132 |
if (i == 0 && j == 0) fa = from.direct(edge, true); |
133 | 133 |
if (i == 0 && j == 0) fe = edge; |
134 | 134 |
} |
135 | 135 |
} |
136 | 136 |
|
137 | 137 |
// Test graph copy |
138 | 138 |
ListGraph to; |
139 | 139 |
ListGraph::NodeMap<int> tnm(to); |
140 | 140 |
ListGraph::ArcMap<int> tam(to); |
141 | 141 |
ListGraph::EdgeMap<int> tem(to); |
142 | 142 |
ListGraph::Node tn; |
143 | 143 |
ListGraph::Arc ta; |
144 | 144 |
ListGraph::Edge te; |
145 | 145 |
|
146 | 146 |
SmartGraph::NodeMap<ListGraph::Node> nr(from); |
147 | 147 |
SmartGraph::ArcMap<ListGraph::Arc> ar(from); |
148 | 148 |
SmartGraph::EdgeMap<ListGraph::Edge> er(from); |
149 | 149 |
|
150 | 150 |
ListGraph::NodeMap<SmartGraph::Node> ncr(to); |
151 | 151 |
ListGraph::ArcMap<SmartGraph::Arc> acr(to); |
152 | 152 |
ListGraph::EdgeMap<SmartGraph::Edge> ecr(to); |
153 | 153 |
|
154 | 154 |
graphCopy(from, to). |
155 | 155 |
nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem). |
156 | 156 |
nodeRef(nr).arcRef(ar).edgeRef(er). |
157 | 157 |
nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr). |
158 | 158 |
node(fn, tn).arc(fa, ta).edge(fe, te).run(); |
159 | 159 |
|
160 | 160 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
161 | 161 |
check(countEdges(from) == countEdges(to), "Wrong copy."); |
162 | 162 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
163 | 163 |
|
164 | 164 |
for (SmartGraph::NodeIt it(from); it != INVALID; ++it) { |
165 | 165 |
check(ncr[nr[it]] == it, "Wrong copy."); |
166 | 166 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
167 | 167 |
} |
168 | 168 |
|
169 | 169 |
for (SmartGraph::ArcIt it(from); it != INVALID; ++it) { |
170 | 170 |
check(acr[ar[it]] == it, "Wrong copy."); |
171 | 171 |
check(fam[it] == tam[ar[it]], "Wrong copy."); |
172 | 172 |
check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy."); |
173 | 173 |
check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy."); |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
for (SmartGraph::EdgeIt it(from); it != INVALID; ++it) { |
177 | 177 |
check(ecr[er[it]] == it, "Wrong copy."); |
178 | 178 |
check(fem[it] == tem[er[it]], "Wrong copy."); |
179 | 179 |
check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]), |
180 | 180 |
"Wrong copy."); |
181 | 181 |
check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]), |
182 | 182 |
"Wrong copy."); |
183 | 183 |
check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])), |
184 | 184 |
"Wrong copy."); |
185 | 185 |
} |
186 | 186 |
|
187 | 187 |
for (ListGraph::NodeIt it(to); it != INVALID; ++it) { |
188 | 188 |
check(nr[ncr[it]] == it, "Wrong copy."); |
189 | 189 |
} |
190 | 190 |
|
191 | 191 |
for (ListGraph::ArcIt it(to); it != INVALID; ++it) { |
192 | 192 |
check(ar[acr[it]] == it, "Wrong copy."); |
193 | 193 |
} |
194 | 194 |
for (ListGraph::EdgeIt it(to); it != INVALID; ++it) { |
195 | 195 |
check(er[ecr[it]] == it, "Wrong copy."); |
196 | 196 |
} |
197 | 197 |
check(tn == nr[fn], "Wrong copy."); |
198 | 198 |
check(ta == ar[fa], "Wrong copy."); |
199 | 199 |
check(te == er[fe], "Wrong copy."); |
200 | 200 |
|
201 | 201 |
// Test repeated copy |
202 | 202 |
graphCopy(from, to).run(); |
203 |
|
|
203 |
|
|
204 | 204 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
205 | 205 |
check(countEdges(from) == countEdges(to), "Wrong copy."); |
206 | 206 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
207 | 207 |
} |
208 | 208 |
|
209 | 209 |
|
210 | 210 |
int main() { |
211 | 211 |
digraph_copy_test(); |
212 | 212 |
graph_copy_test(); |
213 | 213 |
|
214 | 214 |
return 0; |
215 | 215 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <iostream> |
20 | 20 |
#include <fstream> |
21 | 21 |
#include <string> |
22 | 22 |
#include <vector> |
23 | 23 |
|
24 | 24 |
#include <lemon/concept_check.h> |
25 | 25 |
#include <lemon/concepts/heap.h> |
26 | 26 |
|
27 | 27 |
#include <lemon/smart_graph.h> |
28 | 28 |
#include <lemon/lgf_reader.h> |
29 | 29 |
#include <lemon/dijkstra.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
|
32 | 32 |
#include <lemon/bin_heap.h> |
33 | 33 |
#include <lemon/quad_heap.h> |
34 | 34 |
#include <lemon/dheap.h> |
35 | 35 |
#include <lemon/fib_heap.h> |
36 | 36 |
#include <lemon/pairing_heap.h> |
37 | 37 |
#include <lemon/radix_heap.h> |
38 | 38 |
#include <lemon/binomial_heap.h> |
39 | 39 |
#include <lemon/bucket_heap.h> |
40 | 40 |
|
41 | 41 |
#include "test_tools.h" |
42 | 42 |
|
43 | 43 |
using namespace lemon; |
44 | 44 |
using namespace lemon::concepts; |
45 | 45 |
|
46 | 46 |
typedef ListDigraph Digraph; |
47 | 47 |
DIGRAPH_TYPEDEFS(Digraph); |
48 | 48 |
|
49 | 49 |
char test_lgf[] = |
50 | 50 |
"@nodes\n" |
51 | 51 |
"label\n" |
52 | 52 |
"0\n" |
53 | 53 |
"1\n" |
54 | 54 |
"2\n" |
55 | 55 |
"3\n" |
56 | 56 |
"4\n" |
57 | 57 |
"5\n" |
58 | 58 |
"6\n" |
59 | 59 |
"7\n" |
60 | 60 |
"8\n" |
61 | 61 |
"9\n" |
62 | 62 |
"@arcs\n" |
63 | 63 |
" label capacity\n" |
64 | 64 |
"0 5 0 94\n" |
65 | 65 |
"3 9 1 11\n" |
66 | 66 |
"8 7 2 83\n" |
67 | 67 |
"1 2 3 94\n" |
68 | 68 |
"5 7 4 35\n" |
69 | 69 |
"7 4 5 84\n" |
70 | 70 |
"9 5 6 38\n" |
71 | 71 |
"0 4 7 96\n" |
72 | 72 |
"6 7 8 6\n" |
73 | 73 |
"3 1 9 27\n" |
74 | 74 |
"5 2 10 77\n" |
75 | 75 |
"5 6 11 69\n" |
76 | 76 |
"6 5 12 41\n" |
77 | 77 |
"4 6 13 70\n" |
78 | 78 |
"3 2 14 45\n" |
79 | 79 |
"7 9 15 93\n" |
80 | 80 |
"5 9 16 50\n" |
81 | 81 |
"9 0 17 94\n" |
82 | 82 |
"9 6 18 67\n" |
83 | 83 |
"0 9 19 86\n" |
84 | 84 |
"@attributes\n" |
85 | 85 |
"source 3\n"; |
86 | 86 |
|
87 | 87 |
int test_seq[] = { 2, 28, 19, 27, 33, 25, 13, 41, 10, 26, 1, 9, 4, 34}; |
88 | 88 |
int test_inc[] = {20, 28, 34, 16, 0, 46, 44, 0, 42, 32, 14, 8, 6, 37}; |
89 | 89 |
|
90 | 90 |
int test_len = sizeof(test_seq) / sizeof(test_seq[0]); |
91 | 91 |
|
92 | 92 |
template <typename Heap> |
93 | 93 |
void heapSortTest() { |
94 | 94 |
RangeMap<int> map(test_len, -1); |
95 | 95 |
Heap heap(map); |
96 | 96 |
|
97 | 97 |
std::vector<int> v(test_len); |
98 | 98 |
for (int i = 0; i < test_len; ++i) { |
99 | 99 |
v[i] = test_seq[i]; |
100 | 100 |
heap.push(i, v[i]); |
101 | 101 |
} |
102 | 102 |
std::sort(v.begin(), v.end()); |
103 | 103 |
for (int i = 0; i < test_len; ++i) { |
104 | 104 |
check(v[i] == heap.prio(), "Wrong order in heap sort."); |
105 | 105 |
heap.pop(); |
106 | 106 |
} |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
template <typename Heap> |
110 | 110 |
void heapIncreaseTest() { |
111 | 111 |
RangeMap<int> map(test_len, -1); |
112 | 112 |
|
113 | 113 |
Heap heap(map); |
114 | 114 |
|
115 | 115 |
std::vector<int> v(test_len); |
116 | 116 |
for (int i = 0; i < test_len; ++i) { |
117 | 117 |
v[i] = test_seq[i]; |
118 | 118 |
heap.push(i, v[i]); |
119 | 119 |
} |
120 | 120 |
for (int i = 0; i < test_len; ++i) { |
121 | 121 |
v[i] += test_inc[i]; |
122 | 122 |
heap.increase(i, v[i]); |
123 | 123 |
} |
124 | 124 |
std::sort(v.begin(), v.end()); |
125 | 125 |
for (int i = 0; i < test_len; ++i) { |
126 | 126 |
check(v[i] == heap.prio(), "Wrong order in heap increase test."); |
127 | 127 |
heap.pop(); |
128 | 128 |
} |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
template <typename Heap> |
132 | 132 |
void dijkstraHeapTest(const Digraph& digraph, const IntArcMap& length, |
133 | 133 |
Node source) { |
134 | 134 |
|
135 | 135 |
typename Dijkstra<Digraph, IntArcMap>::template SetStandardHeap<Heap>:: |
136 | 136 |
Create dijkstra(digraph, length); |
137 | 137 |
|
138 | 138 |
dijkstra.run(source); |
139 | 139 |
|
140 | 140 |
for(ArcIt a(digraph); a != INVALID; ++a) { |
141 | 141 |
Node s = digraph.source(a); |
142 | 142 |
Node t = digraph.target(a); |
143 | 143 |
if (dijkstra.reached(s)) { |
144 | 144 |
check( dijkstra.dist(t) - dijkstra.dist(s) <= length[a], |
145 | 145 |
"Error in shortest path tree."); |
146 | 146 |
} |
147 | 147 |
} |
148 | 148 |
|
149 | 149 |
for(NodeIt n(digraph); n != INVALID; ++n) { |
150 | 150 |
if ( dijkstra.reached(n) && dijkstra.predArc(n) != INVALID ) { |
151 | 151 |
Arc a = dijkstra.predArc(n); |
152 | 152 |
Node s = digraph.source(a); |
153 | 153 |
check( dijkstra.dist(n) - dijkstra.dist(s) == length[a], |
154 | 154 |
"Error in shortest path tree."); |
155 | 155 |
} |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
int main() { |
161 | 161 |
|
162 | 162 |
typedef int Item; |
163 | 163 |
typedef int Prio; |
164 | 164 |
typedef RangeMap<int> ItemIntMap; |
165 | 165 |
|
166 | 166 |
Digraph digraph; |
167 | 167 |
IntArcMap length(digraph); |
168 | 168 |
Node source; |
169 | 169 |
|
170 | 170 |
std::istringstream input(test_lgf); |
171 | 171 |
digraphReader(digraph, input). |
172 | 172 |
arcMap("capacity", length). |
173 | 173 |
node("source", source). |
174 | 174 |
run(); |
175 | 175 |
|
176 | 176 |
// BinHeap |
177 | 177 |
{ |
178 | 178 |
typedef BinHeap<Prio, ItemIntMap> IntHeap; |
179 | 179 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
180 | 180 |
heapSortTest<IntHeap>(); |
181 | 181 |
heapIncreaseTest<IntHeap>(); |
182 | 182 |
|
183 | 183 |
typedef BinHeap<Prio, IntNodeMap > NodeHeap; |
184 | 184 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
185 | 185 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
186 | 186 |
} |
187 | 187 |
|
188 | 188 |
// QuadHeap |
189 | 189 |
{ |
190 | 190 |
typedef QuadHeap<Prio, ItemIntMap> IntHeap; |
191 | 191 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
192 | 192 |
heapSortTest<IntHeap>(); |
193 | 193 |
heapIncreaseTest<IntHeap>(); |
194 | 194 |
|
195 | 195 |
typedef QuadHeap<Prio, IntNodeMap > NodeHeap; |
196 | 196 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
197 | 197 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
198 | 198 |
} |
199 | 199 |
|
200 | 200 |
// DHeap |
201 | 201 |
{ |
202 | 202 |
typedef DHeap<Prio, ItemIntMap> IntHeap; |
203 | 203 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
204 | 204 |
heapSortTest<IntHeap>(); |
205 | 205 |
heapIncreaseTest<IntHeap>(); |
206 | 206 |
|
207 | 207 |
typedef DHeap<Prio, IntNodeMap > NodeHeap; |
208 | 208 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
209 | 209 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
210 | 210 |
} |
211 | 211 |
|
212 | 212 |
// FibHeap |
213 | 213 |
{ |
214 | 214 |
typedef FibHeap<Prio, ItemIntMap> IntHeap; |
215 | 215 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
216 | 216 |
heapSortTest<IntHeap>(); |
217 | 217 |
heapIncreaseTest<IntHeap>(); |
218 | 218 |
|
219 | 219 |
typedef FibHeap<Prio, IntNodeMap > NodeHeap; |
220 | 220 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
221 | 221 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
222 | 222 |
} |
223 | 223 |
|
224 | 224 |
// PairingHeap |
225 | 225 |
{ |
226 | 226 |
typedef PairingHeap<Prio, ItemIntMap> IntHeap; |
227 | 227 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
228 | 228 |
heapSortTest<IntHeap>(); |
229 | 229 |
heapIncreaseTest<IntHeap>(); |
230 | 230 |
|
231 | 231 |
typedef PairingHeap<Prio, IntNodeMap > NodeHeap; |
232 | 232 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
233 | 233 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
234 | 234 |
} |
235 | 235 |
|
236 | 236 |
// RadixHeap |
237 | 237 |
{ |
238 | 238 |
typedef RadixHeap<ItemIntMap> IntHeap; |
239 | 239 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
240 | 240 |
heapSortTest<IntHeap>(); |
241 | 241 |
heapIncreaseTest<IntHeap>(); |
242 | 242 |
|
243 | 243 |
typedef RadixHeap<IntNodeMap > NodeHeap; |
244 | 244 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
245 | 245 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
246 | 246 |
} |
247 | 247 |
|
248 | 248 |
// BinomialHeap |
249 | 249 |
{ |
250 | 250 |
typedef BinomialHeap<Prio, ItemIntMap> IntHeap; |
251 | 251 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
252 | 252 |
heapSortTest<IntHeap>(); |
253 | 253 |
heapIncreaseTest<IntHeap>(); |
254 | 254 |
|
255 | 255 |
typedef BinomialHeap<Prio, IntNodeMap > NodeHeap; |
256 | 256 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
257 | 257 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
258 | 258 |
} |
259 | 259 |
|
260 | 260 |
// BucketHeap, SimpleBucketHeap |
261 | 261 |
{ |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2011 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/list_graph.h> |
20 | 20 |
#include <lemon/lgf_reader.h> |
21 | 21 |
#include "test_tools.h" |
22 | 22 |
|
23 | 23 |
using namespace lemon; |
24 | 24 |
|
25 | 25 |
char test_lgf[] = |
26 | 26 |
"@nodes\n" |
27 | 27 |
"label\n" |
28 | 28 |
"0\n" |
29 | 29 |
"1\n" |
30 | 30 |
"@arcs\n" |
31 | 31 |
" label\n" |
32 | 32 |
"0 1 0\n" |
33 | 33 |
"1 0 1\n" |
34 | 34 |
"@attributes\n" |
35 | 35 |
"source 0\n" |
36 | 36 |
"target 1\n"; |
37 | 37 |
|
38 | 38 |
char test_lgf_nomap[] = |
39 | 39 |
"@nodes\n" |
40 | 40 |
"label\n" |
41 | 41 |
"0\n" |
42 | 42 |
"1\n" |
43 | 43 |
"@arcs\n" |
44 | 44 |
" -\n" |
45 | 45 |
"0 1\n"; |
46 | 46 |
|
47 | 47 |
char test_lgf_bad1[] = |
48 | 48 |
"@nodes\n" |
49 | 49 |
"label\n" |
50 | 50 |
"0\n" |
51 | 51 |
"1\n" |
52 | 52 |
"@arcs\n" |
53 | 53 |
" - another\n" |
54 | 54 |
"0 1\n"; |
55 | 55 |
|
56 | 56 |
char test_lgf_bad2[] = |
57 | 57 |
"@nodes\n" |
58 | 58 |
"label\n" |
59 | 59 |
"0\n" |
60 | 60 |
"1\n" |
61 | 61 |
"@arcs\n" |
62 | 62 |
" label -\n" |
63 | 63 |
"0 1\n"; |
64 | 64 |
|
65 | 65 |
|
66 |
int main() |
|
66 |
int main() |
|
67 | 67 |
{ |
68 | 68 |
{ |
69 |
ListDigraph d; |
|
69 |
ListDigraph d; |
|
70 | 70 |
ListDigraph::Node s,t; |
71 | 71 |
ListDigraph::ArcMap<int> label(d); |
72 | 72 |
std::istringstream input(test_lgf); |
73 | 73 |
digraphReader(d, input). |
74 | 74 |
node("source", s). |
75 | 75 |
node("target", t). |
76 | 76 |
arcMap("label", label). |
77 | 77 |
run(); |
78 | 78 |
check(countNodes(d) == 2,"There should be 2 nodes"); |
79 | 79 |
check(countArcs(d) == 2,"There should be 2 arcs"); |
80 | 80 |
} |
81 | 81 |
{ |
82 | 82 |
ListGraph g; |
83 | 83 |
ListGraph::Node s,t; |
84 | 84 |
ListGraph::EdgeMap<int> label(g); |
85 | 85 |
std::istringstream input(test_lgf); |
86 | 86 |
graphReader(g, input). |
87 | 87 |
node("source", s). |
88 | 88 |
node("target", t). |
89 | 89 |
edgeMap("label", label). |
90 | 90 |
run(); |
91 | 91 |
check(countNodes(g) == 2,"There should be 2 nodes"); |
92 | 92 |
check(countEdges(g) == 2,"There should be 2 arcs"); |
93 | 93 |
} |
94 | 94 |
|
95 | 95 |
{ |
96 |
ListDigraph d; |
|
96 |
ListDigraph d; |
|
97 | 97 |
std::istringstream input(test_lgf_nomap); |
98 | 98 |
digraphReader(d, input). |
99 | 99 |
run(); |
100 | 100 |
check(countNodes(d) == 2,"There should be 2 nodes"); |
101 | 101 |
check(countArcs(d) == 1,"There should be 1 arc"); |
102 | 102 |
} |
103 | 103 |
{ |
104 | 104 |
ListGraph g; |
105 | 105 |
std::istringstream input(test_lgf_nomap); |
106 | 106 |
graphReader(g, input). |
107 | 107 |
run(); |
108 | 108 |
check(countNodes(g) == 2,"There should be 2 nodes"); |
109 | 109 |
check(countEdges(g) == 1,"There should be 1 edge"); |
110 | 110 |
} |
111 | 111 |
|
112 | 112 |
{ |
113 |
ListDigraph d; |
|
113 |
ListDigraph d; |
|
114 | 114 |
std::istringstream input(test_lgf_bad1); |
115 | 115 |
bool ok=false; |
116 | 116 |
try { |
117 | 117 |
digraphReader(d, input). |
118 | 118 |
run(); |
119 | 119 |
} |
120 |
catch (FormatError& error) |
|
120 |
catch (FormatError& error) |
|
121 | 121 |
{ |
122 | 122 |
ok = true; |
123 | 123 |
} |
124 | 124 |
check(ok,"FormatError exception should have occured"); |
125 | 125 |
} |
126 | 126 |
{ |
127 | 127 |
ListGraph g; |
128 | 128 |
std::istringstream input(test_lgf_bad1); |
129 | 129 |
bool ok=false; |
130 | 130 |
try { |
131 | 131 |
graphReader(g, input). |
132 | 132 |
run(); |
133 | 133 |
} |
134 | 134 |
catch (FormatError& error) |
135 | 135 |
{ |
136 | 136 |
ok = true; |
137 | 137 |
} |
138 | 138 |
check(ok,"FormatError exception should have occured"); |
139 | 139 |
} |
140 | 140 |
|
141 | 141 |
{ |
142 |
ListDigraph d; |
|
142 |
ListDigraph d; |
|
143 | 143 |
std::istringstream input(test_lgf_bad2); |
144 | 144 |
bool ok=false; |
145 | 145 |
try { |
146 | 146 |
digraphReader(d, input). |
147 | 147 |
run(); |
148 | 148 |
} |
149 | 149 |
catch (FormatError& error) |
150 | 150 |
{ |
151 | 151 |
ok = true; |
152 | 152 |
} |
153 | 153 |
check(ok,"FormatError exception should have occured"); |
154 | 154 |
} |
155 | 155 |
{ |
156 | 156 |
ListGraph g; |
157 | 157 |
std::istringstream input(test_lgf_bad2); |
158 | 158 |
bool ok=false; |
159 | 159 |
try { |
160 | 160 |
graphReader(g, input). |
161 | 161 |
run(); |
162 | 162 |
} |
163 | 163 |
catch (FormatError& error) |
164 | 164 |
{ |
165 | 165 |
ok = true; |
166 | 166 |
} |
167 | 167 |
check(ok,"FormatError exception should have occured"); |
168 | 168 |
} |
169 | 169 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <deque> |
20 | 20 |
#include <set> |
21 | 21 |
|
22 | 22 |
#include <lemon/concept_check.h> |
23 | 23 |
#include <lemon/concepts/maps.h> |
24 | 24 |
#include <lemon/maps.h> |
25 | 25 |
#include <lemon/list_graph.h> |
26 | 26 |
#include <lemon/smart_graph.h> |
27 | 27 |
#include <lemon/adaptors.h> |
28 | 28 |
#include <lemon/dfs.h> |
29 | 29 |
#include <algorithm> |
30 | 30 |
|
31 | 31 |
#include "test_tools.h" |
32 | 32 |
|
33 | 33 |
using namespace lemon; |
34 | 34 |
using namespace lemon::concepts; |
35 | 35 |
|
36 | 36 |
struct A {}; |
37 | 37 |
inline bool operator<(A, A) { return true; } |
38 | 38 |
struct B {}; |
39 | 39 |
|
40 | 40 |
class C { |
41 | 41 |
int _x; |
42 | 42 |
public: |
43 | 43 |
C(int x) : _x(x) {} |
44 | 44 |
int get() const { return _x; } |
45 | 45 |
}; |
46 | 46 |
inline bool operator<(C c1, C c2) { return c1.get() < c2.get(); } |
47 | 47 |
inline bool operator==(C c1, C c2) { return c1.get() == c2.get(); } |
48 | 48 |
|
49 | 49 |
C createC(int x) { return C(x); } |
50 | 50 |
|
51 | 51 |
template <typename T> |
52 | 52 |
class Less { |
53 | 53 |
T _t; |
54 | 54 |
public: |
55 | 55 |
Less(T t): _t(t) {} |
56 | 56 |
bool operator()(const T& t) const { return t < _t; } |
57 | 57 |
}; |
58 | 58 |
|
59 | 59 |
class F { |
60 | 60 |
public: |
61 | 61 |
typedef A argument_type; |
62 | 62 |
typedef B result_type; |
63 | 63 |
|
64 | 64 |
B operator()(const A&) const { return B(); } |
65 | 65 |
private: |
66 | 66 |
F& operator=(const F&); |
67 | 67 |
}; |
68 | 68 |
|
69 | 69 |
int func(A) { return 3; } |
70 | 70 |
|
71 | 71 |
int binc(int a, B) { return a+1; } |
72 | 72 |
|
73 | 73 |
template <typename T> |
74 | 74 |
class Sum { |
75 | 75 |
T& _sum; |
76 | 76 |
public: |
77 | 77 |
Sum(T& sum) : _sum(sum) {} |
78 | 78 |
void operator()(const T& t) { _sum += t; } |
79 | 79 |
}; |
80 | 80 |
|
81 | 81 |
typedef ReadMap<A, double> DoubleMap; |
82 | 82 |
typedef ReadWriteMap<A, double> DoubleWriteMap; |
83 | 83 |
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap; |
84 | 84 |
|
85 | 85 |
typedef ReadMap<A, bool> BoolMap; |
86 | 86 |
typedef ReadWriteMap<A, bool> BoolWriteMap; |
87 | 87 |
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap; |
88 | 88 |
|
89 | 89 |
int main() |
90 | 90 |
{ |
91 | 91 |
// Map concepts |
92 | 92 |
checkConcept<ReadMap<A,B>, ReadMap<A,B> >(); |
93 | 93 |
checkConcept<ReadMap<A,C>, ReadMap<A,C> >(); |
94 | 94 |
checkConcept<WriteMap<A,B>, WriteMap<A,B> >(); |
95 | 95 |
checkConcept<WriteMap<A,C>, WriteMap<A,C> >(); |
96 | 96 |
checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >(); |
97 | 97 |
checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >(); |
98 | 98 |
checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >(); |
99 | 99 |
checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >(); |
100 | 100 |
|
101 | 101 |
// NullMap |
102 | 102 |
{ |
103 | 103 |
checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >(); |
104 | 104 |
NullMap<A,B> map1; |
105 | 105 |
NullMap<A,B> map2 = map1; |
106 | 106 |
map1 = nullMap<A,B>(); |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
// ConstMap |
110 | 110 |
{ |
111 | 111 |
checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >(); |
112 | 112 |
checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >(); |
113 | 113 |
ConstMap<A,B> map1; |
114 | 114 |
ConstMap<A,B> map2 = B(); |
115 | 115 |
ConstMap<A,B> map3 = map1; |
116 | 116 |
map1 = constMap<A>(B()); |
117 | 117 |
map1 = constMap<A,B>(); |
118 | 118 |
map1.setAll(B()); |
119 | 119 |
ConstMap<A,C> map4(C(1)); |
120 | 120 |
ConstMap<A,C> map5 = map4; |
121 | 121 |
map4 = constMap<A>(C(2)); |
122 | 122 |
map4.setAll(C(3)); |
123 | 123 |
|
124 | 124 |
checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >(); |
125 | 125 |
check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap"); |
126 | 126 |
|
127 | 127 |
checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >(); |
128 | 128 |
ConstMap<A,Const<int,10> > map6; |
129 | 129 |
ConstMap<A,Const<int,10> > map7 = map6; |
130 | 130 |
map6 = constMap<A,int,10>(); |
131 | 131 |
map7 = constMap<A,Const<int,10> >(); |
132 | 132 |
check(map6[A()] == 10 && map7[A()] == 10, |
133 | 133 |
"Something is wrong with ConstMap"); |
134 | 134 |
} |
135 | 135 |
|
136 | 136 |
// IdentityMap |
137 | 137 |
{ |
138 | 138 |
checkConcept<ReadMap<A,A>, IdentityMap<A> >(); |
139 | 139 |
IdentityMap<A> map1; |
140 | 140 |
IdentityMap<A> map2 = map1; |
141 | 141 |
map1 = identityMap<A>(); |
142 | 142 |
|
143 | 143 |
checkConcept<ReadMap<double,double>, IdentityMap<double> >(); |
144 | 144 |
check(identityMap<double>()[1.0] == 1.0 && |
145 | 145 |
identityMap<double>()[3.14] == 3.14, |
146 | 146 |
"Something is wrong with IdentityMap"); |
147 | 147 |
} |
148 | 148 |
|
149 | 149 |
// RangeMap |
150 | 150 |
{ |
151 | 151 |
checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >(); |
152 | 152 |
RangeMap<B> map1; |
153 | 153 |
RangeMap<B> map2(10); |
154 | 154 |
RangeMap<B> map3(10,B()); |
155 | 155 |
RangeMap<B> map4 = map1; |
156 | 156 |
RangeMap<B> map5 = rangeMap<B>(); |
157 | 157 |
RangeMap<B> map6 = rangeMap<B>(10); |
158 | 158 |
RangeMap<B> map7 = rangeMap(10,B()); |
159 | 159 |
|
160 | 160 |
checkConcept< ReferenceMap<int, double, double&, const double&>, |
161 | 161 |
RangeMap<double> >(); |
162 | 162 |
std::vector<double> v(10, 0); |
163 | 163 |
v[5] = 100; |
164 | 164 |
RangeMap<double> map8(v); |
165 | 165 |
RangeMap<double> map9 = rangeMap(v); |
166 | 166 |
check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100, |
167 | 167 |
"Something is wrong with RangeMap"); |
168 | 168 |
} |
169 | 169 |
|
170 | 170 |
// SparseMap |
171 | 171 |
{ |
172 | 172 |
checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >(); |
173 | 173 |
SparseMap<A,B> map1; |
174 | 174 |
SparseMap<A,B> map2 = B(); |
175 | 175 |
SparseMap<A,B> map3 = sparseMap<A,B>(); |
176 | 176 |
SparseMap<A,B> map4 = sparseMap<A>(B()); |
177 | 177 |
|
178 | 178 |
checkConcept< ReferenceMap<double, int, int&, const int&>, |
179 | 179 |
SparseMap<double, int> >(); |
180 | 180 |
std::map<double, int> m; |
181 | 181 |
SparseMap<double, int> map5(m); |
182 | 182 |
SparseMap<double, int> map6(m,10); |
183 | 183 |
SparseMap<double, int> map7 = sparseMap(m); |
184 | 184 |
SparseMap<double, int> map8 = sparseMap(m,10); |
185 | 185 |
|
186 | 186 |
check(map5[1.0] == 0 && map5[3.14] == 0 && |
187 | 187 |
map6[1.0] == 10 && map6[3.14] == 10, |
188 | 188 |
"Something is wrong with SparseMap"); |
189 | 189 |
map5[1.0] = map6[3.14] = 100; |
190 | 190 |
check(map5[1.0] == 100 && map5[3.14] == 0 && |
191 | 191 |
map6[1.0] == 10 && map6[3.14] == 100, |
192 | 192 |
"Something is wrong with SparseMap"); |
193 | 193 |
} |
194 | 194 |
|
195 | 195 |
// ComposeMap |
196 | 196 |
{ |
197 | 197 |
typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap; |
198 | 198 |
checkConcept<ReadMap<B,double>, CompMap>(); |
199 | 199 |
CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>()); |
200 | 200 |
CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>()); |
201 | 201 |
|
202 | 202 |
SparseMap<double, bool> m1(false); m1[3.14] = true; |
203 | 203 |
RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14; |
204 | 204 |
check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], |
205 | 205 |
"Something is wrong with ComposeMap") |
206 | 206 |
} |
207 | 207 |
|
208 | 208 |
// CombineMap |
209 | 209 |
{ |
210 | 210 |
typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap; |
211 | 211 |
checkConcept<ReadMap<A,double>, CombMap>(); |
212 | 212 |
CombMap map1 = CombMap(DoubleMap(), DoubleMap()); |
213 | 213 |
CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>()); |
214 | 214 |
|
215 | 215 |
check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3, |
216 | 216 |
"Something is wrong with CombineMap"); |
217 | 217 |
} |
218 | 218 |
|
219 | 219 |
// FunctorToMap, MapToFunctor |
220 | 220 |
{ |
221 | 221 |
checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >(); |
222 | 222 |
checkConcept<ReadMap<A,B>, FunctorToMap<F> >(); |
223 | 223 |
FunctorToMap<F> map1; |
224 | 224 |
FunctorToMap<F> map2 = FunctorToMap<F>(F()); |
225 | 225 |
B b = functorToMap(F())[A()]; |
226 | 226 |
|
227 | 227 |
checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >(); |
228 | 228 |
MapToFunctor<ReadMap<A,B> > map = |
229 | 229 |
MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>()); |
230 | 230 |
|
231 | 231 |
check(functorToMap(&func)[A()] == 3, |
232 | 232 |
"Something is wrong with FunctorToMap"); |
233 | 233 |
check(mapToFunctor(constMap<A,int>(2))(A()) == 2, |
234 | 234 |
"Something is wrong with MapToFunctor"); |
235 | 235 |
check(mapToFunctor(functorToMap(&func))(A()) == 3 && |
236 | 236 |
mapToFunctor(functorToMap(&func))[A()] == 3, |
237 | 237 |
"Something is wrong with FunctorToMap or MapToFunctor"); |
238 | 238 |
check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2, |
239 | 239 |
"Something is wrong with FunctorToMap or MapToFunctor"); |
240 | 240 |
} |
241 | 241 |
|
242 | 242 |
// ConvertMap |
243 | 243 |
{ |
244 | 244 |
checkConcept<ReadMap<double,double>, |
245 | 245 |
ConvertMap<ReadMap<double, int>, double> >(); |
246 | 246 |
ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true)); |
247 | 247 |
ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false)); |
248 | 248 |
} |
249 | 249 |
|
250 | 250 |
// ForkMap |
251 | 251 |
{ |
252 | 252 |
checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >(); |
253 | 253 |
|
254 | 254 |
typedef RangeMap<double> RM; |
255 | 255 |
typedef SparseMap<int, double> SM; |
256 | 256 |
RM m1(10, -1); |
257 | 257 |
SM m2(-1); |
258 | 258 |
checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >(); |
259 | 259 |
checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >(); |
260 | 260 |
ForkMap<RM, SM> map1(m1,m2); |
261 | 261 |
ForkMap<SM, RM> map2 = forkMap(m2,m1); |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <iostream> |
20 | 20 |
|
21 | 21 |
#include "test_tools.h" |
22 | 22 |
#include <lemon/smart_graph.h> |
23 | 23 |
#include <lemon/preflow.h> |
24 | 24 |
#include <lemon/concepts/digraph.h> |
25 | 25 |
#include <lemon/concepts/maps.h> |
26 | 26 |
#include <lemon/lgf_reader.h> |
27 | 27 |
#include <lemon/elevator.h> |
28 | 28 |
|
29 | 29 |
using namespace lemon; |
30 | 30 |
|
31 | 31 |
char test_lgf[] = |
32 | 32 |
"@nodes\n" |
33 | 33 |
"label\n" |
34 | 34 |
"0\n" |
35 | 35 |
"1\n" |
36 | 36 |
"2\n" |
37 | 37 |
"3\n" |
38 | 38 |
"4\n" |
39 | 39 |
"5\n" |
40 | 40 |
"6\n" |
41 | 41 |
"7\n" |
42 | 42 |
"8\n" |
43 | 43 |
"9\n" |
44 | 44 |
"@arcs\n" |
45 | 45 |
" label capacity\n" |
46 | 46 |
"0 1 0 20\n" |
47 | 47 |
"0 2 1 0\n" |
48 | 48 |
"1 1 2 3\n" |
49 | 49 |
"1 2 3 8\n" |
50 | 50 |
"1 3 4 8\n" |
51 | 51 |
"2 5 5 5\n" |
52 | 52 |
"3 2 6 5\n" |
53 | 53 |
"3 5 7 5\n" |
54 | 54 |
"3 6 8 5\n" |
55 | 55 |
"4 3 9 3\n" |
56 | 56 |
"5 7 10 3\n" |
57 | 57 |
"5 6 11 10\n" |
58 | 58 |
"5 8 12 10\n" |
59 | 59 |
"6 8 13 8\n" |
60 | 60 |
"8 9 14 20\n" |
61 | 61 |
"8 1 15 5\n" |
62 | 62 |
"9 5 16 5\n" |
63 | 63 |
"@attributes\n" |
64 | 64 |
"source 1\n" |
65 | 65 |
"target 8\n"; |
66 | 66 |
|
67 | 67 |
void checkPreflowCompile() |
68 | 68 |
{ |
69 | 69 |
typedef int VType; |
70 | 70 |
typedef concepts::Digraph Digraph; |
71 | 71 |
|
72 | 72 |
typedef Digraph::Node Node; |
73 | 73 |
typedef Digraph::Arc Arc; |
74 | 74 |
typedef concepts::ReadMap<Arc,VType> CapMap; |
75 | 75 |
typedef concepts::ReadWriteMap<Arc,VType> FlowMap; |
76 | 76 |
typedef concepts::WriteMap<Node,bool> CutMap; |
77 | 77 |
|
78 | 78 |
typedef Elevator<Digraph, Digraph::Node> Elev; |
79 | 79 |
typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev; |
80 | 80 |
|
81 | 81 |
Digraph g; |
82 | 82 |
Node n; |
83 | 83 |
Arc e; |
84 | 84 |
CapMap cap; |
85 | 85 |
FlowMap flow; |
86 | 86 |
CutMap cut; |
87 | 87 |
VType v; |
88 | 88 |
bool b; |
89 | 89 |
|
90 | 90 |
typedef Preflow<Digraph, CapMap> |
91 | 91 |
::SetFlowMap<FlowMap> |
92 | 92 |
::SetElevator<Elev> |
93 | 93 |
::SetStandardElevator<LinkedElev> |
94 | 94 |
::Create PreflowType; |
95 | 95 |
PreflowType preflow_test(g, cap, n, n); |
96 | 96 |
const PreflowType& const_preflow_test = preflow_test; |
97 | 97 |
|
98 | 98 |
const PreflowType::Elevator& elev = const_preflow_test.elevator(); |
99 | 99 |
preflow_test.elevator(const_cast<PreflowType::Elevator&>(elev)); |
100 | 100 |
PreflowType::Tolerance tol = const_preflow_test.tolerance(); |
101 | 101 |
preflow_test.tolerance(tol); |
102 | 102 |
|
103 | 103 |
preflow_test |
104 | 104 |
.capacityMap(cap) |
105 | 105 |
.flowMap(flow) |
106 | 106 |
.source(n) |
107 | 107 |
.target(n); |
108 | 108 |
|
109 | 109 |
preflow_test.init(); |
110 | 110 |
preflow_test.init(cap); |
111 | 111 |
preflow_test.startFirstPhase(); |
112 | 112 |
preflow_test.startSecondPhase(); |
113 | 113 |
preflow_test.run(); |
114 | 114 |
preflow_test.runMinCut(); |
115 | 115 |
|
116 | 116 |
v = const_preflow_test.flowValue(); |
117 | 117 |
v = const_preflow_test.flow(e); |
118 | 118 |
const FlowMap& fm = const_preflow_test.flowMap(); |
119 | 119 |
b = const_preflow_test.minCut(n); |
120 | 120 |
const_preflow_test.minCutMap(cut); |
121 | 121 |
|
122 | 122 |
ignore_unused_variable_warning(fm); |
123 | 123 |
} |
124 | 124 |
|
125 | 125 |
int cutValue (const SmartDigraph& g, |
126 | 126 |
const SmartDigraph::NodeMap<bool>& cut, |
127 | 127 |
const SmartDigraph::ArcMap<int>& cap) { |
128 | 128 |
|
129 | 129 |
int c=0; |
130 | 130 |
for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) { |
131 | 131 |
if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e]; |
132 | 132 |
} |
133 | 133 |
return c; |
134 | 134 |
} |
135 | 135 |
|
136 | 136 |
bool checkFlow(const SmartDigraph& g, |
137 | 137 |
const SmartDigraph::ArcMap<int>& flow, |
138 | 138 |
const SmartDigraph::ArcMap<int>& cap, |
139 | 139 |
SmartDigraph::Node s, SmartDigraph::Node t) { |
140 | 140 |
|
141 | 141 |
for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) { |
142 | 142 |
if (flow[e] < 0 || flow[e] > cap[e]) return false; |
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) { |
146 | 146 |
if (n == s || n == t) continue; |
147 | 147 |
int sum = 0; |
148 | 148 |
for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) { |
149 | 149 |
sum += flow[e]; |
150 | 150 |
} |
151 | 151 |
for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) { |
152 | 152 |
sum -= flow[e]; |
153 | 153 |
} |
154 | 154 |
if (sum != 0) return false; |
155 | 155 |
} |
156 | 156 |
return true; |
157 | 157 |
} |
158 | 158 |
|
159 | 159 |
void initFlowTest() |
160 | 160 |
{ |
161 | 161 |
DIGRAPH_TYPEDEFS(SmartDigraph); |
162 |
|
|
162 |
|
|
163 | 163 |
SmartDigraph g; |
164 | 164 |
SmartDigraph::ArcMap<int> cap(g),iflow(g); |
165 | 165 |
Node s=g.addNode(); Node t=g.addNode(); |
166 | 166 |
Node n1=g.addNode(); Node n2=g.addNode(); |
167 | 167 |
Arc a; |
168 | 168 |
a=g.addArc(s,n1); cap[a]=20; iflow[a]=20; |
169 | 169 |
a=g.addArc(n1,n2); cap[a]=10; iflow[a]=0; |
170 | 170 |
a=g.addArc(n2,t); cap[a]=20; iflow[a]=0; |
171 | 171 |
|
172 | 172 |
Preflow<SmartDigraph> pre(g,cap,s,t); |
173 | 173 |
pre.init(iflow); |
174 | 174 |
pre.startFirstPhase(); |
175 | 175 |
check(pre.flowValue() == 10, "The incorrect max flow value."); |
176 | 176 |
check(pre.minCut(s), "Wrong min cut (Node s)."); |
177 | 177 |
check(pre.minCut(n1), "Wrong min cut (Node n1)."); |
178 | 178 |
check(!pre.minCut(n2), "Wrong min cut (Node n2)."); |
179 | 179 |
check(!pre.minCut(t), "Wrong min cut (Node t)."); |
180 | 180 |
} |
181 | 181 |
|
182 | 182 |
|
183 | 183 |
int main() { |
184 | 184 |
|
185 | 185 |
typedef SmartDigraph Digraph; |
186 | 186 |
|
187 | 187 |
typedef Digraph::Node Node; |
188 | 188 |
typedef Digraph::NodeIt NodeIt; |
189 | 189 |
typedef Digraph::ArcIt ArcIt; |
190 | 190 |
typedef Digraph::ArcMap<int> CapMap; |
191 | 191 |
typedef Digraph::ArcMap<int> FlowMap; |
192 | 192 |
typedef Digraph::NodeMap<bool> CutMap; |
193 | 193 |
|
194 | 194 |
typedef Preflow<Digraph, CapMap> PType; |
195 | 195 |
|
196 | 196 |
Digraph g; |
197 | 197 |
Node s, t; |
198 | 198 |
CapMap cap(g); |
199 | 199 |
std::istringstream input(test_lgf); |
200 | 200 |
DigraphReader<Digraph>(g,input). |
201 | 201 |
arcMap("capacity", cap). |
202 | 202 |
node("source",s). |
203 | 203 |
node("target",t). |
204 | 204 |
run(); |
205 | 205 |
|
206 | 206 |
PType preflow_test(g, cap, s, t); |
207 | 207 |
preflow_test.run(); |
208 | 208 |
|
209 | 209 |
check(checkFlow(g, preflow_test.flowMap(), cap, s, t), |
210 | 210 |
"The flow is not feasible."); |
211 | 211 |
|
212 | 212 |
CutMap min_cut(g); |
213 | 213 |
preflow_test.minCutMap(min_cut); |
214 | 214 |
int min_cut_value=cutValue(g,min_cut,cap); |
215 | 215 |
|
216 | 216 |
check(preflow_test.flowValue() == min_cut_value, |
217 | 217 |
"The max flow value is not equal to the three min cut values."); |
218 | 218 |
|
219 | 219 |
FlowMap flow(g); |
220 | 220 |
for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e]; |
221 | 221 |
|
222 | 222 |
int flow_value=preflow_test.flowValue(); |
223 | 223 |
|
224 | 224 |
for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e]; |
225 | 225 |
preflow_test.init(flow); |
226 | 226 |
preflow_test.startFirstPhase(); |
227 | 227 |
|
228 | 228 |
CutMap min_cut1(g); |
229 | 229 |
preflow_test.minCutMap(min_cut1); |
230 | 230 |
min_cut_value=cutValue(g,min_cut1,cap); |
231 | 231 |
|
232 | 232 |
check(preflow_test.flowValue() == min_cut_value && |
233 | 233 |
min_cut_value == 2*flow_value, |
234 | 234 |
"The max flow value or the min cut value is wrong."); |
235 | 235 |
|
236 | 236 |
preflow_test.startSecondPhase(); |
237 | 237 |
|
238 | 238 |
check(checkFlow(g, preflow_test.flowMap(), cap, s, t), |
239 | 239 |
"The flow is not feasible."); |
240 | 240 |
|
241 | 241 |
CutMap min_cut2(g); |
242 | 242 |
preflow_test.minCutMap(min_cut2); |
243 | 243 |
min_cut_value=cutValue(g,min_cut2,cap); |
244 | 244 |
|
245 | 245 |
check(preflow_test.flowValue() == min_cut_value && |
246 | 246 |
min_cut_value == 2*flow_value, |
247 | 247 |
"The max flow value or the three min cut values were not doubled"); |
248 | 248 |
|
249 | 249 |
|
250 | 250 |
preflow_test.flowMap(flow); |
251 | 251 |
|
252 | 252 |
NodeIt tmp1(g,s); |
253 | 253 |
++tmp1; |
254 | 254 |
if ( tmp1 != INVALID ) s=tmp1; |
255 | 255 |
|
256 | 256 |
NodeIt tmp2(g,t); |
257 | 257 |
++tmp2; |
258 | 258 |
if ( tmp2 != INVALID ) t=tmp2; |
259 | 259 |
|
260 | 260 |
preflow_test.source(s); |
261 | 261 |
preflow_test.target(t); |
262 | 262 |
|
263 | 263 |
preflow_test.run(); |
264 | 264 |
|
265 | 265 |
CutMap min_cut3(g); |
266 | 266 |
preflow_test.minCutMap(min_cut3); |
267 | 267 |
min_cut_value=cutValue(g,min_cut3,cap); |
268 | 268 |
|
269 | 269 |
|
270 | 270 |
check(preflow_test.flowValue() == min_cut_value, |
271 | 271 |
"The max flow value or the three min cut values are incorrect."); |
272 | 272 |
|
273 | 273 |
initFlowTest(); |
274 |
|
|
274 |
|
|
275 | 275 |
return 0; |
276 | 276 |
} |
0 comments (0 inline)