... |
... |
@@ -11,18 +11,18 @@
|
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 |
|
#ifndef LEMON_TOPOLOGY_H
|
20 |
|
#define LEMON_TOPOLOGY_H
|
|
19 |
#ifndef LEMON_CONNECTIVITY_H
|
|
20 |
#define LEMON_CONNECTIVITY_H
|
21 |
21 |
|
22 |
22 |
#include <lemon/dfs.h>
|
23 |
23 |
#include <lemon/bfs.h>
|
24 |
24 |
#include <lemon/core.h>
|
25 |
25 |
#include <lemon/maps.h>
|
26 |
26 |
#include <lemon/adaptors.h>
|
27 |
27 |
|
28 |
28 |
#include <lemon/concepts/digraph.h>
|
... |
... |
@@ -149,17 +149,17 @@
|
149 |
149 |
bfs.processNextNode();
|
150 |
150 |
}
|
151 |
151 |
++compNum;
|
152 |
152 |
}
|
153 |
153 |
}
|
154 |
154 |
return compNum;
|
155 |
155 |
}
|
156 |
156 |
|
157 |
|
namespace _topology_bits {
|
|
157 |
namespace _connectivity_bits {
|
158 |
158 |
|
159 |
159 |
template <typename Digraph, typename Iterator >
|
160 |
160 |
struct LeaveOrderVisitor : public DfsVisitor<Digraph> {
|
161 |
161 |
public:
|
162 |
162 |
typedef typename Digraph::Node Node;
|
163 |
163 |
LeaveOrderVisitor(Iterator it) : _it(it) {}
|
164 |
164 |
|
165 |
165 |
void leave(const Node& node) {
|
... |
... |
@@ -183,29 +183,29 @@
|
183 |
183 |
_map.set(node, _value);
|
184 |
184 |
}
|
185 |
185 |
private:
|
186 |
186 |
Map& _map;
|
187 |
187 |
Value& _value;
|
188 |
188 |
};
|
189 |
189 |
|
190 |
190 |
template <typename Digraph, typename ArcMap>
|
191 |
|
struct StronglyConnectedCutEdgesVisitor : public DfsVisitor<Digraph> {
|
|
191 |
struct StronglyConnectedCutArcsVisitor : public DfsVisitor<Digraph> {
|
192 |
192 |
public:
|
193 |
193 |
typedef typename Digraph::Node Node;
|
194 |
194 |
typedef typename Digraph::Arc Arc;
|
195 |
195 |
|
196 |
|
StronglyConnectedCutEdgesVisitor(const Digraph& digraph,
|
197 |
|
ArcMap& cutMap,
|
198 |
|
int& cutNum)
|
|
196 |
StronglyConnectedCutArcsVisitor(const Digraph& digraph,
|
|
197 |
ArcMap& cutMap,
|
|
198 |
int& cutNum)
|
199 |
199 |
: _digraph(digraph), _cutMap(cutMap), _cutNum(cutNum),
|
200 |
|
_compMap(digraph), _num(0) {
|
|
200 |
_compMap(digraph, -1), _num(-1) {
|
201 |
201 |
}
|
202 |
202 |
|
203 |
|
void stop(const Node&) {
|
|
203 |
void start(const Node&) {
|
204 |
204 |
++_num;
|
205 |
205 |
}
|
206 |
206 |
|
207 |
207 |
void reach(const Node& node) {
|
208 |
208 |
_compMap.set(node, _num);
|
209 |
209 |
}
|
210 |
210 |
|
211 |
211 |
void examine(const Arc& arc) {
|
... |
... |
@@ -243,44 +243,45 @@
|
243 |
243 |
checkConcept<concepts::Digraph, Digraph>();
|
244 |
244 |
|
245 |
245 |
typedef typename Digraph::Node Node;
|
246 |
246 |
typedef typename Digraph::NodeIt NodeIt;
|
247 |
247 |
|
248 |
248 |
typename Digraph::Node source = NodeIt(digraph);
|
249 |
249 |
if (source == INVALID) return true;
|
250 |
250 |
|
251 |
|
using namespace _topology_bits;
|
|
251 |
using namespace _connectivity_bits;
|
252 |
252 |
|
253 |
253 |
typedef DfsVisitor<Digraph> Visitor;
|
254 |
254 |
Visitor visitor;
|
255 |
255 |
|
256 |
256 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor);
|
257 |
257 |
dfs.init();
|
258 |
258 |
dfs.addSource(source);
|
259 |
259 |
dfs.start();
|
260 |
260 |
|
261 |
261 |
for (NodeIt it(digraph); it != INVALID; ++it) {
|
262 |
262 |
if (!dfs.reached(it)) {
|
263 |
263 |
return false;
|
264 |
264 |
}
|
265 |
265 |
}
|
266 |
266 |
|
267 |
267 |
typedef ReverseDigraph<const Digraph> RDigraph;
|
|
268 |
typedef typename RDigraph::NodeIt RNodeIt;
|
268 |
269 |
RDigraph rdigraph(digraph);
|
269 |
270 |
|
270 |
271 |
typedef DfsVisitor<Digraph> RVisitor;
|
271 |
272 |
RVisitor rvisitor;
|
272 |
273 |
|
273 |
274 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor);
|
274 |
275 |
rdfs.init();
|
275 |
276 |
rdfs.addSource(source);
|
276 |
277 |
rdfs.start();
|
277 |
278 |
|
278 |
|
for (NodeIt it(rdigraph); it != INVALID; ++it) {
|
|
279 |
for (RNodeIt it(rdigraph); it != INVALID; ++it) {
|
279 |
280 |
if (!rdfs.reached(it)) {
|
280 |
281 |
return false;
|
281 |
282 |
}
|
282 |
283 |
}
|
283 |
284 |
|
284 |
285 |
return true;
|
285 |
286 |
}
|
286 |
287 |
|
... |
... |
@@ -297,17 +298,17 @@
|
297 |
298 |
/// \param graph The graph.
|
298 |
299 |
/// \return The number of components
|
299 |
300 |
/// \note By definition, the empty graph has zero
|
300 |
301 |
/// strongly connected components.
|
301 |
302 |
template <typename Digraph>
|
302 |
303 |
int countStronglyConnectedComponents(const Digraph& digraph) {
|
303 |
304 |
checkConcept<concepts::Digraph, Digraph>();
|
304 |
305 |
|
305 |
|
using namespace _topology_bits;
|
|
306 |
using namespace _connectivity_bits;
|
306 |
307 |
|
307 |
308 |
typedef typename Digraph::Node Node;
|
308 |
309 |
typedef typename Digraph::Arc Arc;
|
309 |
310 |
typedef typename Digraph::NodeIt NodeIt;
|
310 |
311 |
typedef typename Digraph::ArcIt ArcIt;
|
311 |
312 |
|
312 |
313 |
typedef std::vector<Node> Container;
|
313 |
314 |
typedef typename Container::iterator Iterator;
|
... |
... |
@@ -369,17 +370,17 @@
|
369 |
370 |
///
|
370 |
371 |
template <typename Digraph, typename NodeMap>
|
371 |
372 |
int stronglyConnectedComponents(const Digraph& digraph, NodeMap& compMap) {
|
372 |
373 |
checkConcept<concepts::Digraph, Digraph>();
|
373 |
374 |
typedef typename Digraph::Node Node;
|
374 |
375 |
typedef typename Digraph::NodeIt NodeIt;
|
375 |
376 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
|
376 |
377 |
|
377 |
|
using namespace _topology_bits;
|
|
378 |
using namespace _connectivity_bits;
|
378 |
379 |
|
379 |
380 |
typedef std::vector<Node> Container;
|
380 |
381 |
typedef typename Container::iterator Iterator;
|
381 |
382 |
|
382 |
383 |
Container nodes(countNodes(digraph));
|
383 |
384 |
typedef LeaveOrderVisitor<Digraph, Iterator> Visitor;
|
384 |
385 |
Visitor visitor(nodes.begin());
|
385 |
386 |
|
... |
... |
@@ -433,17 +434,17 @@
|
433 |
434 |
template <typename Digraph, typename ArcMap>
|
434 |
435 |
int stronglyConnectedCutArcs(const Digraph& graph, ArcMap& cutMap) {
|
435 |
436 |
checkConcept<concepts::Digraph, Digraph>();
|
436 |
437 |
typedef typename Digraph::Node Node;
|
437 |
438 |
typedef typename Digraph::Arc Arc;
|
438 |
439 |
typedef typename Digraph::NodeIt NodeIt;
|
439 |
440 |
checkConcept<concepts::WriteMap<Arc, bool>, ArcMap>();
|
440 |
441 |
|
441 |
|
using namespace _topology_bits;
|
|
442 |
using namespace _connectivity_bits;
|
442 |
443 |
|
443 |
444 |
typedef std::vector<Node> Container;
|
444 |
445 |
typedef typename Container::iterator Iterator;
|
445 |
446 |
|
446 |
447 |
Container nodes(countNodes(graph));
|
447 |
448 |
typedef LeaveOrderVisitor<Digraph, Iterator> Visitor;
|
448 |
449 |
Visitor visitor(nodes.begin());
|
449 |
450 |
|
... |
... |
@@ -458,32 +459,32 @@
|
458 |
459 |
|
459 |
460 |
typedef typename Container::reverse_iterator RIterator;
|
460 |
461 |
typedef ReverseDigraph<const Digraph> RDigraph;
|
461 |
462 |
|
462 |
463 |
RDigraph rgraph(graph);
|
463 |
464 |
|
464 |
465 |
int cutNum = 0;
|
465 |
466 |
|
466 |
|
typedef StronglyConnectedCutEdgesVisitor<RDigraph, ArcMap> RVisitor;
|
|
467 |
typedef StronglyConnectedCutArcsVisitor<RDigraph, ArcMap> RVisitor;
|
467 |
468 |
RVisitor rvisitor(rgraph, cutMap, cutNum);
|
468 |
469 |
|
469 |
470 |
DfsVisit<RDigraph, RVisitor> rdfs(rgraph, rvisitor);
|
470 |
471 |
|
471 |
472 |
rdfs.init();
|
472 |
473 |
for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
|
473 |
474 |
if (!rdfs.reached(*it)) {
|
474 |
475 |
rdfs.addSource(*it);
|
475 |
476 |
rdfs.start();
|
476 |
477 |
}
|
477 |
478 |
}
|
478 |
479 |
return cutNum;
|
479 |
480 |
}
|
480 |
481 |
|
481 |
|
namespace _topology_bits {
|
|
482 |
namespace _connectivity_bits {
|
482 |
483 |
|
483 |
484 |
template <typename Digraph>
|
484 |
485 |
class CountBiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
|
485 |
486 |
public:
|
486 |
487 |
typedef typename Digraph::Node Node;
|
487 |
488 |
typedef typename Digraph::Arc Arc;
|
488 |
489 |
typedef typename Digraph::Edge Edge;
|
489 |
490 |
|
... |
... |
@@ -725,17 +726,17 @@
|
725 |
726 |
///
|
726 |
727 |
/// \param graph The graph.
|
727 |
728 |
/// \return The number of components.
|
728 |
729 |
template <typename Graph>
|
729 |
730 |
int countBiNodeConnectedComponents(const Graph& graph) {
|
730 |
731 |
checkConcept<concepts::Graph, Graph>();
|
731 |
732 |
typedef typename Graph::NodeIt NodeIt;
|
732 |
733 |
|
733 |
|
using namespace _topology_bits;
|
|
734 |
using namespace _connectivity_bits;
|
734 |
735 |
|
735 |
736 |
typedef CountBiNodeConnectedComponentsVisitor<Graph> Visitor;
|
736 |
737 |
|
737 |
738 |
int compNum = 0;
|
738 |
739 |
Visitor visitor(graph, compNum);
|
739 |
740 |
|
740 |
741 |
DfsVisit<Graph, Visitor> dfs(graph, visitor);
|
741 |
742 |
dfs.init();
|
... |
... |
@@ -768,17 +769,17 @@
|
768 |
769 |
template <typename Graph, typename EdgeMap>
|
769 |
770 |
int biNodeConnectedComponents(const Graph& graph,
|
770 |
771 |
EdgeMap& compMap) {
|
771 |
772 |
checkConcept<concepts::Graph, Graph>();
|
772 |
773 |
typedef typename Graph::NodeIt NodeIt;
|
773 |
774 |
typedef typename Graph::Edge Edge;
|
774 |
775 |
checkConcept<concepts::WriteMap<Edge, int>, EdgeMap>();
|
775 |
776 |
|
776 |
|
using namespace _topology_bits;
|
|
777 |
using namespace _connectivity_bits;
|
777 |
778 |
|
778 |
779 |
typedef BiNodeConnectedComponentsVisitor<Graph, EdgeMap> Visitor;
|
779 |
780 |
|
780 |
781 |
int compNum = 0;
|
781 |
782 |
Visitor visitor(graph, compMap, compNum);
|
782 |
783 |
|
783 |
784 |
DfsVisit<Graph, Visitor> dfs(graph, visitor);
|
784 |
785 |
dfs.init();
|
... |
... |
@@ -808,17 +809,17 @@
|
808 |
809 |
/// \return The number of the cut nodes.
|
809 |
810 |
template <typename Graph, typename NodeMap>
|
810 |
811 |
int biNodeConnectedCutNodes(const Graph& graph, NodeMap& cutMap) {
|
811 |
812 |
checkConcept<concepts::Graph, Graph>();
|
812 |
813 |
typedef typename Graph::Node Node;
|
813 |
814 |
typedef typename Graph::NodeIt NodeIt;
|
814 |
815 |
checkConcept<concepts::WriteMap<Node, bool>, NodeMap>();
|
815 |
816 |
|
816 |
|
using namespace _topology_bits;
|
|
817 |
using namespace _connectivity_bits;
|
817 |
818 |
|
818 |
819 |
typedef BiNodeConnectedCutNodesVisitor<Graph, NodeMap> Visitor;
|
819 |
820 |
|
820 |
821 |
int cutNum = 0;
|
821 |
822 |
Visitor visitor(graph, cutMap, cutNum);
|
822 |
823 |
|
823 |
824 |
DfsVisit<Graph, Visitor> dfs(graph, visitor);
|
824 |
825 |
dfs.init();
|
... |
... |
@@ -827,17 +828,17 @@
|
827 |
828 |
if (!dfs.reached(it)) {
|
828 |
829 |
dfs.addSource(it);
|
829 |
830 |
dfs.start();
|
830 |
831 |
}
|
831 |
832 |
}
|
832 |
833 |
return cutNum;
|
833 |
834 |
}
|
834 |
835 |
|
835 |
|
namespace _topology_bits {
|
|
836 |
namespace _connectivity_bits {
|
836 |
837 |
|
837 |
838 |
template <typename Digraph>
|
838 |
839 |
class CountBiEdgeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
|
839 |
840 |
public:
|
840 |
841 |
typedef typename Digraph::Node Node;
|
841 |
842 |
typedef typename Digraph::Arc Arc;
|
842 |
843 |
typedef typename Digraph::Edge Edge;
|
843 |
844 |
|
... |
... |
@@ -1048,17 +1049,17 @@
|
1048 |
1049 |
///
|
1049 |
1050 |
/// \param graph The undirected graph.
|
1050 |
1051 |
/// \return The number of components.
|
1051 |
1052 |
template <typename Graph>
|
1052 |
1053 |
int countBiEdgeConnectedComponents(const Graph& graph) {
|
1053 |
1054 |
checkConcept<concepts::Graph, Graph>();
|
1054 |
1055 |
typedef typename Graph::NodeIt NodeIt;
|
1055 |
1056 |
|
1056 |
|
using namespace _topology_bits;
|
|
1057 |
using namespace _connectivity_bits;
|
1057 |
1058 |
|
1058 |
1059 |
typedef CountBiEdgeConnectedComponentsVisitor<Graph> Visitor;
|
1059 |
1060 |
|
1060 |
1061 |
int compNum = 0;
|
1061 |
1062 |
Visitor visitor(graph, compNum);
|
1062 |
1063 |
|
1063 |
1064 |
DfsVisit<Graph, Visitor> dfs(graph, visitor);
|
1064 |
1065 |
dfs.init();
|
... |
... |
@@ -1090,17 +1091,17 @@
|
1090 |
1091 |
///
|
1091 |
1092 |
template <typename Graph, typename NodeMap>
|
1092 |
1093 |
int biEdgeConnectedComponents(const Graph& graph, NodeMap& compMap) {
|
1093 |
1094 |
checkConcept<concepts::Graph, Graph>();
|
1094 |
1095 |
typedef typename Graph::NodeIt NodeIt;
|
1095 |
1096 |
typedef typename Graph::Node Node;
|
1096 |
1097 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
|
1097 |
1098 |
|
1098 |
|
using namespace _topology_bits;
|
|
1099 |
using namespace _connectivity_bits;
|
1099 |
1100 |
|
1100 |
1101 |
typedef BiEdgeConnectedComponentsVisitor<Graph, NodeMap> Visitor;
|
1101 |
1102 |
|
1102 |
1103 |
int compNum = 0;
|
1103 |
1104 |
Visitor visitor(graph, compMap, compNum);
|
1104 |
1105 |
|
1105 |
1106 |
DfsVisit<Graph, Visitor> dfs(graph, visitor);
|
1106 |
1107 |
dfs.init();
|
... |
... |
@@ -1131,17 +1132,17 @@
|
1131 |
1132 |
/// \return The number of cut edges.
|
1132 |
1133 |
template <typename Graph, typename EdgeMap>
|
1133 |
1134 |
int biEdgeConnectedCutEdges(const Graph& graph, EdgeMap& cutMap) {
|
1134 |
1135 |
checkConcept<concepts::Graph, Graph>();
|
1135 |
1136 |
typedef typename Graph::NodeIt NodeIt;
|
1136 |
1137 |
typedef typename Graph::Edge Edge;
|
1137 |
1138 |
checkConcept<concepts::WriteMap<Edge, bool>, EdgeMap>();
|
1138 |
1139 |
|
1139 |
|
using namespace _topology_bits;
|
|
1140 |
using namespace _connectivity_bits;
|
1140 |
1141 |
|
1141 |
1142 |
typedef BiEdgeConnectedCutEdgesVisitor<Graph, EdgeMap> Visitor;
|
1142 |
1143 |
|
1143 |
1144 |
int cutNum = 0;
|
1144 |
1145 |
Visitor visitor(graph, cutMap, cutNum);
|
1145 |
1146 |
|
1146 |
1147 |
DfsVisit<Graph, Visitor> dfs(graph, visitor);
|
1147 |
1148 |
dfs.init();
|
... |
... |
@@ -1151,17 +1152,17 @@
|
1151 |
1152 |
dfs.addSource(it);
|
1152 |
1153 |
dfs.start();
|
1153 |
1154 |
}
|
1154 |
1155 |
}
|
1155 |
1156 |
return cutNum;
|
1156 |
1157 |
}
|
1157 |
1158 |
|
1158 |
1159 |
|
1159 |
|
namespace _topology_bits {
|
|
1160 |
namespace _connectivity_bits {
|
1160 |
1161 |
|
1161 |
1162 |
template <typename Digraph, typename IntNodeMap>
|
1162 |
1163 |
class TopologicalSortVisitor : public DfsVisitor<Digraph> {
|
1163 |
1164 |
public:
|
1164 |
1165 |
typedef typename Digraph::Node Node;
|
1165 |
1166 |
typedef typename Digraph::Arc edge;
|
1166 |
1167 |
|
1167 |
1168 |
TopologicalSortVisitor(IntNodeMap& order, int num)
|
... |
... |
@@ -1188,17 +1189,17 @@
|
1188 |
1189 |
/// \retval order A writable node map. The values will be set from 0 to
|
1189 |
1190 |
/// the number of the nodes in the graph minus one. Each values of the map
|
1190 |
1191 |
/// will be set exactly once, the values will be set descending order.
|
1191 |
1192 |
///
|
1192 |
1193 |
/// \see checkedTopologicalSort
|
1193 |
1194 |
/// \see dag
|
1194 |
1195 |
template <typename Digraph, typename NodeMap>
|
1195 |
1196 |
void topologicalSort(const Digraph& graph, NodeMap& order) {
|
1196 |
|
using namespace _topology_bits;
|
|
1197 |
using namespace _connectivity_bits;
|
1197 |
1198 |
|
1198 |
1199 |
checkConcept<concepts::Digraph, Digraph>();
|
1199 |
1200 |
checkConcept<concepts::WriteMap<typename Digraph::Node, int>, NodeMap>();
|
1200 |
1201 |
|
1201 |
1202 |
typedef typename Digraph::Node Node;
|
1202 |
1203 |
typedef typename Digraph::NodeIt NodeIt;
|
1203 |
1204 |
typedef typename Digraph::Arc Arc;
|
1204 |
1205 |
|
... |
... |
@@ -1229,42 +1230,44 @@
|
1229 |
1230 |
/// from 0 to the number of the nodes in the graph minus one. Each values
|
1230 |
1231 |
/// of the map will be set exactly once, the values will be set descending
|
1231 |
1232 |
/// order.
|
1232 |
1233 |
/// \return %False when the graph is not DAG.
|
1233 |
1234 |
///
|
1234 |
1235 |
/// \see topologicalSort
|
1235 |
1236 |
/// \see dag
|
1236 |
1237 |
template <typename Digraph, typename NodeMap>
|
1237 |
|
bool checkedTopologicalSort(const Digraph& graph, NodeMap& order) {
|
1238 |
|
using namespace _topology_bits;
|
|
1238 |
bool checkedTopologicalSort(const Digraph& digraph, NodeMap& order) {
|
|
1239 |
using namespace _connectivity_bits;
|
1239 |
1240 |
|
1240 |
1241 |
checkConcept<concepts::Digraph, Digraph>();
|
1241 |
1242 |
checkConcept<concepts::ReadWriteMap<typename Digraph::Node, int>,
|
1242 |
1243 |
NodeMap>();
|
1243 |
1244 |
|
1244 |
1245 |
typedef typename Digraph::Node Node;
|
1245 |
1246 |
typedef typename Digraph::NodeIt NodeIt;
|
1246 |
1247 |
typedef typename Digraph::Arc Arc;
|
1247 |
1248 |
|
1248 |
|
order = constMap<Node, int, -1>();
|
|
1249 |
for (NodeIt it(digraph); it != INVALID; ++it) {
|
|
1250 |
order.set(it, -1);
|
|
1251 |
}
|
1249 |
1252 |
|
1250 |
1253 |
TopologicalSortVisitor<Digraph, NodeMap>
|
1251 |
|
visitor(order, countNodes(graph));
|
|
1254 |
visitor(order, countNodes(digraph));
|
1252 |
1255 |
|
1253 |
1256 |
DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> >
|
1254 |
|
dfs(graph, visitor);
|
|
1257 |
dfs(digraph, visitor);
|
1255 |
1258 |
|
1256 |
1259 |
dfs.init();
|
1257 |
|
for (NodeIt it(graph); it != INVALID; ++it) {
|
|
1260 |
for (NodeIt it(digraph); it != INVALID; ++it) {
|
1258 |
1261 |
if (!dfs.reached(it)) {
|
1259 |
1262 |
dfs.addSource(it);
|
1260 |
1263 |
while (!dfs.emptyQueue()) {
|
1261 |
|
Arc edge = dfs.nextArc();
|
1262 |
|
Node target = graph.target(edge);
|
|
1264 |
Arc arc = dfs.nextArc();
|
|
1265 |
Node target = digraph.target(arc);
|
1263 |
1266 |
if (dfs.reached(target) && order[target] == -1) {
|
1264 |
1267 |
return false;
|
1265 |
1268 |
}
|
1266 |
1269 |
dfs.processNextArc();
|
1267 |
1270 |
}
|
1268 |
1271 |
}
|
1269 |
1272 |
}
|
1270 |
1273 |
return true;
|
... |
... |
@@ -1274,39 +1277,39 @@
|
1274 |
1277 |
///
|
1275 |
1278 |
/// \brief Check that the given directed graph is a DAG.
|
1276 |
1279 |
///
|
1277 |
1280 |
/// Check that the given directed graph is a DAG. The DAG is
|
1278 |
1281 |
/// an Directed Acyclic Digraph.
|
1279 |
1282 |
/// \return %False when the graph is not DAG.
|
1280 |
1283 |
/// \see acyclic
|
1281 |
1284 |
template <typename Digraph>
|
1282 |
|
bool dag(const Digraph& graph) {
|
|
1285 |
bool dag(const Digraph& digraph) {
|
1283 |
1286 |
|
1284 |
1287 |
checkConcept<concepts::Digraph, Digraph>();
|
1285 |
1288 |
|
1286 |
1289 |
typedef typename Digraph::Node Node;
|
1287 |
1290 |
typedef typename Digraph::NodeIt NodeIt;
|
1288 |
1291 |
typedef typename Digraph::Arc Arc;
|
1289 |
1292 |
|
1290 |
1293 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap;
|
1291 |
1294 |
|
1292 |
1295 |
typename Dfs<Digraph>::template SetProcessedMap<ProcessedMap>::
|
1293 |
|
Create dfs(graph);
|
|
1296 |
Create dfs(digraph);
|
1294 |
1297 |
|
1295 |
|
ProcessedMap processed(graph);
|
|
1298 |
ProcessedMap processed(digraph);
|
1296 |
1299 |
dfs.processedMap(processed);
|
1297 |
1300 |
|
1298 |
1301 |
dfs.init();
|
1299 |
|
for (NodeIt it(graph); it != INVALID; ++it) {
|
|
1302 |
for (NodeIt it(digraph); it != INVALID; ++it) {
|
1300 |
1303 |
if (!dfs.reached(it)) {
|
1301 |
1304 |
dfs.addSource(it);
|
1302 |
1305 |
while (!dfs.emptyQueue()) {
|
1303 |
1306 |
Arc edge = dfs.nextArc();
|
1304 |
|
Node target = graph.target(edge);
|
|
1307 |
Node target = digraph.target(edge);
|
1305 |
1308 |
if (dfs.reached(target) && !processed[target]) {
|
1306 |
1309 |
return false;
|
1307 |
1310 |
}
|
1308 |
1311 |
dfs.processNextArc();
|
1309 |
1312 |
}
|
1310 |
1313 |
}
|
1311 |
1314 |
}
|
1312 |
1315 |
return true;
|
... |
... |
@@ -1375,17 +1378,17 @@
|
1375 |
1378 |
for (NodeIt it(graph); it != INVALID; ++it) {
|
1376 |
1379 |
if (!dfs.reached(it)) {
|
1377 |
1380 |
return false;
|
1378 |
1381 |
}
|
1379 |
1382 |
}
|
1380 |
1383 |
return true;
|
1381 |
1384 |
}
|
1382 |
1385 |
|
1383 |
|
namespace _topology_bits {
|
|
1386 |
namespace _connectivity_bits {
|
1384 |
1387 |
|
1385 |
1388 |
template <typename Digraph>
|
1386 |
1389 |
class BipartiteVisitor : public BfsVisitor<Digraph> {
|
1387 |
1390 |
public:
|
1388 |
1391 |
typedef typename Digraph::Arc Arc;
|
1389 |
1392 |
typedef typename Digraph::Node Node;
|
1390 |
1393 |
|
1391 |
1394 |
BipartiteVisitor(const Digraph& graph, bool& bipartite)
|
... |
... |
@@ -1444,17 +1447,17 @@
|
1444 |
1447 |
///
|
1445 |
1448 |
/// The function checks if the given undirected \c graph graph is bipartite
|
1446 |
1449 |
/// or not. The \ref Bfs algorithm is used to calculate the result.
|
1447 |
1450 |
/// \param graph The undirected graph.
|
1448 |
1451 |
/// \return %True if \c graph is bipartite, %false otherwise.
|
1449 |
1452 |
/// \sa bipartitePartitions
|
1450 |
1453 |
template<typename Graph>
|
1451 |
1454 |
inline bool bipartite(const Graph &graph){
|
1452 |
|
using namespace _topology_bits;
|
|
1455 |
using namespace _connectivity_bits;
|
1453 |
1456 |
|
1454 |
1457 |
checkConcept<concepts::Graph, Graph>();
|
1455 |
1458 |
|
1456 |
1459 |
typedef typename Graph::NodeIt NodeIt;
|
1457 |
1460 |
typedef typename Graph::ArcIt ArcIt;
|
1458 |
1461 |
|
1459 |
1462 |
bool bipartite = true;
|
1460 |
1463 |
|
... |
... |
@@ -1484,17 +1487,17 @@
|
1484 |
1487 |
/// During the execution, the \c partMap will be set as the two
|
1485 |
1488 |
/// partitions of the graph.
|
1486 |
1489 |
/// \param graph The undirected graph.
|
1487 |
1490 |
/// \retval partMap A writable bool map of nodes. It will be set as the
|
1488 |
1491 |
/// two partitions of the graph.
|
1489 |
1492 |
/// \return %True if \c graph is bipartite, %false otherwise.
|
1490 |
1493 |
template<typename Graph, typename NodeMap>
|
1491 |
1494 |
inline bool bipartitePartitions(const Graph &graph, NodeMap &partMap){
|
1492 |
|
using namespace _topology_bits;
|
|
1495 |
using namespace _connectivity_bits;
|
1493 |
1496 |
|
1494 |
1497 |
checkConcept<concepts::Graph, Graph>();
|
1495 |
1498 |
|
1496 |
1499 |
typedef typename Graph::Node Node;
|
1497 |
1500 |
typedef typename Graph::NodeIt NodeIt;
|
1498 |
1501 |
typedef typename Graph::ArcIt ArcIt;
|
1499 |
1502 |
|
1500 |
1503 |
bool bipartite = true;
|
... |
... |
@@ -1515,58 +1518,58 @@
|
1515 |
1518 |
}
|
1516 |
1519 |
return true;
|
1517 |
1520 |
}
|
1518 |
1521 |
|
1519 |
1522 |
/// \brief Returns true when there are not loop edges in the graph.
|
1520 |
1523 |
///
|
1521 |
1524 |
/// Returns true when there are not loop edges in the graph.
|
1522 |
1525 |
template <typename Digraph>
|
1523 |
|
bool loopFree(const Digraph& graph) {
|
1524 |
|
for (typename Digraph::ArcIt it(graph); it != INVALID; ++it) {
|
1525 |
|
if (graph.source(it) == graph.target(it)) return false;
|
|
1526 |
bool loopFree(const Digraph& digraph) {
|
|
1527 |
for (typename Digraph::ArcIt it(digraph); it != INVALID; ++it) {
|
|
1528 |
if (digraph.source(it) == digraph.target(it)) return false;
|
1526 |
1529 |
}
|
1527 |
1530 |
return true;
|
1528 |
1531 |
}
|
1529 |
1532 |
|
1530 |
1533 |
/// \brief Returns true when there are not parallel edges in the graph.
|
1531 |
1534 |
///
|
1532 |
1535 |
/// Returns true when there are not parallel edges in the graph.
|
1533 |
1536 |
template <typename Digraph>
|
1534 |
|
bool parallelFree(const Digraph& graph) {
|
1535 |
|
typename Digraph::template NodeMap<bool> reached(graph, false);
|
1536 |
|
for (typename Digraph::NodeIt n(graph); n != INVALID; ++n) {
|
1537 |
|
for (typename Digraph::OutArcIt e(graph, n); e != INVALID; ++e) {
|
1538 |
|
if (reached[graph.target(e)]) return false;
|
1539 |
|
reached.set(graph.target(e), true);
|
|
1537 |
bool parallelFree(const Digraph& digraph) {
|
|
1538 |
typename Digraph::template NodeMap<bool> reached(digraph, false);
|
|
1539 |
for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
|
|
1540 |
for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) {
|
|
1541 |
if (reached[digraph.target(a)]) return false;
|
|
1542 |
reached.set(digraph.target(a), true);
|
1540 |
1543 |
}
|
1541 |
|
for (typename Digraph::OutArcIt e(graph, n); e != INVALID; ++e) {
|
1542 |
|
reached.set(graph.target(e), false);
|
|
1544 |
for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) {
|
|
1545 |
reached.set(digraph.target(a), false);
|
1543 |
1546 |
}
|
1544 |
1547 |
}
|
1545 |
1548 |
return true;
|
1546 |
1549 |
}
|
1547 |
1550 |
|
1548 |
1551 |
/// \brief Returns true when there are not loop edges and parallel
|
1549 |
1552 |
/// edges in the graph.
|
1550 |
1553 |
///
|
1551 |
1554 |
/// Returns true when there are not loop edges and parallel edges in
|
1552 |
1555 |
/// the graph.
|
1553 |
1556 |
template <typename Digraph>
|
1554 |
|
bool simpleDigraph(const Digraph& graph) {
|
1555 |
|
typename Digraph::template NodeMap<bool> reached(graph, false);
|
1556 |
|
for (typename Digraph::NodeIt n(graph); n != INVALID; ++n) {
|
|
1557 |
bool simpleDigraph(const Digraph& digraph) {
|
|
1558 |
typename Digraph::template NodeMap<bool> reached(digraph, false);
|
|
1559 |
for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
|
1557 |
1560 |
reached.set(n, true);
|
1558 |
|
for (typename Digraph::OutArcIt e(graph, n); e != INVALID; ++e) {
|
1559 |
|
if (reached[graph.target(e)]) return false;
|
1560 |
|
reached.set(graph.target(e), true);
|
|
1561 |
for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) {
|
|
1562 |
if (reached[digraph.target(a)]) return false;
|
|
1563 |
reached.set(digraph.target(a), true);
|
1561 |
1564 |
}
|
1562 |
|
for (typename Digraph::OutArcIt e(graph, n); e != INVALID; ++e) {
|
1563 |
|
reached.set(graph.target(e), false);
|
|
1565 |
for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) {
|
|
1566 |
reached.set(digraph.target(a), false);
|
1564 |
1567 |
}
|
1565 |
1568 |
reached.set(n, false);
|
1566 |
1569 |
}
|
1567 |
1570 |
return true;
|
1568 |
1571 |
}
|
1569 |
1572 |
|
1570 |
1573 |
} //namespace lemon
|
1571 |
1574 |
|
1572 |
|
#endif //LEMON_TOPOLOGY_H
|
|
1575 |
#endif //LEMON_CONNECTIVITY_H
|