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

The shortest paths of chapter 11 from a node \( r \) of an oriented GRAPH whose edges have a cost \( w( u , v ) \). More...

Files

file  ADS/graph/ShortestPaths.h
 The shortest paths of chapter 11 from a node \( r \) of an oriented GRAPH whose edges have a cost \( w( u , v ) \) [see Graph.h].

Concepts

concept  ADS::PrototypeFrontier
 the structure \( S \) of the prototype as a type: add( v , d , first ) puts v into S at the distance d, first if v was never reached; extract() takes a node out of S; update( v , d ) tells S that the distance of v, already in S, fell to d
concept  ADS::FrontierQueue
 a priority queue that PriorityFrontier and Prim's algorithm take: one that lowers its priorities, natively or lazily, whose priority is a Priority, or whose items are pairs ( key , node )

Classes

struct  ADS::ShortestPathTree< D >
 the result: the distances, infinity() if not reachable, the tree of the parents, NIL for \( r \) and the unreachable nodes, the nodes in the order of extraction from \( S \), and whether a negative cycle was found More...
struct  ADS::FrontierCounts
 what a run of the prototype did: the nodes added to \( S \) (the lines (1) and (3)), the extractions (2), the decreases of a priority (4) and the edges examined; the same on two frontiers that extract the nodes in the same order More...
class  ADS::PriorityFrontier< Q >
 a priority queue of the nodes [see PriorityQueue.h] as \( S \): the priority of a node its distance or, if the priority is a pair, the pair ( distance , node ), which breaks the ties by the names of the nodes, so that any priority queue extracts the same nodes in the same order. More...
class  ADS::QueueFrontier< Q >
 a queue as \( S \), enqueue() and dequeue(): first in, first out, so that a node leaves S at most n times unless a negative cycle is reachable More...
class  ADS::StackFrontier< Q >
 a stack as \( S \), push() and pop() More...
class  ADS::DequeFrontier< Q >
 a deque as \( S \), the rule of Pape and D'Esopo: a node at the tail the first time, at the head afterwards, extracted from the head More...

Enumerations

enum class  ADS::Frontier {
  ADS::Frontier::LIST , ADS::Frontier::HEAP , ADS::Frontier::FIBONACCI , ADS::Frontier::QUEUE ,
  ADS::Frontier::STACK , ADS::Frontier::DEQUE
}
 the structure \( S \) of the prototype More...

Functions

template<class D>
constexpr D ADS::infinity (void)
 the infinite distance of the type D
template<Graph G>
ShortestPathTree< typename G::Weight > ADS::shortestPaths (const G &g, int r, Frontier how)
 the shortest paths from \( r \) with the structure how
template<Graph G, class S>
requires PrototypeFrontier< S , typename G::Weight >
ShortestPathTree< typename G::Weight > ADS::shortestPaths (const G &g, int r, S &frontier, FrontierCounts *c=nullptr)
 the shortest paths of shortestPaths() above, with the structure \( S \) given, its work in *c if given
template<Graph G>
auto ADS::dijkstra (const G &g, int r)
 Dijkstra: a priority queue on an unsorted list, \( O( n^2 ) \).
template<Graph G>
auto ADS::johnson (const G &g, int r)
 Johnson: a priority queue on a binary heap, \( O( m \log n ) \).
template<Graph G>
auto ADS::fredmanTarjan (const G &g, int r)
 Fredman-Tarjan: a priority queue on a Fibonacci heap, \( O( m + n \log n ) \).
template<Graph G>
auto ADS::bellmanFordMoore (const G &g, int r)
 Bellman-Ford-Moore: a queue, \( O( n m ) \), negative cycles detected.
template<Graph G>
auto ADS::papeDEsopo (const G &g, int r)
 Pape-D'Esopo: a deque.
template<Graph G>
ShortestPathTree< typename G::Weight > ADS::dagShortestPaths (const G &g, int r)
 the shortest paths of an acyclic graph, scanning its nodes in topological order, \( O( n + m ) \)
template<Graph G>
bool ADS::isShortestPathTree (const G &g, int r, const ShortestPathTree< typename G::Weight > &R)
 whether \( R \) is a tree of shortest paths from \( r \): d[ r ] = 0, the unreachable nodes at infinity() with no parent, d[ v ] = d[ T[ v ] ]
template<Graph G>
bool ADS::reachesNegativeCycle (const G &g, int r)
 whether a negative cycle is reachable from \( r \), by Bellman-Ford, \( O( n m ) \)

Detailed description

The shortest paths of chapter 11 from a node \( r \) of an oriented GRAPH whose edges have a cost \( w( u , v ) \).

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

Enumeration type documentation

◆ Frontier

enum class ADS::Frontier
strong

the structure \( S \) of the prototype

Enumerator
LIST 

a priority queue on an unsorted list: Dijkstra

HEAP 

a priority queue on a binary heap: Johnson

FIBONACCI 

a priority queue on a Fibonacci heap: Fredman and Tarjan

QUEUE 

a queue: Bellman, Ford and Moore

STACK 

a stack

DEQUE 

a deque: Pape and D'Esopo

Function documentation

◆ bellmanFordMoore()

template<Graph G>
auto ADS::bellmanFordMoore ( const G & g,
int r )

Bellman-Ford-Moore: a queue, \( O( n m ) \), negative cycles detected.

The prototype with its structure S (section 11.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{bellmanFordMoore}( G , r ) \cr 2 & \quad \textbf{return}\ \mathrm{shortestPaths}( G , r , \text{a queue} ) \end{array}} \]

◆ dagShortestPaths()

template<Graph G>
ShortestPathTree< typename G::Weight > ADS::dagShortestPaths ( const G & g,
int r )

the shortest paths of an acyclic graph, scanning its nodes in topological order, \( O( n + m ) \)

In pseudocode (section 11.2): in topological order the nodes before u have their final distances when u is scanned, whatever the signs of the costs.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{dagShortestPaths}( G , r ) \cr 2 & \quad d[ v ] \gets \infty;\ T[ v ] \gets \textbf{nil} \text{ for every node } v;\ d[ r ] \gets 0 \cr 3 & \quad \textbf{foreach}\ u \text{ in the order of } \mathrm{topsort}( G )\ \textbf{do}\quad \triangleright\ d[ u ] \text{ is final when } u \text{ comes} \cr 4 & \qquad \textbf{if}\ d[ u ] = \infty\ \textbf{then continue} \cr 5 & \qquad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{do} \ \textbf{if}\ d[ u ] + w( u , v ) < d[ v ]\ \textbf{then}\ ( d[ v ] , T[ v ] ) \gets ( d[ u ] + w( u , v ) , u ) \cr 6 & \quad \textbf{return}\ ( d , T ) \end{array}} \]

Precondition
\( r \) is a node, and the graph is acyclic (audit)
Postcondition
the conditions of Bellman (audit)

◆ dijkstra()

template<Graph G>
auto ADS::dijkstra ( const G & g,
int r )

Dijkstra: a priority queue on an unsorted list, \( O( n^2 ) \).

The prototype with its structure S (section 11.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{dijkstra}( G , r ) \cr 2 & \quad \textbf{return}\ \mathrm{shortestPaths}( G , r , \text{a } \text{priority queue on an unsorted list} ) \end{array}} \]

◆ fredmanTarjan()

template<Graph G>
auto ADS::fredmanTarjan ( const G & g,
int r )

Fredman-Tarjan: a priority queue on a Fibonacci heap, \( O( m + n \log n ) \).

The prototype with its structure S (section 11.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fredmanTarjan}( G , r ) \cr 2 & \quad \textbf{return}\ \mathrm{shortestPaths}( G , r , \text{a } \text{priority queue on a Fibonacci heap} ) \end{array}} \]

◆ infinity()

template<class D>
D ADS::infinity ( void )
constexpr

the infinite distance of the type D

◆ isShortestPathTree()

template<Graph G>
bool ADS::isShortestPathTree ( const G & g,
int r,
const ShortestPathTree< typename G::Weight > & R )

whether \( R \) is a tree of shortest paths from \( r \): d[ r ] = 0, the unreachable nodes at infinity() with no parent, d[ v ] = d[ T[ v ] ]

  • w( T[ v ] , v ) on the others, and d[ u ] + w( u , v ) >= d[ v ] on each edge from a reached node (Theorem 11.1), \( O( n + m ) \) on lists

In pseudocode (the conditions of Bellman, Theorem 11.1):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isShortestPathTree}( G , r , d , T ) \cr 2 & \quad \textbf{if}\ T[ r ] \ne \textbf{nil}\ \textbf{or}\ d[ r ] \ne 0\ \textbf{then return false} \cr 3 & \quad \textbf{foreach}\ v \ne r\ \textbf{do}\quad \triangleright\ T \text{ realizes } d \cr 4 & \qquad \textbf{if}\ d[ v ] < \infty \text{ and } d[ v ] \ne d[ T[ v ] ] + w( T[ v ] , v ) \text{, or } d[ v ] = \infty \text{ and } T[ v ] \ne \textbf{nil}\ \textbf{then return false} \cr 5 & \quad \textbf{foreach}\ ( u , v ) \in E \text{ with } d[ u ] < \infty\ \textbf{do}\quad \triangleright\ \text{no edge shortens } d \cr 6 & \qquad \textbf{if}\ d[ v ] > d[ u ] + w( u , v ) \ \textbf{then return false} \cr 7 & \quad \textbf{return true} \end{array}} \]

◆ johnson()

template<Graph G>
auto ADS::johnson ( const G & g,
int r )

Johnson: a priority queue on a binary heap, \( O( m \log n ) \).

The prototype with its structure S (section 11.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{johnson}( G , r ) \cr 2 & \quad \textbf{return}\ \mathrm{shortestPaths}( G , r , \text{a } \text{priority queue on a binary heap} ) \end{array}} \]

◆ papeDEsopo()

template<Graph G>
auto ADS::papeDEsopo ( const G & g,
int r )

Pape-D'Esopo: a deque.

The prototype with its structure S (section 11.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{papeDEsopo}( G , r ) \cr 2 & \quad \textbf{return}\ \mathrm{shortestPaths}( G , r , \text{a deque, } \text{a node at its tail the first time and at its head afterwards} ) \end{array}} \]

◆ reachesNegativeCycle()

template<Graph G>
bool ADS::reachesNegativeCycle ( const G & g,
int r )

whether a negative cycle is reachable from \( r \), by Bellman-Ford, \( O( n m ) \)

In pseudocode (exercise 11.2): with a queue no node leaves S more than n times unless a negative cycle is reachable.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{reachesNegativeCycle}( G , r ) \cr 2 & \quad \textbf{return}\ \text{whether } \mathrm{shortestPaths}( G , r , \text{an empty queue} ) \text{ stops on a negative cycle} \end{array}} \]

◆ shortestPaths() [1/2]

template<Graph G>
ShortestPathTree< typename G::Weight > ADS::shortestPaths ( const G & g,
int r,
Frontier how )

the shortest paths from \( r \) with the structure how

In pseudocode, the prototype shortestPaths( G , r , S ) below, with \( S \) the structure that how names:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{shortestPaths}( G , r , \mathit{how} ) \cr 2 & \quad \textbf{return}\ \mathrm{shortestPaths}( G , r , \text{an empty } \text{structure of kind } \mathit{how} ) \end{array}} \]

Precondition
\( r \) is a node; no negative cycle reachable from \( r \) unless how is QUEUE (audit)
Postcondition
the conditions of Bellman, unless a negative cycle was found (audit)

◆ shortestPaths() [2/2]

template<Graph G, class S>
requires PrototypeFrontier< S , typename G::Weight >
ShortestPathTree< typename G::Weight > ADS::shortestPaths ( const G & g,
int r,
S & frontier,
FrontierCounts * c = nullptr )

the shortest paths of shortestPaths() above, with the structure \( S \) given, its work in *c if given

In pseudocode (the prototype of section 11.2), w( u , v ) the cost of the edge:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{shortestPaths}( G , r , S )\quad \triangleright\ S \text{ the frontier, } b[ v ] \text{ whether } v \in S \cr 2 & \quad d[ v ] \gets \infty;\ T[ v ] \gets \textbf{nil} \text{ for every node } v \cr 3 & \quad d[ r ] \gets 0;\ \mathrm{add}( S , r ) \cr 4 & \quad \textbf{while}\ S \ne \emptyset\ \textbf{do} \cr 5 & \qquad u \gets \mathrm{extract}( S )\quad \triangleright \ \text{the frontier decides which node} \cr 6 & \qquad \textbf{if}\ S \text{ is a queue and } u \text{ has now left it } n + 1 \text{ times}\ \textbf{then return} \ \text{a negative cycle} \cr 7 & \qquad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{do} \cr 8 & \qquad\quad \textbf{if}\ d[ v ] \le d[ u ] + w( u , v ) \ \textbf{then continue}\quad \triangleright \ \text{no shorter path to } v \cr 9 & \qquad\quad ( d[ v ] , T[ v ] ) \gets ( d[ u ] + w( u , v ) , u ) \cr 10 & \qquad\quad \textbf{if}\ b[ v ]\ \textbf{then}\ \mathrm{update}( S , v )\ \textbf{else}\ \mathrm{add}( S , v )\quad \triangleright \ \mathrm{update} \text{ lowers a priority} \cr 11 & \quad \textbf{return}\ ( d , T ) \end{array}} \]

Precondition
\( r \) is a node, \( S \) is empty; no negative cycle reachable from \( r \) unless \( S \) is first in, first out (audit)
Postcondition
the conditions of Bellman, unless a negative cycle was found (audit)