51#include <lal/graphs/free_tree.hpp>
52#include <lal/iterators/E_iterator.hpp>
53#include <lal/detail/identity_arrangement.hpp>
54#include <lal/detail/sorting/counting_sort.hpp>
55#include <lal/detail/sorting/sorted_vector.hpp>
56#include <lal/detail/data_array.hpp>
58#define max_pos(u,v) (std::max(arr[u], arr[v]))
74template <
class depflux,
class arrangement_t>
78 const arrangement_t& arr,
79 const std::vector<std::pair<edge_t,uint64_t>>& edge_with_max_pos_at,
81 std::vector<depflux>& flux,
82 std::vector<edge>& cur_deps
90 cur_deps = flux[cur_pos - 1].get_dependencies();
94 if (edge_with_max_pos_at[cur_pos].second > 0) {
95 const auto [first, last] =
97 cur_deps.begin(), cur_deps.end(),
98 edge_with_max_pos_at[cur_pos].first,
100 const auto pos_e1 = max_pos(e1.first, e1.second);
101 const auto pos_e2 = max_pos(e2.first, e2.second);
102 return pos_e1 < pos_e2;
105 if (first != cur_deps.end()) {
106 cur_deps.erase(first, last);
111 for (
const node_t v : t.get_neighbours(u)) {
112 if (arr[v] > cur_pos) {
113 cur_deps.push_back({u,*v});
118 for (
const auto& [v,w] : cur_deps) {
122 for (
node_t v : set_endpoints) {
123 flux[cur_pos].get_left_span() += (arr[v] <= cur_pos);
124 flux[cur_pos].get_right_span() += (arr[v] > cur_pos);
138 if (dependencies.size() <= 1) {
return dependencies.size(); }
141 ug.set_edges(dependencies);
149 const auto find_leaf =
151 for (
node u = 0; u < g.get_num_nodes(); ++u) {
152 if (g.get_degree(u) == 1) {
return u; }
159 while (
const auto leaf = find_leaf(ug)) {
160 const node u = *leaf;
161 const node v = ug.get_neighbours(u)[0];
165 ug.remove_edges_incident_to(v);
178template <
class depflux,
class arrangement_t>
179std::vector<depflux> compute_flux
183 const uint64_t n = t.get_num_nodes();
184 if (n == 1) {
return {}; }
187 std::vector<std::pair<edge_t,uint64_t>> edge_with_max_pos_at(n, {{}, 0});
188 for (iterators::E_iterator e_it(t); not e_it.end(); e_it.next()) {
189 const auto [u,v] = e_it.get_edge_t();
191 edge_with_max_pos_at[max].first = {u,v};
192 ++edge_with_max_pos_at[max].second;
196 graphs::undirected_graph ug(n);
199 sorting::countingsort::memory<edge> mem(n, n);
202 std::vector<depflux> flux(n - 1);
204 for (
position cur_pos = 0; cur_pos < n - 1; ++cur_pos) {
206 auto& cur_deps = flux[cur_pos].get_dependencies();
210 calculate_dependencies_and_span<depflux>
211 (t, arr, edge_with_max_pos_at, cur_pos, flux, cur_deps);
221 <
edge, sorting::non_decreasing_t,
false>
224 cur_deps.begin(), cur_deps.end(),
228 [&](
const edge_t& e) -> std::size_t
229 {
return max_pos(e.first, e.second); },
Sorted vector class.
Definition: sorted_vector.hpp:65
iterator_t insert_sorted(const T &x) noexcept
Insert an element to the vector.
Definition: sorted_vector.hpp:94
Free tree graph class.
Definition: free_tree.hpp:60
Undirected graph class.
Definition: undirected_graph.hpp:67
void counting_sort(const value_iterator_t begin, const value_iterator_t end, const std::size_t largest_key_plus_1, const std::function< std::size_t(const value_t &)> &key, countingsort::memory< value_t > &mem) noexcept
Counting sort algorithm with reusable memory.
Definition: counting_sort.hpp:155
uint64_t calculate_weight(const std::vector< edge > &dependencies, graphs::undirected_graph &ug) noexcept
Calculates the weight of a set of dependencies in a flux.
Definition: dependency_flux.hpp:135
void calculate_dependencies_and_span(const graphs::free_tree &t, const arrangement_t &arr, const std::vector< std::pair< edge_t, uint64_t > > &edge_with_max_pos_at, position cur_pos, std::vector< depflux > &flux, std::vector< edge > &cur_deps) noexcept
Calculate the dependencies and their span.
Definition: dependency_flux.hpp:76
Main namespace of the library.
Definition: basic_types.hpp:50
std::pair< node, node > edge
See Edge page for further details.
Definition: basic_types.hpp:58
uint64_t position
Node's position type.
Definition: basic_types.hpp:55
uint64_t node
Node type. See Node / Vertex page for further details.
Definition: basic_types.hpp:53
std::pair< node_t, node_t > edge_t
Similar to edge.
Definition: basic_types.hpp:163
Typesafe node type.
Definition: basic_types.hpp:67
Typesafe position type.
Definition: basic_types.hpp:168