LAL: Linear Arrangement Library 24.10.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 - 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 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
34 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office 220, 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// lal includes
45#include <lal/linear_arrangement.hpp>
46#include <lal/graphs/rooted_tree.hpp>
47
48namespace lal {
49namespace detail {
50
51// -- for rooted trees
52
62template <class container>
64 const graphs::rooted_tree& T,
65 const node r,
66 const container& data,
67 position& pos,
69)
70noexcept
71{
72 // number of children of 'r' with respect to the tree's root
73 const uint64_t d_out = T.get_out_degree(r);
74
75 // vertex 'r' is a leaf
76 if (d_out == 0) {
77 arr.assign(r, pos++);
78 return;
79 }
80 const auto& interval = data[r];
81 for (std::size_t i = 0; i < interval.size(); ++i) {
82 const node vi = interval[i];
83 if (vi == r) {
84 arr.assign(vi, pos++);
85 }
86 else {
87 make_arrangement_permutations(T, vi, data, pos, arr);
88 }
89 }
90}
91
99template <class container>
101(const graphs::rooted_tree& T, const container& data)
102noexcept
103{
104 linear_arrangement arr(T.get_num_nodes());
105 position pos = 0;
106 make_arrangement_permutations(T, T.get_root(), data, pos, arr);
107 return arr;
108}
109
110// -----------------------------------------------------------------------------
111// -- for free trees
112
123template <class container>
125 const graphs::free_tree& T,
126 node parent, node u,
127 const container& data,
129)
130noexcept
131{
132 // number of children of 'u' with respect to the tree's root
133 const uint64_t d_out = T.get_degree(u) - (u == parent ? 0 : 1);
134
135 // vertex 'u' is a leaf in the rooted version of T
136 if (d_out == 0) {
137 arr.assign(u, pos++);
138 return;
139 }
140 const auto& interval = data[u];
141 for (std::size_t i = 0; i < interval.size(); ++i) {
142 const node vi = interval[i];
143 if (vi == u) {
144 arr.assign(vi, pos++);
145 }
146 else {
147 make_arrangement_permutations(T, u, vi, data, pos, arr);
148 }
149 }
150}
151
160template <class container>
162(const graphs::free_tree& T, node root, const container& data)
163noexcept
164{
165 linear_arrangement arr(T.get_num_nodes());
166 position pos = 0;
167 make_arrangement_permutations(T, root, root, data, pos, arr);
168 return arr;
169}
170
171} // -- namespace detail
172} // -- namespace lal
Free tree graph class.
Definition free_tree.hpp:60
Rooted tree graph class.
Definition rooted_tree.hpp:109
Linear arrangement of vertices.
Definition linear_arrangement.hpp:103
void assign(const NODE u, const POSITION p) noexcept
Assigns a node u to position p.
Definition linear_arrangement.hpp:379
void make_arrangement_permutations(const graphs::rooted_tree &T, const node r, const container &data, position &pos, linear_arrangement &arr) noexcept
Make an arrangement using permutations.
Definition make_arrangement.hpp:63
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