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

a static graph in compressed sparse rows, the arcs of each node sorted by head, their heads and their weights in two arrays or, with ArcLayout::Interleaved, in one array of pairs More...

#include <ADS/graph/StaticGraph.h>

Public types

using Weight = W
 the type of the weights
using Arc = std::conditional_t< weighted , WeightedArc< W > , int >
 an arc: its head, and its weight if the graph is weighted
using ArcCursor
 the cursor of the arcs: a pointer to the heads if the graph is unweighted, to the pairs if interleaved
using ArcRange
 the arcs of a row, random access
using HeadArray
 the heads of the arcs read in place by their places: a pointer, or the heads of the pairs if interleaved
using WeightArray
 the weights of the arcs read in place by their places

Public member functions

 StaticGraph (void)
 the graph with no node
 StaticGraph (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
 StaticGraph (int n, const std::vector< std::pair< int, int > > &E, bool directed=true)
 the unweighted graph of the nodes \( 0 , \ldots , n - 1 \) and of the edges \( E \)
template<Graph G>
 StaticGraph (const G &g)
 the copy of any graph, the names that are not nodes of \( g \) becoming isolated nodes
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
int bound (void) const
bool hasNode (int u) const
int degree (int u) const
std::vector< int > V (void) const
std::vector< int > adj (int u) const
template<class F>
void forEachAdj (int u, F &&f) const
template<class F>
requires ( weighted )
void forEachEdge (int u, F &&f) const
 \( f( v , w ) \) on each arc \( ( u , v ) \) of weight \( w \)
std::optional< W > weight (int u, int v) const
 the weight of the first arc \( ( u , v ) \), nothing if there is none, by a binary search of the row of \( u \), in \( O( \log |\mathrm{adj}( u )| ) \)
bool setWeight (int u, int v, const W &w)
 the weight of the first arc \( ( u , v ) \) set to \( w \), and that of the first arc \( ( v , u ) \) if the graph is unoriented, in \( O( \log |\mathrm{adj}( u )| + \log |\mathrm{adj}( v )| ) \); false if there is no such arc
ArcRange out (int u) const
 the arcs out of \( u \), contiguous, random access: on a weighted graph the WeightedArc values made from heads() and weightData(), or the pairs if interleaved
const int * offsets (void) const
 the offsets of the rows: the arcs of u are those in the places [ offsets()[ u ] , offsets()[ u + 1 ] ) of arcData(), of heads() and of weightData(), \( n + 1 \) of them
const int * heads (void) const
 the heads of all the arcs, row by row, arcs() of them, an array of the separate layout
const W * weightData (void) const
 the weights of all the arcs, in the places of heads(), an array of the separate layout
HeadArray arcHeads (void) const
 the heads of all the arcs read in place, in either layout: heads(), or the heads of the pairs
WeightArray arcWeights (void) const
 the weights of all the arcs read in place, in either layout
ArcCursor arcData (void) const
 all the arcs, row by row: the cursor at the first, whose place \( k \) is the arc \( k \) (the pointer heads() if the graph is unweighted, the pointer to the pairs if interleaved)
StaticGraph transpose (void) const
 the graph with each arc reversed (the same graph if unoriented)
StaticGraph relabel (const std::vector< int > &p) const
 the same graph with the node \( u \) renamed \( p[ u ] \)
auto nodes (void) const
 the nodes, \( 0 , \ldots , n - 1 \), random access
template<class A>
void persist (A &a)
 the members written to, or read from, the archive \( a \) [see Persistence.h]

Static public member functions

static int head (const Arc &a)
 the head of an arc
static constexpr std::uint64_t persistTag (void)
 the type of the weights, in a file

Static public attributes

static constexpr bool weighted = ! std::is_same_v< W , std::monostate >
 whether the arcs have a weight
static constexpr ArcLayout layout = L
 the layout of the arcs
static constexpr bool interleaved
 whether the arcs are the pairs of one array (never if unweighted)
static constexpr std::string_view persistName = "StaticGraph"
 the name of the structure in a file [see Persistence.h]

Detailed description

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
class ADS::StaticGraph< W, L >

a static graph in compressed sparse rows, the arcs of each node sorted by head, their heads and their weights in two arrays or, with ArcLayout::Interleaved, in one array of pairs

Member typedef documentation

◆ Arc

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
using ADS::StaticGraph< W, L >::Arc = std::conditional_t< weighted , WeightedArc< W > , int >

an arc: its head, and its weight if the graph is weighted

◆ ArcCursor

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
using ADS::StaticGraph< W, L >::ArcCursor
Initial value:
std::conditional_t<
! weighted , const int * ,
std::conditional_t< interleaved , const WeightedArc< W > * ,
detail::WeightedArcCursor< W > > >
static constexpr bool weighted
whether the arcs have a weight
Definition StaticGraph.h:282

the cursor of the arcs: a pointer to the heads if the graph is unweighted, to the pairs if interleaved

◆ ArcRange

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
using ADS::StaticGraph< W, L >::ArcRange
Initial value:
std::conditional_t<
! weighted , std::span< const int > ,
std::conditional_t< interleaved , std::span< const WeightedArc< W > > ,
std::ranges::subrange< ArcCursor > > >

the arcs of a row, random access

◆ HeadArray

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
using ADS::StaticGraph< W, L >::HeadArray
Initial value:
std::conditional_t< interleaved ,
detail::ArcMember< W , int , &WeightedArc< W >::to > ,
const int * >
static constexpr bool interleaved
whether the arcs are the pairs of one array (never if unweighted)
Definition StaticGraph.h:288

the heads of the arcs read in place by their places: a pointer, or the heads of the pairs if interleaved

◆ Weight

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
using ADS::StaticGraph< W, L >::Weight = W

the type of the weights

◆ WeightArray

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
using ADS::StaticGraph< W, L >::WeightArray
Initial value:
std::conditional_t<
interleaved , detail::ArcMember< W , W , &WeightedArc< W >::w > ,
const W * >

the weights of the arcs read in place by their places

Constructor and destructor documentation

◆ StaticGraph() [1/4]

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
ADS::StaticGraph< W, L >::StaticGraph ( void )
inline

the graph with no node

◆ StaticGraph() [2/4]

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
ADS::StaticGraph< W, L >::StaticGraph ( 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 (two passes of counting sort, the second stable, so that the heads of each row come sorted):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{build}( n , E ) \cr 2 & \quad A \gets \text{the arcs of } E \text{ , both } ( u , v ) \text{ } \text{and } ( v , u ) \text{ if unoriented} \cr 3 & \quad A \gets \text{the arcs of } A \text{ sorted by head, by } \text{counting} \cr 4 & \quad \textbf{foreach}\ ( u , v ) \in A\ \textbf{do}\ \mathit{off}[ u + 1 ] \gets \mathit{off}[ u + 1 ] + 1 \cr 5 & \quad \textbf{for}\ u \gets 1\ \textbf{to}\ n\ \textbf{do} \ \mathit{off}[ u ] \gets \mathit{off}[ u ] + \mathit{off}[ u - 1 ] \cr 6 & \quad \textbf{foreach}\ ( u , v , w ) \in A \text{ in order } \ \textbf{do}\ p \gets \mathit{next}[ u ];\ \mathit{next}[ u ] \gets p + 1\quad \triangleright\ \mathit{next}[ u ] \text{ starts } \text{at } \mathit{off}[ u ] \cr 7 & \qquad \mathit{head}[ p ] \gets v;\ \mathit{weight}[ p ] \gets w \cr 8 & \quad \textbf{return}\ ( \mathit{off} , \mathit{head} , \mathit{weight} ) \end{array}} \]

Precondition
\( n \ge 0 \), each edge joins two distinct nodes, fewer than \( 2^{31} \) arcs

◆ StaticGraph() [3/4]

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
ADS::StaticGraph< W, L >::StaticGraph ( int n,
const std::vector< std::pair< int, int > > & E,
bool directed = true )

the unweighted graph of the nodes \( 0 , \ldots , n - 1 \) and of the edges \( E \)

Precondition
\( n \ge 0 \), each edge joins two distinct nodes, fewer than \( 2^{31} \) arcs

◆ StaticGraph() [4/4]

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
template<Graph G>
ADS::StaticGraph< W, L >::StaticGraph ( const G & g)
explicit

the copy of any graph, the names that are not nodes of \( g \) becoming isolated nodes

Member function documentation

◆ adj()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
std::vector< int > ADS::StaticGraph< W, L >::adj ( int u) const
nodiscard
Precondition
\( u \) is a node

◆ arcData()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
ArcCursor ADS::StaticGraph< W, L >::arcData ( void ) const
inlinenodiscard

all the arcs, row by row: the cursor at the first, whose place \( k \) is the arc \( k \) (the pointer heads() if the graph is unweighted, the pointer to the pairs if interleaved)

◆ arcHeads()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
HeadArray ADS::StaticGraph< W, L >::arcHeads ( void ) const
inlinenodiscard

the heads of all the arcs read in place, in either layout: heads(), or the heads of the pairs

◆ arcs()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
int ADS::StaticGraph< W, L >::arcs ( void ) const
inlinenodiscard

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

◆ arcWeights()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
WeightArray ADS::StaticGraph< W, L >::arcWeights ( void ) const
inlinenodiscard

the weights of all the arcs read in place, in either layout

◆ bound()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
int ADS::StaticGraph< W, L >::bound ( void ) const
inlinenodiscard

◆ degree()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
int ADS::StaticGraph< W, L >::degree ( int u) const
nodiscard
Precondition
\( u \) is a node

◆ directed()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
bool ADS::StaticGraph< W, L >::directed ( void ) const
inlinenodiscard

whether the graph is oriented

◆ forEachAdj()

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

◆ forEachEdge()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
template<class F>
requires ( weighted )
void ADS::StaticGraph< W, L >::forEachEdge ( int u,
F && f ) const

\( f( v , w ) \) on each arc \( ( u , v ) \) of weight \( w \)

Precondition
\( u \) is a node

◆ hasNode()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
bool ADS::StaticGraph< W, L >::hasNode ( int u) const
inlinenodiscard

◆ head()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
int ADS::StaticGraph< W, L >::head ( const Arc & a)
inlinestatic

the head of an arc

◆ heads()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
const int * ADS::StaticGraph< W, L >::heads ( void ) const
inlinenodiscard

the heads of all the arcs, row by row, arcs() of them, an array of the separate layout

◆ m()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
int ADS::StaticGraph< W, L >::m ( void ) const
inlinenodiscard

the number of edges, an unoriented one counted once

◆ n()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
int ADS::StaticGraph< W, L >::n ( void ) const
inlinenodiscard

the number of nodes

◆ nodes()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
auto ADS::StaticGraph< W, L >::nodes ( void ) const
inlinenodiscard

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

◆ offsets()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
const int * ADS::StaticGraph< W, L >::offsets ( void ) const
inlinenodiscard

the offsets of the rows: the arcs of u are those in the places [ offsets()[ u ] , offsets()[ u + 1 ] ) of arcData(), of heads() and of weightData(), \( n + 1 \) of them

◆ out()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
ArcRange ADS::StaticGraph< W, L >::out ( int u) const
nodiscard

the arcs out of \( u \), contiguous, random access: on a weighted graph the WeightedArc values made from heads() and weightData(), or the pairs if interleaved

Precondition
\( u \) is a node

◆ persist()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
template<class A>
void ADS::StaticGraph< W, L >::persist ( A & a)

the members written to, or read from, the archive \( a \) [see Persistence.h]

◆ persistTag()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
constexpr std::uint64_t ADS::StaticGraph< W, L >::persistTag ( void )
inlinestaticnodiscardconstexpr

the type of the weights, in a file

◆ relabel()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
StaticGraph ADS::StaticGraph< W, L >::relabel ( const std::vector< int > & p) const
nodiscard

the same graph with the node \( u \) renamed \( p[ u ] \)

In pseudocode:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{relabel}( p ) \cr 2 & \quad \textbf{return}\ \mathrm{build}( n , \text{the arcs } ( p[ u ] , p[ v ] ) \text{ for each arc } ( u , v ) ) \end{array}} \]

Precondition
\( p \) is a permutation of the nodes

◆ setWeight()

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

the weight of the first arc \( ( u , v ) \) set to \( w \), and that of the first arc \( ( v , u ) \) if the graph is unoriented, in \( O( \log |\mathrm{adj}( u )| + \log |\mathrm{adj}( v )| ) \); false if there is no such arc

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

◆ transpose()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
StaticGraph ADS::StaticGraph< W, L >::transpose ( void ) const
nodiscard

the graph with each arc reversed (the same graph if unoriented)

In pseudocode:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{transpose}() \cr 2 & \quad \textbf{return}\ \mathrm{build}( n , \text{the arcs } ( v , u ) \text{ for each arc } ( u , v ) ) \end{array}} \]

◆ V()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
std::vector< int > ADS::StaticGraph< W, L >::V ( void ) const
nodiscard

◆ weight()

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

the weight of the first arc \( ( u , v ) \), nothing if there is none, by a binary search of the row of \( u \), in \( O( \log |\mathrm{adj}( u )| ) \)

◆ weightData()

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
const W * ADS::StaticGraph< W, L >::weightData ( void ) const
inlinenodiscard

the weights of all the arcs, in the places of heads(), an array of the separate layout

Member data documentation

◆ interleaved

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
bool ADS::StaticGraph< W, L >::interleaved
staticconstexpr
Initial value:
=
@ Interleaved
the pairs WeightedArc in one array
Definition StaticGraph.h:135

whether the arcs are the pairs of one array (never if unweighted)

◆ layout

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
ArcLayout ADS::StaticGraph< W, L >::layout = L
staticconstexpr

the layout of the arcs

◆ persistName

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
std::string_view ADS::StaticGraph< W, L >::persistName = "StaticGraph"
staticconstexpr

the name of the structure in a file [see Persistence.h]

◆ weighted

template<class W = std::monostate, ArcLayout L = ArcLayout::Separate>
bool ADS::StaticGraph< W, L >::weighted = ! std::is_same_v< W , std::monostate >
staticconstexpr

whether the arcs have a weight


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