ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ShortestPaths.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File ShortestPaths.h --------------------------*/
3/*--------------------------------------------------------------------------*/
90/*--------------------------------------------------------------------------*/
91/*----------------------------- DEFINITIONS --------------------------------*/
92/*--------------------------------------------------------------------------*/
93
94#ifndef __ADS_ShortestPaths
95 #define __ADS_ShortestPaths
96/* self-identification: #endif constant */
97
98/*--------------------------------------------------------------------------*/
99/*------------------------------ INCLUDES ----------------------------------*/
100/*--------------------------------------------------------------------------*/
101
102#include "ADS/Contract.h"
103#include "ADS/Capabilities.h"
104#include "ADS/Portable.h"
105#include "ADS/graph/Graph.h"
106#include "ADS/graph/Visit.h"
111#include "ADS/queue/ArrayQueue.h"
112#include "ADS/queue/Dequeue.h"
113#include "ADS/stack/ArrayStack.h"
114
115#include <algorithm>
116#include <concepts>
117#include <cstddef>
118#include <limits>
119#include <memory>
120#include <type_traits>
121#include <vector>
122
123/*--------------------------------------------------------------------------*/
124/*--------------------------- NAMESPACE ------------------------------------*/
125/*--------------------------------------------------------------------------*/
126
127namespace ADS
128{
129
130/*--------------------------------------------------------------------------*/
131/*-------------------------------- TYPES -----------------------------------*/
132/*--------------------------------------------------------------------------*/
133
143
145template< class D >
146constexpr D infinity( void )
147{
148 if constexpr( std::numeric_limits< D >::has_infinity )
149 return( std::numeric_limits< D >::infinity() );
150 else
151 return( std::numeric_limits< D >::max() );
152 }
153
157template< class D >
159{
160 std::vector< D > d;
161 std::vector< int > T;
162 std::vector< int > order;
163 bool negativeCycle = false;
164 };
165
171{
172 long long adds = 0 ,
173 extracts = 0 ,
175 edges = 0;
176 };
177
182template< class S , class D >
183concept PrototypeFrontier = requires( S s , int v , const D & d , bool b )
184{
185 s.add( v , d , b );
186 { s.extract() } -> std::convertible_to< int >;
187 s.update( v , d );
188 { s.empty() } -> std::same_as< bool >;
189 };
190
194template< class Q >
198 ( requires
199{ typename Q::Priority; } ||
200 requires { typename queue_item_t< Q >::first_type; } );
201
216template< FrontierQueue Q >
218{
219
221 static constexpr bool native =
223
225 static constexpr bool own = ! requires { typename Q::Priority; };
226
228 template< class X , bool N >
229 struct HandleOf
230 {
231 using type = char;
232 };
233
234 template< class X >
235 struct HandleOf< X , true >
236 {
237 using type = queue_handle_t< X >;
238 };
239
241 struct Lazy
242 {
243 std::vector< bool > in;
244 int live = 0;
245 };
246
248 struct Native
249 { };
250
251 public:
254 PriorityFrontier( Q & q , int n ) : f_q( q ) {
255 ADS_PRE( n >= 0 );
256 if constexpr( native )
257 f_h.resize( std::size_t( n ) );
258 else // made anew: assign() inserts after the old bits, none here, and
259 // GCC 13 warns of their copy out of bounds
260 f_l.in = std::vector< bool >( std::size_t( n ) , false );
261 }
262
264 template< class D >
265 void add( int v , const D & d , bool ) {
266 if constexpr( native )
267 f_h[ v ] = insert( v , d );
268 else {
269 insert( v , d );
270 f_l.in[ v ] = true;
271 ++f_l.live;
272 }
273 }
274
276 int extract( void ) {
277 if constexpr( native )
278 return( node( ADS::queue_deleteMin( f_q ) ) );
279 else
280 for( ; ; ) {
281 const int v = node( ADS::queue_deleteMin( f_q ) );
282 if( f_l.in[ v ] ) {
283 f_l.in[ v ] = false;
284 --f_l.live;
285 return( v );
286 }
287 }
288 }
289
291 template< class D >
292 void update( int v , const D & d ) {
293 if constexpr( native )
294 ADS::queue_decrease( f_q , f_h[ v ] , key( v , d ) );
295 else
296 insert( v , d );
297 }
298
300 [[nodiscard]] bool empty( void ) const {
301 if constexpr( native )
302 return( f_q.empty() );
303 else
304 return( f_l.live == 0 );
305 }
306
308 template< class D >
309 static priority_t< Q > key( int v , const D & d ) {
310 using P = priority_t< Q >;
311 if constexpr( requires { typename P::first_type; } )
312 return( P( d , v ) );
313 else
314 return( P( d ) );
315 }
316
317 private:
320 template< class D >
321 decltype( auto ) insert( int v , const D & d ) {
322 if constexpr( own )
323 return( ADS::queue_insert( f_q , key( v , d ) ) );
324 else
325 return( ADS::queue_insert( f_q , v , key( v , d ) ) );
326 }
327
329 template< class X >
330 static int node( const X & x ) {
331 if constexpr( own )
332 return( int( x.second ) );
333 else
334 return( int( x ) );
335 }
336
337 Q & f_q;
339 std::vector< typename HandleOf< Q , native >::type > f_h;
341 ADS_NO_UNIQUE_ADDRESS std::conditional_t< native , Native , Lazy > f_l;
342
343 }; // end( class PriorityFrontier )
344
347template< class Q >
349{
350
351 public:
352 static constexpr bool fifo = true;
353
355 explicit QueueFrontier( Q & q ) : f_q( q ) { }
356
358 template< class D >
359 void add( int v , const D & , bool ) {
360 f_q.enqueue( v );
361 }
362
364 int extract( void ) { return( f_q.dequeue() ); }
365
367 template< class D >
368 void update( int , const D & ) { }
369
371 [[nodiscard]] bool empty( void ) const { return( f_q.empty() ); }
372
373 private:
374 Q & f_q;
375
376 }; // end( class QueueFrontier )
377
379template< class Q >
381{
382
383 public:
385 explicit StackFrontier( Q & q ) : f_q( q ) { }
386
388 template< class D >
389 void add( int v , const D & , bool ) {
390 f_q.push( v );
391 }
392
394 int extract( void ) { return( f_q.pop() ); }
395
397 template< class D >
398 void update( int , const D & ) { }
399
401 [[nodiscard]] bool empty( void ) const { return( f_q.empty() ); }
402
403 private:
404 Q & f_q;
405
406 }; // end( class StackFrontier )
407
410template< class Q >
412{
413
414 public:
416 explicit DequeFrontier( Q & q ) : f_q( q ) { }
417
419 template< class D >
420 void add( int v , const D & , bool first ) {
421 if( first )
422 f_q.insertTail( v );
423 else
424 f_q.insertHead( v );
425 }
426
428 int extract( void ) { return( f_q.removeHead() ); }
429
431 template< class D >
432 void update( int , const D & ) { }
433
435 [[nodiscard]] bool empty( void ) const { return( f_q.empty() ); }
436
437 private:
438 Q & f_q;
439
440 }; // end( class DequeFrontier )
441
442/*--------------------------------------------------------------------------*/
443/*------------------------------ PROTOTYPE ---------------------------------*/
444/*--------------------------------------------------------------------------*/
445
462template< Graph G >
464 Frontier how );
465
500template< Graph G , class S >
503shortestPaths( const G & g , int r , S & frontier ,
504 FrontierCounts * c = nullptr );
505
516template< Graph G >
517auto dijkstra( const G & g , int r );
518
529template< Graph G >
530auto johnson( const G & g , int r );
531
543template< Graph G >
544auto fredmanTarjan( const G & g , int r );
545
555template< Graph G >
556auto bellmanFordMoore( const G & g , int r );
557
568template< Graph G >
569auto papeDEsopo( const G & g , int r );
570
593template< Graph G >
595
620template< Graph G >
621bool isShortestPathTree( const G & g , int r ,
623
636template< Graph G >
637bool reachesNegativeCycle( const G & g , int r );
638
639/*--------------------------------------------------------------------------*/
640
641} // end( namespace ADS )
642
643/*--------------------------------------------------------------------------*/
644/*-------------------------- IMPLEMENTATION --------------------------------*/
645/*--------------------------------------------------------------------------*/
646
647#include "ADS/graph/impl/ShortestPaths.tpp"
648
649/*--------------------------------------------------------------------------*/
650
651#endif /* ShortestPaths.h included */
652
653/*--------------------------------------------------------------------------*/
654/*------------------------ End File ShortestPaths.h ------------------------*/
655/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96
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