ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Visit.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Visit.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
85/*--------------------------------------------------------------------------*/
86/*----------------------------- DEFINITIONS --------------------------------*/
87/*--------------------------------------------------------------------------*/
88
89#ifndef __ADS_Visit
90 #define __ADS_Visit
91/* self-identification: #endif constant */
92
93/*--------------------------------------------------------------------------*/
94/*------------------------------ INCLUDES ----------------------------------*/
95/*--------------------------------------------------------------------------*/
96
97#include "ADS/Contract.h"
98#include "ADS/graph/Graph.h"
99#include "ADS/graph/ListGraph.h"
100#include "ADS/queue/ArrayQueue.h"
101
102#include <algorithm>
103#include <concepts>
104#include <cstddef>
105#include <deque>
106#include <limits>
107#include <set>
108#include <utility>
109#include <vector>
110
111/*--------------------------------------------------------------------------*/
112/*--------------------------- NAMESPACE ------------------------------------*/
113/*--------------------------------------------------------------------------*/
114
115namespace ADS
116{
117
119inline constexpr int INF = std::numeric_limits< int >::max();
120
122enum class Choice {
125 };
126
128inline constexpr int NIL = -1;
129
130/*--------------------------------------------------------------------------*/
131/*--------------------------------- BFS ------------------------------------*/
132/*--------------------------------------------------------------------------*/
133
159template< Graph G >
160std::pair< std::vector< int > , std::vector< std::pair< int , int > > >
161genericVisit( const G & g , int r , Choice c );
162
183template< Graph G , class FN , class FE >
184void bfs( const G & g , int r , FN && node , FE && edge );
185
210template< Graph G >
211std::pair< std::vector< int > , std::vector< int > > distance( const G & g ,
212 int r );
213
217{
218 long long nodes = 0 , edges = 0;
219 };
220
226template< Graph G , class Q >
227 requires requires( Q q , int v )
228{
229 q.enqueue( v );
230 { q.dequeue() } -> std::convertible_to< int >;
231 }
232std::pair< std::vector< int > , std::vector< int > >
233distance( const G & g , int r , Q & S , VisitCounts * c = nullptr );
234
254inline std::vector< int > path( int r , int s , const std::vector< int > & p );
255
256/*--------------------------------------------------------------------------*/
257/*--------------------------------- DFS ------------------------------------*/
258/*--------------------------------------------------------------------------*/
259
299template< Graph G , class Pre , class Edge , class Post >
300void dfs( const G & g , int u , std::vector< bool > & visited , Pre && pre ,
301 Edge && edge , Post && post );
302
328template< Graph G >
329std::vector< int > cc( const G & g , std::vector< int > S );
330
344template< Graph G >
345std::vector< int > cc( const G & g );
346
347/*--------------------------------------------------------------------------*/
348/*--------------------------- CLASS DfsSchema ------------------------------*/
349/*--------------------------------------------------------------------------*/
350
358
361template< Graph G >
363{
364
365/*--------------------------------------------------------------------------*/
366 public:
367 explicit DfsSchema( const G & g );
368
394 template< class Edge >
395 void visit( int u , Edge && edge );
396
398 template< class Edge >
399 void visitAll( Edge && edge );
400
402 [[nodiscard]] int dt( int u ) const;
403
405 [[nodiscard]] int ft( int u ) const;
406
407/*--------------------------------------------------------------------------*/
408 private:
409 const G & f_g;
410 std::vector< int > f_dt;
411 std::vector< int > f_ft;
412 int f_time = 0;
413
414 }; // end( class DfsSchema )
415
416/*--------------------------------------------------------------------------*/
417/*--------------------- CYCLES, ORDERS AND COMPONENTS ----------------------*/
418/*--------------------------------------------------------------------------*/
419
436template< Graph G >
437bool hasCycle( const G & g );
438
457template< Graph G >
458bool isTopologicalOrder( const G & g , const std::vector< int > & S );
459
480template< Graph G >
481std::vector< int > topsort( const G & g );
482
507template< Graph G >
508std::vector< int > topsortKahn( const G & g );
509
523template< Graph G >
524ListGraph<> transpose( const G & g );
525
543template< Graph G >
544std::vector< int > scc( const G & g );
545
568template< Graph G >
569bool unilaterallyConnected( const G & g );
570
571/*--------------------------------------------------------------------------*/
572
573} // end( namespace ADS )
574
575/*--------------------------------------------------------------------------*/
576/*-------------------------- IMPLEMENTATION --------------------------------*/
577/*--------------------------------------------------------------------------*/
578
579#include "ADS/graph/impl/Visit.tpp"
580
581/*--------------------------------------------------------------------------*/
582/*---------------------- THE COMPONENTS BY ONE VISIT -----------------------*/
583/*--------------------------------------------------------------------------*/
584
586
587/*--------------------------------------------------------------------------*/
588
589#endif /* Visit.h included */
590
591/*--------------------------------------------------------------------------*/
592/*---------------------------- End File Visit.h ----------------------------*/
593/*--------------------------------------------------------------------------*/
The QUEUE of the book [see ADT.h] realized with a circular vector of places, the place 0 following t...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The GRAPH with lists of adjacency of section 9.4 [see Graph.h]: for each node the LIST of its adjacen...
The strongly connected components of an oriented graph by one visit in depth, beyond the book,...
int ft(int u) const
void visit(int u, Edge &&edge)
the visit from ; edge( u , v , k ) on each edge examined, of kind
int dt(int u) const
void visitAll(Edge &&edge)
the visits from all the nodes not discovered yet, in increasing order
DfsSchema(const G &g)
a graph with lists of adjacency
Definition ListGraph.h:71
The GRAPH of chapter 9 and its realizations.
std::vector< int > topsortKahn(const G &g)
the topological order by removing the nodes with no entering edge, in the order of V() and then of di...
void dfs(const G &g, int u, std::vector< bool > &visited, Pre &&pre, Edge &&edge, Post &&post)
the visit in depth from , calling pre( u ) in the previsit, edge( u , v ) on each edge and post( u ) ...
bool isTopologicalOrder(const G &g, const std::vector< int > &S)
whether has each node once and each edge has before
void bfs(const G &g, int r, FN &&node, FE &&edge)
the visit in breadth from , calling node( u ) when is examined and edge( u , v ) when is examined
std::pair< std::vector< int >, std::vector< std::pair< int, int > > > genericVisit(const G &g, int r, Choice c)
the general visit from : the nodes in the order in which they join , and the edges of the tree in th...
constexpr int NIL
no node
Definition Visit.h:128
std::vector< int > 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 sta...
std::vector< int > topsort(const G &g)
the nodes by decreasing finish time, which for an acyclic graph is a topological order; the first is ...
bool 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 ea...
ListGraph transpose(const G &g)
the transposed graph, with the edges reversed
EdgeKind
the kind of an edge with respect to a depth-first visit
Definition Visit.h:352
std::pair< std::vector< int >, std::vector< int > > distance(const G &g, int r)
the distances from , INF if not reachable, and the parents in the tree of the shortest paths,...
constexpr int INF
the infinite distance
Definition Visit.h:119
std::vector< int > scc(const G &g)
the strongly connected component of each node, numbered from 1
Choice
the node of that genericVisit() chooses
Definition Visit.h:122
std::vector< int > path(int r, int s, const std::vector< int > &p)
the path from to in the tree of the parents , empty if there is none (the procedure printPath of th...
bool unilaterallyConnected(const G &g)
true if every two nodes have a path between them in one direction
@ CROSS
any other edge, between two subtrees or two trees
Definition Visit.h:356
@ BACK
an edge to an ancestor, not finished yet
Definition Visit.h:354
@ FORWARD
an edge to a descendant, already finished
Definition Visit.h:355
@ TREE
an edge of the tree of the visit, to a node not discovered
Definition Visit.h:353
@ NEWEST
the last one added: the visit in depth
Definition Visit.h:123
@ OLDEST
the first one added: the visit in breadth
Definition Visit.h:124
Definition ADT.h:96
what a visit did: the nodes taken out of its structure and the edges examined
Definition Visit.h:217
long long edges
Definition Visit.h:218
long long nodes
Definition Visit.h:218