LAL: Linear Arrangement Library 23.01.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
Dopt_utils.hpp
1/*********************************************************************
2 *
3 * Linear Arrangement Library - A library that implements a collection
4 * algorithms for linear arrangments of graphs.
5 *
6 * Copyright (C) 2019 - 2023
7 *
8 * This file is part of Linear Arrangement Library. The full code is available
9 * at:
10 * https://github.com/LAL-project/linear-arrangement-library.git
11 *
12 * Linear Arrangement Library is free software: you can redistribute it
13 * and/or modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * Linear Arrangement Library is distributed in the hope that it will be
18 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with Linear Arrangement Library. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * Contact:
26 *
27 * LluĂ­s Alemany Puig (lalemany@cs.upc.edu)
28 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
29 * CQL (Complexity and Quantitative Linguistics Lab)
30 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
31 * Webpage: https://cqllab.upc.edu/people/lalemany/
32 *
33 * Juan Luis Esteban (esteban@cs.upc.edu)
34 * LOGPROG: Logics and Programming Research Group
35 * Office 110, Omega building
36 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
37 * Webpage: https://www.cs.upc.edu/~esteban/
38 *
39 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
40 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
41 * CQL (Complexity and Quantitative Linguistics Lab)
42 * Office S124, Omega building
43 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
44 * Webpage: https://cqllab.upc.edu/people/rferrericancho/
45 *
46 ********************************************************************/
47
48#pragma once
49
50// C++ includes
51#if defined DEBUG
52#include <cassert>
53#endif
54#include <vector>
55
56// lal includes
57#include <lal/linear_arrangement.hpp>
58#include <lal/graphs/rooted_tree.hpp>
59#include <lal/detail/data_array.hpp>
60#include <lal/iterators/E_iterator.hpp>
61#include <lal/detail/graphs/size_subtrees.hpp>
62#include <lal/detail/sorting/counting_sort.hpp>
63#include <lal/detail/properties/tree_centroid.hpp>
64
65namespace lal {
66namespace detail {
67
69namespace Dopt_utils {
70
72typedef unsigned char place;
74typedef unsigned char side;
75
76inline constexpr place PLACE_LEFT_OF = 0;
77inline constexpr place PLACE_RIGHT_OF = 1;
78inline constexpr place PLACE_NONE_OF = 2;
79
80inline constexpr side RIGHT_SIDE = 0;
81inline constexpr side LEFT_SIDE = 1;
82
83// if s = 0 then (s+1)&0x1 = 1
84// if s = 1 then (s+1)&0x1 = 0
85inline constexpr side other_side(side s) noexcept { return ((s + 1)&0x1); }
86
88inline constexpr char LEFT_ANCHOR = -1;
90inline constexpr char RIGHT_ANCHOR = 1;
92inline constexpr char NO_ANCHOR = 0;
94inline constexpr char ANCHOR = 1;
95
96/* ************************************************************************** */
97/* ----------------------- ROOTED ADJACENCY LISTS --------------------------- */
98
99/* Functions to calculate the sorted, rooted
100 * adjacency list of rooted and free trees.
101 */
102
114template <typename sort_type>
116 const graphs::rooted_tree& t,
117 std::vector<std::vector<node_size>>& L
118)
119noexcept
120{
121 const uint64_t n = t.get_num_nodes();
122 const node r = t.get_root();
123
124 // for every edge (u,v), store the tuple
125 // (n_v, (u,v))
126 // at L[u]
127 data_array<edge_size> edge_list(n - 1);
128
129 {
130 const std::size_t k = t.are_size_subtrees_valid() ? 0 : t.get_num_nodes();
131 data_array<uint64_t> size_subtrees(k, 0);
132
134 auto it = edge_list.begin();
135
137 if (t.are_size_subtrees_valid()) {
138 // use the sizes that are already calculated
139 while (not E_it.end()) {
140 const edge e = E_it.get_edge();
141 const node v = e.second;
142 const uint64_t suv = t.get_num_nodes_subtree(v);
143 *it++ = {e, suv};
144 ++memcs.count[suv];
145
146 E_it.next();
147 }
148 }
149 else {
150 // fill in the size of the subtrees
151 detail::get_size_subtrees(t, r, size_subtrees.begin());
152 while (not E_it.end()) {
153 const edge e = E_it.get_edge();
154 const node v = e.second;
155 const uint64_t suv = size_subtrees[v];
156 *it++ = {e, suv};
157 ++memcs.count[suv];
158
159 E_it.next();
160 }
161 }
162
163 // sort all tuples in L using the size of the subtree
165 <edge_size, sort_type, true>
166 (
167 edge_list.begin(), edge_list.end(), n,
168 [](const edge_size& T) -> std::size_t { return T.size; },
169 memcs
170 );
171 }
172
173 // M[u] : adjacency list of vertex u sorted decreasingly according
174 // to the sizes of the subtrees.
175 // This is used to find the optimal projective arrangement of the tree.
176 for (const auto& T : edge_list) {
177 const auto [u, v] = T.e;
178 const uint64_t nv = T.size;
179 L[u].push_back({v,nv});
180#if defined DEBUG
181 assert(t.has_edge(u,v));
182#endif
183 }
184
185#if defined DEBUG
186 for (node u = 0; u < n; ++u) {
187 assert(L[u].size() == t.get_out_degree(u));
188 }
189#endif
190}
191
192} // -- namespcae Dopt_utils
193} // -- namespace detail
194} // -- namespace lal
Rooted tree graph class.
Definition: rooted_tree.hpp:103
Iterator over the set of edges of a graph.
Definition: E_iterator.hpp:97
void next() noexcept
Moves the iterator to the next edge.
Definition: E_iterator.hpp:142
bool end() const noexcept
Returns true if the end of the iteration was reached.
Definition: E_iterator.hpp:117
const edge & get_edge() const noexcept
Returns the current edge.
Definition: E_iterator.hpp:120
unsigned char side
Useful typedef to denote relative position.
Definition: Dopt_utils.hpp:74
constexpr char NO_ANCHOR
The tree is not anchored.
Definition: Dopt_utils.hpp:92
constexpr char ANCHOR
The tree is anchored.
Definition: Dopt_utils.hpp:94
void make_sorted_adjacency_list_rooted(const graphs::rooted_tree &t, std::vector< std::vector< node_size > > &L) noexcept
Make a sorted, rooted adjacency list sorted according to the sizes of the subtrees of the input roote...
Definition: Dopt_utils.hpp:115
constexpr char RIGHT_ANCHOR
The tree is right-anchored.
Definition: Dopt_utils.hpp:90
constexpr char LEFT_ANCHOR
The tree is left-anchored.
Definition: Dopt_utils.hpp:88
unsigned char place
Useful typedef to denote relative position.
Definition: Dopt_utils.hpp:72
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
void get_size_subtrees(const tree_t &t, const node u, const node v, uint64_t *const sizes) noexcept
Calculate the size of every subtree of the tree t.
Definition: size_subtrees.hpp:74
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 node
Node type. See Node / Vertex page for further details.
Definition: basic_types.hpp:53
Wrapper of a C array for autmatic deallocation of memory.
Definition: data_array.hpp:59
T * end() noexcept
Non-constant raw pointer to last+1 element.
Definition: data_array.hpp:293
T * begin() noexcept
Non-constant raw pointer to first element.
Definition: data_array.hpp:291
Struct used in many algorithms to sort edges according to some integer value.
Definition: pairs_utils.hpp:65
edge e
Edege.
Definition: pairs_utils.hpp:67
Memory used for the counting sort algorithm.
Definition: counting_sort.hpp:72
data_array< std::size_t > count
Amount of times the key of an element occurs.
Definition: counting_sort.hpp:74