|
ADS
Algorithms and Data Structures
|
Problems on the paths of a GRAPH solved by the visits of section 9.5 and by the topological order, each in \( O( n + m ) \) time on lists or vectors of adjacency unless said otherwise. More...
Files | |
| file | ADS/graph/Paths.h |
| Problems on the paths of a GRAPH solved by the visits of section 9.5 and by the topological order, each in \( O( n + m ) \) time on lists or vectors of adjacency unless said otherwise: | |
Typedefs | |
| template<class G> | |
| using | ADS::PathLength = typename detail::EdgeLength< G >::type |
| the type of the lengths of the paths of \( G \) | |
Functions | |
| template<Graph G> | |
| std::vector< std::uint64_t > | ADS::countShortestPaths (const G &g, int s) |
| the number of shortest paths, in edges, from \( s \) to each node, modulo \( 2^{64} \): 1 for \( s \), 0 for the nodes not reachable | |
| template<Graph G> | |
| std::pair< std::vector< int >, std::vector< int > > | ADS::multiSourceDistance (const G &g, const std::vector< int > &S) |
| the distance from the nearest node of \( S \), INF if none reaches it, and the parents in the forest of the shortest paths, NIL for the nodes of \( S \) and those not reached | |
| template<Graph G> | |
| int | ADS::setDistance (const G &g, const std::vector< bool > &A, const std::vector< bool > &B) |
| the fewest edges of a path from a node of \( A \) to one of \( B \), 0 if they meet, INF if there is none | |
| template<Graph G> | |
| std::vector< int > | ADS::longestPathLayering (const G &g) |
| the edges of a longest path ending at each node of the acyclic graph, 0 on the sources (and on the names that are not nodes) | |
| template<Graph G, class Keep> | |
| std::vector< PathLength< G > > | ADS::dagLongestPaths (const G &g, Keep &&keep) |
| the largest total length of a path leaving each node along the edges \( ( u , v ) \) with \( \mathrm{keep}( u , v ) \), the empty path included, 0 on the names that are not nodes | |
| template<Graph G> | |
| std::vector< PathLength< G > > | ADS::dagLongestPaths (const G &g) |
| the longest paths of dagLongestPaths() above, along all the edges | |
| template<Graph G, class F> | |
| void | ADS::simpleCycles (const G &g, int s, F &&f) |
| calls \( f( C ) \) on each simple cycle through \( s \), \( C \) its nodes from \( s \) back to \( s \) | |
| template<class T> | |
| std::optional< std::vector< std::size_t > > | ADS::refuelingStops (const std::vector< T > &D, T L, T R) |
| the places in \( D \) of the fewest stops of a trip from 0 to \( L \), a full tank lasting \( R \) and the stations at \( D \), nothing if the trip cannot be made | |
Problems on the paths of a GRAPH solved by the visits of section 9.5 and by the topological order, each in \( O( n + m ) \) time on lists or vectors of adjacency unless said otherwise.
The definitions, the invariants and the costs of the area are in Theory of graphs.
| using ADS::PathLength = typename detail::EdgeLength< G >::type |
the type of the lengths of the paths of \( G \)
| std::vector< std::uint64_t > ADS::countShortestPaths | ( | const G & | g, |
| int | s ) |
the number of shortest paths, in edges, from \( s \) to each node, modulo \( 2^{64} \): 1 for \( s \), 0 for the nodes not reachable
In pseudocode: all the nodes at distance d - 1 leave the queue before those at distance d, so the count of v is complete when v leaves it.
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{countShortestPaths}( G , s ) \cr 2 & \quad \textbf{foreach}\ v \in V\ \textbf{do}\ d[ v ] \gets \infty; \ \sigma[ v ] \gets 0 \cr 3 & \quad d[ s ] \gets 0;\ \sigma[ s ] \gets 1;\ Q \gets ( s ) \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 )\ \textbf{do} \cr 7 & \qquad\quad \textbf{if}\ d[ v ] = \infty\ \textbf{then}\ d[ v ] \gets d[ u ] + 1;\ \mathrm{enqueue}( Q , v ) \cr 8 & \qquad\quad \textbf{if}\ d[ v ] = d[ u ] + 1\ \textbf{then}\ \sigma[ v ] \gets ( \sigma[ v ] + \sigma[ u ] ) \bmod 2^{64} \cr 9 & \quad \textbf{return}\ \sigma \end{array}} \]
| std::vector< PathLength< G > > ADS::dagLongestPaths | ( | const G & | g | ) |
the longest paths of dagLongestPaths() above, along all the edges
In pseudocode:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{dagLongestPaths}( G ) \cr 2 & \quad \textbf{return}\ \mathrm{dagLongestPaths}( G , \text{a } \text{predicate true on every edge} ) \end{array}} \]
| std::vector< PathLength< G > > ADS::dagLongestPaths | ( | const G & | g, |
| Keep && | keep ) |
the largest total length of a path leaving each node along the edges \( ( u , v ) \) with \( \mathrm{keep}( u , v ) \), the empty path included, 0 on the names that are not nodes
In pseudocode (the critical path method), recursive with memoization, while the code keeps the recursion on a stack of its own:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ L( u ) \cr 2 & \quad \textbf{if}\ L[ u ] \text{ is known }\ \textbf{then return}\ L[ u ] \cr 3 & \quad L[ u ] \gets 0 \cr 4 & \quad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{with} \ \mathrm{keep}( u , v )\ \textbf{do} \cr 5 & \qquad L[ u ] \gets \max( L[ u ] , w( u , v ) + L( v ) ) \cr 6 & \quad \textbf{return}\ L[ u ] \end{array}} \]
| std::vector< int > ADS::longestPathLayering | ( | const G & | g | ) |
the edges of a longest path ending at each node of the acyclic graph, 0 on the sources (and on the names that are not nodes)
In pseudocode (the algorithm of Kahn): a node gets its layer when its last entering edge is removed.
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestPathLayering}( G ) \cr 2 & \quad \textbf{foreach}\ ( u , v ) \in E\ \textbf{do}\ \mathit{in}[ v ] \gets \mathit{in}[ v ] + 1 \cr 3 & \quad \textbf{foreach}\ v \in V\ \textbf{do}\ \mathit{layer}[ v ] \gets 0 \cr 4 & \quad S \gets \text{the nodes with } \mathit{in} = 0 \cr 5 & \quad \textbf{while}\ S \ne \emptyset\ \textbf{do} \cr 6 & \qquad u \gets \mathrm{pop}( S ) \cr 7 & \qquad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{do} \cr 8 & \qquad\quad \mathit{layer}[ v ] \gets \max( \mathit{layer}[ v ] , \mathit{layer}[ u ] + 1 );\ \mathit{in}[ v ] \gets \mathit{in}[ v ] - 1 \cr 9 & \qquad\quad \textbf{if}\ \mathit{in}[ v ] = 0\ \textbf{then} \ \mathrm{push}( S , v ) \cr 10 & \quad \textbf{return}\ \mathit{layer} \end{array}} \]
| std::pair< std::vector< int >, std::vector< int > > ADS::multiSourceDistance | ( | const G & | g, |
| const std::vector< int > & | S ) |
the distance from the nearest node of \( S \), INF if none reaches it, and the parents in the forest of the shortest paths, NIL for the nodes of \( S \) and those not reached
In pseudocode, the visit in breadth from all the sources at once:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiSourceDistance}( G , S ) \cr 2 & \quad \textbf{foreach}\ v \in V\ \textbf{do}\ d[ v ] \gets \infty;\ p[ v ] \gets \textbf{nil} \cr 3 & \quad Q \gets () \cr 4 & \quad \textbf{foreach}\ s \in S\ \textbf{with}\ d[ s ] = \infty \ \textbf{do}\ d[ s ] \gets 0;\ \mathrm{enqueue}( Q , s ) \cr 5 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 6 & \qquad u \gets \mathrm{dequeue}( Q ) \cr 7 & \qquad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{with}\ d[ v ] = \infty\ \textbf{do} \cr 8 & \qquad\quad d[ v ] \gets d[ u ] + 1;\ p[ v ] \gets u; \ \mathrm{enqueue}( Q , v ) \cr 9 & \quad \textbf{return}\ ( d , p ) \end{array}} \]
| std::optional< std::vector< std::size_t > > ADS::refuelingStops | ( | const std::vector< T > & | D, |
| T | L, | ||
| T | R ) |
the places in \( D \) of the fewest stops of a trip from 0 to \( L \), a full tank lasting \( R \) and the stations at \( D \), nothing if the trip cannot be made
In pseudocode (a greedy of chapter 14): when the next station is out of reach, the last station in reach is the stop.
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{refuelingStops}( D , L , R )\quad \triangleright\ D \text{ the sorted places of the stations} \cr 2 & \quad S \gets ();\ \mathit{reach} \gets R;\ i \gets 1 \cr 3 & \quad \textbf{while}\ \mathit{reach} < L\ \textbf{do} \cr 4 & \qquad b \gets \text{the last station } i' \ge i \text{ with } D[ i' ] \le \mathit{reach} \text{, or } \textbf{nil}\quad \triangleright \ \text{the farthest within reach} \cr 5 & \qquad \textbf{if}\ b = \textbf{nil}\ \textbf{then return nil} \cr 6 & \qquad \text{append } b \text{ to } S;\ \mathit{reach} \gets D[ b ] + R;\ i \gets b + 1 \cr 7 & \quad \textbf{return}\ S \end{array}} \]
| int ADS::setDistance | ( | const G & | g, |
| const std::vector< bool > & | A, | ||
| const std::vector< bool > & | B ) |
the fewest edges of a path from a node of \( A \) to one of \( B \), 0 if they meet, INF if there is none
In pseudocode, the visit from A stopping at the first node of B:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setDistance}( G , A , B ) \cr 2 & \quad \textbf{foreach}\ v \in V\ \textbf{do}\ d[ v ] \gets \infty \cr 3 & \quad Q \gets () \cr 4 & \quad \textbf{foreach}\ a \in A\ \textbf{do}\ d[ a ] \gets 0; \ \mathrm{enqueue}( Q , a ) \cr 5 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 6 & \qquad u \gets \mathrm{dequeue}( Q ) \cr 7 & \qquad \textbf{if}\ u \in B\ \textbf{then return}\ d[ u ] \cr 8 & \qquad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{with}\ d[ v ] = \infty\ \textbf{do} \cr 9 & \qquad\quad d[ v ] \gets d[ u ] + 1;\ \mathrm{enqueue}( Q , v ) \cr 10 & \quad \textbf{return}\ \infty \end{array}} \]
| void ADS::simpleCycles | ( | const G & | g, |
| int | s, | ||
| F && | f ) |
calls \( f( C ) \) on each simple cycle through \( s \), \( C \) its nodes from \( s \) back to \( s \)
In pseudocode (Johnson's algorithm, beyond the book): a node stays blocked, with the nodes waiting on it in its list B, while no path from it to s avoids the stack.
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{circuit}( v ) \cr 2 & \quad \mathit{found} \gets \textbf{false};\ \mathrm{push}( \mathit{stack} , v );\ \mathit{blocked}[ v ] \gets \textbf{true} \cr 3 & \quad \textbf{foreach}\ x \in G.\mathrm{adj}( v )\ \textbf{do} \cr 4 & \qquad \textbf{if}\ x = s\ \textbf{then}\ f( \mathit{stack} , s ); \ \mathit{found} \gets \textbf{true} \cr 5 & \qquad \textbf{else if not}\ \mathit{blocked}[ x ]\ \textbf{and} \ \mathrm{circuit}( x )\ \textbf{then}\ \mathit{found} \gets \textbf{true} \cr 6 & \quad \textbf{if}\ \mathit{found}\ \textbf{then}\ \mathrm{unblock}( v ) \cr 7 & \quad \textbf{else foreach}\ x \in G.\mathrm{adj}( v )\ \textbf{do}\ B[ x ] \gets B[ x ] \cup \{ v \} \cr 8 & \quad \mathrm{pop}( \mathit{stack} );\ \textbf{return}\ \mathit{found} \cr 9 & \textbf{procedure}\ \mathrm{unblock}( u ) \cr 10 & \quad \mathit{blocked}[ u ] \gets \textbf{false} \cr 11 & \quad \textbf{foreach}\ x \in B[ u ]\ \textbf{do}\ B[ u ] \gets B[ u ] \setminus \{ x \};\ \textbf{if}\ \mathit{blocked}[ x ]\ \textbf{then} \ \mathrm{unblock}( x ) \end{array}} \]