ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::ArrayGraph< W > Class Template Reference

a static graph with vectors of adjacency, all in one vector More...

#include <ADS/graph/ArrayGraph.h>

Public types

using Weight = W
 the type of the weights

Public member functions

 ArrayGraph (int n, const std::vector< std::tuple< int, int, W > > &E, bool directed=true)
 the graph of the nodes \( 0 , \ldots , n - 1 \) and of the given edges; an unoriented edge \( [ u , v ] \) is given once
template<Graph G>
 ArrayGraph (const G &g)
 the copy of any graph, with the nodes 0 , ... , g.bound() - 1: the names that are not nodes of g, deleted ones, become isolated nodes, so that each node keeps its name
bool directed (void) const
 whether the graph is oriented
int n (void) const
 the number of nodes
int m (void) const
 the number of edges, an unoriented one counted once
int arcs (void) const
 the number of arcs, 2 m() if the graph is unoriented, as in StaticGraph
int bound (void) const
 the names of the nodes, \( [ 0 , \mathrm{bound}() ) = [ 0 , n ) \)
bool hasNode (int u) const
 whether \( u \) is a node
std::vector< int > V (void) const
 the set of the nodes, in increasing order
std::vector< int > adj (int u) const
template<class F>
void forEachAdj (int u, F &&f) const
template<class F>
void forEachEdge (int u, F &&f) const
std::optional< W > weight (int u, int v) const
 the weight of \( ( u , v ) \), nothing if there is no such edge, in \( O( |\mathrm{adj}( u )| ) \)
bool setWeight (int u, int v, const W &w)
 the weight of \( ( u , v ) \) set to \( w \) (of both its arcs if unoriented), in \( O( |\mathrm{adj}( u )| + |\mathrm{adj}( v )| ) \); false if there is no such edge
auto nodes (void) const
 the nodes, \( 0 , \ldots , n - 1 \), random access
std::span< const int > neighbors (int u) const
 the nodes adjacent to \( u \), contiguous in the array of the edges
const int * offsets (void) const
 the offsets of the rows, \( n + 1 \) of them: the adjacent nodes of \( u \) are the places from offsets()[ u ] to offsets()[ u + 1 ] of heads(), and their weights the same places of weightData() (the arrays that the adapters to other libraries read in place)
const int * heads (void) const
 the adjacent nodes, row by row
const W * weightData (void) const
 the weights, in the places of heads()
const int * arcHeads (void) const
 the heads of all the arcs read in place, heads(), as in StaticGraph
const W * arcWeights (void) const
 the weights of all the arcs read in place, weightData(), as in StaticGraph

Static public attributes

static constexpr bool weighted = ! std::is_same_v< W , std::monostate >
 whether the arcs have a weight, as in StaticGraph

Detailed description

template<class W = std::monostate>
class ADS::ArrayGraph< W >

a static graph with vectors of adjacency, all in one vector

Member typedef documentation

◆ Weight

template<class W = std::monostate>
using ADS::ArrayGraph< W >::Weight = W

the type of the weights

Constructor and destructor documentation

◆ ArrayGraph() [1/2]

template<class W = std::monostate>
ADS::ArrayGraph< W >::ArrayGraph ( int n,
const std::vector< std::tuple< int, int, W > > & E,
bool directed = true )

the graph of the nodes \( 0 , \ldots , n - 1 \) and of the given edges; an unoriented edge \( [ u , v ] \) is given once

In pseudocode (section 9.4), the offsets by counting:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{build}( n , E )\quad \triangleright \ \text{the adjacent nodes of } u \text{ in } [ \mathit{off}[ u ] , \mathit{off}[ u + 1 ] ) \cr 2 & \quad \mathit{off}[ u + 1 ] \gets \text{the degree of } u \text{ in } E \text{ for each node } u\quad \triangleright \ \text{both ends if unoriented} \cr 3 & \quad \text{make } \mathit{off} \text{ its own prefix sums}; \ \mathit{off}[ 0 ] = 0 \cr 4 & \quad \textbf{foreach}\ ( u , v ) \in E\ \textbf{do}\ \text{write } v \text{ in the first free place of the range of } u \cr & \qquad \text{(and } u \text{ in that of } v \text{ if unoriented)} \cr 5 & \quad \textbf{return}\ ( \mathit{off} , \text{the vector of the adjacent nodes} ) \end{array}} \]

Precondition
\( n \ge 0 \), and each edge joins two distinct nodes

◆ ArrayGraph() [2/2]

template<class W = std::monostate>
template<Graph G>
ADS::ArrayGraph< W >::ArrayGraph ( const G & g)
explicit

the copy of any graph, with the nodes 0 , ... , g.bound() - 1: the names that are not nodes of g, deleted ones, become isolated nodes, so that each node keeps its name

Member function documentation

◆ adj()

template<class W = std::monostate>
std::vector< int > ADS::ArrayGraph< W >::adj ( int u) const
nodiscard
Precondition
\( u \) is a node

◆ arcHeads()

template<class W = std::monostate>
const int * ADS::ArrayGraph< W >::arcHeads ( void ) const
inlinenodiscard

the heads of all the arcs read in place, heads(), as in StaticGraph

◆ arcs()

template<class W = std::monostate>
int ADS::ArrayGraph< W >::arcs ( void ) const
inlinenodiscard

the number of arcs, 2 m() if the graph is unoriented, as in StaticGraph

◆ arcWeights()

template<class W = std::monostate>
const W * ADS::ArrayGraph< W >::arcWeights ( void ) const
inlinenodiscard

the weights of all the arcs read in place, weightData(), as in StaticGraph

◆ bound()

template<class W = std::monostate>
int ADS::ArrayGraph< W >::bound ( void ) const
inlinenodiscard

the names of the nodes, \( [ 0 , \mathrm{bound}() ) = [ 0 , n ) \)

◆ directed()

template<class W = std::monostate>
bool ADS::ArrayGraph< W >::directed ( void ) const
inlinenodiscard

whether the graph is oriented

◆ forEachAdj()

template<class W = std::monostate>
template<class F>
void ADS::ArrayGraph< W >::forEachAdj ( int u,
F && f ) const
Precondition
\( u \) is a node

◆ forEachEdge()

template<class W = std::monostate>
template<class F>
void ADS::ArrayGraph< W >::forEachEdge ( int u,
F && f ) const
Precondition
\( u \) is a node

◆ hasNode()

template<class W = std::monostate>
bool ADS::ArrayGraph< W >::hasNode ( int u) const
nodiscard

whether \( u \) is a node

◆ heads()

template<class W = std::monostate>
const int * ADS::ArrayGraph< W >::heads ( void ) const
inlinenodiscard

the adjacent nodes, row by row

◆ m()

template<class W = std::monostate>
int ADS::ArrayGraph< W >::m ( void ) const
inlinenodiscard

the number of edges, an unoriented one counted once

◆ n()

template<class W = std::monostate>
int ADS::ArrayGraph< W >::n ( void ) const
inlinenodiscard

the number of nodes

◆ neighbors()

template<class W = std::monostate>
std::span< const int > ADS::ArrayGraph< W >::neighbors ( int u) const
nodiscard

the nodes adjacent to \( u \), contiguous in the array of the edges

Precondition
\( u \) is a node

◆ nodes()

template<class W = std::monostate>
auto ADS::ArrayGraph< W >::nodes ( void ) const
inlinenodiscard

the nodes, \( 0 , \ldots , n - 1 \), random access

◆ offsets()

template<class W = std::monostate>
const int * ADS::ArrayGraph< W >::offsets ( void ) const
inlinenodiscard

the offsets of the rows, \( n + 1 \) of them: the adjacent nodes of \( u \) are the places from offsets()[ u ] to offsets()[ u + 1 ] of heads(), and their weights the same places of weightData() (the arrays that the adapters to other libraries read in place)

◆ setWeight()

template<class W = std::monostate>
bool ADS::ArrayGraph< W >::setWeight ( int u,
int v,
const W & w )

the weight of \( ( u , v ) \) set to \( w \) (of both its arcs if unoriented), in \( O( |\mathrm{adj}( u )| + |\mathrm{adj}( v )| ) \); false if there is no such edge

Precondition
\( u \) and \( v \) are nodes

◆ V()

template<class W = std::monostate>
std::vector< int > ADS::ArrayGraph< W >::V ( void ) const
nodiscard

the set of the nodes, in increasing order

◆ weight()

template<class W = std::monostate>
std::optional< W > ADS::ArrayGraph< W >::weight ( int u,
int v ) const
nodiscard

the weight of \( ( u , v ) \), nothing if there is no such edge, in \( O( |\mathrm{adj}( u )| ) \)

◆ weightData()

template<class W = std::monostate>
const W * ADS::ArrayGraph< W >::weightData ( void ) const
inlinenodiscard

the weights, in the places of heads()

Member data documentation

◆ weighted

template<class W = std::monostate>
bool ADS::ArrayGraph< W >::weighted = ! std::is_same_v< W , std::monostate >
staticconstexpr

whether the arcs have a weight, as in StaticGraph


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