33 * This class provides the same API as the BreadthFirstShortestPaths<T>.
44 */
55
6- using System ;
7- using System . Diagnostics ;
8- using System . Collections . Generic ;
9-
106using Algorithms . Common ;
117using DataStructures . Graphs ;
128using DataStructures . Heaps ;
9+ using System ;
10+ using System . Collections . Generic ;
1311
1412namespace Algorithms . Graphs
1513{
@@ -33,7 +31,7 @@ public class DijkstraShortestPaths<TGraph, TVertex>
3331 private Dictionary < int , TVertex > _indicesToNodes ;
3432
3533 // A const that represent an infinite distance
36- private const Int64 Infinity = Int64 . MaxValue ;
34+ private const long Infinity = long . MaxValue ;
3735 private const int NilPredecessor = - 1 ;
3836
3937
@@ -44,9 +42,14 @@ public class DijkstraShortestPaths<TGraph, TVertex>
4442 public DijkstraShortestPaths ( TGraph Graph , TVertex Source )
4543 {
4644 if ( Graph == null )
45+ {
4746 throw new ArgumentNullException ( ) ;
48- else if ( ! Graph . HasVertex ( Source ) )
47+ }
48+
49+ if ( ! Graph . HasVertex ( Source ) )
50+ {
4951 throw new ArgumentException ( "The source vertex doesn't belong to graph." ) ;
52+ }
5053
5154 // Init
5255 _initializeDataMembers ( Graph ) ;
@@ -55,9 +58,10 @@ public DijkstraShortestPaths(TGraph Graph, TVertex Source)
5558 _dijkstra ( Graph , Source ) ;
5659
5760 // check for the acyclic invariant
58- //if (!_checkOptimalityConditions(Graph, Source))
59- // throw new Exception("Graph doesn't match optimality conditions.");
60- Debug . Assert ( _checkOptimalityConditions ( Graph , Source ) ) ;
61+ if ( ! _checkOptimalityConditions ( Graph , Source ) )
62+ {
63+ throw new InvalidOperationException ( "Graph doesn't match optimality condition." ) ;
64+ }
6165 }
6266
6367
@@ -71,7 +75,7 @@ private void _dijkstra(TGraph graph, TVertex source)
7175 {
7276 var minPQ = new MinPriorityQueue < TVertex , long > ( ( uint ) _verticesCount ) ;
7377
74- int srcIndex = _nodesToIndices [ source ] ;
78+ var srcIndex = _nodesToIndices [ source ] ;
7579 _distances [ srcIndex ] = 0 ;
7680
7781 minPQ . Enqueue ( source , _distances [ srcIndex ] ) ;
@@ -89,8 +93,10 @@ private void _dijkstra(TGraph graph, TVertex source)
8993
9094 // calculate a new possible weighted path if the edge weight is less than infinity
9195 var delta = Infinity ;
92- if ( edge . Weight < Infinity && ( Infinity - edge . Weight ) > _distances [ currentIndex ] ) // Handles overflow
96+ if ( edge . Weight < Infinity && Infinity - edge . Weight > _distances [ currentIndex ] ) // Handles overflow
97+ {
9398 delta = _distances [ currentIndex ] + edge . Weight ;
99+ }
94100
95101 // Relax the edge
96102 // if check is true, a shorter path is found from current to adjacent
@@ -102,9 +108,13 @@ private void _dijkstra(TGraph graph, TVertex source)
102108
103109 // decrease priority with a new distance if it exists; otherwise enqueque it
104110 if ( minPQ . Contains ( edge . Destination ) )
111+ {
105112 minPQ . UpdatePriority ( edge . Destination , delta ) ;
113+ }
106114 else
115+ {
107116 minPQ . Enqueue ( edge . Destination , delta ) ;
117+ }
108118 }
109119 } //end-foreach
110120 } //end-while
@@ -118,19 +128,21 @@ private void _initializeDataMembers(TGraph Graph)
118128 _edgesCount = Graph . EdgesCount ;
119129 _verticesCount = Graph . VerticesCount ;
120130
121- _distances = new Int64 [ _verticesCount ] ;
131+ _distances = new long [ _verticesCount ] ;
122132 _predecessors = new int [ _verticesCount ] ;
123133 _edgeTo = new WeightedEdge < TVertex > [ _edgesCount ] ;
124134
125135 _nodesToIndices = new Dictionary < TVertex , int > ( ) ;
126136 _indicesToNodes = new Dictionary < int , TVertex > ( ) ;
127137
128138 // Reset the information arrays
129- int i = 0 ;
139+ var i = 0 ;
130140 foreach ( var node in Graph . Vertices )
131141 {
132142 if ( i >= _verticesCount )
143+ {
133144 break ;
145+ }
134146
135147 _edgeTo [ i ] = null ;
136148 _distances [ i ] = Infinity ;
@@ -151,7 +163,7 @@ private void _initializeDataMembers(TGraph Graph)
151163 private bool _checkOptimalityConditions ( TGraph graph , TVertex source )
152164 {
153165 // Get the source index (to be used with the information arrays).
154- int s = _nodesToIndices [ source ] ;
166+ var s = _nodesToIndices [ source ] ;
155167
156168 // check that edge weights are nonnegative
157169 foreach ( var edge in graph . Edges )
@@ -170,10 +182,13 @@ private bool _checkOptimalityConditions(TGraph graph, TVertex source)
170182 return false ;
171183 }
172184
173- for ( int v = 0 ; v < graph . VerticesCount ; v ++ )
185+ for ( var v = 0 ; v < graph . VerticesCount ; v ++ )
174186 {
175187 if ( v == s )
188+ {
176189 continue ;
190+ }
191+
177192 if ( _predecessors [ v ] == NilPredecessor && _distances [ v ] != Infinity )
178193 {
179194 Console . WriteLine ( "distanceTo[] and edgeTo[] are inconsistent for at least one vertex." ) ;
@@ -184,11 +199,11 @@ private bool _checkOptimalityConditions(TGraph graph, TVertex source)
184199 // check that all edges e = v->w satisfy distTo[w] <= distTo[v] + e.weight()
185200 foreach ( var vertex in graph . Vertices )
186201 {
187- int v = _nodesToIndices [ vertex ] ;
202+ var v = _nodesToIndices [ vertex ] ;
188203
189204 foreach ( var edge in graph . NeighboursMap ( vertex ) )
190205 {
191- int w = _nodesToIndices [ edge . Key ] ;
206+ var w = _nodesToIndices [ edge . Key ] ;
192207
193208 if ( _distances [ v ] + edge . Value < _distances [ w ] )
194209 {
@@ -201,18 +216,22 @@ private bool _checkOptimalityConditions(TGraph graph, TVertex source)
201216 // check that all edges e = v->w on SPT satisfy distTo[w] == distTo[v] + e.weight()
202217 foreach ( var vertex in graph . Vertices )
203218 {
204- int w = _nodesToIndices [ vertex ] ;
219+ var w = _nodesToIndices [ vertex ] ;
205220
206221 if ( _edgeTo [ w ] == null )
222+ {
207223 continue ;
224+ }
208225
209226 var edge = _edgeTo [ w ] ;
210- int v = _nodesToIndices [ edge . Source ] ;
227+ var v = _nodesToIndices [ edge . Source ] ;
211228
212229 if ( ! vertex . IsEqualTo ( edge . Destination ) )
230+ {
213231 return false ;
232+ }
214233
215- if ( ( _distances [ v ] + edge . Weight ) != _distances [ w ] )
234+ if ( _distances [ v ] + edge . Weight != _distances [ w ] )
216235 {
217236 Console . WriteLine ( "edge " + edge . Source + "-" + edge . Destination + " on shortest path not tight" ) ;
218237 return false ;
@@ -232,9 +251,11 @@ private bool _checkOptimalityConditions(TGraph graph, TVertex source)
232251 public bool HasPathTo ( TVertex destination )
233252 {
234253 if ( ! _nodesToIndices . ContainsKey ( destination ) )
254+ {
235255 throw new Exception ( "Graph doesn't have the specified vertex." ) ;
256+ }
236257
237- int index = _nodesToIndices [ destination ] ;
258+ var index = _nodesToIndices [ destination ] ;
238259 return _distances [ index ] != Infinity ;
239260 }
240261
@@ -244,9 +265,11 @@ public bool HasPathTo(TVertex destination)
244265 public long DistanceTo ( TVertex destination )
245266 {
246267 if ( ! _nodesToIndices . ContainsKey ( destination ) )
268+ {
247269 throw new Exception ( "Graph doesn't have the specified vertex." ) ;
270+ }
248271
249- int index = _nodesToIndices [ destination ] ;
272+ var index = _nodesToIndices [ destination ] ;
250273 return _distances [ index ] ;
251274 }
252275
@@ -256,16 +279,22 @@ public long DistanceTo(TVertex destination)
256279 public IEnumerable < TVertex > ShortestPathTo ( TVertex destination )
257280 {
258281 if ( ! _nodesToIndices . ContainsKey ( destination ) )
282+ {
259283 throw new Exception ( "Graph doesn't have the specified vertex." ) ;
284+ }
260285 else if ( ! HasPathTo ( destination ) )
286+ {
261287 return null ;
288+ }
262289
263- int dstIndex = _nodesToIndices [ destination ] ;
290+ var dstIndex = _nodesToIndices [ destination ] ;
264291 var stack = new DataStructures . Lists . Stack < TVertex > ( ) ;
265292
266293 int index ;
267294 for ( index = dstIndex ; _distances [ index ] != 0 ; index = _predecessors [ index ] )
295+ {
268296 stack . Push ( _indicesToNodes [ index ] ) ;
297+ }
269298
270299 // Push the source vertex
271300 stack . Push ( _indicesToNodes [ index ] ) ;
0 commit comments