LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
lal::graphs::directed_graph Class Reference

Directed graph class. More...

#include <directed_graph.hpp>

Inheritance diagram for lal::graphs::directed_graph:
lal::graphs::graph lal::graphs::rooted_tree

Public Member Functions

 directed_graph () noexcept
 Empty constructor.
 
 directed_graph (const uint64_t n) noexcept
 Constructor with number of nodes.
 
 directed_graph (const directed_graph &g) noexcept
 Copy constructor.
 
 directed_graph (directed_graph &&g) noexcept
 Move constructor.
 
virtual ~directed_graph () noexcept
 Destructor.
 
directed_graphoperator= (const directed_graph &g) noexcept
 Copy assignment operator.
 
directed_graphoperator= (directed_graph &&g) noexcept
 Move assignment operator.
 
void reserve_in_degree (const node u, const uint64_t d) noexcept
 Predicts that the in-degree of node u is d.
 
void reserve_out_degree (const node u, const uint64_t d) noexcept
 Predicts that the out-degree of node u is d.
 
void normalize () noexcept
 Normalizes the graph.
 
bool check_normalized () noexcept
 Checks if the graph is normalized.
 
virtual directed_graphadd_node () noexcept
 Adds a node to the graph.
 
virtual directed_graphremove_node (const node u, const bool norm=true, const bool check_norm=true) noexcept
 Remove a node from this graph.
 
virtual directed_graphadd_edge (const node s, const node t, const bool norm=true, const bool check_norm=true) noexcept
 Adds a directed edge to the graph.
 
directed_graphadd_edge_bulk (const node s, const node t) noexcept
 Adds an edge to the graph.
 
void finish_bulk_add (const bool norm=true, const bool check=true) noexcept
 Completes the inner structure of the graph after adding a bulk of edges.
 
virtual directed_graphadd_edges (const std::vector< edge > &edges, const bool norm=true, const bool check_norm=true) noexcept
 Adds a list of directed edges to the graph.
 
virtual directed_graphset_edges (const std::vector< edge > &edges, const bool norm=true, const bool check_norm=true) noexcept
 Sets the list of edges to the graph.
 
virtual directed_graphremove_edge_bulk (const node s, const node t) noexcept
 Removes an edge from the graph.
 
void finish_bulk_remove (const bool norm=true, const bool check=true) noexcept
 Completes the inner structure of the graph after removing edges in bulk.
 
virtual directed_graphremove_edge (const node s, const node t, const bool norm=true, const bool check_norm=true) noexcept
 Remove an edge from this graph.
 
virtual directed_graphremove_edges (const std::vector< edge > &edges, const bool norm=true, const bool check_norm=true) noexcept
 Remove an edge from this graph.
 
virtual directed_graphremove_edges_incident_to (const node u, const bool norm=true, const bool check_norm=true) noexcept
 Remove all edges incident to a given vertex.
 
directed_graphdisjoint_union (const directed_graph &g) noexcept
 Disjoint union of graphs.
 
std::vector< edge_pairget_Q () const noexcept
 Returns all independent pairs of edges of this graph.
 
std::vector< edgeget_edges () const noexcept
 Returns all edges of this graph.
 
bool has_edge (const node u, const node v) const noexcept
 Returns true if the edge \((u,v)\) exists in the graph.
 
const neighbourhoodget_out_neighbors (const node u) const noexcept
 Returns the out-neighbors of node u.
 
const neighbourhoodget_in_neighbors (const node u) const noexcept
 Returns the in-neighbors of node u.
 
uint64_t get_degree (const node u) const noexcept
 Returns the in-degree plus the out-degree of this vertex.
 
uint64_t get_out_degree (const node u) const noexcept
 Returns the out-degree of a node.
 
uint64_t get_in_degree (const node u) const noexcept
 Returns the in-degree of a node.
 
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.
 
undirected_graph to_undirected (const bool norm=true, const bool check=true) const noexcept
 Converts this directed graph into an undirected graph.
 
std::vector< directed_graphget_connected_components () const noexcept
 Returns all the connected components of this graph as individual graphs.
 
virtual void init (const uint64_t n) noexcept
 Allocates the necessary memory for this class.
 
virtual void clear () noexcept
 Frees the memory occupied by this graph.
 
void set_normalized (const bool v=true) noexcept
 Sets whether this graph is normalized or not.
 
bool has_node (const node u) const noexcept
 Returns true if node u is in this graph.
 
uint64_t get_num_nodes () const noexcept
 Returns the number of ndoes.
 
uint64_t get_num_edges () const noexcept
 Returns the number of edges.
 
bool is_normalized () const noexcept
 Returns whether this graph is normalized or not.
 

Protected Member Functions

virtual void _init (const uint64_t n) noexcept
 Initializes the memory in the graph hierarchy.
 
virtual void _clear () noexcept
 Clears the memory of directed_graph and graph classes.
 
virtual void actions_after_add_edge (const node u, const node v) noexcept
 Do some extra work after the addition of an edge.
 
virtual void actions_after_add_edges (const edge_list &e) noexcept
 Do some extra work after the addition of several edges.
 
virtual void actions_after_add_edges_bulk () noexcept
 Do some extra work after the addition of several edges in bulk.
 
virtual void actions_after_remove_edge (const node u, const node v) noexcept
 Do some extra work after the removal of an edge.
 
virtual void actions_after_remove_edges (const edge_list &e) noexcept
 Do some extra work after the removal of several edges.
 
virtual void actions_after_remove_edges_bulk () noexcept
 Do some extra work after the removal of several edges in bulk.
 
virtual void actions_before_remove_edges_incident_to (const node u) noexcept
 Do some work before all edges incident to a node is removed.
 
virtual void actions_after_remove_node (const node u) noexcept
 Do some work before the removal of a vertex.
 
void copy_full_directed_graph (const directed_graph &d) noexcept
 Copies all members of this class and the parent class.
 
void move_full_directed_graph (directed_graph &&d) noexcept
 Moves all members of this class and the parent 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 __add_node () noexcept
 Adds a node to the graph.
 
void __disjoint_union (const graph &g) noexcept
 Disjoint union of graphs.
 
void normalize_after_edge_addition (const bool norm, const bool check) noexcept
 Normalize the graph after one (or more) edges have been added.
 
void normalize_after_edge_removal (const bool norm, const bool check) noexcept
 Normalize the graph after one (or more) edges have been removed.
 

Protected Attributes

std::vector< neighbourhoodm_in_adjacency_list
 In-neighbors for every node.
 
std::vector< neighbourhoodm_adjacency_list
 Data structure that implements the graph.
 
uint64_t m_num_edges = 0
 Amount of edges of this graph.
 
bool m_is_normalized = true
 Is this graph normalized?
 

Private Member Functions

void remove_single_edge (const node u, const node v, neighbourhood &out_u, neighbourhood &in_v) noexcept
 Removes a single edge.
 

Detailed Description

Directed graph class.

Class implementing a directed graph, using the adjacency list data structure.

An object of this class must be initialized either with its constructor or with the init method. Edges can then be added one by one (see add_edge) or all at the same time (see add_edges).

Constructor & Destructor Documentation

◆ directed_graph() [1/3]

lal::graphs::directed_graph::directed_graph ( const uint64_t n)
inlinenoexcept

Constructor with number of nodes.

Parameters
nNumber of nodes.

◆ directed_graph() [2/3]

lal::graphs::directed_graph::directed_graph ( const directed_graph & g)
inlinenoexcept

Copy constructor.

Parameters
gDirected graph.

◆ directed_graph() [3/3]

lal::graphs::directed_graph::directed_graph ( directed_graph && g)
inlinenoexcept

Move constructor.

Parameters
gDirected graph.

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 normalized only if it was normalized before the call and g is also normalized.

◆ _init()

virtual void lal::graphs::directed_graph::_init ( const uint64_t n)
inlineprotectedvirtualnoexcept

Initializes the memory in the graph hierarchy.

Initializes memory of lal::graphs::directed_graph and lal::graphs::graph classes.

Parameters
nNumber of nodes.
Precondition
The graph is cleared.

Reimplemented from lal::graphs::graph.

Reimplemented in lal::graphs::rooted_tree.

◆ actions_after_add_edge()

virtual void lal::graphs::directed_graph::actions_after_add_edge ( const node u,
const node v )
protectedvirtualnoexcept

Do some extra work after the addition of an edge.

Parameters
uNode of the edge
vNode of the edge

Reimplemented from lal::graphs::graph.

Reimplemented in lal::graphs::rooted_tree.

◆ actions_after_add_edges()

virtual void lal::graphs::directed_graph::actions_after_add_edges ( const edge_list & e)
protectedvirtualnoexcept

Do some extra work after the addition of several edges.

Parameters
eList of edges.

Reimplemented from lal::graphs::graph.

Reimplemented in lal::graphs::rooted_tree.

◆ actions_after_add_edges_bulk()

virtual void lal::graphs::directed_graph::actions_after_add_edges_bulk ( )
protectedvirtualnoexcept

Do some extra work after the addition of several edges in bulk.

This method should only be called after several calls to undirected_graph::add_edge_bulk or directed_graph::add_edge_bulk.

Reimplemented from lal::graphs::graph.

Reimplemented in lal::graphs::rooted_tree.

◆ actions_after_remove_edge()

virtual void lal::graphs::directed_graph::actions_after_remove_edge ( const node u,
const node v )
protectedvirtualnoexcept

Do some extra work after the removal of an edge.

Parameters
uNode of the edge
vNode of the edge

Reimplemented from lal::graphs::graph.

Reimplemented in lal::graphs::rooted_tree.

◆ actions_after_remove_edges()

virtual void lal::graphs::directed_graph::actions_after_remove_edges ( const edge_list & e)
protectedvirtualnoexcept

Do some extra work after the removal of several edges.

Parameters
eList of edges.

Reimplemented from lal::graphs::graph.

Reimplemented in lal::graphs::rooted_tree.

◆ actions_after_remove_edges_bulk()

virtual void lal::graphs::directed_graph::actions_after_remove_edges_bulk ( )
protectedvirtualnoexcept

Do some extra work after the removal of several edges in bulk.

This method should only be called after several calls to undirected_graph::remove_edge_bulk or directed_graph::remove_edge_bulk.

Reimplemented from lal::graphs::graph.

Reimplemented in lal::graphs::rooted_tree.

◆ actions_after_remove_node()

virtual void lal::graphs::directed_graph::actions_after_remove_node ( const node u)
protectedvirtualnoexcept

Do some work before the removal of a vertex.

Parameters
uNode to be removed.

Reimplemented from lal::graphs::graph.

Reimplemented in lal::graphs::rooted_tree.

◆ actions_before_remove_edges_incident_to()

virtual void lal::graphs::directed_graph::actions_before_remove_edges_incident_to ( const node u)
protectedvirtualnoexcept

Do some work before all edges incident to a node is removed.

Parameters
uNode whose incident edges are to be removed.

Reimplemented from lal::graphs::graph.

Reimplemented in lal::graphs::rooted_tree.

◆ add_edge()

virtual directed_graph & lal::graphs::directed_graph::add_edge ( const node s,
const node t,
const bool norm = true,
const bool check_norm = true )
virtualnoexcept

Adds a directed edge to the graph.

Method actions_after_add_edge is called after the edge has been added.

Parameters
sValid node index: \(0 \le s < n\).
tValid node index: \(0 \le t < n\).
normNormalize the graph after the insertion.
check_normIf norm is false then, should we check whether the result is normalized or not? This might be useful in case the resulting graph is normalized. If norm is true then check_norm is ignored.
Precondition
\(u \neq v\). The directed edge \((s,t)\) is not part of the graph.
Postcondition
If norm is true the graph is guaranteed to be normalized after the addition of the edge.

Reimplemented in lal::graphs::rooted_tree.

◆ add_edge_bulk()

directed_graph & lal::graphs::directed_graph::add_edge_bulk ( const node s,
const node t )
noexcept

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.

Parameters
sValid node index: \(0 \le s < n\).
tValid node index: \(0 \le t < n\).
Precondition
\(u \neq v\).
The edge \(\{s,t\}\) is not part of the graph.
Postcondition
If norm is true the graph is guaranteed to be normalized after the addition of the edge.

◆ add_edges()

virtual directed_graph & lal::graphs::directed_graph::add_edges ( const std::vector< edge > & edges,
const bool norm = true,
const bool check_norm = true )
virtualnoexcept

Adds a list of directed edges to the graph.

This operation is faster than adding edges one by one with add_edge since the edges are added in bulk.

Parameters
edgesThe edges to be added.
normNormalize the graph after the insertions.
check_normIf norm is false then, should we check whether the result is normalized or not? This might be useful in case the resulting graph is normalized. If norm is true then check_norm is ignored.
Precondition
All the edges in edges must meet the precondition of method add_edge(node,node,bool,bool).
Postcondition
If norm is true the graph is guaranteed to be normalized after the addition of the edge.

Reimplemented in lal::graphs::rooted_tree.

◆ check_normalized()

bool lal::graphs::directed_graph::check_normalized ( )
nodiscardvirtualnoexcept

Checks if the graph is normalized.

Checks, whether the graph's adjacency structure is normalized or not. In case it is, attribute m_is_normalized is set to true, so method is_normalized evaluates to true.

Reimplemented from lal::graphs::graph.

◆ clear()

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

Frees the memory occupied by this graph.

See _clear for details.

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

◆ disjoint_union()

directed_graph & lal::graphs::directed_graph::disjoint_union ( const directed_graph & g)
noexcept

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.
Postcondition
The graph is normalized only if it was normalized before the call and g is also normalized.

◆ finish_bulk_add()

void lal::graphs::directed_graph::finish_bulk_add ( const bool norm = true,
const bool check = true )
virtualnoexcept

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
normNormalize the graph.
checkCheck wether the graph is normalized or not.

Implements lal::graphs::graph.

Reimplemented in lal::graphs::rooted_tree.

◆ finish_bulk_remove()

void lal::graphs::directed_graph::finish_bulk_remove ( const bool norm = true,
const bool check = true )
virtualnoexcept

Completes the inner structure of the graph after removing edges in bulk.

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

Parameters
normNormalize the graph.
checkCheck wether the graph is normalized or not.

Implements lal::graphs::graph.

Reimplemented in lal::graphs::rooted_tree.

◆ get_degree()

uint64_t lal::graphs::directed_graph::get_degree ( const node u) const
inlinenodiscardnoexcept

Returns the in-degree plus the out-degree of this vertex.

Returns the degree of this vertex in its underlying undirected structure. Same as get_in_degree + get_out_degree.

Parameters
uVertex
Returns
The (in + out) degree of this vertex.

◆ get_in_neighbors()

const neighbourhood & lal::graphs::directed_graph::get_in_neighbors ( const node u) const
inlinenodiscardnoexcept

Returns the in-neighbors of node u.

Parameters
uNode
Returns
The list of nodes entering at node u.

◆ get_out_neighbors()

const neighbourhood & lal::graphs::directed_graph::get_out_neighbors ( const node u) const
inlinenodiscardnoexcept

Returns the out-neighbors of node u.

Parameters
uNode
Returns
The list of nodes leaving node u.

◆ get_Q()

std::vector< edge_pair > lal::graphs::directed_graph::get_Q ( ) const
nodiscardvirtualnoexcept

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.

◆ init()

virtual void lal::graphs::graph::init ( const uint64_t n)
virtualnoexceptinherited

Allocates the necessary memory for this class.

See _init for details.

Parameters
nNumber of nodes.

◆ is_normalized()

bool lal::graphs::graph::is_normalized ( ) const
inlinenodiscardnoexceptinherited

Returns whether this graph is normalized or not.

A graph is normalized if every node's adjacency list is sorted increasingly. For this, use method normalize().

Returns
The value of m_is_normalized.

◆ normalize()

void lal::graphs::directed_graph::normalize ( )
virtualnoexcept

Normalizes the graph.

Sorts this graph's adjacency list structure in increasing order.

Besides expensive, this method may be unnecessary. Method check_normalized() checks whether the graph is normalized or not; in case it is, using this method is completely unnecessary.

Postcondition
Method is_normalized evaluates to true.

Reimplemented from lal::graphs::graph.

◆ operator=() [1/2]

directed_graph & lal::graphs::directed_graph::operator= ( const directed_graph & g)
inlinenoexcept

Copy assignment operator.

Parameters
gDirected graph.

◆ operator=() [2/2]

directed_graph & lal::graphs::directed_graph::operator= ( directed_graph && g)
inlinenoexcept

Move assignment operator.

Parameters
gDirected graph.

◆ remove_edge()

virtual directed_graph & lal::graphs::directed_graph::remove_edge ( const node s,
const node t,
const bool norm = true,
const bool check_norm = true )
virtualnoexcept

Remove an edge from this graph.

Method actions_after_remove_edge is called after the edge has been removed.

Parameters
sValid node index: \(0 \le s < n\).
tValid node index: \(0 \le t < n\).
normNormalize the graph after the deletion.
check_normIf norm is false then, should we check whether the result is normalized or not? This might be useful in case the resulting graph is normalized. If norm is true then check_norm is ignored.
Precondition
The edge must exist.
Postcondition
If norm is true the graph is guaranteed to be normalized after the addition of the edge.

Reimplemented in lal::graphs::rooted_tree.

◆ remove_edge_bulk()

virtual directed_graph & lal::graphs::directed_graph::remove_edge_bulk ( const node s,
const node t )
virtualnoexcept

Removes an edge from the graph.

This method only removes an edge, and does no other work: normalisation is not checked, and no extra work per edge is done.

Parameters
sValid node index: \(0 \le s < n\).
tValid node index: \(0 \le t < n\).
Precondition
\(u \neq v\).
The edge \(\{s,t\}\) is part of the graph.
Postcondition
If norm is true the graph is guaranteed to be normalized after the removal of the edge.

Reimplemented in lal::graphs::rooted_tree.

◆ remove_edges()

virtual directed_graph & lal::graphs::directed_graph::remove_edges ( const std::vector< edge > & edges,
const bool norm = true,
const bool check_norm = true )
virtualnoexcept

Remove an edge from this graph.

This operation is faster than removing edges one by one with remove_edge since the edges are removed in bulk.

Parameters
edgesThe edges to be deleted.
normNormalize the graph after the deletion.
check_normIf norm is false then, should we check whether the result is normalized or not? This might be useful in case the resulting graph is normalized. If norm is true then check_norm is ignored.
Precondition
All the edges in edges must meet the precondition of method add_edge(node,node,bool,bool).
Postcondition
If norm is true the graph is guaranteed to be normalized after the addition of the edge.

Reimplemented in lal::graphs::rooted_tree.

◆ remove_edges_incident_to()

virtual directed_graph & lal::graphs::directed_graph::remove_edges_incident_to ( const node u,
const bool norm = true,
const bool check_norm = true )
virtualnoexcept

Remove all edges incident to a given vertex.

This operation is faster than removing edges one by one with remove_edge since the edges are removed in bulk.

Method actions_after_remove_edge is called after each edge has been removed.

Parameters
uThe node whose incident vertices are to be removed.
normNormalize the graph after the deletion.
check_normIf norm is false then, should we check whether the result is normalized or not? This might be useful in case the resulting graph is normalized. If norm is true then check_norm is ignored.
Postcondition
If norm is true the graph is guaranteed to be normalized after the addition of the edge.

Reimplemented in lal::graphs::rooted_tree.

◆ remove_node()

virtual directed_graph & lal::graphs::directed_graph::remove_node ( const node u,
const bool norm = true,
const bool check_norm = true )
virtualnoexcept

Remove a node from this graph.

Parameters
uValid node index: \(0 \le u < n\).
normNormalize the graph after the deletion.
check_normIf norm is false then, should we check whether the result is normalized or not? This might be useful in case the resulting graph is normalized. If norm is true then check_norm is ignored.
Precondition
The node must exist.
Postcondition
If norm is true the graph is guaranteed to be normalized after the addition of the edge.

Reimplemented in lal::graphs::rooted_tree.

◆ remove_single_edge()

void lal::graphs::directed_graph::remove_single_edge ( const node u,
const node v,
neighbourhood & out_u,
neighbourhood & in_v )
privatenoexcept

Removes a single edge.

Parameters
uFirst node of edge.
vSecond node of edge.
out_uOut-neighbourhood of node u.
in_vIn-neighbourhood of node v.

◆ reserve_in_degree()

void lal::graphs::directed_graph::reserve_in_degree ( const node u,
const uint64_t d )
inlinenoexcept

Predicts that the in-degree of node u is d.

Memory of size d is reserved so that adding edges is done more efficiently.

Parameters
uNode to reserve the degree for.
dThe amount of memory to reserve.
Precondition
The graph must have been initialized.

◆ reserve_out_degree()

void lal::graphs::directed_graph::reserve_out_degree ( const node u,
const uint64_t d )
inlinenoexcept

Predicts that the out-degree of node u is d.

Memory of size d is reserved so that adding edges is done more efficiently.

Parameters
uNode to reserve the degree for.
dThe amount of memory to reserve.
Precondition
The graph must have been initialized.

◆ set_edges()

virtual directed_graph & lal::graphs::directed_graph::set_edges ( const std::vector< edge > & edges,
const bool norm = true,
const bool check_norm = true )
virtualnoexcept

Sets the list of 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).

Moreover, the current structure of the graph is cleared before setting the new edges.

Parameters
edgesThe edges to be added.
normNormalize the graph after the insertions.
check_normIf norm is false then, should we check whether the result is normalized or not? This might be useful in case the resulting graph is normalized. If norm is true then check_norm is ignored.
Precondition
The graph has been initialized with as many nodes as vertices in the list of edges.
There are no repeated edges in the list.
Postcondition
If norm is true the graph is guaranteed to be normalized after the addition of the edge.

Reimplemented in lal::graphs::rooted_tree.

◆ to_undirected()

undirected_graph lal::graphs::directed_graph::to_undirected ( const bool norm = true,
const bool check = true ) const
nodiscardnoexcept

Converts this directed graph into an undirected graph.

The undirected graph returned connects two vertices \(u,v\) if these two vertices are connected by a directed edge ( \((u,v)\) or \((v,u)\)) in this graph. In other words, if two vertices are connected by a single directed edge, the direction is dropped. If two edges are connected by two directed edges (of opposite directions) then the two are merged into a single undirected edge.

Parameters
normNormalize the graph.
checkChech whether the resulting graph is normalized or not.
Returns
This graph in which the edges are undirected.

Member Data Documentation

◆ m_is_normalized

bool lal::graphs::graph::m_is_normalized = true
protectedinherited

Is this graph normalized?

An undirected graph is normalized 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-neighbors and in-neighbors of nodes be sorted.

This attribute is set to 'true' in all graph's initialisation and destruction (when clear() method is called).


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