ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Visit.h

The visits of a GRAPH of section 9.5 and their applications, each in \( \Theta( n + m ) \) time on lists or vectors of adjacency. More...

Files

file  ADS/graph/Visit.h
 The visits of a GRAPH of section 9.5 and their applications, each in \( \Theta( n + m ) \) time on lists or vectors of adjacency:

Classes

struct  ADS::VisitCounts
 what a visit did: the nodes taken out of its structure and the edges examined More...
class  ADS::DfsSchema< G >
 the discovery and finish times and the kind of each edge, the procedure dfs-schema of the book More...

Enumerations

enum class  ADS::Choice { ADS::Choice::NEWEST , ADS::Choice::OLDEST }
 the node of \( Q \) that genericVisit() chooses More...
enum class  ADS::EdgeKind { ADS::EdgeKind::TREE , ADS::EdgeKind::BACK , ADS::EdgeKind::FORWARD , ADS::EdgeKind::CROSS }
 the kind of an edge with respect to a depth-first visit More...

Functions

template<Graph G>
std::pair< std::vector< int >, std::vector< std::pair< int, int > > > ADS::genericVisit (const G &g, int r, Choice c)
 the general visit from \( r \): the nodes in the order in which they join \( R \), and the edges of the tree \( B \) in the order in which they join it
template<Graph G, class FN, class FE>
void ADS::bfs (const G &g, int r, FN &&node, FE &&edge)
 the visit in breadth from \( r \), calling node( u ) when \( u \) is examined and edge( u , v ) when \( ( u , v ) \) is examined
template<Graph G>
std::pair< std::vector< int >, std::vector< int > > ADS::distance (const G &g, int r)
 the distances from \( r \), INF if not reachable, and the parents in the tree of the shortest paths, NIL for \( r \) and the unreachable nodes
template<Graph G, class Q>
requires requires( Q q , int v ) { q.enqueue( v ); { q.dequeue() } -> std::convertible_to< int >; }
std::pair< std::vector< int >, std::vector< int > > ADS::distance (const G &g, int r, Q &S, VisitCounts *c=nullptr)
 the distances of distance() above, with the queue \( S \) given, empty, whose enqueue() and dequeue() keep the order of arrival [see ArrayQueue.h]; its work in *c, if given
std::vector< int > ADS::path (int r, int s, const std::vector< int > &p)
 the path from \( r \) to \( s \) in the tree of the parents \( p \), empty if there is none (the procedure printPath of the book)
template<Graph G, class Pre, class Edge, class Post>
void ADS::dfs (const G &g, int u, std::vector< bool > &visited, Pre &&pre, Edge &&edge, Post &&post)
 the visit in depth from \( u \), calling pre( u ) in the previsit, edge( u , v ) on each edge and post( u ) in the postvisit
template<Graph G>
std::vector< int > ADS::cc (const G &g, std::vector< int > S)
 the component of each node, numbered from 1, the visits starting from the nodes as they leave the stack \( S \) (its last item first)
template<Graph G>
std::vector< int > ADS::cc (const G &g)
 the components of cc() above, the visits starting from the nodes in increasing order
template<Graph G>
bool ADS::hasCycle (const G &g)
 true if the oriented graph has a cycle, i.e., if a visit finds a back edge; on an unoriented graph each edge, met again from its other end, is a back edge, so it is true if and only if there is an edge
template<Graph G>
bool ADS::isTopologicalOrder (const G &g, const std::vector< int > &S)
 whether \( S \) has each node once and each edge \( ( u , v ) \) has \( u \) before \( v \)
template<Graph G>
std::vector< int > ADS::topsort (const G &g)
 the nodes by decreasing finish time, which for an acyclic graph is a topological order; the first is the top of the stack of the book
template<Graph G>
std::vector< int > ADS::topsortKahn (const G &g)
 the topological order by removing the nodes with no entering edge, in the order of V() and then of discovery; fewer than \( n \) nodes if there is a cycle
template<Graph G>
ListGraph ADS::transpose (const G &g)
 the transposed graph, with the edges reversed
template<Graph G>
std::vector< int > ADS::scc (const G &g)
 the strongly connected component of each node, numbered from 1
template<Graph G>
bool ADS::unilaterallyConnected (const G &g)
 true if every two nodes have a path between them in one direction

Variables

constexpr int ADS::INF = std::numeric_limits< int >::max()
 the infinite distance
constexpr int ADS::NIL = -1
 no node

Detailed description

The visits of a GRAPH of section 9.5 and their applications, each in \( \Theta( n + m ) \) time on lists or vectors of adjacency.

The definitions, the invariants and the costs of the area are in Theory of graphs.

Enumeration type documentation

◆ Choice

enum class ADS::Choice
strong

the node of \( Q \) that genericVisit() chooses

Enumerator
NEWEST 

the last one added: the visit in depth

OLDEST 

the first one added: the visit in breadth

◆ EdgeKind

enum class ADS::EdgeKind
strong

the kind of an edge with respect to a depth-first visit

Enumerator
TREE 

an edge of the tree of the visit, to a node not discovered

BACK 

an edge to an ancestor, not finished yet

FORWARD 

an edge to a descendant, already finished

CROSS 

any other edge, between two subtrees or two trees

Function documentation

◆ bfs()

template<Graph G, class FN, class FE>
void ADS::bfs ( const G & g,
int r,
FN && node,
FE && edge )

the visit in breadth from \( r \), calling node( u ) when \( u \) is examined and edge( u , v ) when \( ( u , v ) \) is examined

In pseudocode (section 9.5):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{bfs}( G , r )\quad \triangleright \ \text{a node is marked when it enters } Q \cr 2 & \quad \mathit{visited}[ r ] \gets \textbf{true};\ Q \gets \text{a queue holding } r \cr 3 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 4 & \qquad u \gets \mathrm{dequeue}( Q );\ \mathrm{node}( u ) \cr 5 & \qquad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{do} \cr 6 & \qquad\quad \mathrm{edge}( u , v ) \cr 7 & \qquad\quad \textbf{if}\ \textbf{not}\ \mathit{visited}[ v ] \ \textbf{then}\ \mathit{visited}[ v ] \gets \textbf{true}; \ \mathrm{enqueue}( Q , v ) \end{array}} \]

Precondition
\( r \) is a node

◆ cc() [1/2]

template<Graph G>
std::vector< int > ADS::cc ( const G & g)

the components of cc() above, the visits starting from the nodes in increasing order

It is cc( G , S ) with S the nodes in decreasing order, so that the smallest one leaves S first.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{cc}( G ) \cr 2 & \quad \textbf{return}\ \mathrm{cc}( G , \text{the nodes of } G \text{ } \text{in decreasing order} ) \end{array}} \]

Postcondition
each node has a component (audit)

◆ cc() [2/2]

template<Graph G>
std::vector< int > ADS::cc ( const G & g,
std::vector< int > S )

the component of each node, numbered from 1, the visits starting from the nodes as they leave the stack \( S \) (its last item first)

In pseudocode (section 9.5), each visit numbering the nodes it reaches:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{cc}( G , S )\quad \triangleright\ S \text{ the order of the roots, } 0 \text{ an unlabeled node} \cr 2 & \quad \mathit{id}[ v ] \gets 0 \text{ for every node } v;\ c \gets 0 \cr 3 & \quad \textbf{while}\ S \ne \emptyset\ \textbf{do} \cr 4 & \qquad u \gets \mathrm{pop}( S );\ \textbf{if}\ \mathit{id}[ u ] \ne 0\ \textbf{then continue} \cr 5 & \qquad c \gets c + 1;\ \mathit{id}[ u ] \gets c;\ T \gets \text{a stack holding } u\quad \triangleright\ \text{a new component} \cr 6 & \qquad \textbf{while}\ T \ne \emptyset\ \textbf{do} \cr 7 & \qquad\quad x \gets \mathrm{pop}( T ) \cr 8 & \qquad\quad \textbf{foreach}\ v \in G.\mathrm{adj}( x ) \text{ with } \mathit{id}[ v ] = 0\ \textbf{do}\ \mathit{id}[ v ] \gets c; \ \mathrm{push}( T , v ) \cr 9 & \quad \textbf{return}\ \mathit{id} \end{array}} \]

Precondition
the items of \( S \) are nodes (audit)

◆ dfs()

template<Graph G, class Pre, class Edge, class Post>
void ADS::dfs ( const G & g,
int u,
std::vector< bool > & visited,
Pre && pre,
Edge && edge,
Post && post )

the visit in depth from \( u \), calling pre( u ) in the previsit, edge( u , v ) on each edge and post( u ) in the postvisit

In pseudocode (section 9.5), recursive as in the book, while the code keeps the recursion on a stack of its own:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{dfs}( G , u , \mathit{visited} ) \cr 2 & \quad \mathit{visited}[ u ] \gets \textbf{true};\ \mathrm{pre}( u )\quad \triangleright\ \text{on the way down} \cr 3 & \quad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{do} \cr 4 & \qquad \mathrm{edge}( u , v );\ \textbf{if}\ \textbf{not} \ \mathit{visited}[ v ]\ \textbf{then}\ \mathrm{dfs}( G , v , \mathit{visited} ) \cr 5 & \quad \mathrm{post}( u )\quad \triangleright\ \text{on the way up} \end{array}} \]

and on the stack \( S \) of the code, a frame \( ( x , A , i ) \) holding a node, its adjacent nodes and the place of the next one:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{dfs}( G , u , \mathit{visited} ) \cr 2 & \quad \mathit{visited}[ u ] \gets \textbf{true};\ \mathrm{pre}( u ); \ S \gets ( ( u , G.\mathrm{adj}( u ) , 0 ) ) \cr 3 & \quad \textbf{while}\ S \ne ()\ \textbf{do} \cr 4 & \qquad ( x , A , i ) \gets \text{the top of } S \cr 5 & \qquad \textbf{if}\ i = \lvert A \rvert\ \textbf{then} \ \text{pop } S;\ \mathrm{post}( x );\ \textbf{continue} \quad \triangleright\ \text{the return} \cr 6 & \qquad v \gets A[ i ];\ i \gets i + 1 \text{ in the top frame}; \ \mathrm{edge}( x , v ) \cr 7 & \qquad \textbf{if not}\ \mathit{visited}[ v ]\ \textbf{then} \quad \triangleright\ \text{the call} \cr 8 & \qquad\quad \mathit{visited}[ v ] \gets \textbf{true}; \ \mathrm{pre}( v );\ \text{push } ( v , G.\mathrm{adj}( v ) , 0 ) \text{ on } S \end{array}} \]

Precondition
\( u \) is a node not visited, and visited has a place per name

◆ distance() [1/2]

template<Graph G>
std::pair< std::vector< int >, std::vector< int > > ADS::distance ( const G & g,
int r )

the distances from \( r \), INF if not reachable, and the parents in the tree of the shortest paths, NIL for \( r \) and the unreachable nodes

In pseudocode (section 9.5), each node entering the queue once:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{distance}( G , r )\quad \triangleright \ \text{the visit in breadth, } d \text{ in place of the marks} \cr 2 & \quad d[ v ] \gets \infty;\ p[ v ] \gets \textbf{nil} \text{ for every node } v \cr 3 & \quad d[ r ] \gets 0;\ Q \gets \text{a queue holding } r \cr 4 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 5 & \qquad u \gets \mathrm{dequeue}( Q ) \cr 6 & \qquad \textbf{foreach}\ v \in G.\mathrm{adj}( u ) \text{ with } d[ v ] = \infty\ \textbf{do}\ ( d[ v ] , p[ v ] ) \gets ( d[ u ] + 1 , u );\ \mathrm{enqueue}( Q , v ) \cr 7 & \quad \textbf{return}\ ( d , p ) \end{array}} \]

Precondition
\( r \) is a node
Postcondition
each edge \( ( u , v ) \) from a reached \( u \) has \( d[ v ] \le d[ u ] + 1 \), and each reached \( v \ne r \) has \( d[ v ] = d[ p[ v ] ] + 1 \) (audit)

◆ distance() [2/2]

template<Graph G, class Q>
requires requires( Q q , int v ) { q.enqueue( v ); { q.dequeue() } -> std::convertible_to< int >; }
std::pair< std::vector< int >, std::vector< int > > ADS::distance ( const G & g,
int r,
Q & S,
VisitCounts * c = nullptr )

the distances of distance() above, with the queue \( S \) given, empty, whose enqueue() and dequeue() keep the order of arrival [see ArrayQueue.h]; its work in *c, if given

Precondition
\( r \) is a node, \( S \) is empty
Postcondition
as distance()

◆ genericVisit()

template<Graph G>
std::pair< std::vector< int >, std::vector< std::pair< int, int > > > ADS::genericVisit ( const G & g,
int r,
Choice c )

the general visit from \( r \): the nodes in the order in which they join \( R \), and the edges of the tree \( B \) in the order in which they join it

In pseudocode (section 9.5):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{genericVisit}( G , r )\quad \triangleright\ R \text{ reached, } Q \text{ open, } B \text{ the tree} \cr 2 & \quad R \gets \{ r \};\ Q \gets \{ r \};\ B \gets \emptyset \cr 3 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 4 & \qquad j \gets \text{a node of } Q\quad \triangleright \ \text{the last added for depth, the first for breadth} \cr 5 & \qquad \textbf{if}\ G.\mathrm{adj}( j ) \subseteq R\ \textbf{then}\ Q \gets Q \setminus \{ j \}\quad \triangleright\ j \text{ is closed} \cr 6 & \qquad \textbf{else}\ k \gets \text{a node of } G.\mathrm{adj}( j ) \setminus R;\ R \gets R \cup \{ k \};\ Q \gets Q \cup \{ k \};\ B \gets B \cup \{ ( j , k ) \} \cr 7 & \quad \textbf{return}\ ( R , B ) \end{array}} \]

Precondition
\( r \) is a node
Postcondition
\( |B| = |R| - 1 \), each edge of \( B \) from a node already in \( R \) (audit)

◆ hasCycle()

template<Graph G>
bool ADS::hasCycle ( const G & g)

true if the oriented graph has a cycle, i.e., if a visit finds a back edge; on an unoriented graph each edge, met again from its other end, is a back edge, so it is true if and only if there is an edge

In pseudocode (section 9.5), on the visits of DfsSchema:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{hasCycle}( G )\quad \triangleright \ \text{a cycle exists iff a visit in depth meets a back edge} \cr 2 & \quad \textbf{foreach}\ u \text{ with } \mathit{dt}[ u ] = 0 \ \textbf{do}\ \mathrm{visit}( u ) \text{ of DfsSchema, stopping with } \textbf{true} \text{ on a back edge} \cr 3 & \quad \textbf{return false} \end{array}} \]

◆ isTopologicalOrder()

template<Graph G>
bool ADS::isTopologicalOrder ( const G & g,
const std::vector< int > & S )

whether \( S \) has each node once and each edge \( ( u , v ) \) has \( u \) before \( v \)

In pseudocode:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isTopologicalOrder}( G , S ) \cr 2 & \quad \textbf{if}\ |S| \ne n\ \textbf{then return false} \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad \textbf{if}\ S[ i ] \text{ is not a node, or is repeated } \ \textbf{then return false} \cr 5 & \qquad \mathit{at}[ S[ i ] ] \gets i \cr 6 & \quad \textbf{foreach}\ ( u , v ) \in E\ \textbf{do} \cr 7 & \qquad \textbf{if}\ \mathit{at}[ u ] > \mathit{at}[ v ]\ \textbf{then return false} \cr 8 & \quad \textbf{return true} \end{array}} \]

◆ path()

std::vector< int > ADS::path ( int r,
int s,
const std::vector< int > & p )
inline

the path from \( r \) to \( s \) in the tree of the parents \( p \), empty if there is none (the procedure printPath of the book)

In pseudocode (section 9.5), the path read backwards from s:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{path}( r , s , p )\quad \triangleright \ \text{from } s \text{ back to } r \text{ along } p \cr 2 & \quad P \gets ( s ) \cr 3 & \quad \textbf{while}\ \text{the head } x \text{ of } P \text{ is not } r\ \textbf{do} \cr 4 & \qquad \textbf{if}\ p[ x ] = \textbf{nil}\ \textbf{then return} \ ()\quad \triangleright\ s \text{ is not reached from } r \cr 5 & \qquad \text{put } p[ x ] \text{ before the head of } P \cr 6 & \quad \textbf{return}\ P \end{array}} \]

Precondition
\( r \) and \( s \) are places of \( p \)
Postcondition
the path is empty or goes from \( r \) to \( s \)

◆ scc()

template<Graph G>
std::vector< int > ADS::scc ( const G & g)

the strongly connected component of each node, numbered from 1

In pseudocode (Kosaraju's algorithm, section 9.5): if there is an edge from the component C to C' then ft( C ) > ft( C' ), so the visits of the transposed graph, started by decreasing finish time, meet C first and cannot leave it.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{scc}( G ) \cr 2 & \quad S \gets \mathrm{topsort}( G )\quad \triangleright \ \text{the first node of } S \text{ finished last} \cr 3 & \quad \textbf{return}\ \mathrm{cc}( \mathrm{transpose}( G ) , S )\quad \triangleright\ \text{the roots taken from the top of } S \end{array}} \]

Postcondition
each node has a component, and an edge from the component \( C \) to \( C' \ne C \) gives \( C < C' \) (audit)

◆ topsort()

template<Graph G>
std::vector< int > ADS::topsort ( const G & g)

the nodes by decreasing finish time, which for an acyclic graph is a topological order; the first is the top of the stack of the book

In pseudocode (section 9.5): since an edge ( u , v ) of an acyclic graph has ft[ v ] < ft[ u ], the nodes by decreasing finish time come in a topological order.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{topsort}( G ) \cr 2 & \quad S \gets \text{an empty stack} \cr 3 & \quad \textbf{foreach}\ u \text{ not yet visited}\ \textbf{do} \ \mathrm{dfs}( G , u , \mathit{visited} ) \text{ with } \mathrm{post}( v ) = \mathrm{push}( S , v ) \cr 4 & \quad \textbf{return}\ \text{the nodes of } S \text{ from the top}\quad \triangleright \ \text{by decreasing finish time} \end{array}} \]

Postcondition
a topological order if the graph is acyclic (audit)

◆ topsortKahn()

template<Graph G>
std::vector< int > ADS::topsortKahn ( const G & g)

the topological order by removing the nodes with no entering edge, in the order of V() and then of discovery; fewer than \( n \) nodes if there is a cycle

In pseudocode (Kahn's algorithm, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{topsortKahn}( G ) \cr 2 & \quad \textbf{foreach}\ ( u , v ) \in E\ \textbf{do}\ \mathit{in}[ v ] \gets \mathit{in}[ v ] + 1 \cr 3 & \quad L \gets \text{the nodes } u \text{ with } \mathit{in}[ u ] = 0 \text{ in increasing order};\ i \gets 1 \cr 4 & \quad \textbf{while}\ i \le |L|\ \textbf{do} \cr 5 & \qquad \textbf{foreach}\ v \in G.\mathrm{adj}( L[ i ] )\ \textbf{do} \cr 6 & \qquad\quad \mathit{in}[ v ] \gets \mathit{in}[ v ] - 1 \cr 7 & \qquad\quad \textbf{if}\ \mathit{in}[ v ] = 0\ \textbf{then} \ \text{append } v \text{ to } L \cr 8 & \qquad i \gets i + 1 \cr 9 & \quad \textbf{return}\ L\quad \triangleright\ \text{fewer than } n \text{ nodes if there is a cycle} \end{array}} \]

Postcondition
a topological order if it has all the nodes (audit)

◆ transpose()

template<Graph G>
ListGraph ADS::transpose ( const G & g)

the transposed graph, with the edges reversed

In pseudocode (section 9.5):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{transpose}( G ) \cr 2 & \quad \textbf{return}\ \text{the graph on the nodes of } G \text{ with an edge } ( v , u ) \text{ for each edge } ( u , v ) \text{ of } G \end{array}} \]

Postcondition
\( ( v , u ) \) is an edge of it for each edge \( ( u , v ) \) (audit)

◆ unilaterallyConnected()

template<Graph G>
bool ADS::unilaterallyConnected ( const G & g)

true if every two nodes have a path between them in one direction

In pseudocode: the graph of the strongly connected components is acyclic, and it has a path through all of them if and only if its topological order is unique, i.e., if the algorithm of Kahn never has two sources at once.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{unilaterallyConnected}( G ) \cr 2 & \quad C \gets \mathrm{scc}( G ) \cr 3 & \quad H \gets \text{the graph with an edge } ( C[ u ] , C[ v ] ) \text{ for each } ( u , v ) \in E \text{ with } C[ u ] \ne C[ v ] \cr 4 & \quad S \gets \text{the nodes of } H \text{ with no entering edge} \cr 5 & \quad \textbf{while}\ S \ne \emptyset\ \textbf{do} \cr 6 & \qquad \textbf{if}\ |S| > 1\ \textbf{then return false} \cr 7 & \qquad c \gets \mathrm{pop}( S ) \cr 8 & \qquad \textbf{foreach}\ d \in H.\mathrm{adj}( c )\ \textbf{do} \cr 9 & \qquad\quad \mathit{in}[ d ] \gets \mathit{in}[ d ] - 1 \cr 10 & \qquad\quad \textbf{if}\ \mathit{in}[ d ] = 0\ \textbf{then} \ \mathrm{push}( S , d ) \cr 11 & \quad \textbf{return true} \end{array}} \]

Variable documentation

◆ INF

int ADS::INF = std::numeric_limits< int >::max()
inlineconstexpr

the infinite distance

◆ NIL

int ADS::NIL = -1
inlineconstexpr

no node