ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Dijkstra.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File Dijkstra.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
39/*--------------------------------------------------------------------------*/
40/*----------------------------- DEFINITIONS --------------------------------*/
41/*--------------------------------------------------------------------------*/
42
43#ifndef __ADS_interop_boost_Dijkstra
44 #define __ADS_interop_boost_Dijkstra
45/* self-identification: #endif constant */
46
47#if defined( ADS_WITH_BOOST )
48
49/*--------------------------------------------------------------------------*/
50/*------------------------------ INCLUDES ----------------------------------*/
51/*--------------------------------------------------------------------------*/
52
53#include "ADS/Contract.h"
55
56#include <boost/graph/breadth_first_search.hpp>
57#include <boost/graph/graph_traits.hpp>
58#include <boost/graph/properties.hpp>
59#include <boost/property_map/property_map.hpp>
60
61#include <limits>
62#include <vector>
63
64/*--------------------------------------------------------------------------*/
65/*--------------------------- NAMESPACE ------------------------------------*/
66/*--------------------------------------------------------------------------*/
67
68namespace ADS::bgl
69{
70
71/*--------------------------------------------------------------------------*/
72/*-------------------------- CLASS UpdatableQueue --------------------------*/
73/*--------------------------------------------------------------------------*/
74
78template< FrontierQueue Q , class DistanceMap >
79class UpdatableQueue
80{
81
82 public:
83 using value_type = int;
84
88 UpdatableQueue( Q & q , int n , DistanceMap d ) : f_S( q , n ) , f_d( d ) { }
89
91 void push( int v ) { f_S.add( v , get( f_d , v ) , true ); }
92
94 void update( int v ) { f_S.update( v , get( f_d , v ) ); }
95
98 int top( void ) {
99 if( f_top < 0 )
100 f_top = f_S.extract();
101 return( f_top );
102 }
103
106 void pop( void ) {
107 ( void )top();
108 f_top = -1;
109 }
110
112 [[nodiscard]] bool empty( void ) const {
113 return( ( f_top < 0 ) && f_S.empty() );
114 }
115
116 private:
117 PriorityFrontier< Q > f_S;
118 DistanceMap f_d;
119 int f_top = -1;
120
121 }; // end( class UpdatableQueue )
122
123/*--------------------------------------------------------------------------*/
124/*----------------------------- THE ALGORITHM ------------------------------*/
125/*--------------------------------------------------------------------------*/
126
128namespace detail
129{
130
134template< class B , class WeightMap , class PredecessorMap ,
135 class DistanceMap >
136struct RelaxVisitor : boost::default_bfs_visitor
137{
138 B * queue;
139 WeightMap w;
140 PredecessorMap p;
141 DistanceMap d;
142
144
145 template< class E , class G >
146 bool relax( const E & e , const G & g ) const {
147 const auto u = source( e , g ) , v = target( e , g );
148 const auto du = get( d , u ) + get( w , e );
149 if( du < get( d , v ) ) {
150 put( d , v , du );
151 put( p , v , u );
152 return( true );
153 }
154 return( false );
155 }
156
158 template< class E , class G >
159 void tree_edge( const E & e , const G & g ) const {
160 ( void )relax( e , g );
161 }
162
164 template< class E , class G >
165 void gray_target( const E & e , const G & g ) const {
166 if( relax( e , g ) )
167 queue->update( int( target( e , g ) ) );
168 }
169 };
170
171} // end( namespace detail )
173
201template< class G , FrontierQueue Q , class WeightMap , class PredecessorMap ,
202 class DistanceMap >
203void dijkstraShortestPaths( const G & g , int s , Q & q , WeightMap w ,
204 PredecessorMap p , DistanceMap d )
205{
206 using D = typename boost::property_traits< DistanceMap >::value_type;
207 const int n = int( num_vertices( g ) );
208 ADS_PRE( ( s >= 0 ) && ( s < n ) && q.empty() );
209 for( int v = 0 ; v < n ; ++v ) {
210 put( d , v , std::numeric_limits< D >::max() );
211 put( p , v , v );
212 }
213 put( d , s , D( 0 ) );
214 using B = UpdatableQueue< Q , DistanceMap >;
215 B buffer( q , n , d );
216 std::vector< boost::default_color_type > color( std::size_t( n ) ,
217 boost::white_color );
218 detail::RelaxVisitor< B , WeightMap , PredecessorMap , DistanceMap > vis;
219 vis.queue = &buffer;
220 vis.w = w;
221 vis.p = p;
222 vis.d = d;
223 boost::breadth_first_visit( // not color.data(), which another header of
224 g , s , buffer , vis , // Boost.Graph may hide
225 boost::make_iterator_property_map(
226 color.data() , boost::typed_identity_property_map< int >() ) );
227 }
228
231template< class G , FrontierQueue Q , class PredecessorMap ,
232 class DistanceMap >
233void dijkstraShortestPaths( const G & g , int s , Q & q , PredecessorMap p ,
234 DistanceMap d )
235{
236 dijkstraShortestPaths( g , s , q , get( boost::edge_weight , g ) , p , d );
237 }
238
239/*--------------------------------------------------------------------------*/
240
241} // end( namespace ADS::bgl )
242
243#endif /* ADS_WITH_BOOST */
244
245/*--------------------------------------------------------------------------*/
246
247#endif /* Dijkstra.h included */
248
249/*--------------------------------------------------------------------------*/
250/*--------------------------- End File Dijkstra.h --------------------------*/
251/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The shortest paths of chapter 11 from a node of an oriented GRAPH whose edges have a cost [see Grap...
#define ADS_PRE(cond)
[check]
Definition Contract.h:121