LAL: Linear Arrangement Library 21.07.01
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
lal::graphs::tree Class Referenceabstract

Tree graph class. More...

#include <tree.hpp>

Inheritance diagram for lal::graphs::tree:
lal::graphs::graph lal::graphs::free_tree lal::graphs::rooted_tree

Public Member Functions

 tree () noexcept
 Empty constructor.
 
 tree (const tree &t) noexcept
 Copy constructor.
 
 tree (tree &&t) noexcept
 Move constructor.
 
virtual ~tree () noexcept
 Destructor.
 
treeoperator= (const tree &t) noexcept
 Copy assignment operator.
 
treeoperator= (tree &&t) noexcept
 Move assignment operator.
 
virtual void calculate_tree_type () noexcept=0
 Calculates the type of tree.
 
bool is_tree () const noexcept
 Is this graph is an actual tree?
 
virtual bool is_rooted () const noexcept=0
 Returns whether this tree is a rooted 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.
 
virtual void finish_bulk_add (bool norm=true, bool check=true) noexcept=0
 Completes the inner structure of the graph after adding a bulk of edges.
 
void set_normalised (bool v=true) noexcept
 Sets whether this graph is normalised or not.
 
virtual std::vector< edge_pairget_Q () const noexcept=0
 Returns all independent pairs of edges of this graph.
 
bool has_node (node u) const noexcept
 Returns true if node u is in this graph.
 
virtual bool has_edge (node u, node v) const =0
 Returns true if the undirected edge (u, v) exists in the 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.
 
virtual std::vector< edgeget_edges () const noexcept=0
 Returns all edges of this graph.
 
bool is_normalised () const noexcept
 Returns whether this graph is normalised or not.
 
virtual bool is_directed () const noexcept=0
 Returns whether this graph is directed or not.
 
virtual bool is_undirected () const noexcept=0
 Returns whether this graph is undirected or not.
 

Protected Member Functions

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
 
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.
 
virtual void call_union_find_after_add (node u, node v, uint32_t *const root_of, uint32_t *const root_size) const noexcept=0
 A const call to the union find method.
 
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.
 
virtual void call_union_find_after_remove (node u, node v, uint32_t *const root_of, uint32_t *const root_size) const noexcept=0
 A const call to the union find method.
 
virtual void _init (uint32_t n) noexcept
 Initialises memory of graph class.
 
virtual void _clear () noexcept
 Clears memory for the graph class.
 
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< nodem_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_sizem_tree_type
 The type of this tree.
 
bool m_is_tree_type_valid = false
 Is the type of this tree valid?
 
std::vector< neighbourhoodm_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?
 

Detailed Description

Tree graph class.

This is an abstract class for those tree-like graphs. Classes that implement different abstractions of trees and that inherit from this class are: free_tree, rooted_tree.

In these classes the addition of edges is constrained so as to ensure that the edges added actually yield trees, i.e., that cycles are never produced. For the sake of efficiency, only debug compilations of the library (compilations where the DEBUG symbol is defined) check that such additions do not produce cycles. In case of doubt, one can query the class using methods can_add_edge or can_add_edges prior to adding one or several edges.

Constructor & Destructor Documentation

◆ tree() [1/2]

lal::graphs::tree::tree ( const tree & t)
inlinenoexcept

Copy constructor.

Parameters
tTree.

◆ tree() [2/2]

lal::graphs::tree::tree ( tree && t)
inlinenoexcept

Move constructor.

Parameters
tTree.

Member Function Documentation

◆ __disjoint_union()

void lal::graphs::graph::__disjoint_union ( const graph & g)
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.

Parameters
gInput graph.
Precondition
This graph and g must be of the same type (both must be either undirected, or both directed).
Postcondition
The graph is normalised only if it was normalised before the call and g is also normalised.

◆ calculate_tree_type()

virtual void lal::graphs::tree::calculate_tree_type ( )
pure virtualnoexcept

Calculates the type of tree.

See tree_type for the list of different tree types.

Implemented in lal::graphs::free_tree, and lal::graphs::rooted_tree.

◆ call_union_find_after_add() [1/2]

virtual void lal::graphs::tree::call_union_find_after_add ( node u,
node v,
uint32_t *const root_of,
uint32_t *const root_size ) const
protectedpure virtualnoexcept

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.

Parameters
uNode that is connected to v.
vNode that is connected to u.
root_ofArray of n elements relating each vertex to its root in the union find data structure.
root_sizeArray of n elements relating each vertex to the size of the connected component it belongs to.

Implemented in lal::graphs::free_tree, and lal::graphs::rooted_tree.

◆ call_union_find_after_add() [2/2]

virtual void lal::graphs::tree::call_union_find_after_add ( node u,
node v,
uint32_t *const root_of,
uint32_t *const root_size )
protectedpure virtualnoexcept

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.

Parameters
uNode that is connected to v.
vNode that is connected to u.
root_ofArray of n elements relating each vertex to its root in the union find data structure.
root_sizeArray of n elements relating each vertex to the size of the connected component it belongs to.

Implemented in lal::graphs::free_tree, and lal::graphs::rooted_tree.

◆ call_union_find_after_remove() [1/2]

virtual void lal::graphs::tree::call_union_find_after_remove ( node u,
node v,
uint32_t *const root_of,
uint32_t *const root_size ) const
protectedpure virtualnoexcept

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.

Parameters
uNode that is connected to v.
vNode that is connected to u.
root_ofArray of n elements relating each vertex to its root in the union find data structure.
root_sizeArray of n elements relating each vertex to the size of the connected component it belongs to.

Implemented in lal::graphs::free_tree, and lal::graphs::rooted_tree.

◆ call_union_find_after_remove() [2/2]

virtual void lal::graphs::tree::call_union_find_after_remove ( node u,
node v,
uint32_t *const root_of,
uint32_t *const root_size )
protectedpure virtualnoexcept

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.

Parameters
uNode that is connected to v.
vNode that is connected to u.
root_ofArray of n elements relating each vertex to its root in the union find data structure.
root_sizeArray of n elements relating each vertex to the size of the connected component it belongs to.

Implemented in lal::graphs::free_tree, and lal::graphs::rooted_tree.

◆ can_add_edge()

bool lal::graphs::tree::can_add_edge ( node s,
node t ) const
noexcept

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.

Parameters
sFirst node of the edge.
tSecond node of the edge.
Returns
Whether the addition of this new edge can be added to the tree without producing cycles.

◆ can_add_edges()

bool lal::graphs::tree::can_add_edges ( const std::vector< edge > & edges) const
noexcept

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.

Parameters
edgesList of edges.
Returns
Whether the addition of these new edges can be added to the tree without producing cycles.

◆ check_normalised()

virtual bool lal::graphs::graph::check_normalised ( )
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.

◆ clear()

virtual void lal::graphs::graph::clear ( )
virtualnoexceptinherited

Frees the memory occupied by this graph.

See _clear for details.

Postcondition
The graph is normalised. The number of edges is 0.

◆ fill_union_find()

void lal::graphs::tree::fill_union_find ( )
protectednoexcept

Fills the Union-Find data structure assuming that the graph structure has all of its edges.

◆ finish_bulk_add()

virtual void lal::graphs::graph::finish_bulk_add ( bool norm = true,
bool check = true )
pure virtualnoexceptinherited

Completes the inner structure of the graph after adding a bulk of edges.

This is meant to be used after several calls to undirected_graph::add_edge_bulk, directed_graph::add_edge_bulk.

Parameters
normNormalise the graph
checkCheck wether the graph is normalised or not.

Implemented in lal::graphs::directed_graph, lal::graphs::free_tree, lal::graphs::rooted_tree, and lal::graphs::undirected_graph.

◆ get_num_nodes_component()

uint32_t lal::graphs::tree::get_num_nodes_component ( node u) const
inlinenoexcept

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.

Parameters
uInput node.
Returns
The size of the connected component of u.

◆ get_Q()

virtual std::vector< edge_pair > lal::graphs::graph::get_Q ( ) const
pure 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.

Implemented in lal::graphs::directed_graph, and lal::graphs::undirected_graph.

◆ get_tree_type_list()

std::vector< std::string > lal::graphs::tree::get_tree_type_list ( ) const
noexcept

Returns the list of types as a list of strings.

Returns
The list of types as a list of strings.

◆ init()

virtual void lal::graphs::graph::init ( uint32_t n)
virtualnoexceptinherited

Allocates the necessary memory for this class.

See _init for details.

Parameters
nNumber of nodes.

◆ is_normalised()

bool lal::graphs::graph::is_normalised ( ) const
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().

Returns
The value of m_normalised.

◆ is_of_tree_type()

bool lal::graphs::tree::is_of_tree_type ( const tree_type & tt) const
inlinenoexcept

Returns whether this tree is of type tt.

See method calculate_tree_type to know how to calculate a tree's type.

Parameters
ttType of tree (see lal::graphs::tree_type).
Returns
True if this tree is of type tt.

◆ is_tree()

bool lal::graphs::tree::is_tree ( ) const
inlinenoexcept

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).

Returns
True or false depending on whether this graph fits the defintion of tree.

◆ is_tree_type_valid()

bool lal::graphs::tree::is_tree_type_valid ( ) const
inlinenoexcept

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.

Returns
True or false depending on whether the tree type was calculated or not.

◆ normalise()

virtual void lal::graphs::graph::normalise ( )
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.

Postcondition
Method is_normalised evaluates to true.

Reimplemented in lal::graphs::directed_graph.

◆ operator=() [1/2]

tree & lal::graphs::tree::operator= ( const tree & t)
inlinenoexcept

Copy assignment operator.

Parameters
tTree.

◆ operator=() [2/2]

tree & lal::graphs::tree::operator= ( tree && t)
inlinenoexcept

Move assignment operator.

Parameters
tTree.

◆ tree_only_extra_work_edges_set()

void lal::graphs::tree::tree_only_extra_work_edges_set ( )
protectednoexcept

Updates the data structures of a tree after the graph structure has had its set of edges set.

◆ tree_only_init()

void lal::graphs::tree::tree_only_init ( uint32_t n)
protectednoexcept

Initialises only the memory of class tree.

Parameters
nNumber of vertices.

Member Data Documentation

◆ m_is_tree_type_valid

bool lal::graphs::tree::m_is_tree_type_valid = false
protected

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.

◆ m_normalised

bool lal::graphs::graph::m_normalised = true
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).

◆ m_root_size

std::vector<uint32_t> lal::graphs::tree::m_root_size
protected

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.


The documentation for this class was generated from the following file: