LAL: Linear Arrangement Library 21.07.01
A library focused on algorithms on linear arrangements of graphs.
|
Free tree graph class. More...
#include <free_tree.hpp>
Public Member Functions | |
free_tree () noexcept | |
Empty constructor. | |
free_tree (uint32_t n) noexcept | |
Constructor with number of vertices. | |
free_tree (const free_tree &t) noexcept | |
Copy constructor. | |
free_tree (free_tree &&t) noexcept | |
Move constructor. | |
free_tree (const undirected_graph &t) noexcept | |
Copy constructor with undirected graph. | |
free_tree (undirected_graph &&t) noexcept | |
Move constructor with undirected graph. | |
virtual | ~free_tree () noexcept |
Destructor. | |
free_tree & | operator= (const free_tree &f) noexcept |
Copy assignment operator. | |
free_tree & | operator= (free_tree &&f) noexcept |
Copy assignment operator. | |
free_tree & | add_edge (node s, node t, bool norm=true, bool check_norm=true) noexcept |
Adds an edge to the tree. | |
free_tree & | add_edge_bulk (node s, node t) noexcept |
Adds an edge to the graph. | |
void | finish_bulk_add (bool norm=true, bool check=true) noexcept |
Finishes adding edges in bulk. | |
free_tree & | add_edges (const std::vector< edge > &edges, bool norm=true, bool check_norm=true) noexcept |
Adds a list of edges to the graph. | |
free_tree & | set_edges (const std::vector< edge > &edges, bool norm=true, bool check_norm=true) noexcept |
Sets the edges to the graph. | |
virtual free_tree & | remove_edges_incident_to (node u, bool norm=true, bool check_norm=true) noexcept |
Remove all edges incident to a given vertex. | |
void | disjoint_union (const free_tree &t) noexcept |
Disjoint union of trees. | |
void | calculate_tree_type () noexcept |
Calculates the type of tree. | |
bool | is_rooted () const noexcept |
Returns whether this tree is a rooted tree. | |
head_vector | get_head_vector (node r=0) const noexcept |
Converts a free tree into a head vector. | |
virtual undirected_graph & | remove_edge (node s, node t, bool norm=false, bool check_norm=true) noexcept |
Remove an edge from this graph. | |
virtual undirected_graph & | remove_edges (const std::vector< edge > &edges, bool norm=true, bool check_norm=true) noexcept |
Remove an edge from this graph. | |
std::vector< edge_pair > | get_Q () const noexcept |
Returns all independent pairs of edges of this graph. | |
std::vector< edge > | get_edges () const noexcept |
Returns all edges of this graph. | |
const neighbourhood & | get_neighbours (node u) const noexcept |
Returns the neighbourhood of node u. | |
uint32_t | get_degree (node u) const noexcept |
Returns the number of neighbours of u. | |
bool | has_edge (node u, node v) const noexcept |
Returns true if the edge \(\{u,v\}\) exists in the graph. | |
bool | is_directed () const noexcept |
Returns whether this graph is directed or not. | |
bool | is_undirected () const noexcept |
Returns whether this graph is undirected or not. | |
bool | is_tree () const noexcept |
Is this graph is an actual tree? | |
bool | can_add_edge (node s, node t) const noexcept |
Can this edge be added? | |
bool | can_add_edges (const std::vector< edge > &edges) const noexcept |
Can these edges be added? | |
uint32_t | get_num_nodes_component (node u) const noexcept |
Amount of nodes in a connected component of the tree. | |
bool | is_of_tree_type (const tree_type &tt) const noexcept |
Returns whether this tree is of type tt. | |
bool | is_tree_type_valid () const noexcept |
Is the type of this tree valid? | |
std::vector< std::string > | get_tree_type_list () const noexcept |
Returns the list of types as a list of strings. | |
virtual void | init (uint32_t n) noexcept |
Allocates the necessary memory for this class. | |
virtual void | clear () noexcept |
Frees the memory occupied by this graph. | |
virtual void | normalise () noexcept |
Normalises the graph. | |
virtual bool | check_normalised () noexcept |
Checks if the graph is normalised. | |
void | set_normalised (bool v=true) noexcept |
Sets whether this graph is normalised or not. | |
bool | has_node (node u) const noexcept |
Returns true if node u is in this graph. | |
uint32_t | get_num_nodes () const noexcept |
Returns the number of ndoes. | |
uint32_t | get_num_edges () const noexcept |
Returns the number of edges. | |
bool | is_normalised () const noexcept |
Returns whether this graph is normalised or not. | |
Protected Member Functions | |
virtual void | _init (uint32_t n) noexcept |
virtual void | _clear () noexcept |
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. | |
void | call_union_find_after_add (node u, node v, uint32_t *const root_of, uint32_t *const root_size) const noexcept |
A const call to the union find method. | |
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 | call_union_find_after_remove (node u, node v, uint32_t *const root_of, uint32_t *const root_size) const noexcept |
A const call to the union find method. | |
void | copy_full_free_tree (const free_tree &f) noexcept |
Copies all members of this class and the parent class. | |
void | move_full_free_tree (free_tree &&f) noexcept |
Moves all members of this class and the parent class. | |
void | copy_full_undirected_graph (const undirected_graph &u) noexcept |
Copies all members of this class and the parent class. | |
void | move_full_undirected_graph (undirected_graph &&u) noexcept |
Moves all members of this class and the parent class. | |
void | tree_only_init (uint32_t n) noexcept |
Initialises only the memory of class tree. | |
void | tree_only_clear () noexcept |
Clears the memory used by only class tree. | |
void | tree_only_copy (const tree &t) noexcept |
Copies only members of class tree. | |
void | tree_only_move (tree &&t) noexcept |
Moves only members of class tree. | |
void | extra_work_per_edge_add (node u, node v) noexcept |
Do some extra work after an edge has been added. | |
void | extra_work_per_edge_remove (node u, node v) noexcept |
Do some extra work after an edge has been removed. | |
void | tree_only_extra_work_edges_set () noexcept |
void | fill_union_find () noexcept |
void | copy_full_graph (const graph &g) noexcept |
Copies all members of this class. | |
void | move_full_graph (graph &&g) noexcept |
Moves all members of this class. | |
void | __disjoint_union (const graph &g) noexcept |
Disjoint union of graphs. | |
void | normalise_after_add (bool norm, bool check) noexcept |
Normalise the graph after one (or more) edges have been added. | |
void | normalise_after_remove (bool norm, bool check) noexcept |
Normalise the graph after one (or more) edges have been removed. | |
Protected Attributes | |
std::vector< node > | m_root_of |
The root of every vertex in the union-find data structure. | |
std::vector< uint32_t > | m_root_size |
The size of the connected component that a root belongs to. | |
std::array< bool, __tree_type_size > | m_tree_type |
The type of this tree. | |
bool | m_is_tree_type_valid = false |
Is the type of this tree valid? | |
std::vector< neighbourhood > | m_adjacency_list |
Data structure that implements the graph. | |
uint32_t | m_num_edges = 0 |
Amount of edges of this graph. | |
bool | m_normalised = true |
Is this graph normalised? | |
Private Member Functions | |
void | disjoint_union (const undirected_graph &g) noexcept |
Disjoint union of graphs. | |
void | remove_single_edge (node u, node v, neighbourhood &out_u, neighbourhood &in_v) noexcept |
Removes a single edge. | |
Free tree graph class.
This class constrains the addition of edges so that the resulting graphs does not contain cycles. Furthermore, the edges added are undirected.
For another type of tree-like graphs, see rooted_tree.
|
inlinenoexcept |
Constructor with number of vertices.
n | Number of vertices |
|
inlinenoexcept |
Copy constructor.
t | Free tree. |
|
inlinenoexcept |
Move constructor.
t | Free tree. |
|
noexcept |
Copy constructor with undirected graph.
t | An undirected graph. |
|
noexcept |
Move constructor with undirected graph.
t | An undirected graph. |
|
protectednoexceptinherited |
Disjoint union of graphs.
Given a graph, append it to the current graph.
All the nodes in g are relabelled starting at n, the number of nodes of the current graph.
g | Input graph. |
|
protectedvirtualnoexcept |
Clears the memory of free_tree, undirected_graph and graph classes.
Reimplemented from lal::graphs::undirected_graph.
|
protectedvirtualnoexcept |
Initialises memory of free_tree, undirected_graph and graph classes.
Reimplemented from lal::graphs::undirected_graph.
|
virtualnoexcept |
Adds an edge to the tree.
This operation checks that the edge added does not produce cycles only in a debug compilation of the library. For a more controlled addition of the edges, see can_add_edge.
For developers: method graph::extra_work_per_edge_add is called after the edge has been added.
s | Valid node index: \(0 \le s < n\). |
t | Valid node index: \(0 \le t < n\). |
norm | Should the graph be normalised? |
check_norm | If norm is false then, should we check whether the result is normalised or not? This might be useful in case the resulting graph is normalised. If norm is true then check_norm is ignored. |
Reimplemented from lal::graphs::undirected_graph.
Adds an edge to the graph.
This method only adds an edge, and does no other work: normalisation is not checked, and no extra work per edge is done.
s | Valid node index: \(0 \le s < n\). |
t | Valid node index: \(0 \le t < n\). |
|
virtualnoexcept |
Adds a list of edges to the graph.
This function checks that edges will not produce cycles only in a debug compilation of the library. Moreover, this operation is faster than calling add_edge since the edges are added in bulk. For a more controlled addition of the edges, see can_add_edges.
For developers: method graph::extra_work_per_edge_add is called after each edge has been added.
edges | The edges to be added. |
norm | Normalise the graph after the insertions. |
check_norm | If norm is false then, should we check whether the result is normalised or not? This might be useful in case the resulting graph is normalised. If norm is true then check_norm is ignored. |
Reimplemented from lal::graphs::undirected_graph.
|
virtualnoexcept |
Calculates the type of tree.
See tree_type for the list of different tree types.
Implements lal::graphs::tree.
|
protectedvirtualnoexcept |
A const call to the union find method.
This is a helper method to be able to call a template in the lal::internal namespace which updates the union find data structure under addition of an edge.
u | Node that is connected to v. |
v | Node that is connected to u. |
root_of | Array of n elements relating each vertex to its root in the union find data structure. |
root_size | Array of n elements relating each vertex to the size of the connected component it belongs to. |
Implements lal::graphs::tree.
|
protectedvirtualnoexcept |
A call to the union find method.
This is a helper method to be able to call a template in the lal::internal namespace which updates the union find data structure under addition of an edge.
u | Node that is connected to v. |
v | Node that is connected to u. |
root_of | Array of n elements relating each vertex to its root in the union find data structure. |
root_size | Array of n elements relating each vertex to the size of the connected component it belongs to. |
Implements lal::graphs::tree.
|
protectedvirtualnoexcept |
A const call to the union find method.
This is a helper method to be able to call a template in the lal::internal namespace which updates the union find data structure under removal of an edge.
u | Node that is connected to v. |
v | Node that is connected to u. |
root_of | Array of n elements relating each vertex to its root in the union find data structure. |
root_size | Array of n elements relating each vertex to the size of the connected component it belongs to. |
Implements lal::graphs::tree.
|
protectedvirtualnoexcept |
A call to the union find method.
This is a helper method to be able to call a template in the lal::internal namespace which updates the union find data structure under removal of an edge.
u | Node that is connected to v. |
v | Node that is connected to u. |
root_of | Array of n elements relating each vertex to its root in the union find data structure. |
root_size | Array of n elements relating each vertex to the size of the connected component it belongs to. |
Implements lal::graphs::tree.
Can this edge be added?
In a tree, an edge can only be added if it does not produce cycles, and it has not been added before.
s | First node of the edge. |
t | Second node of the edge. |
|
noexceptinherited |
Can these edges be added?
In a tree, a set of edges can only be added if their addition to the tree do not produce cycles and none of them have been added before.
edges | List of edges. |
|
virtualnoexceptinherited |
Checks if the graph is normalised.
Checks, whether the graph's adjacency structure is normalised or not. In case it is, attribute m_normalised is set to true, so method is_normalised evaluates to true.
Reimplemented in lal::graphs::directed_graph.
|
virtualnoexceptinherited |
Frees the memory occupied by this graph.
See _clear for details.
|
noexcept |
Disjoint union of trees.
Given a free tree, append it to the current tree.
All the nodes in t are relabelled starting at n, the number of nodes of the current tree.
t | Input tree. |
|
privatenoexcept |
Disjoint union of graphs.
Given a graph, append it to the current graph.
All the nodes in g are relabelled starting at n, the number of nodes of the current graph.
g | Input graph. |
|
protectednoexceptinherited |
Fills the Union-Find data structure assuming that the graph structure has all of its edges.
|
virtualnoexcept |
Finishes adding edges in bulk.
norm | Normalise the tree. |
check | Check whether the tree is normalised or not. |
Implements lal::graphs::graph.
|
inlinenoexceptinherited |
Returns the number of neighbours of u.
u | Node to be queried. |
|
noexcept |
Converts a free tree into a head vector.
A head vector of an n-vertex tree is a list of non-negative integer numbers. The number at position i denotes the parent node of the vertex at said position. Value '0' denotes the root. In this case, the vertex corresponding to the value '0' is not labelled as a root.
Each tree is formatted as a list of whole, positive numbers (including zero), each representing a node of the tree. The number 0 denotes the root of the tree, and a number at a certain position indicates its parent node. For example, when number 4 is at position 9 it means that node 9 has parent node 4. Therefore, if number 0 is at position 1 it means that node 1 is the root of the tree. A complete example of such a tree's representation is the following
0 3 4 1 6 3
which should be interpreted as
(a) predecessor: 0 3 4 1 6 3 (b) node of the tree: 1 2 3 4 5 6
Note that lines like these are not valid:
(1) 0 2 2 2 2 2 (2) 2 0 0
Line (1) is not valid due to a self-reference in the second position, and (2) is not valid since it contains two '0' (i.e., two roots).
Methods lal::io::read_head_vector read a head vector from a file in disk.
r | A fictional root to be used to calculate the head vector. |
|
inlinenoexceptinherited |
Returns the neighbourhood of node u.
u | Node. |
|
inlinenoexceptinherited |
Amount of nodes in a connected component of the tree.
When tree has had an edge removed, or when it is not completely built, i.e., it lack some edges, the resulting graph is clearly a forest. This function returns the size of the forest node u belongs to.
In directed trees one has to see this amount as the number of nodes of the component in the undirected version of the forest.
u | Input node. |
|
virtualnoexceptinherited |
Returns all independent pairs of edges of this graph.
The set \(Q(G)\) is defined as the pairs of edges of \(G\), \(E(G) \times E(G)\), that are independent, that is, that share no nodes.
Implements lal::graphs::graph.
|
noexceptinherited |
Returns the list of types as a list of strings.
|
virtualnoexceptinherited |
|
inlinenoexceptinherited |
Returns whether this graph is normalised or not.
A graph is normalised if every node's adjacency list is sorted increasingly. For this, use method normalise().
|
inlinenoexceptinherited |
Returns whether this tree is of type tt.
See method calculate_tree_type to know how to calculate a tree's type.
tt | Type of tree (see lal::graphs::tree_type). |
|
inlinenoexceptinherited |
Is this graph is an actual tree?
Returns true if the number of edges is one less than the number of nodes. Note that this would not really be true if the addition of edges was not constrained. Since it is constrained in a way that no cycles can be produced (for example, see free_tree::add_edge, or free_tree::add_edges), then we only need to check for the number of edges.
For further characterisations of a tree see [19] (chapter 4, pages 32-33).
|
inlinenoexceptinherited |
Is the type of this tree valid?
This function enables users determine when this tree's type should be calculated.
In case this function returns false, users should call function calculate_tree_type in order to obtain a valid tree type. Note, however, that prior to calling the function the type of this tree might be lal::graphs::tree_type::unknown and that the tree type may remain lal::graphs::tree_type::unknown even after the type has been calculated. Nevertheless, users should be suspicious of a tree being of lal::graphs::tree_type::unknown (in fact, of any) type if this method returns false, yet they should be sure of it if the type was calculated via method calculate_tree_type.
|
virtualnoexceptinherited |
Normalises the graph.
Sorts this graph's adjacency list structure in increasing order.
Besides expensive, this method may be unnecessary. Method check_normalised() checks whether the graph is normalised or not; in case it is, using this method is completely unnecessary.
Reimplemented in lal::graphs::directed_graph.
Copy assignment operator.
f | A lal::graphs::free_tree. |
Copy assignment operator.
f | A lal::graphs::free_tree. |
|
virtualnoexceptinherited |
Remove an edge from this graph.
For developers: method graph::extra_work_per_edge_remove is called after the edge has been removed.
s | Valid node index: \(0 \le s < n\). |
t | Valid node index: \(0 \le t < n\). |
norm | Normalise the graph after the deletion. |
check_norm | If norm is false then, should we check whether the result is normalised or not? This might be useful in case the resulting graph is normalised. If norm is true then check_norm is ignored. |
|
virtualnoexceptinherited |
Remove an edge from this graph.
This operation is faster than removing edges one by one with remove_edge(node,node,bool,bool) since the edges are removed in bulk.
For developers: method graph::extra_work_per_edge_remove is called after each edge has been removed.
edges | The edges to be deleted. |
norm | Normalise the graph after the deletion. |
check_norm | If norm is false then, should we check whether the result is normalised or not? This might be useful in case the resulting graph is normalised. If norm is true then check_norm is ignored. |
|
virtualnoexcept |
Remove all edges incident to a given vertex.
This operation is faster than removing edges one by one with remove_edge(node,node,bool,bool) since the edges are removed in bulk.
For developers: method lal::graphs::graph::extra_work_per_edge_remove is called after each edge has been removed.
u | The node whose incident vertices are to be removed. |
norm | Normalise the graph after the deletion. |
check_norm | If norm is false then, should we check whether the result is normalised or not? This might be useful in case the resulting graph is normalised. If norm is true then check_norm is ignored. |
Reimplemented from lal::graphs::undirected_graph.
|
privatenoexceptinherited |
Removes a single edge.
u | First node of edge. |
v | Second node of edge. |
out_u | Out-neighbourhood of node u. |
in_v | In-neighbourhood of node v. |
|
virtualnoexcept |
Sets the edges to the graph.
Sets the edges of this graph assuming that the nodes indexed in the list are, at most, the number of nodes of this graph.
This list of edges is assumed to be all the edges that are going to be added to this graph. This means that the internal data structures are constructed more efficiently than when adding edges one by one (see add_edge) or in several chunks (see add_edges). For a more controlled addition of the edges, see can_add_edges.
Moreover, the current structure of the graph is cleared before setting the new edges.
edges | The edges to be added. |
norm | Normalise the graph after the insertions. |
check_norm | If norm is false then, should we check whether the result is normalised or not? This might be useful in case the resulting graph is normalised. If norm is true then check_norm is ignored. |
Reimplemented from lal::graphs::undirected_graph.
|
protectednoexceptinherited |
Updates the data structures of a tree after the graph structure has had its set of edges set.
|
protectednoexceptinherited |
Initialises only the memory of class tree.
n | Number of vertices. |
|
protectedinherited |
Is the type of this tree valid?
This attribute keeps track of whether or not the function calculate_tree_type should be called before querying the type of this tree via function is_of_tree_type.
|
protectedinherited |
Is this graph normalised?
An undirected graph is normalised iff every node's adjacency list is sorted in increasing order.
In directed graphs, however, it is necessary that the adjacency lists of the out-neighbours and in-neighbours of nodes be sorted.
This attribute is set to 'true' in all graph's initialisation and destruction (when clear() method is called).
|
protectedinherited |
The size of the connected component that a root belongs to.
Formally, m_size_of[v] is the size of the connected component of a root vertex v. A vertex u is a root vertex if there exists a vertex w such that m_root_of[w] = u.
In this context, root is within the union-find data structure.