LAL: Linear Arrangement Library 21.07.01
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 - 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#if defined DEBUG
46#include <cassert>
47#endif
48#include <string>
49#include <vector>
50#include <array>
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#ifndef SWIG
91 tree(tree&& t) noexcept {
92 tree_only_move(std::move(t));
93 }
94#endif
96 virtual ~tree() noexcept { }
97
98 /* OPERATORS */
99
100#ifndef SWIG
105 tree& operator= (const tree& t) noexcept {
107 return *this;
108 }
113 tree& operator= (tree&& t) noexcept {
114 tree_only_move(std::move(t));
115 return *this;
116 }
117#endif
118
119 /* MODIFIERS */
120
126 virtual void calculate_tree_type() noexcept = 0;
127
128 /* GETTERS */
129
145 bool is_tree() const noexcept {
146 // NOTE: this would not really be true if the addition of edges
147 // was not constrained. Since it is, in a way that no cycles can
148 // be produced, then we only need to check for the number of edges.
149 return (get_num_nodes() == 0 ? true : get_num_edges() == get_num_nodes() - 1);
150
151 // NOTE 2: this is only true in a debug compilation of the library
152 // since a release compilation does not actually constrain the addition
153 // of edges
154 }
155
157 virtual bool is_rooted() const noexcept = 0;
158
169 bool can_add_edge(node s, node t) const noexcept;
170
180 bool can_add_edges(const std::vector<edge>& edges) const noexcept;
181
195 inline uint32_t get_num_nodes_component(node u) const noexcept {
196#if defined DEBUG
197 assert(has_node(u));
198#endif
199 return m_root_size[m_root_of[u]];
200 }
201
209 inline bool is_of_tree_type(const tree_type& tt) const noexcept {
210 return m_tree_type[static_cast<std::size_t>(tt)];
211 }
212
231 inline bool is_tree_type_valid() const noexcept { return m_is_tree_type_valid; }
232
237 std::vector<std::string> get_tree_type_list() const noexcept;
238
239protected:
241 std::vector<node> m_root_of;
251 std::vector<uint32_t> m_root_size;
252
263
264protected:
269 void tree_only_init(uint32_t n) noexcept;
271 void tree_only_clear() noexcept;
272
274 void tree_only_copy(const tree& t) noexcept;
276 void tree_only_move(tree&& t) noexcept;
277
278 void extra_work_per_edge_add(node u, node v) noexcept;
280
284
287 void fill_union_find() noexcept;
288
303 node u, node v,
304 uint32_t * const root_of, uint32_t * const root_size
305 ) noexcept = 0;
320 node u, node v,
321 uint32_t * const root_of, uint32_t * const root_size
322 ) const noexcept = 0;
337 node u, node v,
338 uint32_t * const root_of, uint32_t * const root_size
339 ) noexcept = 0;
354 node u, node v,
355 uint32_t * const root_of, uint32_t * const root_size
356 ) const noexcept = 0;
357};
358
359} // -- namespace graphs
360} // -- 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:188
graph() noexcept
Empty constructor.
Definition graph.hpp:74
uint32_t get_num_nodes() const noexcept
Returns the number of ndoes.
Definition graph.hpp:194
uint32_t get_num_edges() const noexcept
Returns the number of edges.
Definition graph.hpp:198
Tree graph class.
Definition tree.hpp:73
tree & operator=(const tree &t) noexcept
Copy assignment operator.
Definition tree.hpp:105
bool is_of_tree_type(const tree_type &tt) const noexcept
Returns whether this tree is of type tt.
Definition tree.hpp:209
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:145
tree() noexcept
Empty constructor.
Definition tree.hpp:78
bool can_add_edge(node s, node t) const noexcept
Can this edge be added?
void tree_only_copy(const tree &t) noexcept
Copies only members of class tree.
void fill_union_find() noexcept
void tree_only_move(tree &&t) noexcept
Moves only members of class tree.
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:241
void extra_work_per_edge_add(node u, node v) noexcept
Do some extra work after an edge has been added.
uint32_t get_num_nodes_component(node u) const noexcept
Amount of nodes in a connected component of the tree.
Definition tree.hpp:195
bool m_is_tree_type_valid
Is the type of this tree valid?
Definition tree.hpp:262
bool is_tree_type_valid() const noexcept
Is the type of this tree valid?
Definition tree.hpp:231
void tree_only_init(uint32_t n) noexcept
Initialises only the memory of class tree.
virtual void calculate_tree_type() noexcept=0
Calculates the type of tree.
void extra_work_per_edge_remove(node u, node v) noexcept
Do some extra work after an edge has been removed.
virtual void call_union_find_after_remove(node u, node v, uint32_t *const root_of, uint32_t *const root_size) noexcept=0
A call to the union find method.
std::array< bool, __tree_type_size > m_tree_type
The type of this tree.
Definition tree.hpp:254
tree(tree &&t) noexcept
Move constructor.
Definition tree.hpp:91
virtual void call_union_find_after_add(node u, node v, uint32_t *const root_of, uint32_t *const root_size) noexcept=0
A call to the union find method.
bool can_add_edges(const std::vector< edge > &edges) const noexcept
Can these edges be added?
std::vector< uint32_t > m_root_size
The size of the connected component that a root belongs to.
Definition tree.hpp:251
void tree_only_clear() noexcept
Clears the memory used by only class tree.
void tree_only_extra_work_edges_set() noexcept
std::vector< std::string > get_tree_type_list() const noexcept
Returns the list of types as a list of strings.
constexpr std::size_t __tree_type_size
Number of elements within enumeration lal::graphs::tree_type.
Definition tree_type.hpp:110
tree_type
Enumeration of several types of trees.
Definition tree_type.hpp:55
Main namespace of the library.
Definition definitions.hpp:48
std::pair< node, node > edge
Edge type.
Definition definitions.hpp:75
uint32_t node
Node type.
Definition definitions.hpp:51