LAL: Linear Arrangement Library 23.01.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
make_arrangement.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 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
34 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office S124, Omega building
37 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
38 * Webpage: https://cqllab.upc.edu/people/rferrericancho/
39 *
40 ********************************************************************/
41
42#pragma once
43
44// C++ includes
45#include <vector>
46
47// lal includes
48#include <lal/linear_arrangement.hpp>
49#include <lal/graphs/rooted_tree.hpp>
50
51namespace lal {
52namespace detail {
53
54// -- for rooted trees
55
65template <class container>
67 const graphs::rooted_tree& T, node r,
68 const container& data,
70)
71noexcept
72{
73 // number of children of 'r' with respect to the tree's root
74 const uint64_t d_out = T.get_out_degree(r);
75
76 // vertex 'r' is a leaf
77 if (d_out == 0) {
78 arr.assign(r, pos++);
79 return;
80 }
81 const auto& interval = data[r];
82 for (std::size_t i = 0; i < interval.size(); ++i) {
83 const node vi = interval[i];
84 if (vi == r) {
85 arr.assign(vi, pos++);
86 }
87 else {
88 make_arrangement_permutations(T, vi, data, pos, arr);
89 }
90 }
91}
92
100template <class container>
102 const graphs::rooted_tree& T,
103 const container& data
104)
105noexcept
106{
107 linear_arrangement arr(T.get_num_nodes());
108 position pos = 0;
109 make_arrangement_permutations(T, T.get_root(), data, pos, arr);
110 return arr;
111}
112
113// -----------------------------------------------------------------------------
114// -- for free trees
115
126template <class container>
128 const graphs::free_tree& T,
129 node parent, node u,
130 const container& data,
132)
133noexcept
134{
135 // number of children of 'u' with respect to the tree's root
136 const uint64_t d_out = T.get_degree(u) - (u == parent ? 0 : 1);
137
138 // vertex 'u' is a leaf in the rooted version of T
139 if (d_out == 0) {
140 arr.assign(u, pos++);
141 return;
142 }
143 const auto& interval = data[u];
144 for (std::size_t i = 0; i < interval.size(); ++i) {
145 const node vi = interval[i];
146 if (vi == u) {
147 arr.assign(vi, pos++);
148 }
149 else {
150 make_arrangement_permutations(T, u, vi, data, pos, arr);
151 }
152 }
153}
154
163template <class container>
165 const graphs::free_tree& T, node root,
166 const container& data
167)
168noexcept
169{
170 linear_arrangement arr(T.get_num_nodes());
171 position pos = 0;
172 make_arrangement_permutations(T, root, root, data, pos, arr);
173 return arr;
174}
175
176} // -- namespace detail
177} // -- namespace lal
Free tree graph class.
Definition: free_tree.hpp:60
Rooted tree graph class.
Definition: rooted_tree.hpp:103
Linear arrangement of vertices.
Definition: linear_arrangement.hpp:103
void make_arrangement_permutations(const graphs::rooted_tree &T, node r, const container &data, position &pos, linear_arrangement &arr) noexcept
Make an arrangement using permutations.
Definition: make_arrangement.hpp:66
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