LAL: Linear Arrangement Library 24.10.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 - 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#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
140 virtual void finish_bulk_add_complete(const bool norm = true, const bool check = true) noexcept = 0;
141
156 virtual void finish_bulk_remove_complete(const bool norm = true, const bool check = true) noexcept = 0;
157
158 /* GETTERS */
159
177 [[nodiscard]] bool is_tree() const noexcept {
178 // NOTE: this would not really be true if the addition of edges
179 // was not constrained. Since it is, in a way that no cycles can
180 // be produced, then we only need to check for the number of edges.
181 return (get_num_nodes() == 0 ? true : get_num_edges() == get_num_nodes() - 1);
182
183 // NOTE 2: this is only true in a debug compilation of the library
184 // since a release compilation does not actually constrain the addition
185 // of edges
186 }
187
189 [[nodiscard]] virtual bool is_rooted() const noexcept = 0;
190
205 [[nodiscard]] virtual bool can_add_edge(node const s, const node t) const noexcept;
206
220 [[nodiscard]] virtual bool can_add_edges(const std::vector<edge>& edges) const noexcept;
221
237 [[nodiscard]] uint64_t get_component_representative(const node u) const noexcept {
238#if defined DEBUG
239 assert(has_node(u));
240#endif
241 return m_union_find__root_of[u];
242 }
243
250 [[nodiscard]] bool are_nodes_in_same_component(const node u, const node v) const noexcept {
252 }
253
266 [[nodiscard]] uint64_t get_num_nodes_component(const node u) const noexcept {
267#if defined DEBUG
268 assert(has_node(u));
269#endif
271 }
272
280 [[nodiscard]] bool is_of_tree_type(const tree_type& tt) const noexcept {
281 return m_tree_type[static_cast<std::size_t>(tt)];
282 }
283
302 [[nodiscard]] bool is_tree_type_valid() const noexcept { return m_is_tree_type_valid; }
303
308 [[nodiscard]] std::vector<std::string> get_tree_type_list() const noexcept;
309
310protected:
322 std::vector<uint64_t> m_union_find__root_size;
323
334
335protected:
340 void tree_only_init(const uint64_t n) noexcept {
341 m_union_find__root_of = std::vector<uint64_t>(n);
342 m_union_find__root_size = std::vector<uint64_t>(n);
343 for (node u = 0; u < n; ++u) {
346 }
347 std::fill(m_tree_type.begin(), m_tree_type.end() - 1, false);
349 }
351 void tree_only_clear() noexcept {
352 m_union_find__root_of.clear();
354 std::fill(m_tree_type.begin(), m_tree_type.end() - 1, false);
356 }
357
359 void tree_only_copy(const tree& t) noexcept {
360 // copy this class' members
361 m_union_find__root_of = t.m_union_find__root_of;
362 m_union_find__root_size = t.m_union_find__root_size;
363 m_is_tree_type_valid = t.m_is_tree_type_valid;
364 m_tree_type = t.m_tree_type;
365 }
367 void tree_only_move(tree&& t) noexcept {
368 // move this class' members
369 m_union_find__root_of = std::move(t.m_union_find__root_of);
370 m_union_find__root_size = std::move(t.m_union_find__root_size);
371 m_is_tree_type_valid = t.m_is_tree_type_valid;
372 m_tree_type = std::move(t.m_tree_type);
373
374 t.tree_only_invalidate();
375 }
376
382 void tree_only_add_node() noexcept {
383 const node new_node = m_union_find__root_of.size();
384 m_union_find__root_of.push_back(new_node);
385 m_union_find__root_size.push_back(1);
387 }
388
395 void tree_only_invalidate() noexcept {
396 m_is_tree_type_valid = false;
397 }
398
406 void tree_only_actions_after_add_edge(const node u, const node v) noexcept;
407
415
425
435
445
455
463 void tree_only_actions_after_remove_edge(const node u, const node v) noexcept;
464
472
480
488
495 void tree_only_set_edges() noexcept;
496
503 void tree_only_remove_node(const node u) noexcept;
504
507 void fill_union_find() noexcept {
508 const uint64_t n = get_num_nodes();
509 for (node u = 0; u < n; ++u) {
510 // all vertices point to root zero
512 }
513 // the size of the connected component of the root 0 is n
515 }
516
518 void empty_union_find() noexcept {
519 for (node u = 0; u < get_num_nodes(); ++u) {
520 // all vertices point to root zero
523 }
524 }
525
539 (
540 const node u,
541 const node v,
542 uint64_t * const root_of,
543 uint64_t * const root_size
544 )
545 const noexcept = 0;
546
559 (
560 const edge_list& edges,
561 uint64_t * const root_of,
562 uint64_t * const root_size
563 )
564 const noexcept = 0;
565
577 (
578 uint64_t * const root_of,
579 uint64_t * const root_size
580 )
581 const noexcept = 0;
582
594 (
595 uint64_t * const root_of,
596 uint64_t * const root_size
597 )
598 const noexcept = 0;
599
613 const node u,
614 const node v,
615 uint64_t * const root_of,
616 uint64_t * const root_size
617 )
618 const noexcept = 0;
619
632 (
633 const edge_list& edges,
634 uint64_t * const root_of,
635 uint64_t * const root_size
636 )
637 const noexcept = 0;
638
653 (
654 const node u,
655 uint64_t * const root_of,
656 uint64_t * const root_size
657 )
658 const noexcept = 0;
659};
660
661} // -- namespace graphs
662} // -- namespace lal
Abstract class for graphs.
Definition graph.hpp:68
uint64_t get_num_edges() const noexcept
Returns the number of edges.
Definition graph.hpp:211
uint64_t get_num_nodes() const noexcept
Returns the number of ndoes.
Definition graph.hpp:207
graph() noexcept
Empty constructor.
Definition graph.hpp:73
bool has_node(const node u) const noexcept
Returns true if node u is in this graph.
Definition graph.hpp:201
Tree graph class.
Definition tree.hpp:73
void tree_only_remove_node(const node u) noexcept
Removes a vertex from the union-find data structure.
tree & operator=(const tree &t) noexcept
Copy assignment operator.
Definition tree.hpp:104
bool is_of_tree_type(const tree_type &tt) const noexcept
Returns whether this tree is of type tt.
Definition tree.hpp:280
std::vector< uint64_t > m_union_find__root_size
The size of the connected component that a root belongs to.
Definition tree.hpp:322
virtual void update_union_find_after_remove_edges_bulk(uint64_t *const root_of, uint64_t *const root_size) const noexcept=0
Update the union find data structure after the removal of several edges.
tree(const tree &t) noexcept
Copy constructor.
Definition tree.hpp:83
uint64_t get_component_representative(const node u) const noexcept
Representative node of the connected component in which u belongs.
Definition tree.hpp:237
virtual bool is_rooted() const noexcept=0
Returns whether this tree is a rooted tree.
virtual void update_union_find_after_add_edges(const edge_list &edges, uint64_t *const root_of, uint64_t *const root_size) const noexcept=0
Update the union find data structure after the addition of a set of edges.
bool is_tree() const noexcept
Is this graph an actual tree?
Definition tree.hpp:177
tree() noexcept
Empty constructor.
Definition tree.hpp:78
void tree_only_invalidate() noexcept
Invalidates the aggregated information of the tree.
Definition tree.hpp:395
virtual bool can_add_edge(node const s, const node t) const noexcept
Can this edge be added?
void tree_only_copy(const tree &t) noexcept
Copies only members of class tree.
Definition tree.hpp:359
void tree_only_actions_after_remove_edges_bulk_complete() noexcept
Do some work after the removal of several edges in bulk.
void fill_union_find() noexcept
Definition tree.hpp:507
void tree_only_move(tree &&t) noexcept
Moves only members of class tree.
Definition tree.hpp:367
virtual ~tree() noexcept
Destructor.
Definition tree.hpp:96
uint64_t get_num_nodes_component(const node u) const noexcept
Amount of nodes in a connected component of the tree.
Definition tree.hpp:266
void tree_only_actions_after_add_edges_bulk_complete() noexcept
Do some work after the addition of several edges in bulk.
void tree_only_actions_after_remove_edges(const edge_list &e) noexcept
Do some work after the removal of several edges.
void empty_union_find() noexcept
Empties the Union-Find data structure assuming that the tree has no edges.
Definition tree.hpp:518
void tree_only_add_node() noexcept
Adds a node to this tree.
Definition tree.hpp:382
bool are_nodes_in_same_component(const node u, const node v) const noexcept
Checks if two nodes are in the same connected component.
Definition tree.hpp:250
virtual bool can_add_edges(const std::vector< edge > &edges) const noexcept
Can these edges be added?
void tree_only_actions_before_remove_edges_incident_to(const node u) noexcept
Do some work before the removal of all edges incident to a vertex.
void tree_only_actions_after_remove_edge(const node u, const node v) noexcept
Do some work after the removal of an edge.
bool m_is_tree_type_valid
Is the type of this tree valid?
Definition tree.hpp:333
void tree_only_init(const uint64_t n) noexcept
Initializes only the memory of class tree.
Definition tree.hpp:340
void tree_only_actions_after_remove_edges_bulk() noexcept
Do some work after the removal of several edges in bulk.
virtual void update_union_find_before_remove_incident_edges_to(const 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.
bool is_tree_type_valid() const noexcept
Is the type of this tree valid?
Definition tree.hpp:302
void tree_only_actions_after_add_edges(const edge_list &e) noexcept
Do some work after the addition of several edges.
virtual void calculate_tree_type() noexcept=0
Calculates the type of tree.
void tree_only_actions_after_add_edge(const node u, const node v) noexcept
Do some work after the addition of an edge.
virtual void finish_bulk_add_complete(const bool norm=true, const bool check=true) noexcept=0
Completes the inner structure of the tree after adding edges in bulk.
void tree_only_actions_after_remove_node(const node u) noexcept
Do some work before the removal of a vertex.
std::vector< node > m_union_find__root_of
The root of every vertex in the union-find data structure.
Definition tree.hpp:312
std::array< bool, __tree_type_size > m_tree_type
The type of this tree.
Definition tree.hpp:325
tree(tree &&t) noexcept
Move constructor.
Definition tree.hpp:91
void tree_only_actions_after_add_edges_bulk() noexcept
Do some work after the addition of several edges in bulk.
virtual void update_union_find_after_remove_edge(const node u, const 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.
void tree_only_clear() noexcept
Clears the memory used by only class tree.
Definition tree.hpp:351
virtual void update_union_find_after_add_edge(const node u, const 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.
virtual void update_union_find_after_add_edges_bulk(uint64_t *const root_of, uint64_t *const root_size) const noexcept=0
Update the union find data structure after the addition of several edges.
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_remove_edges(const edge_list &edges, uint64_t *const root_of, uint64_t *const root_size) const noexcept=0
Update the union find data structure after the removal of a set of edges.
virtual void finish_bulk_remove_complete(const bool norm=true, const bool check=true) noexcept=0
Completes the inner structure of the tree after removing edges in bulk.
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:149
tree_type
Enumeration of several types of trees.
Definition tree_type.hpp:57
Main namespace of the library.
Definition basic_types.hpp:48
std::pair< node, node > edge
See Edge page for further details.
Definition basic_types.hpp:56
std::vector< edge > edge_list
See Edge list page for further details.
Definition basic_types.hpp:60
uint64_t node
Node type. See Node / Vertex page for further details.
Definition basic_types.hpp:51