LAL: Linear Arrangement Library 21.07.01
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 - 2021
7 *
8 * This file is part of Linear Arrangement Library. To see the full code
9 * visit the webpage:
10 * https://github.com/lluisalemanypuig/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/graphs/rooted_tree.hpp>
49
50namespace lal {
51namespace internal {
52
53namespace __lal {
54inline void __make_arrangement_intervals(
55 const graphs::rooted_tree& T, node r,
56 const std::vector<std::vector<lal::node>>& data,
58)
59noexcept
60{
61 // number of children of 'r' with respect to the tree's root
62 const uint32_t d_out = T.get_out_degree(r);
63
64 // vertex 'r' is a leaf
65 if (d_out == 0) {
66 arr[r] = pos++;
67 return;
68 }
69 const std::vector<lal::node>& interval = data[r];
70 for (size_t i = 0; i < interval.size(); ++i) {
71 const node vi = interval[i];
72 if (vi == r) {
73 arr[vi] = pos++;
74 }
75 else {
76 __make_arrangement_intervals(T, vi, data, pos, arr);
77 }
78 }
79}
80
81inline void __make_arrangement_intervals(
82 const graphs::free_tree& T, node parent, node u,
83 const std::vector<std::vector<lal::node>>& data,
85)
86noexcept
87{
88 // number of children of 'u' with respect to the tree's root
89 const uint32_t d_out = T.get_degree(u) - (u == parent ? 0 : 1);
90
91 // vertex 'u' is a leaf
92 if (d_out == 0) {
93 arr[u] = pos++;
94 return;
95 }
96 const std::vector<lal::node>& interval = data[u];
97 for (size_t i = 0; i < interval.size(); ++i) {
98 const node vi = interval[i];
99 if (vi == u) {
100 arr[vi] = pos++;
101 }
102 else {
103 __make_arrangement_intervals(T, u, vi, data, pos, arr);
104 }
105 }
106}
107} // -- namespace __lal
108
109inline linear_arrangement make_arrangement_intervals(
110 const graphs::rooted_tree& T,
111 const std::vector<std::vector<lal::node>>& data
112)
113noexcept
114{
115 linear_arrangement arr(T.get_num_nodes());
116 position pos = 0;
117 __lal::__make_arrangement_intervals(T, T.get_root(), data, pos, arr);
118 return arr;
119}
120
121inline linear_arrangement make_arrangement_intervals(
122 const graphs::free_tree& T, node root,
123 const std::vector<std::vector<lal::node>>& data
124)
125noexcept
126{
127 linear_arrangement arr(T.get_num_nodes());
128 position pos = 0;
129 __lal::__make_arrangement_intervals(T, root, root, data, pos, arr);
130 return arr;
131}
132
133} // -- namespace internal
134} // -- namespace lal
Main namespace of the library.
Definition definitions.hpp:48
uint32_t position
Node's position type.
Definition definitions.hpp:53
uint32_t node
Node type.
Definition definitions.hpp:51
std::vector< position > linear_arrangement
A linear arrangement of the nodes of a graph.
Definition definitions.hpp:72