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

a graph with an adjacency matrix More...

#include <ADS/graph/MatrixGraph.h>

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

Classes

struct  Present
 the columns of the edges in a row, for SlotIterator More...

Public types

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

Public member functions

 MatrixGraph (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, a new row and a new column, \( O( n ) \) time
void insertEdge (int u, int v, const W &w=W())
 the edge \( ( u , v ) \), or \( [ u , v ] \), of weight \( w \), \( O( 1 ) \) time; only the weight changes if it is there
void deleteEdge (int u, int v)
void deleteNode (int u)
 removes the node \( u \) and its edges, \( \Theta( n ) \) time
bool hasEdge (int u, int v) const
 \( O( 1 ) \) time
std::optional< W > weight (int u, int v) const
 the weight of \( ( u , v ) \), nothing (the infinity of the book) if there is no such edge
std::vector< int > adj (int u) const
 the set of the nodes adjacent to \( u \), in increasing order, \( \Theta( n ) \) time
std::vector< int > inAdj (int u) const
 the nodes with an edge to \( u \), in increasing order, the column of \( u \), \( \Theta( n ) \) time (the inEdges( i ) of the AdjacencyMatrix of section 12.1 of Morin's book)
template<class F>
void forEachAdj (int u, F &&f) const
template<class F>
void forEachEdge (int u, F &&f) const
auto neighbors (int u) const
 the nodes adjacent to \( u \) in increasing order, 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::MatrixGraph< W, T >

a graph with an adjacency matrix

Member typedef documentation

◆ Label

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

the type of the labels

◆ Weight

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

the type of the weights

Constructor and destructor documentation

◆ MatrixGraph()

template<class W = std::monostate, class T = std::monostate>
ADS::MatrixGraph< W, T >::MatrixGraph ( 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::MatrixGraph< W, T >::adj ( int u) const
nodiscard

the set of the nodes adjacent to \( u \), in increasing order, \( \Theta( n ) \) time

Precondition
\( u \) is a node

◆ deleteEdge()

template<class W = std::monostate, class T = std::monostate>
void ADS::MatrixGraph< W, T >::deleteEdge ( int u,
int v )
Precondition
\( u \) and \( v \) are distinct nodes
Postcondition
not hasEdge( u , v )

◆ deleteNode()

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

removes the node \( u \) and its edges, \( \Theta( n ) \) time

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

◆ forEachAdj()

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

◆ forEachEdge()

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

◆ hasEdge()

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

\( O( 1 ) \) time

◆ inAdj()

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

the nodes with an edge to \( u \), in increasing order, the column of \( u \), \( \Theta( n ) \) time (the inEdges( i ) of the AdjacencyMatrix of section 12.1 of Morin's book)

In pseudocode (section 12.1 of Morin's book), \( M \) the matrix:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{inAdj}( u ) \cr 2 & \quad L \gets () \cr 3 & \quad \textbf{for}\ v \gets 0\ \textbf{to}\ \mathrm{bound}() - 1 \ \textbf{do} \cr 4 & \qquad \textbf{if}\ M[ v ][ u ] \ne \textbf{nil}\ \textbf{then} \ L \gets L \cdot ( v ) \cr 5 & \quad \textbf{return}\ L \end{array}} \]

Precondition
\( u \) is a node

◆ insertEdge()

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

the edge \( ( u , v ) \), or \( [ u , v ] \), of weight \( w \), \( O( 1 ) \) time; only the weight changes if it is there

In pseudocode (section 9.3), \( M \) the matrix:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insertEdge}( u , v , w )\quad \triangleright\ \textbf{nil} \text{ marks no edge} \cr 2 & \quad m \gets m + [ M[ u ][ v ] = \textbf{nil} ];\ M[ u ][ v ] \gets w \cr 3 & \quad \textbf{if}\ G \text{ is unoriented}\ \textbf{then}\ M[ v ][ u ] \gets w\quad \triangleright\ \text{a symmetric matrix} \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::MatrixGraph< W, T >::insertNode ( const T & t = T())

a new node, a new row and a new column, \( O( n ) \) time

◆ neighbors()

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

the nodes adjacent to \( u \) in increasing order, bidirectional

Precondition
\( u \) is a node

◆ weight()

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

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


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