45#include <lal/properties/connected_components.hpp>
46#include <lal/detail/graphs/traversal.hpp>
51template <
bool full_structure,
class graph_t>
52[[nodiscard]] std::conditional_t<
54 properties::connected_components<graph_t>,
57connected_components(
const graph_t& g)
60 const auto n = g.get_num_nodes();
62 bfs.set_use_rev_edges(
true);
64 std::unordered_map<node, node> map_nodes_to_current_cc;
65 std::vector<node> nodes_current_cc;
66 std::size_t num_ccs = 0;
68 properties::connected_components<graph_t> all_ccs_full;
69 std::vector<graph_t> all_ccs_simple;
71 if constexpr (full_structure) {
75 bfs.set_process_current(
76 [&](
const auto&,
node u) {
77 map_nodes_to_current_cc.insert({u, map_nodes_to_current_cc.size()});
78 nodes_current_cc.push_back(u);
79 if constexpr (full_structure) {
80 all_ccs_full.set_node_cc(u, num_ccs);
85 for (
node u = 0; u < n; ++u) {
86 if (bfs.node_was_visited(u)) {
continue; }
88 nodes_current_cc.clear();
89 nodes_current_cc.reserve(n);
90 map_nodes_to_current_cc.clear();
91 map_nodes_to_current_cc.reserve(n);
94 graph_t cc(nodes_current_cc.size());
95 for (
node v : nodes_current_cc) {
97 if constexpr (std::is_base_of_v<graphs::directed_graph, graph_t>) {
98 for (
node w : g.get_out_neighbors(v)) {
101 map_nodes_to_current_cc[v],
102 map_nodes_to_current_cc[w]
106 for (
node w : g.get_in_neighbors(v)) {
109 map_nodes_to_current_cc[w],
110 map_nodes_to_current_cc[v]
115 else if constexpr (std::is_base_of_v<graphs::undirected_graph, graph_t>) {
116 for (
node w : g.get_neighbors(v)) {
119 map_nodes_to_current_cc[v],
120 map_nodes_to_current_cc[w]
127 if constexpr (full_structure) {
128 all_ccs_full.add_graph( std::move(cc) );
130 for (
node w : nodes_current_cc) {
131 all_ccs_full.set_label_graph_node_to_cc_node
132 (w, map_nodes_to_current_cc[w]);
133 all_ccs_full.set_label_cc_node_to_graph_node
134 (num_ccs, map_nodes_to_current_cc[w], w);
138 all_ccs_simple.push_back( std::move(cc) );
144 if constexpr (full_structure) {
148 return all_ccs_simple;
Main namespace of the library.
Definition basic_types.hpp:48
uint64_t node
Node type. See Node / Vertex page for further details.
Definition basic_types.hpp:51