Update INSTALL to use CMAKE. Also update AUTHORS and LICENSE
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_CONNECTIVITY_H
20 #define LEMON_CONNECTIVITY_H
22 #include <lemon/dfs.h>
23 #include <lemon/bfs.h>
24 #include <lemon/core.h>
25 #include <lemon/maps.h>
26 #include <lemon/adaptors.h>
28 #include <lemon/concepts/digraph.h>
29 #include <lemon/concepts/graph.h>
30 #include <lemon/concept_check.h>
35 /// \ingroup graph_properties
37 /// \brief Connectivity algorithms
39 /// Connectivity algorithms
43 /// \ingroup graph_properties
45 /// \brief Check whether an undirected graph is connected.
47 /// This function checks whether the given undirected graph is connected,
48 /// i.e. there is a path between any two nodes in the graph.
50 /// \return \c true if the graph is connected.
51 /// \note By definition, the empty graph is connected.
53 /// \see countConnectedComponents(), connectedComponents()
54 /// \see stronglyConnected()
55 template <typename Graph>
56 bool connected(const Graph& graph) {
57 checkConcept<concepts::Graph, Graph>();
58 typedef typename Graph::NodeIt NodeIt;
59 if (NodeIt(graph) == INVALID) return true;
60 Dfs<Graph> dfs(graph);
61 dfs.run(NodeIt(graph));
62 for (NodeIt it(graph); it != INVALID; ++it) {
63 if (!dfs.reached(it)) {
70 /// \ingroup graph_properties
72 /// \brief Count the number of connected components of an undirected graph
74 /// This function counts the number of connected components of the given
77 /// The connected components are the classes of an equivalence relation
78 /// on the nodes of an undirected graph. Two nodes are in the same class
79 /// if they are connected with a path.
81 /// \return The number of connected components.
82 /// \note By definition, the empty graph consists
83 /// of zero connected components.
85 /// \see connected(), connectedComponents()
86 template <typename Graph>
87 int countConnectedComponents(const Graph &graph) {
88 checkConcept<concepts::Graph, Graph>();
89 typedef typename Graph::Node Node;
90 typedef typename Graph::Arc Arc;
92 typedef NullMap<Node, Arc> PredMap;
93 typedef NullMap<Node, int> DistMap;
97 template SetPredMap<PredMap>::
98 template SetDistMap<DistMap>::
102 bfs.predMap(predMap);
105 bfs.distMap(distMap);
108 for(typename Graph::NodeIt n(graph); n != INVALID; ++n) {
109 if (!bfs.reached(n)) {
118 /// \ingroup graph_properties
120 /// \brief Find the connected components of an undirected graph
122 /// This function finds the connected components of the given undirected
125 /// The connected components are the classes of an equivalence relation
126 /// on the nodes of an undirected graph. Two nodes are in the same class
127 /// if they are connected with a path.
129 /// \image html connected_components.png
130 /// \image latex connected_components.eps "Connected components" width=\textwidth
132 /// \param graph The undirected graph.
133 /// \retval compMap A writable node map. The values will be set from 0 to
134 /// the number of the connected components minus one. Each value of the map
135 /// will be set exactly once, and the values of a certain component will be
136 /// set continuously.
137 /// \return The number of connected components.
138 /// \note By definition, the empty graph consists
139 /// of zero connected components.
141 /// \see connected(), countConnectedComponents()
142 template <class Graph, class NodeMap>
143 int connectedComponents(const Graph &graph, NodeMap &compMap) {
144 checkConcept<concepts::Graph, Graph>();
145 typedef typename Graph::Node Node;
146 typedef typename Graph::Arc Arc;
147 checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
149 typedef NullMap<Node, Arc> PredMap;
150 typedef NullMap<Node, int> DistMap;
153 typename Bfs<Graph>::
154 template SetPredMap<PredMap>::
155 template SetDistMap<DistMap>::
159 bfs.predMap(predMap);
162 bfs.distMap(distMap);
165 for(typename Graph::NodeIt n(graph); n != INVALID; ++n) {
166 if(!bfs.reached(n)) {
168 while (!bfs.emptyQueue()) {
169 compMap.set(bfs.nextNode(), compNum);
170 bfs.processNextNode();
178 namespace _connectivity_bits {
180 template <typename Digraph, typename Iterator >
181 struct LeaveOrderVisitor : public DfsVisitor<Digraph> {
183 typedef typename Digraph::Node Node;
184 LeaveOrderVisitor(Iterator it) : _it(it) {}
186 void leave(const Node& node) {
194 template <typename Digraph, typename Map>
195 struct FillMapVisitor : public DfsVisitor<Digraph> {
197 typedef typename Digraph::Node Node;
198 typedef typename Map::Value Value;
200 FillMapVisitor(Map& map, Value& value)
201 : _map(map), _value(value) {}
203 void reach(const Node& node) {
204 _map.set(node, _value);
211 template <typename Digraph, typename ArcMap>
212 struct StronglyConnectedCutArcsVisitor : public DfsVisitor<Digraph> {
214 typedef typename Digraph::Node Node;
215 typedef typename Digraph::Arc Arc;
217 StronglyConnectedCutArcsVisitor(const Digraph& digraph,
220 : _digraph(digraph), _cutMap(cutMap), _cutNum(cutNum),
221 _compMap(digraph, -1), _num(-1) {
224 void start(const Node&) {
228 void reach(const Node& node) {
229 _compMap.set(node, _num);
232 void examine(const Arc& arc) {
233 if (_compMap[_digraph.source(arc)] !=
234 _compMap[_digraph.target(arc)]) {
235 _cutMap.set(arc, true);
240 const Digraph& _digraph;
244 typename Digraph::template NodeMap<int> _compMap;
251 /// \ingroup graph_properties
253 /// \brief Check whether a directed graph is strongly connected.
255 /// This function checks whether the given directed graph is strongly
256 /// connected, i.e. any two nodes of the digraph are
257 /// connected with directed paths in both direction.
259 /// \return \c true if the digraph is strongly connected.
260 /// \note By definition, the empty digraph is strongly connected.
262 /// \see countStronglyConnectedComponents(), stronglyConnectedComponents()
264 template <typename Digraph>
265 bool stronglyConnected(const Digraph& digraph) {
266 checkConcept<concepts::Digraph, Digraph>();
268 typedef typename Digraph::Node Node;
269 typedef typename Digraph::NodeIt NodeIt;
271 typename Digraph::Node source = NodeIt(digraph);
272 if (source == INVALID) return true;
274 using namespace _connectivity_bits;
276 typedef DfsVisitor<Digraph> Visitor;
279 DfsVisit<Digraph, Visitor> dfs(digraph, visitor);
281 dfs.addSource(source);
284 for (NodeIt it(digraph); it != INVALID; ++it) {
285 if (!dfs.reached(it)) {
290 typedef ReverseDigraph<const Digraph> RDigraph;
291 typedef typename RDigraph::NodeIt RNodeIt;
292 RDigraph rdigraph(digraph);
294 typedef DfsVisitor<RDigraph> RVisitor;
297 DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor);
299 rdfs.addSource(source);
302 for (RNodeIt it(rdigraph); it != INVALID; ++it) {
303 if (!rdfs.reached(it)) {
311 /// \ingroup graph_properties
313 /// \brief Count the number of strongly connected components of a
316 /// This function counts the number of strongly connected components of
317 /// the given directed graph.
319 /// The strongly connected components are the classes of an
320 /// equivalence relation on the nodes of a digraph. Two nodes are in
321 /// the same class if they are connected with directed paths in both
324 /// \return The number of strongly connected components.
325 /// \note By definition, the empty digraph has zero
326 /// strongly connected components.
328 /// \see stronglyConnected(), stronglyConnectedComponents()
329 template <typename Digraph>
330 int countStronglyConnectedComponents(const Digraph& digraph) {
331 checkConcept<concepts::Digraph, Digraph>();
333 using namespace _connectivity_bits;
335 typedef typename Digraph::Node Node;
336 typedef typename Digraph::Arc Arc;
337 typedef typename Digraph::NodeIt NodeIt;
338 typedef typename Digraph::ArcIt ArcIt;
340 typedef std::vector<Node> Container;
341 typedef typename Container::iterator Iterator;
343 Container nodes(countNodes(digraph));
344 typedef LeaveOrderVisitor<Digraph, Iterator> Visitor;
345 Visitor visitor(nodes.begin());
347 DfsVisit<Digraph, Visitor> dfs(digraph, visitor);
349 for (NodeIt it(digraph); it != INVALID; ++it) {
350 if (!dfs.reached(it)) {
356 typedef typename Container::reverse_iterator RIterator;
357 typedef ReverseDigraph<const Digraph> RDigraph;
359 RDigraph rdigraph(digraph);
361 typedef DfsVisitor<Digraph> RVisitor;
364 DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor);
369 for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
370 if (!rdfs.reached(*it)) {
379 /// \ingroup graph_properties
381 /// \brief Find the strongly connected components of a directed graph
383 /// This function finds the strongly connected components of the given
384 /// directed graph. In addition, the numbering of the components will
385 /// satisfy that there is no arc going from a higher numbered component
386 /// to a lower one (i.e. it provides a topological order of the components).
388 /// The strongly connected components are the classes of an
389 /// equivalence relation on the nodes of a digraph. Two nodes are in
390 /// the same class if they are connected with directed paths in both
393 /// \image html strongly_connected_components.png
394 /// \image latex strongly_connected_components.eps "Strongly connected components" width=\textwidth
396 /// \param digraph The digraph.
397 /// \retval compMap A writable node map. The values will be set from 0 to
398 /// the number of the strongly connected components minus one. Each value
399 /// of the map will be set exactly once, and the values of a certain
400 /// component will be set continuously.
401 /// \return The number of strongly connected components.
402 /// \note By definition, the empty digraph has zero
403 /// strongly connected components.
405 /// \see stronglyConnected(), countStronglyConnectedComponents()
406 template <typename Digraph, typename NodeMap>
407 int stronglyConnectedComponents(const Digraph& digraph, NodeMap& compMap) {
408 checkConcept<concepts::Digraph, Digraph>();
409 typedef typename Digraph::Node Node;
410 typedef typename Digraph::NodeIt NodeIt;
411 checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
413 using namespace _connectivity_bits;
415 typedef std::vector<Node> Container;
416 typedef typename Container::iterator Iterator;
418 Container nodes(countNodes(digraph));
419 typedef LeaveOrderVisitor<Digraph, Iterator> Visitor;
420 Visitor visitor(nodes.begin());
422 DfsVisit<Digraph, Visitor> dfs(digraph, visitor);
424 for (NodeIt it(digraph); it != INVALID; ++it) {
425 if (!dfs.reached(it)) {
431 typedef typename Container::reverse_iterator RIterator;
432 typedef ReverseDigraph<const Digraph> RDigraph;
434 RDigraph rdigraph(digraph);
438 typedef FillMapVisitor<RDigraph, NodeMap> RVisitor;
439 RVisitor rvisitor(compMap, compNum);
441 DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor);
444 for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
445 if (!rdfs.reached(*it)) {
454 /// \ingroup graph_properties
456 /// \brief Find the cut arcs of the strongly connected components.
458 /// This function finds the cut arcs of the strongly connected components
459 /// of the given digraph.
461 /// The strongly connected components are the classes of an
462 /// equivalence relation on the nodes of a digraph. Two nodes are in
463 /// the same class if they are connected with directed paths in both
465 /// The strongly connected components are separated by the cut arcs.
467 /// \param digraph The digraph.
468 /// \retval cutMap A writable arc map. The values will be set to \c true
469 /// for the cut arcs (exactly once for each cut arc), and will not be
470 /// changed for other arcs.
471 /// \return The number of cut arcs.
473 /// \see stronglyConnected(), stronglyConnectedComponents()
474 template <typename Digraph, typename ArcMap>
475 int stronglyConnectedCutArcs(const Digraph& digraph, ArcMap& cutMap) {
476 checkConcept<concepts::Digraph, Digraph>();
477 typedef typename Digraph::Node Node;
478 typedef typename Digraph::Arc Arc;
479 typedef typename Digraph::NodeIt NodeIt;
480 checkConcept<concepts::WriteMap<Arc, bool>, ArcMap>();
482 using namespace _connectivity_bits;
484 typedef std::vector<Node> Container;
485 typedef typename Container::iterator Iterator;
487 Container nodes(countNodes(digraph));
488 typedef LeaveOrderVisitor<Digraph, Iterator> Visitor;
489 Visitor visitor(nodes.begin());
491 DfsVisit<Digraph, Visitor> dfs(digraph, visitor);
493 for (NodeIt it(digraph); it != INVALID; ++it) {
494 if (!dfs.reached(it)) {
500 typedef typename Container::reverse_iterator RIterator;
501 typedef ReverseDigraph<const Digraph> RDigraph;
503 RDigraph rdigraph(digraph);
507 typedef StronglyConnectedCutArcsVisitor<RDigraph, ArcMap> RVisitor;
508 RVisitor rvisitor(rdigraph, cutMap, cutNum);
510 DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor);
513 for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
514 if (!rdfs.reached(*it)) {
522 namespace _connectivity_bits {
524 template <typename Digraph>
525 class CountBiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
527 typedef typename Digraph::Node Node;
528 typedef typename Digraph::Arc Arc;
529 typedef typename Digraph::Edge Edge;
531 CountBiNodeConnectedComponentsVisitor(const Digraph& graph, int &compNum)
532 : _graph(graph), _compNum(compNum),
533 _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
535 void start(const Node& node) {
536 _predMap.set(node, INVALID);
539 void reach(const Node& node) {
540 _numMap.set(node, _num);
541 _retMap.set(node, _num);
545 void discover(const Arc& edge) {
546 _predMap.set(_graph.target(edge), _graph.source(edge));
549 void examine(const Arc& edge) {
550 if (_graph.source(edge) == _graph.target(edge) &&
551 _graph.direction(edge)) {
555 if (_predMap[_graph.source(edge)] == _graph.target(edge)) {
558 if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) {
559 _retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]);
563 void backtrack(const Arc& edge) {
564 if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
565 _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
567 if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) {
573 const Digraph& _graph;
576 typename Digraph::template NodeMap<int> _numMap;
577 typename Digraph::template NodeMap<int> _retMap;
578 typename Digraph::template NodeMap<Node> _predMap;
582 template <typename Digraph, typename ArcMap>
583 class BiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
585 typedef typename Digraph::Node Node;
586 typedef typename Digraph::Arc Arc;
587 typedef typename Digraph::Edge Edge;
589 BiNodeConnectedComponentsVisitor(const Digraph& graph,
590 ArcMap& compMap, int &compNum)
591 : _graph(graph), _compMap(compMap), _compNum(compNum),
592 _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
594 void start(const Node& node) {
595 _predMap.set(node, INVALID);
598 void reach(const Node& node) {
599 _numMap.set(node, _num);
600 _retMap.set(node, _num);
604 void discover(const Arc& edge) {
605 Node target = _graph.target(edge);
606 _predMap.set(target, edge);
607 _edgeStack.push(edge);
610 void examine(const Arc& edge) {
611 Node source = _graph.source(edge);
612 Node target = _graph.target(edge);
613 if (source == target && _graph.direction(edge)) {
614 _compMap.set(edge, _compNum);
618 if (_numMap[target] < _numMap[source]) {
619 if (_predMap[source] != _graph.oppositeArc(edge)) {
620 _edgeStack.push(edge);
623 if (_predMap[source] != INVALID &&
624 target == _graph.source(_predMap[source])) {
627 if (_retMap[source] > _numMap[target]) {
628 _retMap.set(source, _numMap[target]);
632 void backtrack(const Arc& edge) {
633 Node source = _graph.source(edge);
634 Node target = _graph.target(edge);
635 if (_retMap[source] > _retMap[target]) {
636 _retMap.set(source, _retMap[target]);
638 if (_numMap[source] <= _retMap[target]) {
639 while (_edgeStack.top() != edge) {
640 _compMap.set(_edgeStack.top(), _compNum);
643 _compMap.set(edge, _compNum);
650 const Digraph& _graph;
654 typename Digraph::template NodeMap<int> _numMap;
655 typename Digraph::template NodeMap<int> _retMap;
656 typename Digraph::template NodeMap<Arc> _predMap;
657 std::stack<Edge> _edgeStack;
662 template <typename Digraph, typename NodeMap>
663 class BiNodeConnectedCutNodesVisitor : public DfsVisitor<Digraph> {
665 typedef typename Digraph::Node Node;
666 typedef typename Digraph::Arc Arc;
667 typedef typename Digraph::Edge Edge;
669 BiNodeConnectedCutNodesVisitor(const Digraph& graph, NodeMap& cutMap,
671 : _graph(graph), _cutMap(cutMap), _cutNum(cutNum),
672 _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
674 void start(const Node& node) {
675 _predMap.set(node, INVALID);
679 void reach(const Node& node) {
680 _numMap.set(node, _num);
681 _retMap.set(node, _num);
685 void discover(const Arc& edge) {
686 _predMap.set(_graph.target(edge), _graph.source(edge));
689 void examine(const Arc& edge) {
690 if (_graph.source(edge) == _graph.target(edge) &&
691 _graph.direction(edge)) {
692 if (!_cutMap[_graph.source(edge)]) {
693 _cutMap.set(_graph.source(edge), true);
698 if (_predMap[_graph.source(edge)] == _graph.target(edge)) return;
699 if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) {
700 _retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]);
704 void backtrack(const Arc& edge) {
705 if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
706 _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
708 if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) {
709 if (_predMap[_graph.source(edge)] != INVALID) {
710 if (!_cutMap[_graph.source(edge)]) {
711 _cutMap.set(_graph.source(edge), true);
714 } else if (rootCut) {
715 if (!_cutMap[_graph.source(edge)]) {
716 _cutMap.set(_graph.source(edge), true);
726 const Digraph& _graph;
730 typename Digraph::template NodeMap<int> _numMap;
731 typename Digraph::template NodeMap<int> _retMap;
732 typename Digraph::template NodeMap<Node> _predMap;
733 std::stack<Edge> _edgeStack;
740 template <typename Graph>
741 int countBiNodeConnectedComponents(const Graph& graph);
743 /// \ingroup graph_properties
745 /// \brief Check whether an undirected graph is bi-node-connected.
747 /// This function checks whether the given undirected graph is
748 /// bi-node-connected, i.e. any two edges are on same circle.
750 /// \return \c true if the graph bi-node-connected.
751 /// \note By definition, the empty graph is bi-node-connected.
753 /// \see countBiNodeConnectedComponents(), biNodeConnectedComponents()
754 template <typename Graph>
755 bool biNodeConnected(const Graph& graph) {
756 return countBiNodeConnectedComponents(graph) <= 1;
759 /// \ingroup graph_properties
761 /// \brief Count the number of bi-node-connected components of an
762 /// undirected graph.
764 /// This function counts the number of bi-node-connected components of
765 /// the given undirected graph.
767 /// The bi-node-connected components are the classes of an equivalence
768 /// relation on the edges of a undirected graph. Two edges are in the
769 /// same class if they are on same circle.
771 /// \return The number of bi-node-connected components.
773 /// \see biNodeConnected(), biNodeConnectedComponents()
774 template <typename Graph>
775 int countBiNodeConnectedComponents(const Graph& graph) {
776 checkConcept<concepts::Graph, Graph>();
777 typedef typename Graph::NodeIt NodeIt;
779 using namespace _connectivity_bits;
781 typedef CountBiNodeConnectedComponentsVisitor<Graph> Visitor;
784 Visitor visitor(graph, compNum);
786 DfsVisit<Graph, Visitor> dfs(graph, visitor);
789 for (NodeIt it(graph); it != INVALID; ++it) {
790 if (!dfs.reached(it)) {
798 /// \ingroup graph_properties
800 /// \brief Find the bi-node-connected components of an undirected graph.
802 /// This function finds the bi-node-connected components of the given
803 /// undirected graph.
805 /// The bi-node-connected components are the classes of an equivalence
806 /// relation on the edges of a undirected graph. Two edges are in the
807 /// same class if they are on same circle.
809 /// \image html node_biconnected_components.png
810 /// \image latex node_biconnected_components.eps "bi-node-connected components" width=\textwidth
812 /// \param graph The undirected graph.
813 /// \retval compMap A writable edge map. The values will be set from 0
814 /// to the number of the bi-node-connected components minus one. Each
815 /// value of the map will be set exactly once, and the values of a
816 /// certain component will be set continuously.
817 /// \return The number of bi-node-connected components.
819 /// \see biNodeConnected(), countBiNodeConnectedComponents()
820 template <typename Graph, typename EdgeMap>
821 int biNodeConnectedComponents(const Graph& graph,
823 checkConcept<concepts::Graph, Graph>();
824 typedef typename Graph::NodeIt NodeIt;
825 typedef typename Graph::Edge Edge;
826 checkConcept<concepts::WriteMap<Edge, int>, EdgeMap>();
828 using namespace _connectivity_bits;
830 typedef BiNodeConnectedComponentsVisitor<Graph, EdgeMap> Visitor;
833 Visitor visitor(graph, compMap, compNum);
835 DfsVisit<Graph, Visitor> dfs(graph, visitor);
838 for (NodeIt it(graph); it != INVALID; ++it) {
839 if (!dfs.reached(it)) {
847 /// \ingroup graph_properties
849 /// \brief Find the bi-node-connected cut nodes in an undirected graph.
851 /// This function finds the bi-node-connected cut nodes in the given
852 /// undirected graph.
854 /// The bi-node-connected components are the classes of an equivalence
855 /// relation on the edges of a undirected graph. Two edges are in the
856 /// same class if they are on same circle.
857 /// The bi-node-connected components are separted by the cut nodes of
860 /// \param graph The undirected graph.
861 /// \retval cutMap A writable node map. The values will be set to
862 /// \c true for the nodes that separate two or more components
863 /// (exactly once for each cut node), and will not be changed for
865 /// \return The number of the cut nodes.
867 /// \see biNodeConnected(), biNodeConnectedComponents()
868 template <typename Graph, typename NodeMap>
869 int biNodeConnectedCutNodes(const Graph& graph, NodeMap& cutMap) {
870 checkConcept<concepts::Graph, Graph>();
871 typedef typename Graph::Node Node;
872 typedef typename Graph::NodeIt NodeIt;
873 checkConcept<concepts::WriteMap<Node, bool>, NodeMap>();
875 using namespace _connectivity_bits;
877 typedef BiNodeConnectedCutNodesVisitor<Graph, NodeMap> Visitor;
880 Visitor visitor(graph, cutMap, cutNum);
882 DfsVisit<Graph, Visitor> dfs(graph, visitor);
885 for (NodeIt it(graph); it != INVALID; ++it) {
886 if (!dfs.reached(it)) {
894 namespace _connectivity_bits {
896 template <typename Digraph>
897 class CountBiEdgeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
899 typedef typename Digraph::Node Node;
900 typedef typename Digraph::Arc Arc;
901 typedef typename Digraph::Edge Edge;
903 CountBiEdgeConnectedComponentsVisitor(const Digraph& graph, int &compNum)
904 : _graph(graph), _compNum(compNum),
905 _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
907 void start(const Node& node) {
908 _predMap.set(node, INVALID);
911 void reach(const Node& node) {
912 _numMap.set(node, _num);
913 _retMap.set(node, _num);
917 void leave(const Node& node) {
918 if (_numMap[node] <= _retMap[node]) {
923 void discover(const Arc& edge) {
924 _predMap.set(_graph.target(edge), edge);
927 void examine(const Arc& edge) {
928 if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) {
931 if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
932 _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
936 void backtrack(const Arc& edge) {
937 if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
938 _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
943 const Digraph& _graph;
946 typename Digraph::template NodeMap<int> _numMap;
947 typename Digraph::template NodeMap<int> _retMap;
948 typename Digraph::template NodeMap<Arc> _predMap;
952 template <typename Digraph, typename NodeMap>
953 class BiEdgeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
955 typedef typename Digraph::Node Node;
956 typedef typename Digraph::Arc Arc;
957 typedef typename Digraph::Edge Edge;
959 BiEdgeConnectedComponentsVisitor(const Digraph& graph,
960 NodeMap& compMap, int &compNum)
961 : _graph(graph), _compMap(compMap), _compNum(compNum),
962 _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
964 void start(const Node& node) {
965 _predMap.set(node, INVALID);
968 void reach(const Node& node) {
969 _numMap.set(node, _num);
970 _retMap.set(node, _num);
971 _nodeStack.push(node);
975 void leave(const Node& node) {
976 if (_numMap[node] <= _retMap[node]) {
977 while (_nodeStack.top() != node) {
978 _compMap.set(_nodeStack.top(), _compNum);
981 _compMap.set(node, _compNum);
987 void discover(const Arc& edge) {
988 _predMap.set(_graph.target(edge), edge);
991 void examine(const Arc& edge) {
992 if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) {
995 if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
996 _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
1000 void backtrack(const Arc& edge) {
1001 if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
1002 _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
1007 const Digraph& _graph;
1011 typename Digraph::template NodeMap<int> _numMap;
1012 typename Digraph::template NodeMap<int> _retMap;
1013 typename Digraph::template NodeMap<Arc> _predMap;
1014 std::stack<Node> _nodeStack;
1019 template <typename Digraph, typename ArcMap>
1020 class BiEdgeConnectedCutEdgesVisitor : public DfsVisitor<Digraph> {
1022 typedef typename Digraph::Node Node;
1023 typedef typename Digraph::Arc Arc;
1024 typedef typename Digraph::Edge Edge;
1026 BiEdgeConnectedCutEdgesVisitor(const Digraph& graph,
1027 ArcMap& cutMap, int &cutNum)
1028 : _graph(graph), _cutMap(cutMap), _cutNum(cutNum),
1029 _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
1031 void start(const Node& node) {
1032 _predMap[node] = INVALID;
1035 void reach(const Node& node) {
1036 _numMap.set(node, _num);
1037 _retMap.set(node, _num);
1041 void leave(const Node& node) {
1042 if (_numMap[node] <= _retMap[node]) {
1043 if (_predMap[node] != INVALID) {
1044 _cutMap.set(_predMap[node], true);
1050 void discover(const Arc& edge) {
1051 _predMap.set(_graph.target(edge), edge);
1054 void examine(const Arc& edge) {
1055 if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) {
1058 if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
1059 _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
1063 void backtrack(const Arc& edge) {
1064 if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
1065 _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
1070 const Digraph& _graph;
1074 typename Digraph::template NodeMap<int> _numMap;
1075 typename Digraph::template NodeMap<int> _retMap;
1076 typename Digraph::template NodeMap<Arc> _predMap;
1081 template <typename Graph>
1082 int countBiEdgeConnectedComponents(const Graph& graph);
1084 /// \ingroup graph_properties
1086 /// \brief Check whether an undirected graph is bi-edge-connected.
1088 /// This function checks whether the given undirected graph is
1089 /// bi-edge-connected, i.e. any two nodes are connected with at least
1090 /// two edge-disjoint paths.
1092 /// \return \c true if the graph is bi-edge-connected.
1093 /// \note By definition, the empty graph is bi-edge-connected.
1095 /// \see countBiEdgeConnectedComponents(), biEdgeConnectedComponents()
1096 template <typename Graph>
1097 bool biEdgeConnected(const Graph& graph) {
1098 return countBiEdgeConnectedComponents(graph) <= 1;
1101 /// \ingroup graph_properties
1103 /// \brief Count the number of bi-edge-connected components of an
1104 /// undirected graph.
1106 /// This function counts the number of bi-edge-connected components of
1107 /// the given undirected graph.
1109 /// The bi-edge-connected components are the classes of an equivalence
1110 /// relation on the nodes of an undirected graph. Two nodes are in the
1111 /// same class if they are connected with at least two edge-disjoint
1114 /// \return The number of bi-edge-connected components.
1116 /// \see biEdgeConnected(), biEdgeConnectedComponents()
1117 template <typename Graph>
1118 int countBiEdgeConnectedComponents(const Graph& graph) {
1119 checkConcept<concepts::Graph, Graph>();
1120 typedef typename Graph::NodeIt NodeIt;
1122 using namespace _connectivity_bits;
1124 typedef CountBiEdgeConnectedComponentsVisitor<Graph> Visitor;
1127 Visitor visitor(graph, compNum);
1129 DfsVisit<Graph, Visitor> dfs(graph, visitor);
1132 for (NodeIt it(graph); it != INVALID; ++it) {
1133 if (!dfs.reached(it)) {
1141 /// \ingroup graph_properties
1143 /// \brief Find the bi-edge-connected components of an undirected graph.
1145 /// This function finds the bi-edge-connected components of the given
1146 /// undirected graph.
1148 /// The bi-edge-connected components are the classes of an equivalence
1149 /// relation on the nodes of an undirected graph. Two nodes are in the
1150 /// same class if they are connected with at least two edge-disjoint
1153 /// \image html edge_biconnected_components.png
1154 /// \image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth
1156 /// \param graph The undirected graph.
1157 /// \retval compMap A writable node map. The values will be set from 0 to
1158 /// the number of the bi-edge-connected components minus one. Each value
1159 /// of the map will be set exactly once, and the values of a certain
1160 /// component will be set continuously.
1161 /// \return The number of bi-edge-connected components.
1163 /// \see biEdgeConnected(), countBiEdgeConnectedComponents()
1164 template <typename Graph, typename NodeMap>
1165 int biEdgeConnectedComponents(const Graph& graph, NodeMap& compMap) {
1166 checkConcept<concepts::Graph, Graph>();
1167 typedef typename Graph::NodeIt NodeIt;
1168 typedef typename Graph::Node Node;
1169 checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
1171 using namespace _connectivity_bits;
1173 typedef BiEdgeConnectedComponentsVisitor<Graph, NodeMap> Visitor;
1176 Visitor visitor(graph, compMap, compNum);
1178 DfsVisit<Graph, Visitor> dfs(graph, visitor);
1181 for (NodeIt it(graph); it != INVALID; ++it) {
1182 if (!dfs.reached(it)) {
1190 /// \ingroup graph_properties
1192 /// \brief Find the bi-edge-connected cut edges in an undirected graph.
1194 /// This function finds the bi-edge-connected cut edges in the given
1195 /// undirected graph.
1197 /// The bi-edge-connected components are the classes of an equivalence
1198 /// relation on the nodes of an undirected graph. Two nodes are in the
1199 /// same class if they are connected with at least two edge-disjoint
1201 /// The bi-edge-connected components are separted by the cut edges of
1204 /// \param graph The undirected graph.
1205 /// \retval cutMap A writable edge map. The values will be set to \c true
1206 /// for the cut edges (exactly once for each cut edge), and will not be
1207 /// changed for other edges.
1208 /// \return The number of cut edges.
1210 /// \see biEdgeConnected(), biEdgeConnectedComponents()
1211 template <typename Graph, typename EdgeMap>
1212 int biEdgeConnectedCutEdges(const Graph& graph, EdgeMap& cutMap) {
1213 checkConcept<concepts::Graph, Graph>();
1214 typedef typename Graph::NodeIt NodeIt;
1215 typedef typename Graph::Edge Edge;
1216 checkConcept<concepts::WriteMap<Edge, bool>, EdgeMap>();
1218 using namespace _connectivity_bits;
1220 typedef BiEdgeConnectedCutEdgesVisitor<Graph, EdgeMap> Visitor;
1223 Visitor visitor(graph, cutMap, cutNum);
1225 DfsVisit<Graph, Visitor> dfs(graph, visitor);
1228 for (NodeIt it(graph); it != INVALID; ++it) {
1229 if (!dfs.reached(it)) {
1238 namespace _connectivity_bits {
1240 template <typename Digraph, typename IntNodeMap>
1241 class TopologicalSortVisitor : public DfsVisitor<Digraph> {
1243 typedef typename Digraph::Node Node;
1244 typedef typename Digraph::Arc edge;
1246 TopologicalSortVisitor(IntNodeMap& order, int num)
1247 : _order(order), _num(num) {}
1249 void leave(const Node& node) {
1250 _order.set(node, --_num);
1260 /// \ingroup graph_properties
1262 /// \brief Check whether a digraph is DAG.
1264 /// This function checks whether the given digraph is DAG, i.e.
1265 /// \e Directed \e Acyclic \e Graph.
1266 /// \return \c true if there is no directed cycle in the digraph.
1268 template <typename Digraph>
1269 bool dag(const Digraph& digraph) {
1271 checkConcept<concepts::Digraph, Digraph>();
1273 typedef typename Digraph::Node Node;
1274 typedef typename Digraph::NodeIt NodeIt;
1275 typedef typename Digraph::Arc Arc;
1277 typedef typename Digraph::template NodeMap<bool> ProcessedMap;
1279 typename Dfs<Digraph>::template SetProcessedMap<ProcessedMap>::
1280 Create dfs(digraph);
1282 ProcessedMap processed(digraph);
1283 dfs.processedMap(processed);
1286 for (NodeIt it(digraph); it != INVALID; ++it) {
1287 if (!dfs.reached(it)) {
1289 while (!dfs.emptyQueue()) {
1290 Arc arc = dfs.nextArc();
1291 Node target = digraph.target(arc);
1292 if (dfs.reached(target) && !processed[target]) {
1295 dfs.processNextArc();
1302 /// \ingroup graph_properties
1304 /// \brief Sort the nodes of a DAG into topolgical order.
1306 /// This function sorts the nodes of the given acyclic digraph (DAG)
1307 /// into topolgical order.
1309 /// \param digraph The digraph, which must be DAG.
1310 /// \retval order A writable node map. The values will be set from 0 to
1311 /// the number of the nodes in the digraph minus one. Each value of the
1312 /// map will be set exactly once, and the values will be set descending
1315 /// \see dag(), checkedTopologicalSort()
1316 template <typename Digraph, typename NodeMap>
1317 void topologicalSort(const Digraph& digraph, NodeMap& order) {
1318 using namespace _connectivity_bits;
1320 checkConcept<concepts::Digraph, Digraph>();
1321 checkConcept<concepts::WriteMap<typename Digraph::Node, int>, NodeMap>();
1323 typedef typename Digraph::Node Node;
1324 typedef typename Digraph::NodeIt NodeIt;
1325 typedef typename Digraph::Arc Arc;
1327 TopologicalSortVisitor<Digraph, NodeMap>
1328 visitor(order, countNodes(digraph));
1330 DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> >
1331 dfs(digraph, visitor);
1334 for (NodeIt it(digraph); it != INVALID; ++it) {
1335 if (!dfs.reached(it)) {
1342 /// \ingroup graph_properties
1344 /// \brief Sort the nodes of a DAG into topolgical order.
1346 /// This function sorts the nodes of the given acyclic digraph (DAG)
1347 /// into topolgical order and also checks whether the given digraph
1350 /// \param digraph The digraph.
1351 /// \retval order A readable and writable node map. The values will be
1352 /// set from 0 to the number of the nodes in the digraph minus one.
1353 /// Each value of the map will be set exactly once, and the values will
1354 /// be set descending order.
1355 /// \return \c false if the digraph is not DAG.
1357 /// \see dag(), topologicalSort()
1358 template <typename Digraph, typename NodeMap>
1359 bool checkedTopologicalSort(const Digraph& digraph, NodeMap& order) {
1360 using namespace _connectivity_bits;
1362 checkConcept<concepts::Digraph, Digraph>();
1363 checkConcept<concepts::ReadWriteMap<typename Digraph::Node, int>,
1366 typedef typename Digraph::Node Node;
1367 typedef typename Digraph::NodeIt NodeIt;
1368 typedef typename Digraph::Arc Arc;
1370 for (NodeIt it(digraph); it != INVALID; ++it) {
1374 TopologicalSortVisitor<Digraph, NodeMap>
1375 visitor(order, countNodes(digraph));
1377 DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> >
1378 dfs(digraph, visitor);
1381 for (NodeIt it(digraph); it != INVALID; ++it) {
1382 if (!dfs.reached(it)) {
1384 while (!dfs.emptyQueue()) {
1385 Arc arc = dfs.nextArc();
1386 Node target = digraph.target(arc);
1387 if (dfs.reached(target) && order[target] == -1) {
1390 dfs.processNextArc();
1397 /// \ingroup graph_properties
1399 /// \brief Check whether an undirected graph is acyclic.
1401 /// This function checks whether the given undirected graph is acyclic.
1402 /// \return \c true if there is no cycle in the graph.
1404 template <typename Graph>
1405 bool acyclic(const Graph& graph) {
1406 checkConcept<concepts::Graph, Graph>();
1407 typedef typename Graph::Node Node;
1408 typedef typename Graph::NodeIt NodeIt;
1409 typedef typename Graph::Arc Arc;
1410 Dfs<Graph> dfs(graph);
1412 for (NodeIt it(graph); it != INVALID; ++it) {
1413 if (!dfs.reached(it)) {
1415 while (!dfs.emptyQueue()) {
1416 Arc arc = dfs.nextArc();
1417 Node source = graph.source(arc);
1418 Node target = graph.target(arc);
1419 if (dfs.reached(target) &&
1420 dfs.predArc(source) != graph.oppositeArc(arc)) {
1423 dfs.processNextArc();
1430 /// \ingroup graph_properties
1432 /// \brief Check whether an undirected graph is tree.
1434 /// This function checks whether the given undirected graph is tree.
1435 /// \return \c true if the graph is acyclic and connected.
1436 /// \see acyclic(), connected()
1437 template <typename Graph>
1438 bool tree(const Graph& graph) {
1439 checkConcept<concepts::Graph, Graph>();
1440 typedef typename Graph::Node Node;
1441 typedef typename Graph::NodeIt NodeIt;
1442 typedef typename Graph::Arc Arc;
1443 if (NodeIt(graph) == INVALID) return true;
1444 Dfs<Graph> dfs(graph);
1446 dfs.addSource(NodeIt(graph));
1447 while (!dfs.emptyQueue()) {
1448 Arc arc = dfs.nextArc();
1449 Node source = graph.source(arc);
1450 Node target = graph.target(arc);
1451 if (dfs.reached(target) &&
1452 dfs.predArc(source) != graph.oppositeArc(arc)) {
1455 dfs.processNextArc();
1457 for (NodeIt it(graph); it != INVALID; ++it) {
1458 if (!dfs.reached(it)) {
1465 namespace _connectivity_bits {
1467 template <typename Digraph>
1468 class BipartiteVisitor : public BfsVisitor<Digraph> {
1470 typedef typename Digraph::Arc Arc;
1471 typedef typename Digraph::Node Node;
1473 BipartiteVisitor(const Digraph& graph, bool& bipartite)
1474 : _graph(graph), _part(graph), _bipartite(bipartite) {}
1476 void start(const Node& node) {
1479 void discover(const Arc& edge) {
1480 _part.set(_graph.target(edge), !_part[_graph.source(edge)]);
1482 void examine(const Arc& edge) {
1483 _bipartite = _bipartite &&
1484 _part[_graph.target(edge)] != _part[_graph.source(edge)];
1489 const Digraph& _graph;
1490 typename Digraph::template NodeMap<bool> _part;
1494 template <typename Digraph, typename PartMap>
1495 class BipartitePartitionsVisitor : public BfsVisitor<Digraph> {
1497 typedef typename Digraph::Arc Arc;
1498 typedef typename Digraph::Node Node;
1500 BipartitePartitionsVisitor(const Digraph& graph,
1501 PartMap& part, bool& bipartite)
1502 : _graph(graph), _part(part), _bipartite(bipartite) {}
1504 void start(const Node& node) {
1505 _part.set(node, true);
1507 void discover(const Arc& edge) {
1508 _part.set(_graph.target(edge), !_part[_graph.source(edge)]);
1510 void examine(const Arc& edge) {
1511 _bipartite = _bipartite &&
1512 _part[_graph.target(edge)] != _part[_graph.source(edge)];
1517 const Digraph& _graph;
1523 /// \ingroup graph_properties
1525 /// \brief Check whether an undirected graph is bipartite.
1527 /// The function checks whether the given undirected graph is bipartite.
1528 /// \return \c true if the graph is bipartite.
1530 /// \see bipartitePartitions()
1531 template<typename Graph>
1532 bool bipartite(const Graph &graph){
1533 using namespace _connectivity_bits;
1535 checkConcept<concepts::Graph, Graph>();
1537 typedef typename Graph::NodeIt NodeIt;
1538 typedef typename Graph::ArcIt ArcIt;
1540 bool bipartite = true;
1542 BipartiteVisitor<Graph>
1543 visitor(graph, bipartite);
1544 BfsVisit<Graph, BipartiteVisitor<Graph> >
1545 bfs(graph, visitor);
1547 for(NodeIt it(graph); it != INVALID; ++it) {
1548 if(!bfs.reached(it)){
1550 while (!bfs.emptyQueue()) {
1551 bfs.processNextNode();
1552 if (!bipartite) return false;
1559 /// \ingroup graph_properties
1561 /// \brief Find the bipartite partitions of an undirected graph.
1563 /// This function checks whether the given undirected graph is bipartite
1564 /// and gives back the bipartite partitions.
1566 /// \image html bipartite_partitions.png
1567 /// \image latex bipartite_partitions.eps "Bipartite partititions" width=\textwidth
1569 /// \param graph The undirected graph.
1570 /// \retval partMap A writable node map of \c bool (or convertible) value
1571 /// type. The values will be set to \c true for one component and
1572 /// \c false for the other one.
1573 /// \return \c true if the graph is bipartite, \c false otherwise.
1575 /// \see bipartite()
1576 template<typename Graph, typename NodeMap>
1577 bool bipartitePartitions(const Graph &graph, NodeMap &partMap){
1578 using namespace _connectivity_bits;
1580 checkConcept<concepts::Graph, Graph>();
1581 checkConcept<concepts::WriteMap<typename Graph::Node, bool>, NodeMap>();
1583 typedef typename Graph::Node Node;
1584 typedef typename Graph::NodeIt NodeIt;
1585 typedef typename Graph::ArcIt ArcIt;
1587 bool bipartite = true;
1589 BipartitePartitionsVisitor<Graph, NodeMap>
1590 visitor(graph, partMap, bipartite);
1591 BfsVisit<Graph, BipartitePartitionsVisitor<Graph, NodeMap> >
1592 bfs(graph, visitor);
1594 for(NodeIt it(graph); it != INVALID; ++it) {
1595 if(!bfs.reached(it)){
1597 while (!bfs.emptyQueue()) {
1598 bfs.processNextNode();
1599 if (!bipartite) return false;
1606 /// \ingroup graph_properties
1608 /// \brief Check whether the given graph contains no loop arcs/edges.
1610 /// This function returns \c true if there are no loop arcs/edges in
1611 /// the given graph. It works for both directed and undirected graphs.
1612 template <typename Graph>
1613 bool loopFree(const Graph& graph) {
1614 for (typename Graph::ArcIt it(graph); it != INVALID; ++it) {
1615 if (graph.source(it) == graph.target(it)) return false;
1620 /// \ingroup graph_properties
1622 /// \brief Check whether the given graph contains no parallel arcs/edges.
1624 /// This function returns \c true if there are no parallel arcs/edges in
1625 /// the given graph. It works for both directed and undirected graphs.
1626 template <typename Graph>
1627 bool parallelFree(const Graph& graph) {
1628 typename Graph::template NodeMap<int> reached(graph, 0);
1630 for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
1631 for (typename Graph::OutArcIt a(graph, n); a != INVALID; ++a) {
1632 if (reached[graph.target(a)] == cnt) return false;
1633 reached[graph.target(a)] = cnt;
1640 /// \ingroup graph_properties
1642 /// \brief Check whether the given graph is simple.
1644 /// This function returns \c true if the given graph is simple, i.e.
1645 /// it contains no loop arcs/edges and no parallel arcs/edges.
1646 /// The function works for both directed and undirected graphs.
1647 /// \see loopFree(), parallelFree()
1648 template <typename Graph>
1649 bool simpleGraph(const Graph& graph) {
1650 typename Graph::template NodeMap<int> reached(graph, 0);
1652 for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
1654 for (typename Graph::OutArcIt a(graph, n); a != INVALID; ++a) {
1655 if (reached[graph.target(a)] == cnt) return false;
1656 reached[graph.target(a)] = cnt;
1665 #endif //LEMON_CONNECTIVITY_H