LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
branchless_path.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#include <optional>
46#if defined DEBUG
47#include <cassert>
48#endif
49
50// lal includes
51#include <lal/basic_types.hpp>
52#include <lal/detail/array.hpp>
53#include <lal/graphs/tree.hpp>
54
55namespace lal {
56namespace properties {
57
74public:
75
76 /* MODIFIERS */
77
85 void init(std::size_t n) noexcept {
87 m_h1 = m_h2 = n + 1;
88 m_vertex_sequence.clear();
89 m_vertex_sequence.reserve(n);
90 m_vertex_set.resize(n, 0);
91 m_position.resize(n, n + 1);
92 }
93
99 void add_node(node u) noexcept {
100 m_vertex_set[u] = 1;
101 m_vertex_sequence.push_back(u);
103 }
104
105 /* SETTERS */
106
108 void set_h1(node h) noexcept { m_h1 = h; }
110 void set_h2(node h) noexcept { m_h2 = h; }
113
114 /* GETTERS */
115
117 [[nodiscard]] node operator[](std::size_t i) const noexcept {
118#if defined DEBUG
119 assert(i < m_vertex_sequence.size());
120#endif
121 return m_vertex_sequence[i];
122 }
123
125 [[nodiscard]] node get_h1() const noexcept { return m_h1; }
127 [[nodiscard]] node get_h2() const noexcept { return m_h2; }
128
135 [[nodiscard]] bool has_lowest_lexicographic() const noexcept {
136 return m_lowest_lexicographic.has_value();
137 }
144 [[nodiscard]] node get_lowest_lexicographic() const noexcept {
145#if defined DEBUG
146 assert(has_lowest_lexicographic());
147#endif
149 }
158 [[nodiscard]] const std::vector<node>& get_vertex_sequence() const noexcept
159 { return m_vertex_sequence; }
160
167 [[nodiscard]] std::size_t get_num_nodes() const noexcept
168 { return m_vertex_sequence.size(); }
175 [[nodiscard]] std::size_t get_num_edges() const noexcept
176 { return m_vertex_sequence.size() - 1; }
178 [[nodiscard]] bool has_node(node u) const noexcept { return m_vertex_set[u] == 1; }
180 [[nodiscard]] std::size_t get_position(node u) const noexcept {
181#if defined DEBUG
182 assert(has_node(u));
183#endif
184 return m_position[u];
185 }
186
196 template <class graph_t>
197 [[nodiscard]] bool is_antenna(const graph_t& t) const noexcept {
198 static_assert(std::is_base_of_v<graphs::graph, graph_t>);
199 return t.get_degree(m_h1) == 1 or t.get_degree(m_h2) == 1;
200 }
201
202private:
208 std::vector<node> m_vertex_sequence;
209
219 std::optional<node> m_lowest_lexicographic;
220};
221
222} // -- namespace properties
223} // -- namespace lal
Branchless paths in trees.
Definition branchless_path.hpp:73
void set_h2(node h) noexcept
Sets the second vertex of degree different from 2.
Definition branchless_path.hpp:110
std::vector< node > m_vertex_sequence
The vertex sequence of this branchless path (includes h1 and h2).
Definition branchless_path.hpp:208
std::size_t get_position(node u) const noexcept
Returns the get_position of node u in m_vertex_sequence.
Definition branchless_path.hpp:180
detail::array< std::size_t > m_position
The position in m_vertex_sequence of each vertex.
Definition branchless_path.hpp:206
bool has_node(node u) const noexcept
Does this path include node u?
Definition branchless_path.hpp:178
const std::vector< node > & get_vertex_sequence() const noexcept
Gets the vertex sequence of this path as a vector.
Definition branchless_path.hpp:158
void set_h1(node h) noexcept
Sets the first vertex of degree different from 2.
Definition branchless_path.hpp:108
node m_h1
The first endpoint of this path.
Definition branchless_path.hpp:211
node get_h1() const noexcept
Gets the first vertex of degree different from 2.
Definition branchless_path.hpp:125
void set_lowest_lexicographic(node l) noexcept
Set lowest lexicographic vertex.
Definition branchless_path.hpp:112
void add_node(node u) noexcept
Adds a node to this path.
Definition branchless_path.hpp:99
node get_lowest_lexicographic() const noexcept
Gets the lowest lexicographic vertex.
Definition branchless_path.hpp:144
std::size_t get_num_edges() const noexcept
Number of edges in this path.
Definition branchless_path.hpp:175
std::optional< node > m_lowest_lexicographic
The internal vertex with lowest index lexicographically.
Definition branchless_path.hpp:219
void init(std::size_t n) noexcept
Initializes this path.
Definition branchless_path.hpp:85
bool is_antenna(const graph_t &t) const noexcept
Is the given path an antenna?
Definition branchless_path.hpp:197
std::size_t get_num_nodes() const noexcept
Number of vertices in this path.
Definition branchless_path.hpp:167
node get_h2() const noexcept
Gets the second vertex of degree different from 2.
Definition branchless_path.hpp:127
detail::array< char > m_vertex_set
A 0-1 array to indicate if a vertex belongs to this path or not.
Definition branchless_path.hpp:204
node m_h2
The second endpoint of this path.
Definition branchless_path.hpp:213
bool has_lowest_lexicographic() const noexcept
Does this path have a lowest lexicographic vertex?
Definition branchless_path.hpp:135
node operator[](std::size_t i) const noexcept
Access the i-th node in the path.
Definition branchless_path.hpp:117
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
Wrapper of a C array for automatic deallocation of memory.
Definition array.hpp:59
std::size_t size() const noexcept
Size of the array.
Definition array.hpp:215
void resize(const std::size_t new_size) noexcept
Resize the array.
Definition array.hpp:187