LAL: Linear Arrangement Library 21.07.01
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
rooted_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 <vector>
49
50// lal includes
51#include <lal/graphs/directed_graph.hpp>
52#include <lal/graphs/tree.hpp>
53#include <lal/graphs/free_tree.hpp>
54
55namespace lal {
56namespace graphs {
57
107class rooted_tree : public directed_graph, virtual public tree {
108public:
109 /* CONSTRUCTORS */
110
112 rooted_tree() noexcept : tree(), directed_graph() { }
117 rooted_tree(uint32_t n) noexcept {
119 }
124 rooted_tree(const rooted_tree& r) noexcept : graph(), tree(), directed_graph() {
126 }
127#ifndef SWIG
132 rooted_tree(rooted_tree&& r) noexcept {
133 move_full_rooted_tree(std::move(r));
134 }
135#endif
137 rooted_tree(const free_tree& t, node r) noexcept {
138 init_rooted(t, r);
139 }
141 virtual ~rooted_tree() noexcept { }
142
143 /* OPERATORS */
144
145#ifndef SWIG
150 rooted_tree& operator= (const rooted_tree& r) noexcept {
152 return *this;
153 }
159 move_full_rooted_tree(std::move(r));
160 return *this;
161 }
162#endif
163
164 /* MODIFIERS */
165
188 (node s, node t, bool norm = true, bool check_norm = true) noexcept;
189
203
210 void finish_bulk_add(bool norm = true, bool check = true) noexcept;
211
236 (const std::vector<edge>& edges, bool norm = true, bool check_norm = true)
237 noexcept;
238
273 (const std::vector<edge>& edges, bool norm = true, bool check_norm = true)
274 noexcept;
275
296 (node s, node t, bool norm = false, bool check_norm = true) noexcept;
297
322 (const std::vector<edge>& edges, bool norm = true, bool check_norm = true)
323 noexcept;
324
345 (node u, bool norm = true, bool check_norm = true)
346 noexcept;
347
373 void disjoint_union(const rooted_tree& t, bool connect_roots = true)
374 noexcept;
375
396 bool find_edge_orientation() noexcept;
397
411 inline void set_valid_orientation(bool valid) noexcept {
412 m_valid_orientation = valid;
413 }
414
429 void init_rooted(const free_tree& t, node r) noexcept;
430
437 void calculate_size_subtrees() noexcept;
438
439 void calculate_tree_type() noexcept;
440
441 /* SETTERS */
442
453 void set_root(node r) noexcept;
454
455 /* GETTERS */
456
457 inline bool is_rooted() const noexcept { return true; }
458
470 inline bool is_rooted_tree() const noexcept
471 { return is_tree() and has_root() and is_orientation_valid(); }
472
482 inline bool is_orientation_valid() const noexcept { return m_valid_orientation; }
483
485 inline node get_root() const noexcept {
486#if defined DEBUG
487 assert(has_root());
488#endif
489 return m_root;
490 }
493 inline bool has_root() const noexcept { return m_has_root; }
494
501 inline uint32_t get_num_nodes_subtree(node u) const noexcept {
502#if defined DEBUG
503 assert(has_node(u));
504#endif
505 return m_size_subtrees[u];
506 }
507
518 inline bool are_size_subtrees_valid() const noexcept
519 { return m_are_size_subtrees_valid; }
520
552 std::vector<edge> get_edges_subtree(node u, bool relab = false) const noexcept;
553
563 rooted_tree get_subtree(node u) const noexcept;
564
571 (bool norm = true, bool check = true) const noexcept;
572
612 head_vector get_head_vector() const noexcept;
613
614protected:
618 bool m_has_root = false;
619
622
629 std::vector<uint32_t> m_size_subtrees;
632
633protected:
636 virtual void _init(uint32_t n) noexcept;
639 virtual void _clear() noexcept;
640
642 node u, node v,
643 uint32_t * const root_of,
644 uint32_t * const root_size
645 ) noexcept;
647 node u, node v,
648 uint32_t * const root_of,
649 uint32_t * const root_size
650 ) const noexcept;
652 node u, node v,
653 uint32_t * const root_of,
654 uint32_t * const root_size
655 ) noexcept;
657 node u, node v,
658 uint32_t * const root_of,
659 uint32_t * const root_size
660 ) const noexcept;
661
663 void copy_full_rooted_tree(const rooted_tree& r) noexcept;
666
667private:
669};
670
671} // -- namespace graphs
672} // -- namespace lal
Directed graph class.
Definition directed_graph.hpp:68
directed_graph() noexcept
Empty constructor.
Definition directed_graph.hpp:73
Free tree graph class.
Definition free_tree.hpp:59
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
Rooted tree graph class.
Definition rooted_tree.hpp:107
bool find_edge_orientation() noexcept
Finds the orientation of the edges.
rooted_tree(rooted_tree &&r) noexcept
Move constructor.
Definition rooted_tree.hpp:132
virtual rooted_tree & remove_edges_incident_to(node u, bool norm=true, bool check_norm=true) noexcept
Remove all edges incident to a given vertex.
bool m_has_root
Has the root been set?
Definition rooted_tree.hpp:618
void copy_full_rooted_tree(const rooted_tree &r) noexcept
Copies all members of this class and the parent class.
rooted_tree & add_edge_bulk(node s, node t) noexcept
Adds an edge to the graph.
rooted_tree(uint32_t n) noexcept
Constructor with number of nodes and root node.
Definition rooted_tree.hpp:117
bool is_orientation_valid() const noexcept
Is the orientation of the edges valid?
Definition rooted_tree.hpp:482
void disjoint_union(const rooted_tree &t, bool connect_roots=true) noexcept
Disjoint union of trees.
void set_valid_orientation(bool valid) noexcept
Sets wether the type of the rooted tree is valid or not.
Definition rooted_tree.hpp:411
node m_root
Root of the tree.
Definition rooted_tree.hpp:616
rooted_tree & operator=(const rooted_tree &r) noexcept
Copy assignment operator.
Definition rooted_tree.hpp:150
rooted_tree get_subtree(node u) const noexcept
Retrieve the subtree rooted at node u.
head_vector get_head_vector() const noexcept
Converts a rooted tree into a head vector.
rooted_tree & add_edge(node s, node t, bool norm=true, bool check_norm=true) noexcept
Adds an edge to the tree.
void call_union_find_after_add(node u, node v, uint32_t *const root_of, uint32_t *const root_size) noexcept
A call to the union find method.
virtual ~rooted_tree() noexcept
Destructor.
Definition rooted_tree.hpp:141
void calculate_size_subtrees() noexcept
Calculates the number of nodes at every rooted subtree.
void set_root(node r) noexcept
Set the root of this tree.
bool m_valid_orientation
Is the orientation of the edges valid?
Definition rooted_tree.hpp:621
void init_rooted(const free_tree &t, node r) noexcept
Initialiser with tree and root node.
rooted_tree & set_edges(const std::vector< edge > &edges, bool norm=true, bool check_norm=true) noexcept
Sets the edges to the graph.
rooted_tree & remove_edge(node s, node t, bool norm=false, bool check_norm=true) noexcept
Remove an edge from this graph.
rooted_tree & add_edges(const std::vector< edge > &edges, bool norm=true, bool check_norm=true) noexcept
Adds a list of edges to the graph.
std::vector< edge > get_edges_subtree(node u, bool relab=false) const noexcept
Retrieve the edges of the subtree rooted at u.
std::vector< uint32_t > m_size_subtrees
Number of nodes of the subtrees rooted at a certain node.
Definition rooted_tree.hpp:629
rooted_tree & remove_edges(const std::vector< edge > &edges, bool norm=true, bool check_norm=true) noexcept
Remove an edge from this graph.
bool has_root() const noexcept
Definition rooted_tree.hpp:493
node get_root() const noexcept
Return the root of this tree.
Definition rooted_tree.hpp:485
bool is_rooted() const noexcept
Returns whether this tree is a rooted tree.
Definition rooted_tree.hpp:457
uint32_t get_num_nodes_subtree(node u) const noexcept
Get the size of a subtree rooted at a given node.
Definition rooted_tree.hpp:501
rooted_tree(const rooted_tree &r) noexcept
Copy constructor.
Definition rooted_tree.hpp:124
void call_union_find_after_remove(node u, node v, uint32_t *const root_of, uint32_t *const root_size) noexcept
A call to the union find method.
void calculate_tree_type() noexcept
Calculates the type of tree.
virtual void _init(uint32_t n) noexcept
rooted_tree() noexcept
Empty constructor.
Definition rooted_tree.hpp:112
void finish_bulk_add(bool norm=true, bool check=true) noexcept
Finishes adding edges in bulk.
virtual void _clear() noexcept
void move_full_rooted_tree(rooted_tree &&r) noexcept
Moves all members of this class and the parent class.
bool are_size_subtrees_valid() const noexcept
Is a recalculation of the subtree's sizes needed?
Definition rooted_tree.hpp:518
free_tree to_free_tree(bool norm=true, bool check=true) const noexcept
Converts this rooted tree into a free tree (see free_tree).
rooted_tree(const free_tree &t, node r) noexcept
Constructor with tree and root node.
Definition rooted_tree.hpp:137
bool is_rooted_tree() const noexcept
Is this tree a valid rooted tree?
Definition rooted_tree.hpp:470
bool m_are_size_subtrees_valid
Are the contents of m_size_subtrees valid?
Definition rooted_tree.hpp:631
Tree graph class.
Definition tree.hpp:73
bool is_tree() const noexcept
Is this graph is an actual tree?
Definition tree.hpp:145
tree() noexcept
Empty constructor.
Definition tree.hpp:78
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
std::vector< uint32_t > head_vector
A head vector representation of a (usually) rooted tree.
Definition definitions.hpp:114