94#ifndef __ADS_ShortestPaths
95 #define __ADS_ShortestPaths
120#include <type_traits>
148 if constexpr( std::numeric_limits< D >::has_infinity )
149 return( std::numeric_limits< D >::infinity() );
151 return( std::numeric_limits< D >::max() );
161 std::vector< int >
T;
182template<
class S ,
class D >
186 { s.extract() } -> std::convertible_to< int >;
188 { s.empty() } -> std::same_as< bool >;
199{
typename Q::Priority; } ||
216template< FrontierQueue Q >
221 static constexpr bool native =
225 static constexpr bool own = !
requires {
typename Q::Priority; };
228 template<
class X ,
bool N >
235 struct HandleOf< X , true >
243 std::vector< bool > in;
256 if constexpr( native )
257 f_h.resize( std::size_t( n ) );
260 f_l.in = std::vector< bool >( std::size_t( n ) ,
false );
265 void add(
int v ,
const D & d ,
bool ) {
266 if constexpr( native )
267 f_h[ v ] = insert( v , d );
277 if constexpr( native )
293 if constexpr( native )
300 [[nodiscard]]
bool empty(
void )
const {
301 if constexpr( native )
302 return( f_q.empty() );
304 return( f_l.live == 0 );
311 if constexpr(
requires {
typename P::first_type; } )
312 return( P( d , v ) );
321 decltype( auto ) insert(
int v ,
const D & d ) {
330 static int node(
const X & x ) {
332 return(
int( x.second ) );
339 std::vector< typename HandleOf< Q , native >::type > f_h;
352 static constexpr bool fifo =
true;
359 void add(
int v ,
const D & ,
bool ) {
364 int extract(
void ) {
return( f_q.dequeue() ); }
371 [[nodiscard]]
bool empty(
void )
const {
return( f_q.empty() ); }
389 void add(
int v ,
const D & ,
bool ) {
401 [[nodiscard]]
bool empty(
void )
const {
return( f_q.empty() ); }
420 void add(
int v ,
const D & ,
bool first ) {
428 int extract(
void ) {
return( f_q.removeHead() ); }
435 [[nodiscard]]
bool empty(
void )
const {
return( f_q.empty() ); }
500template< Graph G ,
class S >
647#include "ADS/graph/impl/ShortestPaths.tpp"
The QUEUE of the book [see ADT.h] realized with a circular vector of places, the place 0 following t...
The STACK of the book [see ADT.h] realized with a vector of a given capacity : the items are in the ...
The capabilities of the structures, beyond the book: what an algorithm may ask of its argument to cho...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The DEQUEUE (double ended queue) of exercise 4.9, a sequence changed at both ends,...
The PRIORITYQUEUE [see PriorityQueue.h] on a Fibonacci heap, the structure that section 11....
The PRIORITYQUEUE [see PriorityQueue.h] on a heap.
The PRIORITYQUEUE [see PriorityQueue.h] on a list.
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
The PRIORITYQUEUE of section 10.1, with decreasing priority, in two concepts.
The visits of a GRAPH of section 9.5 and their applications, each in time on lists or vectors of adj...
DequeFrontier(Q &q)
on the empty structure
Definition ShortestPaths.h:416
int extract(void)
a node out of
Definition ShortestPaths.h:428
void add(int v, const D &, bool first)
at the tail of if first, at its head if not
Definition ShortestPaths.h:420
bool empty(void) const
whether is empty
Definition ShortestPaths.h:435
void update(int, const D &)
nothing: the order of does not depend on the distances
Definition ShortestPaths.h:432
void update(int v, const D &d)
the priority of , already in , falls to
Definition ShortestPaths.h:292
PriorityFrontier(Q &q, int n)
on the queue , empty, for the nodes
Definition ShortestPaths.h:254
bool empty(void) const
whether is empty; a lazy queue may still hold stale copies
Definition ShortestPaths.h:300
void add(int v, const D &d, bool)
into at the distance
Definition ShortestPaths.h:265
static priority_t< Q > key(int v, const D &d)
the priority of at the distance
Definition ShortestPaths.h:309
int extract(void)
a node out of ; the stale copies of a lazy queue skipped
Definition ShortestPaths.h:276
void update(int, const D &)
nothing: the order of does not depend on the distances
Definition ShortestPaths.h:368
QueueFrontier(Q &q)
on the empty structure
Definition ShortestPaths.h:355
void add(int v, const D &, bool)
into
Definition ShortestPaths.h:359
int extract(void)
a node out of
Definition ShortestPaths.h:364
bool empty(void) const
whether is empty
Definition ShortestPaths.h:371
static constexpr bool fifo
the bound of the extractions
Definition ShortestPaths.h:352
StackFrontier(Q &q)
on the empty structure
Definition ShortestPaths.h:385
void update(int, const D &)
nothing: the order of does not depend on the distances
Definition ShortestPaths.h:398
bool empty(void) const
whether is empty
Definition ShortestPaths.h:401
int extract(void)
a node out of
Definition ShortestPaths.h:394
void add(int v, const D &, bool)
into
Definition ShortestPaths.h:389
a priority queue that PriorityFrontier and Prim's algorithm take: one that lowers its priorities,...
Definition ShortestPaths.h:195
the PRIORITYQUEUE of section 10.1 without handles: the type of the items (Item or value_type),...
Definition PriorityQueue.h:102
the structure of the prototype as a type: add( v , d , first ) puts v into S at the distance d,...
Definition ShortestPaths.h:183
The GRAPH of chapter 9 and its realizations.
constexpr decrease_key_kind decrease_key_of(void)
how the queue Q lowers a priority: Q::decrease_key if it declares it (e.g., a queue whose decrease() ...
Definition Capabilities.h:578
typename detail::priority_of< Q >::type priority_t
the priorities of the queue Q: Q::Priority, else Q::Key (the first key of a queue of two,...
Definition Capabilities.h:190
@ none
Definition Capabilities.h:553
@ native
Definition Capabilities.h:553
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
bool isShortestPathTree(const G &g, int r, const ShortestPathTree< typename G::Weight > &R)
whether is a tree of shortest paths from : d[ r ] = 0, the unreachable nodes at infinity() with no p...
bool reachesNegativeCycle(const G &g, int r)
whether a negative cycle is reachable from , by Bellman-Ford,
Frontier
the structure of the prototype
Definition ShortestPaths.h:135
auto dijkstra(const G &g, int r)
Dijkstra: a priority queue on an unsorted list, .
ShortestPathTree< typename G::Weight > dagShortestPaths(const G &g, int r)
the shortest paths of an acyclic graph, scanning its nodes in topological order,
auto fredmanTarjan(const G &g, int r)
Fredman-Tarjan: a priority queue on a Fibonacci heap, .
auto johnson(const G &g, int r)
Johnson: a priority queue on a binary heap, .
auto papeDEsopo(const G &g, int r)
Pape-D'Esopo: a deque.
constexpr D infinity(void)
the infinite distance of the type D
Definition ShortestPaths.h:146
ShortestPathTree< typename G::Weight > shortestPaths(const G &g, int r, Frontier how)
the shortest paths from with the structure how
auto bellmanFordMoore(const G &g, int r)
Bellman-Ford-Moore: a queue, , negative cycles detected.
@ STACK
a stack
Definition ShortestPaths.h:140
@ LIST
a priority queue on an unsorted list: Dijkstra
Definition ShortestPaths.h:136
@ FIBONACCI
a priority queue on a Fibonacci heap: Fredman and Tarjan
Definition ShortestPaths.h:138
@ DEQUE
a deque: Pape and D'Esopo
Definition ShortestPaths.h:141
@ HEAP
a priority queue on a binary heap: Johnson
Definition ShortestPaths.h:137
@ QUEUE
a queue: Bellman, Ford and Moore
Definition ShortestPaths.h:139
constexpr detail::QueueDeleteMin queue_deleteMin
the item of smallest priority of , removed: q.deleteMin(), else queue_deleteMin( q ) found by ADL,...
Definition QueueCustomization.h:279
constexpr detail::QueueDecrease queue_decrease
the priority of the item of the handle of lowered to , i.e., the item moved toward the minimum: q....
Definition QueueCustomization.h:295
typename detail::queue_handle< Q >::type queue_handle_t
the handles of the queue Q, which its insertions return: Q::Handle, else Q::handle_type (the mutable ...
Definition QueueCustomization.h:250
typename detail::queue_item< Q >::type queue_item_t
the items of the queue Q: Q::Item, else Q::value_type
Definition QueueCustomization.h:93
constexpr detail::QueueInsert queue_insert
inserted in with the keys of its order: q.insert( x , a... ), else queue_insert( q ,...
Definition QueueCustomization.h:284
what a run of the prototype did: the nodes added to (the lines (1) and (3)), the extractions (2),...
Definition ShortestPaths.h:171
long long adds
the nodes added to
Definition ShortestPaths.h:172
long long decreases
the decreases of a priority
Definition ShortestPaths.h:174
long long extracts
the extractions
Definition ShortestPaths.h:173
long long edges
the edges examined
Definition ShortestPaths.h:175
the result: the distances, infinity() if not reachable, the tree of the parents, NIL for and the unr...
Definition ShortestPaths.h:159
std::vector< int > T
the parent of each node
Definition ShortestPaths.h:161
bool negativeCycle
whether a negative cycle was found
Definition ShortestPaths.h:163
std::vector< int > order
the nodes in the order of extraction
Definition ShortestPaths.h:162
std::vector< D > d
the distances
Definition ShortestPaths.h:160