LAL: Linear Arrangement Library 23.01.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
tree.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#if defined DEBUG
46#include <cassert>
47#endif
48#include <vector>
49#include <array>
50#include <string>
51
52// lal includes
53#include <lal/graphs/graph.hpp>
54#include <lal/graphs/tree_type.hpp>
55
56namespace lal {
57namespace graphs {
58
73class tree : virtual public graph {
74public:
75 /* CONSTRUCTORS */
76
78 tree() noexcept { }
83 tree(const tree& t) noexcept : graph() {
85 }
86
91 tree(tree&& t) noexcept {
92 tree_only_move(std::move(t));
93 }
94
96 virtual ~tree() noexcept { }
97
98 /* OPERATORS */
99
104 tree& operator= (const tree& t) noexcept {
106 return *this;
107 }
112 tree& operator= (tree&& t) noexcept {
113 tree_only_move(std::move(t));
114 return *this;
115 }
116
117 /* MODIFIERS */
118
124 virtual void calculate_tree_type() noexcept = 0;
125
126 /* GETTERS */
127
143 bool is_tree() const noexcept {
144 // NOTE: this would not really be true if the addition of edges
145 // was not constrained. Since it is, in a way that no cycles can
146 // be produced, then we only need to check for the number of edges.
147 return (get_num_nodes() == 0 ? true : get_num_edges() == get_num_nodes() - 1);
148
149 // NOTE 2: this is only true in a debug compilation of the library
150 // since a release compilation does not actually constrain the addition
151 // of edges
152 }
153
155 virtual bool is_rooted() const noexcept = 0;
156
171 virtual bool can_add_edge(node s, node t) const noexcept;
172
186 virtual bool can_add_edges(const std::vector<edge>& edges) const noexcept;
187
203 uint64_t get_component_representative(node u) const noexcept {
204#if defined DEBUG
205 assert(has_node(u));
206#endif
207 return m_root_of[u];
208 }
209
216 bool are_nodes_in_same_component(node u, node v) const noexcept {
218 }
219
232 uint64_t get_num_nodes_component(node u) const noexcept {
233#if defined DEBUG
234 assert(has_node(u));
235#endif
236 return m_root_size[m_root_of[u]];
237 }
238
246 bool is_of_tree_type(const tree_type& tt) const noexcept {
247 return m_tree_type[static_cast<std::size_t>(tt)];
248 }
249
268 bool is_tree_type_valid() const noexcept { return m_is_tree_type_valid; }
269
274 std::vector<std::string> get_tree_type_list() const noexcept;
275
276protected:
278 std::vector<node> m_root_of;
288 std::vector<uint64_t> m_root_size;
289
300
301protected:
306 void tree_only_init(uint64_t n) noexcept {
307 m_root_of = std::vector<uint64_t>(n);
308 m_root_size = std::vector<uint64_t>(n);
309 for (node u = 0; u < n; ++u) {
310 m_root_of[u] = u;
311 m_root_size[u] = 1;
312 }
313 std::fill(m_tree_type.begin(), m_tree_type.end() - 1, false);
314 m_is_tree_type_valid = false;
315 m_tree_type[static_cast<std::size_t>(tree_type::unknown)] = true;
316 }
318 void tree_only_clear() noexcept {
319 m_root_of.clear();
320 m_root_size.clear();
321 std::fill(m_tree_type.begin(), m_tree_type.end() - 1, false);
322 m_is_tree_type_valid = false;
323 m_tree_type[static_cast<std::size_t>(tree_type::unknown)] = true;
324 }
325
327 void tree_only_copy(const tree& t) noexcept {
328 // copy this class' members
329 m_root_of = t.m_root_of;
330 m_root_size = t.m_root_size;
331 m_is_tree_type_valid = t.m_is_tree_type_valid;
332 m_tree_type = t.m_tree_type;
333 }
335 void tree_only_move(tree&& t) noexcept {
336 // move this class' members
337 m_root_of = std::move(t.m_root_of);
338 m_root_size = std::move(t.m_root_size);
339 m_is_tree_type_valid = t.m_is_tree_type_valid;
340 m_tree_type = std::move(t.m_tree_type);
341
342 t.m_is_tree_type_valid = false;
343 }
344
359 void actions_after_add_edge(node u, node v) noexcept;
375
382 void tree_only_set_edges() noexcept;
383
390 void tree_only_remove_node(node u) noexcept;
391
394 void fill_union_find() noexcept {
395 for (node u = 0; u < get_num_nodes(); ++u) {
396 // all vertices point to root zero
397 m_root_of[u] = 0;
398 }
399 // the size of the connected component of the root 0 is n
401 }
402
417 node u, node v,
418 uint64_t * const root_of, uint64_t * const root_size
419 ) noexcept = 0;
434 node u, node v,
435 uint64_t * const root_of, uint64_t * const root_size
436 ) const noexcept = 0;
437
452 node u, node v,
453 uint64_t * const root_of, uint64_t * const root_size
454 ) noexcept = 0;
469 node u, node v,
470 uint64_t * const root_of, uint64_t * const root_size
471 ) const noexcept = 0;
472
487 node u,
488 uint64_t * const root_of, uint64_t * const root_size
489 ) noexcept = 0;
504 node u,
505 uint64_t * const root_of, uint64_t * const root_size
506 ) const noexcept = 0;
507};
508
509} // -- namespace graphs
510} // -- namespace lal
Abstract class for graphs.
Definition: graph.hpp:69
bool has_node(node u) const noexcept
Returns true if node u is in this graph.
Definition: graph.hpp:192
uint64_t get_num_edges() const noexcept
Returns the number of edges.
Definition: graph.hpp:202
uint64_t get_num_nodes() const noexcept
Returns the number of ndoes.
Definition: graph.hpp:198
graph() noexcept
Empty constructor.
Definition: graph.hpp:74
Tree graph class.
Definition: tree.hpp:73
tree & operator=(const tree &t) noexcept
Copy assignment operator.
Definition: tree.hpp:104
void actions_after_remove_node(node u) noexcept
Do some work before an node is removed.
bool is_of_tree_type(const tree_type &tt) const noexcept
Returns whether this tree is of type tt.
Definition: tree.hpp:246
tree(const tree &t) noexcept
Copy constructor.
Definition: tree.hpp:83
virtual bool is_rooted() const noexcept=0
Returns whether this tree is a rooted tree.
bool is_tree() const noexcept
Is this graph is an actual tree?
Definition: tree.hpp:143
virtual void update_union_find_before_incident_edges_removed(node u, uint64_t *const root_of, uint64_t *const root_size) noexcept=0
Update the union find data structure before the removal of all edges incident to a node.
tree() noexcept
Empty constructor.
Definition: tree.hpp:78
virtual void update_union_find_after_edge_add(node u, node v, uint64_t *const root_of, uint64_t *const root_size) const noexcept=0
Update the union find data structure after an edge addition.
uint64_t get_component_representative(node u) const noexcept
Representative node of the connected component in which u belongs.
Definition: tree.hpp:203
void tree_only_copy(const tree &t) noexcept
Copies only members of class tree.
Definition: tree.hpp:327
void actions_after_add_edge(node u, node v) noexcept
Do some work after the addition of an edge.
void fill_union_find() noexcept
Definition: tree.hpp:394
void tree_only_move(tree &&t) noexcept
Moves only members of class tree.
Definition: tree.hpp:335
virtual ~tree() noexcept
Destructor.
Definition: tree.hpp:96
std::vector< node > m_root_of
The root of every vertex in the union-find data structure.
Definition: tree.hpp:278
virtual void update_union_find_after_edge_add(node u, node v, uint64_t *const root_of, uint64_t *const root_size) noexcept=0
Update the union find data structure after an edge addition.
uint64_t get_num_nodes_component(node u) const noexcept
Amount of nodes in a connected component of the tree.
Definition: tree.hpp:232
void actions_after_remove_edge(node u, node v) noexcept
Do some work after the removal of an edge.
virtual bool can_add_edges(const std::vector< edge > &edges) const noexcept
Can these edges be added?
void tree_only_init(uint64_t n) noexcept
Initialises only the memory of class tree.
Definition: tree.hpp:306
void actions_before_remove_edges_incident_to(node u) noexcept
Do some work before all edges incident to a node is removed.
bool m_is_tree_type_valid
Is the type of this tree valid?
Definition: tree.hpp:299
virtual bool can_add_edge(node s, node t) const noexcept
Can this edge be added?
bool is_tree_type_valid() const noexcept
Is the type of this tree valid?
Definition: tree.hpp:268
virtual void update_union_find_before_incident_edges_removed(node u, uint64_t *const root_of, uint64_t *const root_size) const noexcept=0
Update the union find data structure before the removal of all edges incident to a node.
virtual void calculate_tree_type() noexcept=0
Calculates the type of tree.
virtual void update_union_find_after_edge_remove(node u, node v, uint64_t *const root_of, uint64_t *const root_size) const noexcept=0
Update the union find data structure after an edge removal.
bool are_nodes_in_same_component(node u, node v) const noexcept
Checks if two nodes are in the same connected component.
Definition: tree.hpp:216
void tree_only_remove_node(node u) noexcept
Removes a vertex from the union-find data structure.
std::array< bool, __tree_type_size > m_tree_type
The type of this tree.
Definition: tree.hpp:291
tree(tree &&t) noexcept
Move constructor.
Definition: tree.hpp:91
std::vector< uint64_t > m_root_size
The size of the connected component that a root belongs to.
Definition: tree.hpp:288
void tree_only_clear() noexcept
Clears the memory used by only class tree.
Definition: tree.hpp:318
std::vector< std::string > get_tree_type_list() const noexcept
Returns the list of types as a list of strings.
virtual void update_union_find_after_edge_remove(node u, node v, uint64_t *const root_of, uint64_t *const root_size) noexcept=0
Update the union find data structure after an edge removal.
void tree_only_set_edges() noexcept
Updates the data structures of a tree after the graph structure has had its set of edges set.
constexpr std::size_t __tree_type_size
Number of elements within enumeration lal::graphs::tree_type.
Definition: tree_type.hpp:92
tree_type
Enumeration of several types of trees.
Definition: tree_type.hpp:57
@ unknown
The tree could not be classified.
Main namespace of the library.
Definition: basic_types.hpp:50
std::pair< node, node > edge
See Edge page for further details.
Definition: basic_types.hpp:58
uint64_t node
Node type. See Node / Vertex page for further details.
Definition: basic_types.hpp:53