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

The shortest paths between all the pairs of nodes, section 13.5. More...

Files

file  ADS/graph/AllPairsShortestPaths.h
 The shortest paths between all the pairs of nodes, section 13.5: with the nodes \( v_0 , \ldots , v_{n-1} \), a path is \( k \)-bound if it passes through no node of { v_k , ... , v_{n-1} } but its ends, and the least cost of the k-bound paths from u to v is.

Classes

struct  ADS::AllPairs< D >
 the distances, the predecessors and whether there is a negative cycle More...

Functions

template<Graph G>
AllPairs< typename G::Weight > ADS::floydWarshall (const G &g)
 the shortest paths between all the pairs of nodes
template<class D>
void ADS::addEdge (AllPairs< D > &R, int a, int b, const D &w)
 the insertion of the edge \( ( a , b ) \) of cost \( w \), \( O( n^2 ) \)

Detailed description

The shortest paths between all the pairs of nodes, section 13.5.

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

Function documentation

◆ addEdge()

template<class D>
void ADS::addEdge ( AllPairs< D > & R,
int a,
int b,
const D & w )

the insertion of the edge \( ( a , b ) \) of cost \( w \), \( O( n^2 ) \)

In pseudocode (exercise 13.5), a path that improves using the edge once:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{addEdge}( d , p , a , b , w )\quad \triangleright\ \text{the new edge } ( a , b ) \text{ of weight } w \cr 2 & \quad \textbf{if}\ d[ a ][ b ] \le w\ \textbf{then return}\quad \triangleright\ \text{no path gets shorter} \cr 3 & \quad ( d' , p' ) \gets ( d , p )\quad \triangleright \ \text{the old tables} \cr 4 & \quad \textbf{foreach}\ u , v \in V \text{ with } d'[ u ][ a ] + w + d'[ b ][ v ] < d[ u ][ v ]\ \textbf{do}\quad \triangleright \ \text{through } ( a , b ) \cr 5 & \qquad d[ u ][ v ] \gets d'[ u ][ a ] + w + d'[ b ][ v ];\ p[ u ][ v ] \gets a \text{ if } v = b \text{, else } p'[ b ][ v ] \end{array}} \]

Precondition
\( a \) and \( b \) are distinct places of \( R \), and \( w + d[ b ][ a ] \ge 0 \)
Postcondition
\( d[ a ][ b ] \le w \), and the distances are closed under the triangle inequality (audit)

◆ floydWarshall()

template<Graph G>
AllPairs< typename G::Weight > ADS::floydWarshall ( const G & g)

the shortest paths between all the pairs of nodes

In pseudocode (section 13.5), d and p overwritten for k = 1 , ... , n:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{floydWarshall}( G )\quad \triangleright \ p[ u ][ v ] \text{ the node before } v \text{ on the path} \cr 2 & \quad \textbf{foreach}\ u , v \in V\ \textbf{do}\quad \triangleright \ \text{the paths with no intermediate node} \cr 3 & \qquad ( d[ u ][ v ] , p[ u ][ v ] ) \gets ( 0 , \textbf{nil} ) \text{ if } u = v \text{, } ( w( u , v ) , u ) \text{ if } ( u , v ) \in E \text{, else } ( \infty , \textbf{nil} ) \cr 4 & \quad \textbf{foreach}\ k \in V\ \textbf{do}\quad \triangleright \ \text{now also through } k \cr 5 & \qquad \textbf{foreach}\ u , v \in V\ \textbf{do} \cr 6 & \qquad\quad \textbf{if}\ d[ u ][ k ] + d[ k ][ v ] < d[ u ][ v ] \ \textbf{then}\ ( d[ u ][ v ] , p[ u ][ v ] ) \gets ( d[ u ][ k ] + d[ k ][ v ] , p[ k ][ v ] ) \cr 7 & \quad \textbf{return}\ ( d , p , \text{whether } d[ u ][ u ] < 0 \text{ for some } u ) \end{array}} \]

Postcondition
the distances are closed under the triangle inequality, bounded by the edges and realized by \( p \), unless negativeCycle (audit)