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

a graph with lists of adjacency More...

#include <ADS/graph/ListGraph.h>

Inheritance diagram for ADS::ListGraph< W, T >:
ADS::GraphNodes< std::monostate >

Public types

using Weight = W
 the type of the weights
using Label = T
 the type of the labels

Public member functions

 ListGraph (int n=0, bool directed=true)
 the graph of the nodes \( 0 , \ldots , n - 1 \), with no edge
int insertNode (const T &t=T())
 a new node, with no edge, and its name
void insertEdge (int u, int v, const W &w=W())
 the edge \( ( u , v ) \), or \( [ u , v ] \), of weight \( w \); only the weight changes if it is there, \( O( \deg( u ) ) \) time (\( O( \deg( u ) + \deg( v ) ) \) if unoriented) for the search of the edge
void deleteEdge (int u, int v)
 removes the edge \( ( u , v ) \), or \( [ u , v ] \), if it is there
void deleteNode (int u)
 removes the node \( u \) and its edges, \( O( n + m ) \) time
bool hasEdge (int u, int v) const
 whether \( ( u , v ) \) is an edge
std::optional< W > weight (int u, int v) const
 the weight of \( ( u , v ) \), nothing if there is no such edge
std::vector< int > adj (int u) const
 the set of the nodes adjacent to \( u \), in the order of insertion
std::vector< int > inAdj (int u) const
 the nodes with an edge to \( u \), in increasing order, by a scan of all the lists, \( O( n + m ) \) time (the inEdges( i ) of the AdjacencyLists of section 12.2 of Morin's book); the lists of an unoriented graph give them in \( O( \deg( u ) ) \)
template<class F>
void forEachAdj (int u, F &&f) const
 \( f( v ) \) on the nodes adjacent to \( u \)
template<class F>
void forEachEdge (int u, F &&f) const
 \( f( v , w ) \) on the nodes adjacent to \( u \) and the weights of the edges
auto neighbors (int u) const
 the nodes adjacent to \( u \), in the order of its list, bidirectional
Public member functions inherited from ADS::GraphNodes< std::monostate >
bool directed (void) const
 whether the graph is oriented
int n (void) const
 the number of nodes, the G.n of the book
int m (void) const
 the number of edges, an unoriented one counted once
int bound (void) const
 the names of the nodes are in \( [ 0 , \mathrm{bound}() ) \)
bool hasNode (int u) const
 whether \( u \) is a node
std::vector< int > V (void) const
 the set of the nodes, in increasing order
const std::monostate & label (int u) const
 the label of the node \( u \)
void setLabel (int u, const std::monostate &t)
 the label of the node \( u \) becomes \( t \)
auto nodes (void) const
 the nodes in increasing order, bidirectional

Additional Inherited Members

Protected member functions inherited from ADS::GraphNodes< std::monostate >
 GraphNodes (int n, bool directed)
int addNode (const std::monostate &t)
 a new node of label \( t \), and its name
int check (int u) const
 \( u \), which must be a node
void edge (int u, int v) const
 \( u \) and \( v \) must be two distinct nodes
bool nodesCounted (void) const
 n() counts the names in use, and each has a label
Protected attributes inherited from ADS::GraphNodes< std::monostate >
bool f_directed
 oriented or not
std::vector< bool > f_alive
 whether a name is a node
std::vector< std::monostate > f_label
 the labels
int f_n
 the number of nodes
int f_m
 the number of edges

Detailed description

template<class W = std::monostate, class T = std::monostate>
class ADS::ListGraph< W, T >

a graph with lists of adjacency

Member typedef documentation

◆ Label

template<class W = std::monostate, class T = std::monostate>
using ADS::ListGraph< W, T >::Label = T

the type of the labels

◆ Weight

template<class W = std::monostate, class T = std::monostate>
using ADS::ListGraph< W, T >::Weight = W

the type of the weights

Constructor and destructor documentation

◆ ListGraph()

template<class W = std::monostate, class T = std::monostate>
ADS::ListGraph< W, T >::ListGraph ( int n = 0,
bool directed = true )
explicit

the graph of the nodes \( 0 , \ldots , n - 1 \), with no edge

Precondition
\( n \ge 0 \)

Member function documentation

◆ adj()

template<class W = std::monostate, class T = std::monostate>
std::vector< int > ADS::ListGraph< W, T >::adj ( int u) const
nodiscard

the set of the nodes adjacent to \( u \), in the order of insertion

Precondition
\( u \) is a node

◆ deleteEdge()

template<class W = std::monostate, class T = std::monostate>
void ADS::ListGraph< W, T >::deleteEdge ( int u,
int v )

removes the edge \( ( u , v ) \), or \( [ u , v ] \), if it is there

Precondition
\( u \) and \( v \) are distinct nodes
Postcondition
not hasEdge( u , v )

◆ deleteNode()

template<class W = std::monostate, class T = std::monostate>
void ADS::ListGraph< W, T >::deleteNode ( int u)

removes the node \( u \) and its edges, \( O( n + m ) \) time

In pseudocode:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{deleteNode}( u ) \cr 2 & \quad \textbf{foreach}\ x \in V\ \textbf{do}\ \text{remove } u \text{ from the list of } x \cr 3 & \quad \text{empty the list of } u;\ \text{mark the name } u \text{ } \text{unused} \end{array}} \]

Precondition
\( u \) is a node
Postcondition
not hasNode( u )

◆ forEachAdj()

template<class W = std::monostate, class T = std::monostate>
template<class F>
void ADS::ListGraph< W, T >::forEachAdj ( int u,
F && f ) const

\( f( v ) \) on the nodes adjacent to \( u \)

Precondition
\( u \) is a node

◆ forEachEdge()

template<class W = std::monostate, class T = std::monostate>
template<class F>
void ADS::ListGraph< W, T >::forEachEdge ( int u,
F && f ) const

\( f( v , w ) \) on the nodes adjacent to \( u \) and the weights of the edges

Precondition
\( u \) is a node

◆ hasEdge()

template<class W = std::monostate, class T = std::monostate>
bool ADS::ListGraph< W, T >::hasEdge ( int u,
int v ) const
nodiscard

whether \( ( u , v ) \) is an edge

◆ inAdj()

template<class W = std::monostate, class T = std::monostate>
std::vector< int > ADS::ListGraph< W, T >::inAdj ( int u) const
nodiscard

the nodes with an edge to \( u \), in increasing order, by a scan of all the lists, \( O( n + m ) \) time (the inEdges( i ) of the AdjacencyLists of section 12.2 of Morin's book); the lists of an unoriented graph give them in \( O( \deg( u ) ) \)

In pseudocode (section 12.2 of Morin's book), for an oriented graph:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{inAdj}( u ) \cr 2 & \quad I \gets () \cr 3 & \quad \textbf{foreach}\ v \in V\ \textbf{do} \cr 4 & \qquad \textbf{foreach}\ x \in G.\mathrm{adj}( v )\ \textbf{with} \ x = u\ \textbf{do}\ I \gets I \cdot ( v ) \cr 5 & \quad \textbf{return}\ I \end{array}} \]

Precondition
\( u \) is a node

◆ insertEdge()

template<class W = std::monostate, class T = std::monostate>
void ADS::ListGraph< W, T >::insertEdge ( int u,
int v,
const W & w = W() )

the edge \( ( u , v ) \), or \( [ u , v ] \), of weight \( w \); only the weight changes if it is there, \( O( \deg( u ) ) \) time (\( O( \deg( u ) + \deg( v ) ) \) if unoriented) for the search of the edge

In pseudocode (section 9.4), \( L_u \) the list of \( u \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insertEdge}( u , v , w ) \cr 2 & \quad c \gets \text{the cell of } v \text{ in } L_u \text{, by a scan} \cr 3 & \quad \textbf{if}\ c \ne \textbf{nil}\ \textbf{then}\ \text{give } c \text{ the weight } w\quad \triangleright\ \text{and the cell of } u \text{ in } L_v \text{ if unoriented} \cr 4 & \quad \textbf{else}\ \text{append } ( v , w ) \text{ to } L_u \text{, and } ( u , w ) \text{ to } L_v \text{ if unoriented};\ m \gets m + 1 \end{array}} \]

Precondition
\( u \) and \( v \) are distinct nodes
Postcondition
hasEdge( u , v )

◆ insertNode()

template<class W = std::monostate, class T = std::monostate>
int ADS::ListGraph< W, T >::insertNode ( const T & t = T())

a new node, with no edge, and its name

◆ neighbors()

template<class W = std::monostate, class T = std::monostate>
auto ADS::ListGraph< W, T >::neighbors ( int u) const
inlinenodiscard

the nodes adjacent to \( u \), in the order of its list, bidirectional

Precondition
\( u \) is a node

◆ weight()

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

the weight of \( ( u , v ) \), nothing if there is no such edge


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