30 #include <lemon/concept_check.h> |
30 #include <lemon/concept_check.h> |
31 |
31 |
32 #include <stack> |
32 #include <stack> |
33 #include <functional> |
33 #include <functional> |
34 |
34 |
35 /// \ingroup connectivity |
35 /// \ingroup graph_properties |
36 /// \file |
36 /// \file |
37 /// \brief Connectivity algorithms |
37 /// \brief Connectivity algorithms |
38 /// |
38 /// |
39 /// Connectivity algorithms |
39 /// Connectivity algorithms |
40 |
40 |
41 namespace lemon { |
41 namespace lemon { |
42 |
42 |
43 /// \ingroup connectivity |
43 /// \ingroup graph_properties |
44 /// |
44 /// |
45 /// \brief Check whether the given undirected graph is connected. |
45 /// \brief Check whether the given undirected graph is connected. |
46 /// |
46 /// |
47 /// Check whether the given undirected graph is connected. |
47 /// Check whether the given undirected graph is connected. |
48 /// \param graph The undirected graph. |
48 /// \param graph The undirected graph. |
103 } |
103 } |
104 } |
104 } |
105 return compNum; |
105 return compNum; |
106 } |
106 } |
107 |
107 |
108 /// \ingroup connectivity |
108 /// \ingroup graph_properties |
109 /// |
109 /// |
110 /// \brief Find the connected components of an undirected graph |
110 /// \brief Find the connected components of an undirected graph |
111 /// |
111 /// |
112 /// Find the connected components of an undirected graph. |
112 /// Find the connected components of an undirected graph. |
|
113 /// |
|
114 /// \image html connected_components.png |
|
115 /// \image latex connected_components.eps "Connected components" width=\textwidth |
113 /// |
116 /// |
114 /// \param graph The graph. It must be undirected. |
117 /// \param graph The graph. It must be undirected. |
115 /// \retval compMap A writable node map. The values will be set from 0 to |
118 /// \retval compMap A writable node map. The values will be set from 0 to |
116 /// the number of the connected components minus one. Each values of the map |
119 /// the number of the connected components minus one. Each values of the map |
117 /// will be set exactly once, the values of a certain component will be |
120 /// will be set exactly once, the values of a certain component will be |
118 /// set continuously. |
121 /// set continuously. |
119 /// \return The number of components |
122 /// \return The number of components |
120 /// |
|
121 template <class Graph, class NodeMap> |
123 template <class Graph, class NodeMap> |
122 int connectedComponents(const Graph &graph, NodeMap &compMap) { |
124 int connectedComponents(const Graph &graph, NodeMap &compMap) { |
123 checkConcept<concepts::Graph, Graph>(); |
125 checkConcept<concepts::Graph, Graph>(); |
124 typedef typename Graph::Node Node; |
126 typedef typename Graph::Node Node; |
125 typedef typename Graph::Arc Arc; |
127 typedef typename Graph::Arc Arc; |
359 /// relationship when there are directed paths between them in both |
361 /// relationship when there are directed paths between them in both |
360 /// direction. In addition, the numbering of components will satisfy |
362 /// direction. In addition, the numbering of components will satisfy |
361 /// that there is no arc going from a higher numbered component to |
363 /// that there is no arc going from a higher numbered component to |
362 /// a lower. |
364 /// a lower. |
363 /// |
365 /// |
|
366 /// \image html strongly_connected_components.png |
|
367 /// \image latex strongly_connected_components.eps "Strongly connected components" width=\textwidth |
|
368 /// |
364 /// \param digraph The digraph. |
369 /// \param digraph The digraph. |
365 /// \retval compMap A writable node map. The values will be set from 0 to |
370 /// \retval compMap A writable node map. The values will be set from 0 to |
366 /// the number of the strongly connected components minus one. Each value |
371 /// the number of the strongly connected components minus one. Each value |
367 /// of the map will be set exactly once, the values of a certain component |
372 /// of the map will be set exactly once, the values of a certain component |
368 /// will be set continuously. |
373 /// will be set continuously. |
369 /// \return The number of components |
374 /// \return The number of components |
370 /// |
|
371 template <typename Digraph, typename NodeMap> |
375 template <typename Digraph, typename NodeMap> |
372 int stronglyConnectedComponents(const Digraph& digraph, NodeMap& compMap) { |
376 int stronglyConnectedComponents(const Digraph& digraph, NodeMap& compMap) { |
373 checkConcept<concepts::Digraph, Digraph>(); |
377 checkConcept<concepts::Digraph, Digraph>(); |
374 typedef typename Digraph::Node Node; |
378 typedef typename Digraph::Node Node; |
375 typedef typename Digraph::NodeIt NodeIt; |
379 typedef typename Digraph::NodeIt NodeIt; |
698 } |
702 } |
699 |
703 |
700 template <typename Graph> |
704 template <typename Graph> |
701 int countBiNodeConnectedComponents(const Graph& graph); |
705 int countBiNodeConnectedComponents(const Graph& graph); |
702 |
706 |
703 /// \ingroup connectivity |
707 /// \ingroup graph_properties |
704 /// |
708 /// |
705 /// \brief Checks the graph is bi-node-connected. |
709 /// \brief Checks the graph is bi-node-connected. |
706 /// |
710 /// |
707 /// This function checks that the undirected graph is bi-node-connected |
711 /// This function checks that the undirected graph is bi-node-connected |
708 /// graph. The graph is bi-node-connected if any two undirected edge is |
712 /// graph. The graph is bi-node-connected if any two undirected edge is |
713 template <typename Graph> |
717 template <typename Graph> |
714 bool biNodeConnected(const Graph& graph) { |
718 bool biNodeConnected(const Graph& graph) { |
715 return countBiNodeConnectedComponents(graph) <= 1; |
719 return countBiNodeConnectedComponents(graph) <= 1; |
716 } |
720 } |
717 |
721 |
718 /// \ingroup connectivity |
722 /// \ingroup graph_properties |
719 /// |
723 /// |
720 /// \brief Count the biconnected components. |
724 /// \brief Count the biconnected components. |
721 /// |
725 /// |
722 /// This function finds the bi-node-connected components in an undirected |
726 /// This function finds the bi-node-connected components in an undirected |
723 /// graph. The biconnected components are the classes of an equivalence |
727 /// graph. The biconnected components are the classes of an equivalence |
748 } |
752 } |
749 } |
753 } |
750 return compNum; |
754 return compNum; |
751 } |
755 } |
752 |
756 |
753 /// \ingroup connectivity |
757 /// \ingroup graph_properties |
754 /// |
758 /// |
755 /// \brief Find the bi-node-connected components. |
759 /// \brief Find the bi-node-connected components. |
756 /// |
760 /// |
757 /// This function finds the bi-node-connected components in an undirected |
761 /// This function finds the bi-node-connected components in an undirected |
758 /// graph. The bi-node-connected components are the classes of an equivalence |
762 /// graph. The bi-node-connected components are the classes of an equivalence |
759 /// relation on the undirected edges. Two undirected edge are in relationship |
763 /// relation on the undirected edges. Two undirected edge are in relationship |
760 /// when they are on same circle. |
764 /// when they are on same circle. |
|
765 /// |
|
766 /// \image html node_biconnected_components.png |
|
767 /// \image latex node_biconnected_components.eps "bi-node-connected components" width=\textwidth |
761 /// |
768 /// |
762 /// \param graph The graph. |
769 /// \param graph The graph. |
763 /// \retval compMap A writable uedge map. The values will be set from 0 |
770 /// \retval compMap A writable uedge map. The values will be set from 0 |
764 /// to the number of the biconnected components minus one. Each values |
771 /// to the number of the biconnected components minus one. Each values |
765 /// of the map will be set exactly once, the values of a certain component |
772 /// of the map will be set exactly once, the values of a certain component |
766 /// will be set continuously. |
773 /// will be set continuously. |
767 /// \return The number of components. |
774 /// \return The number of components. |
768 /// |
|
769 template <typename Graph, typename EdgeMap> |
775 template <typename Graph, typename EdgeMap> |
770 int biNodeConnectedComponents(const Graph& graph, |
776 int biNodeConnectedComponents(const Graph& graph, |
771 EdgeMap& compMap) { |
777 EdgeMap& compMap) { |
772 checkConcept<concepts::Graph, Graph>(); |
778 checkConcept<concepts::Graph, Graph>(); |
773 typedef typename Graph::NodeIt NodeIt; |
779 typedef typename Graph::NodeIt NodeIt; |
1021 } |
1027 } |
1022 |
1028 |
1023 template <typename Graph> |
1029 template <typename Graph> |
1024 int countBiEdgeConnectedComponents(const Graph& graph); |
1030 int countBiEdgeConnectedComponents(const Graph& graph); |
1025 |
1031 |
1026 /// \ingroup connectivity |
1032 /// \ingroup graph_properties |
1027 /// |
1033 /// |
1028 /// \brief Checks that the graph is bi-edge-connected. |
1034 /// \brief Checks that the graph is bi-edge-connected. |
1029 /// |
1035 /// |
1030 /// This function checks that the graph is bi-edge-connected. The undirected |
1036 /// This function checks that the graph is bi-edge-connected. The undirected |
1031 /// graph is bi-edge-connected when any two nodes are connected with two |
1037 /// graph is bi-edge-connected when any two nodes are connected with two |
1036 template <typename Graph> |
1042 template <typename Graph> |
1037 bool biEdgeConnected(const Graph& graph) { |
1043 bool biEdgeConnected(const Graph& graph) { |
1038 return countBiEdgeConnectedComponents(graph) <= 1; |
1044 return countBiEdgeConnectedComponents(graph) <= 1; |
1039 } |
1045 } |
1040 |
1046 |
1041 /// \ingroup connectivity |
1047 /// \ingroup graph_properties |
1042 /// |
1048 /// |
1043 /// \brief Count the bi-edge-connected components. |
1049 /// \brief Count the bi-edge-connected components. |
1044 /// |
1050 /// |
1045 /// This function count the bi-edge-connected components in an undirected |
1051 /// This function count the bi-edge-connected components in an undirected |
1046 /// graph. The bi-edge-connected components are the classes of an equivalence |
1052 /// graph. The bi-edge-connected components are the classes of an equivalence |
1071 } |
1077 } |
1072 } |
1078 } |
1073 return compNum; |
1079 return compNum; |
1074 } |
1080 } |
1075 |
1081 |
1076 /// \ingroup connectivity |
1082 /// \ingroup graph_properties |
1077 /// |
1083 /// |
1078 /// \brief Find the bi-edge-connected components. |
1084 /// \brief Find the bi-edge-connected components. |
1079 /// |
1085 /// |
1080 /// This function finds the bi-edge-connected components in an undirected |
1086 /// This function finds the bi-edge-connected components in an undirected |
1081 /// graph. The bi-edge-connected components are the classes of an equivalence |
1087 /// graph. The bi-edge-connected components are the classes of an equivalence |
1082 /// relation on the nodes. Two nodes are in relationship when they are |
1088 /// relation on the nodes. Two nodes are in relationship when they are |
1083 /// connected at least two edge-disjoint paths. |
1089 /// connected at least two edge-disjoint paths. |
|
1090 /// |
|
1091 /// \image html edge_biconnected_components.png |
|
1092 /// \image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
1084 /// |
1093 /// |
1085 /// \param graph The graph. |
1094 /// \param graph The graph. |
1086 /// \retval compMap A writable node map. The values will be set from 0 to |
1095 /// \retval compMap A writable node map. The values will be set from 0 to |
1087 /// the number of the biconnected components minus one. Each values |
1096 /// the number of the biconnected components minus one. Each values |
1088 /// of the map will be set exactly once, the values of a certain component |
1097 /// of the map will be set exactly once, the values of a certain component |
1089 /// will be set continuously. |
1098 /// will be set continuously. |
1090 /// \return The number of components. |
1099 /// \return The number of components. |
1091 /// |
|
1092 template <typename Graph, typename NodeMap> |
1100 template <typename Graph, typename NodeMap> |
1093 int biEdgeConnectedComponents(const Graph& graph, NodeMap& compMap) { |
1101 int biEdgeConnectedComponents(const Graph& graph, NodeMap& compMap) { |
1094 checkConcept<concepts::Graph, Graph>(); |
1102 checkConcept<concepts::Graph, Graph>(); |
1095 typedef typename Graph::NodeIt NodeIt; |
1103 typedef typename Graph::NodeIt NodeIt; |
1096 typedef typename Graph::Node Node; |
1104 typedef typename Graph::Node Node; |
1476 } |
1484 } |
1477 } |
1485 } |
1478 return true; |
1486 return true; |
1479 } |
1487 } |
1480 |
1488 |
1481 /// \ingroup connectivity |
1489 /// \ingroup graph_properties |
1482 /// |
1490 /// |
1483 /// \brief Check if the given undirected graph is bipartite or not |
1491 /// \brief Check if the given undirected graph is bipartite or not |
1484 /// |
1492 /// |
1485 /// The function checks if the given undirected graph is bipartite |
1493 /// The function checks if the given undirected graph is bipartite |
1486 /// or not. The \ref Bfs algorithm is used to calculate the result. |
1494 /// or not. The \ref Bfs algorithm is used to calculate the result. |
1487 /// During the execution, the \c partMap will be set as the two |
1495 /// During the execution, the \c partMap will be set as the two |
1488 /// partitions of the graph. |
1496 /// partitions of the graph. |
|
1497 /// |
|
1498 /// \image html bipartite_partitions.png |
|
1499 /// \image latex bipartite_partitions.eps "Bipartite partititions" width=\textwidth |
|
1500 /// |
1489 /// \param graph The undirected graph. |
1501 /// \param graph The undirected graph. |
1490 /// \retval partMap A writable bool map of nodes. It will be set as the |
1502 /// \retval partMap A writable bool map of nodes. It will be set as the |
1491 /// two partitions of the graph. |
1503 /// two partitions of the graph. |
1492 /// \return \c true if \c graph is bipartite, \c false otherwise. |
1504 /// \return \c true if \c graph is bipartite, \c false otherwise. |
1493 template<typename Graph, typename NodeMap> |
1505 template<typename Graph, typename NodeMap> |