LAL: Linear Arrangement Library 23.01.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
DMax_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#include <lal/detail/macros/basic_convert.hpp>
65#include <lal/detail/linarr/Dopt_utils.hpp>
66
67namespace lal {
68namespace detail {
69
71namespace DMax_utils {
72
73using namespace Dopt_utils;
74
75/* ************************************************************************** */
76/* ---------------------- INTERVAL-based methods ---------------------------- */
77
78/* The following namespace contains functions for the interval-based algorithms
79 * to calculate the planar and projective maximum sum of edge lengths.
80 */
81
105template <place r_place, bool make_arrangement>
106uint64_t arrange
107(
108 const std::vector<std::vector<node_size>>& L,
109 const node r,
110 position ini, position fin,
112)
113noexcept
114{
115#if defined DEBUG
116 assert(ini <= fin);
117#endif
118
119 if constexpr (make_arrangement) {
120 if constexpr (r_place == PLACE_LEFT_OF) {
121 arr.assign(r, ini);
122 }
123 else {
124 // It is clear that for the case 'r_place == PLACE_RIGHT_OF', we
125 // need the code below. For the case 'r_place == PLACE_NONE_OF', the
126 // code below is an arbitrary choice, but it is in accordance with
127 // the steps of this algorithm.
128 arr.assign(r, fin);
129 }
130 }
131
132 // sizes of the subtrees
133 const auto& children = L[r];
134
135 // accumulated size of the subtrees
136 uint64_t acc_size = 0;
137
138 // sum of the optimal D for every subtree +
139 // the length of the edge from 'r' to its parent (if any)
140 uint64_t D = 0;
141
142 // Auxiliary variables that contain the next
143 // starting position and the next ending position.
144 // Initialized so the compiler does not cry
145 position next_ini = 0, next_fin = 0;
146
147 constexpr place next_place =
148 (r_place == PLACE_LEFT_OF ? PLACE_RIGHT_OF : PLACE_LEFT_OF);
149
150 // while placing the children, calculate the
151 // length of the edge from 'r' to vertex 'vi'
152 for (const auto& [vi, ni] : children) {
153
154 if constexpr (make_arrangement) {
155 if constexpr (r_place == PLACE_LEFT_OF) {
156 next_ini = ini + acc_size + 1;
157 next_fin = next_ini + ni - 1;
158 }
159 else {
160 // It is clear that for the case 'r_place == PLACE_RIGHT_OF', we
161 // need the code below. For the case 'r_place == PLACE_NONE_OF', the
162 // code below is an arbitrary choice, but it is in accordance with
163 // the steps of this algorithm.
164 next_fin = fin - acc_size - 1;
165 next_ini = next_fin - ni + 1;
166 }
167 }
168
169 // recursive call: make the interval of 'vi'
170 D += arrange<next_place, make_arrangement>(L, vi, next_ini, next_fin, arr);
171
172 D += 1 + acc_size;
173 acc_size += ni;
174 }
175
176 if constexpr (r_place != PLACE_NONE_OF) {
177 // accumulate this subtree's anchor
178 D += acc_size;
179 }
180 return D;
181}
182
196inline uint64_t arrange_projective
197(
198 uint64_t n, const std::vector<std::vector<node_size>>& L,
200)
201noexcept
202{
203 return arrange<PLACE_NONE_OF, true>(L, r, 0, n-1, arr);
204}
205
219inline uint64_t arrange_projective
220(uint64_t n, const std::vector<std::vector<node_size>>& L, node r)
221noexcept
222{
224 return arrange<PLACE_NONE_OF, false>(L, r, 0, n-1, arr);
225}
226
227} // -- namespcae DMax_utils
228} // -- namespace detail
229} // -- namespace lal
Linear arrangement of vertices.
Definition: linear_arrangement.hpp:103
uint64_t arrange_projective(uint64_t n, const std::vector< std::vector< node_size > > &L, node r, linear_arrangement &arr) noexcept
Wrapper method for the recursive method arrange.
Definition: DMax_utils.hpp:197
uint64_t arrange(const std::vector< std::vector< node_size > > &L, const node r, position ini, position fin, linear_arrangement &arr) noexcept
Make a maximum projective arrangement using the sorted, rooted adjacency list L.
Definition: DMax_utils.hpp:107
unsigned char place
Useful typedef to denote relative position.
Definition: Dopt_utils.hpp:72
Main namespace of the library.
Definition: basic_types.hpp:50
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