LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
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 - 2024
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 (lluis.alemany.puig@upc.edu)
28 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
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 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
41 * CQL (Complexity and Quantitative Linguistics Lab)
42 * Office 220, 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/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#include <lal/detail/macros/basic_convert.hpp>
65#include <lal/detail/linarr/D/Dopt_utils.hpp>
66
67namespace lal {
68namespace detail {
69
71namespace DMax_utils {
72
73/* ************************************************************************** */
74/* ---------------------- INTERVAL-based methods ---------------------------- */
75
76/* The following namespace contains functions for the interval-based algorithms
77 * to calculate the planar and projective maximum sum of edge lengths.
78 */
79
103template <Dopt_utils::place r_place, bool make_arrangement>
104[[nodiscard]] uint64_t arrange
105(
106 const std::vector<std::vector<node_size>>& L,
107 const node r,
108 const position ini,
109 const position fin,
111)
112noexcept
113{
114#if defined DEBUG
115 assert(ini <= fin);
116#endif
117
118 if constexpr (make_arrangement) {
119 if constexpr (r_place == Dopt_utils::PLACE_LEFT_OF) {
120 arr.assign(r, ini);
121 }
122 else {
123 // It is clear that for the case 'r_place == PLACE_RIGHT_OF', we
124 // need the code below. For the case 'r_place == PLACE_NONE_OF', the
125 // code below is an arbitrary choice, but it is in accordance with
126 // the steps of this algorithm.
127 arr.assign(r, fin);
128 }
129 }
130
131 // sizes of the subtrees
132 const auto& children = L[r];
133
134 // accumulated size of the subtrees
135 uint64_t acc_size = 0;
136
137 // sum of the optimal D for every subtree +
138 // the length of the edge from 'r' to its parent (if any)
139 uint64_t D = 0;
140
141 // Auxiliary variables that contain the next
142 // starting position and the next ending position.
143 // Initialized so the compiler does not cry
144 position next_ini = 0, next_fin = 0;
145
146 constexpr Dopt_utils::place next_place =
147 r_place == Dopt_utils::PLACE_LEFT_OF ?
150
151 // while placing the children, calculate the
152 // length of the edge from 'r' to vertex 'vi'
153 for (const auto& [vi, ni] : children) {
154
155 if constexpr (make_arrangement) {
156 if constexpr (r_place == Dopt_utils::PLACE_LEFT_OF) {
157 next_ini = ini + acc_size + 1;
158 next_fin = next_ini + ni - 1;
159 }
160 else {
161 // It is clear that for the case 'r_place == PLACE_RIGHT_OF', we
162 // need the code below. For the case 'r_place == PLACE_NONE_OF', the
163 // code below is an arbitrary choice, but it is in accordance with
164 // the steps of this algorithm.
165 next_fin = fin - acc_size - 1;
166 next_ini = next_fin - ni + 1;
167 }
168 }
169
170 // recursive call: make the interval of 'vi'
171 D += arrange<next_place, make_arrangement>(L, vi, next_ini, next_fin, arr);
172
173 D += 1 + acc_size;
174 acc_size += ni;
175 }
176
177 if constexpr (r_place != Dopt_utils::PLACE_NONE_OF) {
178 // accumulate this subtree's anchor
179 D += acc_size;
180 }
181 return D;
182}
183
198template <bool make_arrangement>
199[[nodiscard]] inline uint64_t arrange_projective
200(
201 const uint64_t n,
202 const std::vector<std::vector<node_size>>& L,
203 const node r,
205)
206noexcept
207{
209}
210
211} // -- namespcae DMax_utils
212} // -- namespace detail
213} // -- namespace lal
Linear arrangement of vertices.
Definition linear_arrangement.hpp:103
uint64_t arrange(const std::vector< std::vector< node_size > > &L, const node r, const position ini, const position fin, linear_arrangement &arr) noexcept
Make a maximum projective arrangement using the sorted, rooted adjacency list L.
Definition utils.hpp:105
uint64_t arrange_projective(const uint64_t n, const std::vector< std::vector< node_size > > &L, const node r, linear_arrangement &arr) noexcept
Wrapper method for the recursive method arrange.
Definition utils.hpp:200
static constexpr place PLACE_RIGHT_OF
A vertex is to be placed to the right of a vertex.
Definition Dopt_utils.hpp:79
static constexpr place PLACE_LEFT_OF
A vertex is to be placed to the left of a vertex.
Definition Dopt_utils.hpp:77
static constexpr place PLACE_NONE_OF
There is no vertex to use as reference to determine the side.
Definition Dopt_utils.hpp:81
unsigned char place
Useful typedef to denote relative position.
Definition Dopt_utils.hpp:72
Main namespace of the library.
Definition basic_types.hpp:48
uint64_t position
Node's position type.
Definition basic_types.hpp:53
uint64_t node
Node type. See Node / Vertex page for further details.
Definition basic_types.hpp:51