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

an oriented graph of node objects linked by pointers More...

#include <ADS/graph/PointerGraph.h>

Classes

struct  Edge
 an edge, to its other end More...
struct  Node
 a node More...

Public member functions

 PointerGraph (void)=default
 the empty graph
 PointerGraph (const PointerGraph &)=delete
 no copy, since the nodes are linked by pointers
PointerGraph & operator= (const PointerGraph &)=delete
 no copy, since the nodes are linked by pointers
int n (void) const
 the number of nodes
Node * insertNode (const T &t)
 a new isolated node of label \( t \)
Node * find (const T &t)
 the first node of label \( t \), nullptr if none
bool hasEdge (const Node *u, const Node *v) const
void insertEdge (Node *u, Node *v, const W &w=W())
 the edge \( ( u , v ) \) of weight \( w \), if not there
void deleteEdge (Node *u, Node *v)
bool deleteNode (Node *u)
 deletes \( u \) if isolated; false otherwise
void forceDeleteNode (Node *u)
 deletes the edges of \( u \) and then \( u \)
std::vector< Node * > adjacent (const Node *u) const
 the nodes adjacent to \( u \)
template<class F>
void dfs (Node *r, F &&f)
 the depth first visit from \( r \), \( f \) on each node reached
template<class F>
void bfs (Node *r, F &&f)
 the breadth first visit from \( r \)

Detailed description

template<class T, class W = int>
class ADS::PointerGraph< T, W >

an oriented graph of node objects linked by pointers

Constructor and destructor documentation

◆ PointerGraph() [1/2]

template<class T, class W = int>
ADS::PointerGraph< T, W >::PointerGraph ( void )
default

the empty graph

◆ PointerGraph() [2/2]

template<class T, class W = int>
ADS::PointerGraph< T, W >::PointerGraph ( const PointerGraph< T, W > & )
delete

no copy, since the nodes are linked by pointers

Member function documentation

◆ adjacent()

template<class T, class W = int>
std::vector< Node * > ADS::PointerGraph< T, W >::adjacent ( const Node * u) const
nodiscard

the nodes adjacent to \( u \)

Precondition
\( u \) is a node of the graph

◆ bfs()

template<class T, class W = int>
template<class F>
void ADS::PointerGraph< T, W >::bfs ( Node * r,
F && f )

the breadth first visit from \( r \)

In pseudocode (section 9.5), the flags of the nodes cleared first:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{bfs}( r , f ) \cr 2 & \quad r.\mathit{flag} \gets \textbf{true};\ Q \gets ( r )\quad \triangleright\ \text{marked on entering } Q \cr 3 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 4 & \qquad u \gets \mathrm{dequeue}( Q );\ f( u ) \cr 5 & \qquad \textbf{foreach}\ v \in \mathrm{adjacent}( u ) \text{ with no flag}\ \textbf{do}\ v.\mathit{flag} \gets \textbf{true};\ \mathrm{enqueue}( Q , v ) \end{array}} \]

Precondition
\( r \) is a node of the graph

◆ deleteEdge()

template<class T, class W = int>
void ADS::PointerGraph< T, W >::deleteEdge ( Node * u,
Node * v )
Precondition
\( u \) and \( v \) are nodes of the graph
Postcondition
not hasEdge( u , v )

◆ deleteNode()

template<class T, class W = int>
bool ADS::PointerGraph< T, W >::deleteNode ( Node * u)

deletes \( u \) if isolated; false otherwise

Precondition
\( u \) is a node of the graph

◆ dfs()

template<class T, class W = int>
template<class F>
void ADS::PointerGraph< T, W >::dfs ( Node * r,
F && f )

the depth first visit from \( r \), \( f \) on each node reached

In pseudocode (section 9.5), the flags of the nodes cleared first:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{dfs}( r , f )\quad \triangleright \ \text{the flag of a node marks it reached} \cr 2 & \quad r.\mathit{flag} \gets \textbf{true};\ f( r ) \cr 3 & \quad \textbf{foreach}\ v \in \mathrm{adjacent}( r )\ \textbf{do} \ \textbf{if}\ \textbf{not}\ v.\mathit{flag}\ \textbf{then} \ \mathrm{dfs}( v , f ) \end{array}} \]

Precondition
\( r \) is a node of the graph

◆ find()

template<class T, class W = int>
Node * ADS::PointerGraph< T, W >::find ( const T & t)
nodiscard

the first node of label \( t \), nullptr if none

◆ forceDeleteNode()

template<class T, class W = int>
void ADS::PointerGraph< T, W >::forceDeleteNode ( Node * u)

deletes the edges of \( u \) and then \( u \)

In pseudocode:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{forceDeleteNode}( u ) \cr 2 & \quad \textbf{foreach}\ x \in V\ \textbf{do}\ \text{remove the edge } \text{from } x \text{ to } u \text{ , if there is one} \cr 3 & \quad \text{remove the edges of } u;\ \mathrm{deleteNode}( u ) \end{array}} \]

Precondition
\( u \) is a node of the graph

◆ hasEdge()

template<class T, class W = int>
bool ADS::PointerGraph< T, W >::hasEdge ( const Node * u,
const Node * v ) const
nodiscard
Precondition
\( u \) and \( v \) are nodes of the graph

◆ insertEdge()

template<class T, class W = int>
void ADS::PointerGraph< T, W >::insertEdge ( Node * u,
Node * v,
const W & w = W() )

the edge \( ( u , v ) \) of weight \( w \), if not there

Precondition
\( u \) and \( v \) are nodes of the graph
Postcondition
hasEdge( u , v )

◆ insertNode()

template<class T, class W = int>
Node * ADS::PointerGraph< T, W >::insertNode ( const T & t)

a new isolated node of label \( t \)

◆ n()

template<class T, class W = int>
int ADS::PointerGraph< T, W >::n ( void ) const
inlinenodiscard

the number of nodes

◆ operator=()

template<class T, class W = int>
PointerGraph & ADS::PointerGraph< T, W >::operator= ( const PointerGraph< T, W > & )
delete

no copy, since the nodes are linked by pointers


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