Graphle 1.0.0
A C++20 header-only graph library
Loading...
Searching...
No Matches
strongly_connected_components.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <common.hpp>
4#include <graph/graph.hpp>
11
12#include <concepts>
13#include <iterator>
14#include <limits>
15
16
17namespace graphle::alg {
18 namespace detail {
19 template <graph_ref G> using out_edge_range_t = decltype(util::out_edges(std::declval<G>(), std::declval<vertex_of<G>>()));
20
21
22 template <graph_ref G> struct tarjan_vertex_data {
24 index(index),
26 stacked(true),
27 edges(util::out_edges(graph, vertex)),
28 edge_iterator(rng::begin(edges))
29 {}
30
31
32 std::size_t index;
33 std::size_t low_link;
34 bool stacked;
35
37 rng::iterator_t<out_edge_range_t<G>> edge_iterator;
38 };
39 }
40
41
70 template <
72 typename Target,
79 > requires (
82 std::output_iterator<std::remove_cvref_t<Target>, typename std::remove_cvref_t<Target>::value_type> &&
83 std::output_iterator<typename std::remove_cvref_t<Target>::value_type, vertex_of<G>>
84 ) constexpr inline void strongly_connected_components(
85 G&& graph,
86 Target&& target,
87 std::size_t min_size = 0,
91 ) {
92 decltype(auto) call_stack = stack_provider();
93 decltype(auto) low_stack = min_stack_provider();
94 decltype(auto) data = map_provider();
95 std::size_t index = 0;
96
97
98 for (auto vertex : graph.get_vertices()) {
99 if (!data.contains(vertex)) {
100 call_stack.push_back(vertex);
101
102
103 while (!rng::empty(call_stack)) {
104 auto v = util::take_back(call_stack);
105
106 if (!data.contains(v)) {
107 data.emplace(v, detail::tarjan_vertex_data<G> { index++, graph, v });
108 low_stack.push_back(v);
109 } else {
110 auto& vd = data.at(v);
111 vd.low_link = std::min(vd.low_link, data.at(vd.edge_iterator->second).low_link);
112 }
113
114
115 auto& vd = data.at(v);
116 auto& it = vd.edge_iterator;
117
118 for (/* no init */; it != vd.edges.end() && data.contains(it->second); ++it) {
119 auto w = it->second;
120 auto& wd = data.at(w);
121
122 if (wd.stacked) vd.low_link = std::min(vd.low_link, wd.index);
123 }
124
125
126 if (it != vd.edges.end()) {
127 call_stack.push_back(v);
128 call_stack.push_back(it->second);
129
130 ++it;
131 continue;
132 }
133
134
135 if (vd.low_link == vd.index) {
136 // Output iterators are not guaranteed to be dereferencable multiple times for the same element,
137 // so only dereference it if we're actually going to output an SCC.
138 const bool should_output = min_size < 2 || [&] {
139 std::size_t count = 0;
140
141 for (auto w : low_stack | views::reverse) {
142 if (!vertex_compare_of<G>(v, w)) ++count;
143 else break;
144 }
145
146 return count >= min_size;
147 } ();
148
149
150 vertex_of<G> w;
151
152 auto unstack = [&] {
153 w = util::take_back(low_stack);
154 data.at(w).stacked = false;
155 return w;
156 };
157
158
159 if (should_output) {
160 decltype(auto) scc_target = *target;
161
162 do *scc_target++ = unstack();
163 while (!vertex_compare_of<G>(v, w));
164 } else {
165 do unstack();
166 while (!vertex_compare_of<G>(v, w));
167 }
168 }
169 }
170 }
171 }
172 }
173
174
209 template <
221 > requires (
224 ) constexpr inline decltype(auto) strongly_connected_components(
225 G&& graph,
226 std::size_t min_size = 0,
228 OuterPR&& outer_ret_provider = store::get_default_storage_provider<store::storage_type::VECTOR, store::provided_storage_value_type<InnerPR>>(),
230 PVM&& min_stack_provider = store::get_default_storage_provider<store::storage_type::VECTOR, vertex_of<G>>(),
232 ) {
233 decltype(auto) result = outer_ret_provider();
234
236 graph,
237 util::vec_of_vecs_output_iterator { result, inner_ret_provider },
238 min_size,
239 GRAPHLE_FWD(stack_provider),
240 GRAPHLE_FWD(min_stack_provider),
241 GRAPHLE_FWD(map_provider)
242 );
243
244 return result;
245 }
246}
#define GRAPHLE_FWD(...)
Definition: common.hpp:14
constexpr void strongly_connected_components(G &&graph, Target &&target, std::size_t min_size=0, PV &&stack_provider=store::get_default_storage_provider< store::storage_type::VECTOR, vertex_of< G > >(), PVM &&min_stack_provider=store::get_default_storage_provider< store::storage_type::VECTOR, vertex_of< G > >(), PM &&map_provider=store::get_default_storage_provider< store::storage_type::UNORDERED_MAP, vertex_of< G >, detail::tarjan_vertex_data< G >, vertex_hash_of< G >, vertex_compare_of< G > >())
typename std::remove_cvref_t< G >::vertex_hash_t vertex_hash_of
Definition: graph.hpp:99
typename std::remove_cvref_t< G >::vertex_compare_t vertex_compare_of
Definition: graph.hpp:95
typename std::remove_cvref_t< G >::vertex_type vertex_of
Definition: graph.hpp:91
constexpr auto out_edges(G &&graph, vertex_of< G > vertex)
Definition: edge_utils.hpp:75
decltype(util::out_edges(std::declval< G >(), std::declval< vertex_of< G > >())) out_edge_range_t
decltype(get_default_storage_provider< ST, Args... >()) default_provided_t
constexpr auto get_default_storage_provider(void)
constexpr rng::range_value_t< C > take_back(C &container)
#define GRAPHLE_MULTIPLE
tarjan_vertex_data(std::size_t index, G &graph, vertex_of< G > vertex)
rng::iterator_t< out_edge_range_t< G > > edge_iterator
GetVertices get_vertices
Definition: graph.hpp:78