|
ADS
Algorithms and Data Structures
|
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]. More...
#include "ADS/Contract.h"#include "ADS/Capabilities.h"#include "ADS/Portable.h"#include "ADS/graph/Graph.h"#include "ADS/graph/Visit.h"#include "ADS/priority/FibonacciHeapPriorityQueue.h"#include "ADS/priority/HeapPriorityQueue.h"#include "ADS/priority/ListPriorityQueue.h"#include "ADS/priority/PriorityQueue.h"#include "ADS/queue/ArrayQueue.h"#include "ADS/queue/Dequeue.h"#include "ADS/stack/ArrayStack.h"#include <algorithm>#include <concepts>#include <cstddef>#include <limits>#include <memory>#include <type_traits>#include <vector>#include "ADS/graph/impl/ShortestPaths.tpp"Go to the source code of this file.
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... | |
Namespaces | |
| namespace | ADS |
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 ) | |
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 ) \) | |
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].
A feasible solution is a tree T rooted in r, each node v at distance \( d_v \) from r in T; by the theorem of Bellman (Theorem 11.1) T is optimal if and only if
\[ d_v = d_u + w( u , v ) \quad \forall ( u , v ) \in T , \qquad d_u + w( u , v ) \geq d_v \quad \forall ( u , v ) \in E . \]
The prototype (section 11.2) starts from d = 0 in r and \( + \infty \) elsewhere and, while an edge violates the condition, sets \( d_v = d_u + w( u , v ) \) and makes u the parent of v; a structure S holds the nodes whose edges must be checked. Its kind gives the algorithm [see Frontier]:
With a priority queue a node already in S has its priority decreased (the line (4) of the book), by decrease() on its handle if the queue has handles and by a new insertion otherwise, whose stale copies are skipped when they come out (the lazy deletion, as decrease_key_of() of Capabilities.h chooses); in the code of the book the new distance and parent of v are written in both branches of that test. The structure S is a type [see PrototypeFrontier]: shortestPaths() with a Frontier runs the prototype on the six structures of the book, and the prototype runs as well on any priority queue of section 10.1 [see PriorityFrontier], queue, stack or deque of another library; with the priorities \( ( d_v , v ) \) the ties are broken by the names of the nodes, and every priority queue extracts the same nodes in the same order, so that the counts of a run [see FrontierCounts] measure the same work on any of them. Without negative cycles every frontier gives the shortest paths; with negative costs the priority queues are still correct but may extract a node more than once. dagShortestPaths() scans an acyclic graph in topological order, \( O( n + m ) \) whatever the signs of the costs. The distances are sums in D, not checked for overflow: the cost of every path from r must stay strictly between -infinity< D >() and infinity< D >(), and a node at distance infinity< D >() counts as unreachable.
The contracts [see Contract.h]: r is a node; with a frontier other than QUEUE no negative cycle is reachable from r, and dagShortestPaths() needs an acyclic graph, both checked at the level of the audits (the first by Bellman-Ford, \( O( n m ) \)); the tree found satisfies the conditions of Bellman unless a negative cycle was found, which isShortestPathTree() checks. The functions are defined in ShortestPaths.tpp.