LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
branchless_paths_compute.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// C++ includes
45#if defined DEBUG
46#include <cassert>
47#endif
48
49// lal includes
50#include <lal/basic_types.hpp>
51#include <lal/graphs/free_tree.hpp>
52#include <lal/graphs/rooted_tree.hpp>
53#include <lal/properties/branchless_path.hpp>
54#include <lal/detail/array.hpp>
55#include <lal/detail/graphs/traversal.hpp>
56
57namespace lal {
58namespace detail {
59
73template <class tree_t>
75(
76 const tree_t& t,
77 const node u, const node v,
78 BFS<tree_t>& bfs,
79 std::vector<properties::branchless_path>& res,
81)
82noexcept
83{
84 const uint64_t n = t.get_num_nodes();
85
86 // the case of an edge...
87 if (t.get_degree(u) != 2 and t.get_degree(v) != 2) {
88
89 // avoid symmetric paths
90 if (u > v) { return; }
91
92#if defined DEBUG
93 assert(t.has_edge(u, v) or t.has_edge(v, u));
94#endif
95 // initialize the path
96 p.init(n);
97 // set and add the first and second non-internal vertices
98 p.add_node(u);
99 p.set_h1(u);
100 p.add_node(v);
101 p.set_h2(v);
102 // push the new path
103 res.push_back(std::move(p));
104 return;
105 }
106
107#if defined DEBUG
108 assert(not bfs.node_was_visited(u));
109#endif
110
111 if (bfs.node_was_visited(v)) { return; }
112
113 // initialize the path
114 p.init(n);
115 // set the first non-internal vertex and add it
116 p.add_node(u);
117 p.set_h1(u);
118
119 // set 'u' as visited to avoid going 'back' in the tree
120 bfs.set_visited(u, 1);
121
122 // expand the new path
123 bfs.start_at(v);
124
125 // find the lowest *internal* vertex in lexicographic order
126 const auto& seq = p.get_vertex_sequence();
127 node lowest_lexicographic = n + 1;
128 for (std::size_t i = 1; i < seq.size() - 1; ++i) {
129 lowest_lexicographic = std::min(lowest_lexicographic, seq[i]);
130 }
131 p.set_lowest_lexicographic(lowest_lexicographic);
132
133 // only let the internal vertices of the paths be visited
134 bfs.set_visited(p.get_h1(), 0);
135 bfs.set_visited(p.get_h2(), 0);
136
137 // push the new path
138 res.push_back(std::move(p));
139}
140
147template <class tree_t>
148[[nodiscard]] std::vector<properties::branchless_path>
150noexcept
151{
152 const uint64_t n = t.get_num_nodes();
153
154 // result of the function (to be returned)
155 std::vector<properties::branchless_path> res;
156
157 // path to be filled
159
160 BFS bfs(t);
161
162 // detect the last hub
163 bfs.set_process_current(
164 [&](const auto&, node u) {
165 p.add_node(u);
166 if (t.get_degree(u) != 2) {
167 // The exploration will stop in the
168 // next call to the 'terminate' function.
169 p.set_h2(u);
170 }
171 }
172 );
173 // stop the traversal as soon as we find a vertex of degree != 2
174 bfs.set_terminate(
175 [&](const auto&, node u) { return t.get_degree(u) != 2; }
176 );
177
178 // find all paths starting at vertices of degree != 2
179 for (node u = 0; u < n; ++u) {
180 if (t.get_degree(u) == 2) { continue; }
181
182 if constexpr (std::is_base_of_v<graphs::free_tree, tree_t>) {
183 for (node v : t.get_neighbors(u)) {
184 expand_branchless_path(t, u, v, bfs, res, p);
185 }
186 }
187 else if constexpr (std::is_base_of_v<graphs::rooted_tree, tree_t>) {
188 for (node v : t.get_out_neighbors(u)) {
189 expand_branchless_path(t, u, v, bfs, res, p);
190 }
191 for (node v : t.get_in_neighbors(u)) {
192 expand_branchless_path(t, u, v, bfs, res, p);
193 }
194 }
195 }
196
197 return res;
198}
199
200} // -- namespace detail
201} // -- namespace lal
Abstract graph Breadth-First Search traversal.
Definition traversal.hpp:89
Branchless paths in trees.
Definition branchless_path.hpp:73
void add_node(node u) noexcept
Adds a node to this path.
Definition branchless_path.hpp:99
void expand_branchless_path(const tree_t &t, const node u, const node v, BFS< tree_t > &bfs, std::vector< properties::branchless_path > &res, properties::branchless_path &p) noexcept
Completes the branchless path.
Definition branchless_paths_compute.hpp:75
std::vector< properties::branchless_path > branchless_paths_compute(const tree_t &t) noexcept
Finds all branchless paths in a tree.
Definition branchless_paths_compute.hpp:149
Main namespace of the library.
Definition basic_types.hpp:48
uint64_t node
Node type. See Node / Vertex page for further details.
Definition basic_types.hpp:51