nxpp
Header-only graph utilities on top of Boost Graph Library
Loading...
Searching...
No Matches
components.hpp
Go to the documentation of this file.
1#pragma once
2
11#include <boost/graph/connected_components.hpp>
12#include <boost/graph/strong_components.hpp>
13
14#include "graph.hpp"
15
16namespace nxpp {
17
18// Algorithms: Components
19
20template <typename GraphWrapper>
28[[deprecated("Use G.connected_component_groups() instead.")]]
29auto connected_component_groups(const GraphWrapper& G) {
30 return G.connected_component_groups();
31}
32
33template <typename GraphWrapper>
40[[deprecated("Use G.connected_components() instead.")]]
41auto connected_components(const GraphWrapper& G) {
42 return G.connected_components();
43}
44
45template <typename GraphWrapper>
53[[deprecated("Use G.strongly_connected_component_groups() instead.")]]
54auto strongly_connected_component_groups(const GraphWrapper& G) {
55 return G.strongly_connected_component_groups();
56}
57
58template <typename GraphWrapper>
66[[deprecated("Use G.strong_component_map() instead.")]]
67auto strong_component_map(const GraphWrapper& G) {
68 return G.strong_component_map();
69}
70
71template <typename GraphWrapper>
78[[deprecated("Use G.strong_components() instead.")]]
79auto strong_components(const GraphWrapper& G) {
80 return G.strong_components();
81}
82
83template <typename GraphWrapper>
91[[deprecated("Use G.connected_component_map() instead.")]]
92auto connected_component_map(const GraphWrapper& G) {
93 return G.connected_component_map();
94}
95
96template <typename GraphWrapper>
104[[deprecated("Use G.strongly_connected_components() instead.")]]
105auto strongly_connected_components(const GraphWrapper& G) {
106 return G.strongly_connected_components();
107}
108
109template <typename GraphWrapper>
117[[deprecated("Use G.strongly_connected_component_map() instead.")]]
118auto strongly_connected_component_map(const GraphWrapper& G) {
119 return G.strongly_connected_component_map();
120}
121
122template <typename GraphWrapper>
130[[deprecated("Use G.strongly_connected_component_roots() instead.")]]
131auto strongly_connected_component_roots(const GraphWrapper& G) {
132 return G.strongly_connected_component_roots();
133}
134
135template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector>
137 const int n = static_cast<int>(boost::num_vertices(g));
138 std::vector<int> comp(n);
139 std::vector<boost::default_color_type> color(n);
140 const int num = boost::connected_components(
141 g,
142 boost::make_iterator_property_map(comp.begin(), vertex_index_map),
143 boost::color_map(boost::make_iterator_property_map(color.begin(), vertex_index_map))
144 .vertex_index_map(vertex_index_map)
145 );
146 std::vector<std::vector<NodeID>> components(num);
147 for (auto [v, vend] = boost::vertices(g); v != vend; ++v) {
148 const auto index = static_cast<int>(get_vertex_index(*v));
149 components[comp[index]].push_back(get_node_id(*v));
150 }
151 return components;
152}
153
154template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector>
156 const int n = static_cast<int>(boost::num_vertices(g));
157 std::vector<int> comp(n);
158 std::vector<boost::default_color_type> color(n);
159 boost::connected_components(
160 g,
161 boost::make_iterator_property_map(comp.begin(), vertex_index_map),
162 boost::color_map(boost::make_iterator_property_map(color.begin(), vertex_index_map))
163 .vertex_index_map(vertex_index_map)
164 );
165 return build_node_indexed_result<int>([&](VertexDesc v) {
166 return comp[get_vertex_index(v)];
167 });
168}
169
170template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector>
172 const int n = static_cast<int>(boost::num_vertices(g));
173 std::vector<int> comp(n);
174 std::vector<boost::default_color_type> color(n);
175 const int num = boost::strong_components(
176 g,
177 boost::make_iterator_property_map(comp.begin(), vertex_index_map),
178 boost::color_map(boost::make_iterator_property_map(color.begin(), vertex_index_map))
179 .vertex_index_map(vertex_index_map)
180 );
181 std::vector<std::vector<NodeID>> components(num);
182 for (auto [v, vend] = boost::vertices(g); v != vend; ++v) {
183 const auto index = static_cast<int>(get_vertex_index(*v));
184 components[comp[index]].push_back(get_node_id(*v));
185 }
186 return components;
187}
188
189template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector>
191 const int n = static_cast<int>(boost::num_vertices(g));
192 std::vector<int> comp(n);
193 std::vector<boost::default_color_type> color(n);
194 boost::strong_components(
195 g,
196 boost::make_iterator_property_map(comp.begin(), vertex_index_map),
197 boost::color_map(boost::make_iterator_property_map(color.begin(), vertex_index_map))
198 .vertex_index_map(vertex_index_map)
199 );
200 return build_node_indexed_result<int>([&](VertexDesc v) {
201 return comp[get_vertex_index(v)];
202 });
203}
204
205template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector>
207 const int n = static_cast<int>(boost::num_vertices(g));
208 std::vector<int> comp(n);
209 std::vector<VertexDesc> roots(n);
210 std::vector<boost::default_color_type> color(n);
211 boost::strong_components(
212 g,
213 boost::make_iterator_property_map(comp.begin(), vertex_index_map),
214 boost::root_map(boost::make_iterator_property_map(roots.begin(), vertex_index_map))
215 .color_map(boost::make_iterator_property_map(color.begin(), vertex_index_map))
216 .vertex_index_map(vertex_index_map)
217 );
218 return build_node_indexed_result<NodeID>([&](VertexDesc v) {
219 return get_node_id(roots[get_vertex_index(v)]);
220 });
221}
222
223template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector>
225
226template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector>
228
229template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector>
231
232template <typename NodeID, typename EdgeWeight, bool Directed, bool Multi, bool Weighted, typename OutEdgeSelector, typename VertexSelector>
234
235} // namespace nxpp
auto strongly_connected_component_roots() const
Returns one representative root node for each strong component.
Definition components.hpp:233
auto strongly_connected_component_map() const
Alias for strong_component_map().
Definition components.hpp:230
auto connected_component_groups() const
Groups each connected component as a vector of node IDs.
Definition components.hpp:136
auto connected_component_map() const
Returns the connected-component index assigned to each node.
Definition components.hpp:224
auto strong_component_map() const
Returns the component index assigned to each node in a directed graph.
Definition components.hpp:190
auto strong_components() const
Returns the number of strongly connected components in a directed graph.
Definition components.hpp:206
auto connected_components() const
Returns the number of connected components in an undirected graph.
Definition components.hpp:155
auto strongly_connected_component_groups() const
Groups each strongly connected component as a vector of node IDs.
Definition components.hpp:171
auto strongly_connected_components() const
Alias for strongly_connected_component_groups().
Definition components.hpp:227
auto strongly_connected_component_map(const GraphWrapper &G)
Deprecated free-function alias for G.strongly_connected_component_map().
Definition components.hpp:118
auto strongly_connected_component_roots(const GraphWrapper &G)
Deprecated free-function alias for G.strongly_connected_component_roots().
Definition components.hpp:131
auto strongly_connected_components(const GraphWrapper &G)
Deprecated free-function alias for G.strongly_connected_components().
Definition components.hpp:105
auto connected_component_groups(const GraphWrapper &G)
Deprecated free-function alias for G.connected_component_groups().
Definition components.hpp:29
auto strong_components(const GraphWrapper &G)
Deprecated free-function alias for G.strong_components().
Definition components.hpp:79
auto strong_component_map(const GraphWrapper &G)
Deprecated free-function alias for G.strong_component_map().
Definition components.hpp:67
auto connected_component_map(const GraphWrapper &G)
Deprecated free-function alias for G.connected_component_map().
Definition components.hpp:92
auto connected_components(const GraphWrapper &G)
Deprecated free-function alias for G.connected_components().
Definition components.hpp:41
auto strongly_connected_component_groups(const GraphWrapper &G)
Deprecated free-function alias for G.strongly_connected_component_groups().
Definition components.hpp:54
Core graph wrapper, proxy surface, and public alias presets.