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

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...

#include "ADS/Contract.h"
#include "ADS/graph/Graph.h"
#include "ADS/graph/ListGraph.h"
#include "ADS/queue/ArrayQueue.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <deque>
#include <limits>
#include <set>
#include <utility>
#include <vector>
#include "ADS/graph/impl/Visit.tpp"
#include "ADS/graph/StrongComponents.h"

Go to the source code of this file.

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...

Namespaces

namespace  ADS

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:

  • genericVisit(), the general scheme of which the others are cases: the reached nodes R and the open ones Q start as { r }, the tree B as empty; while Q is not empty a node j is chosen from Q, and if some k adjacent to j is not in R then k joins R and Q and ( j , k ) joins B, otherwise j leaves Q. Choosing the newest node of Q gives the visit in depth, the oldest the visit in breadth; B is a tree spanning the nodes reachable from r. A cursor in the adjacency of each node makes each edge examined once;
  • bfs(), the visit in breadth with a QUEUE, which examines the nodes by increasing distance from the root r; distance() computes the distance (the minimum number of edges, the Erdos number of the example) and the tree of the shortest paths as a vector of parents, and path() reads a path from it, as printPath of the book does; distance() runs on an ArrayQueue or on any queue given, with enqueue() and dequeue() [see VisitCounts];
  • dfs(), the visit in depth, recursive, with a previsit and a postvisit;
  • cc(), the connected components of an unoriented graph, starting the visits from the nodes in the order in which they leave a STACK;
  • DfsSchema, the procedure dfs-schema of the book, which numbers the discovery time dt[ u ] and the finish time ft[ u ] of each node and says of each edge ( u , v ) whether it is in the tree T (dt[ v ] = 0), back (dt[ u ] > dt[ v ] and ft[ v ] = 0), forward (dt[ u ] < dt[ v ] and ft[ v ] != 0) or cross (the others);
  • hasCycle(), true if and only if a visit finds a back edge;
  • topsort(), the nodes by decreasing finish time, a topological order of an acyclic graph: if ( u , v ) is an edge then ft[ v ] < ft[ u ]; topsortKahn() builds one by removing the nodes with no entering edge, and isTopologicalOrder() checks one;
  • unilaterallyConnected(), true if for every two nodes u and v there is a path from u to v or one from v to u: so it is exactly when the graph of the strongly connected components, which is acyclic, has a path through all of them, i.e., when its topological order is unique, which the algorithm of Kahn finds out by never having two sources at once;
  • transpose() and scc(), the strongly connected components of an oriented graph by the algorithm of Kosaraju: a visit gives the nodes by decreasing finish time, and cc() on the transposed graph starts its visits in that order. By Theorem 9.1, if there is an edge from the component C to C' then ft( C ) > ft( C' ), so the second visit meets C first and, the edge being reversed, cannot reach C' from it.

The strongly connected components by one visit in depth and no transposed graph, sccTarjan() and sccPearce() (beyond the book), are in StrongComponents.h, which this header includes.

Nil is -1 and the infinite distance is INF. The visits in depth and path() keep their recursion on an explicit stack, in the order of the recursive ones, so that a path of 10^5 nodes does not overflow the stack of the calls.

The properties on which the visits rest, and the arguments behind their bounds, are in the theory page of the area (see Visits).

The contracts [see Contract.h]: a visit starts from a node of the graph (and dfs() from one not visited yet, with a vector of a place per name); distance() ensures, at the level of the audits, that its distances are those of a breadth first tree, topsort() and topsortKahn() that their order is topological when the graph is acyclic, transpose() that it reverses each edge. The functions are defined in Visit.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 9.5, https://algoritmicamente.it/materiali/libro/capitoli/cap09.pdf
E. F. Moore, The shortest path through a maze, Proceedings of the International Symposium on the Theory of Switching, 285-292, 1959
R. E. Tarjan, Depth-first search and linear graph algorithms, SIAM Journal on Computing 1(2), 146-160, 1972, doi:10.1137/0201010
A. B. Kahn, Topological sorting of large networks, Communications of the ACM 5(11), 558-562, 1962, doi:10.1145/368996.369025
M. Sharir, A strong-connectivity algorithm and its applications in data flow analysis, Computers & Mathematics with Applications 7(1), 67-72, 1981, doi:10.1016/0898-1221(81)90008-0
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa