@@ -190,10 +190,136 @@ public void DijkstraTest4_Success()
190190 shortestPathList [ 2 ] . ToString ( ) . Should ( )
191191 . Be ( $ "Vertex: { c } - Distance: { 3 } - Previous: { a } ") ;
192192
193- // Vertex D won't be visited in this dijkstra implementation which is valid only for cyclic graphs,
194- // since it is necessary to backtrack all unvisited vertices and place them
195- // to the priority queue, which is not implemented yet in this repository.
196- // If algo goes to the next vertex with minimal distance and this vertex is leaf -- algorithm stops.
193+ shortestPathList [ 3 ] . Vertex . Should ( ) . Be ( d ) ;
194+ shortestPathList [ 3 ] . Distance . Should ( ) . Be ( 8 ) ;
195+ shortestPathList [ 3 ] . PreviousVertex . Should ( ) . Be ( c ) ;
196+ shortestPathList [ 3 ] . ToString ( ) . Should ( )
197+ . Be ( $ "Vertex: { d } - Distance: { 8 } - Previous: { c } ") ;
198+ }
199+
200+ [ Test ]
201+ public void DijkstraTest5_Success ( )
202+ {
203+ // here test case is from https://www.youtube.com/watch?v=pVfj6mxhdMw
204+
205+ var graph = new DirectedWeightedGraph < char > ( 7 ) ;
206+ var a = graph . AddVertex ( 'A' ) ;
207+ var b = graph . AddVertex ( 'B' ) ;
208+ var c = graph . AddVertex ( 'C' ) ;
209+ var d = graph . AddVertex ( 'D' ) ;
210+ var e = graph . AddVertex ( 'E' ) ;
211+ var w = graph . AddVertex ( 'W' ) ;
212+ var z = graph . AddVertex ( 'Z' ) ;
213+
214+ graph . AddEdge ( a , b , 6 ) ;
215+ graph . AddEdge ( b , a , 6 ) ;
216+
217+ graph . AddEdge ( a , d , 1 ) ;
218+ graph . AddEdge ( d , a , 1 ) ;
219+
220+ graph . AddEdge ( d , e , 1 ) ;
221+ graph . AddEdge ( e , d , 1 ) ;
222+
223+ graph . AddEdge ( d , b , 2 ) ;
224+ graph . AddEdge ( b , d , 2 ) ;
225+
226+ graph . AddEdge ( e , b , 2 ) ;
227+ graph . AddEdge ( b , e , 2 ) ;
228+
229+ graph . AddEdge ( e , c , 5 ) ;
230+ graph . AddEdge ( c , e , 5 ) ;
231+
232+ graph . AddEdge ( c , b , 5 ) ;
233+ graph . AddEdge ( b , c , 5 ) ;
234+
235+ graph . AddEdge ( a , w , 50 ) ;
236+ graph . AddEdge ( w , a , 50 ) ;
237+
238+ graph . AddEdge ( w , z , 1 ) ;
239+ graph . AddEdge ( z , w , 1 ) ;
240+
241+ var shortestPathList = DijkstraAlgorithm . GenerateShortestPath ( graph , a ) ;
242+ shortestPathList . Length . Should ( ) . Be ( 7 ) ;
243+
244+ shortestPathList [ 0 ] . Vertex . Should ( ) . Be ( a ) ;
245+ shortestPathList [ 0 ] . Distance . Should ( ) . Be ( 0 ) ;
246+ shortestPathList [ 0 ] . PreviousVertex . Should ( ) . Be ( a ) ;
247+ shortestPathList [ 0 ] . ToString ( ) . Should ( )
248+ . Be ( $ "Vertex: { a } - Distance: { 0 } - Previous: { a } ") ;
249+
250+ shortestPathList [ 1 ] . Vertex . Should ( ) . Be ( b ) ;
251+ shortestPathList [ 1 ] . Distance . Should ( ) . Be ( 3 ) ;
252+ shortestPathList [ 1 ] . PreviousVertex . Should ( ) . Be ( d ) ;
253+ shortestPathList [ 1 ] . ToString ( ) . Should ( )
254+ . Be ( $ "Vertex: { b } - Distance: { 3 } - Previous: { d } ") ;
255+
256+ shortestPathList [ 2 ] . Vertex . Should ( ) . Be ( c ) ;
257+ shortestPathList [ 2 ] . Distance . Should ( ) . Be ( 7 ) ;
258+ shortestPathList [ 2 ] . PreviousVertex . Should ( ) . Be ( e ) ;
259+ shortestPathList [ 2 ] . ToString ( ) . Should ( )
260+ . Be ( $ "Vertex: { c } - Distance: { 7 } - Previous: { e } ") ;
261+
262+ shortestPathList [ 3 ] . Vertex . Should ( ) . Be ( d ) ;
263+ shortestPathList [ 3 ] . Distance . Should ( ) . Be ( 1 ) ;
264+ shortestPathList [ 3 ] . PreviousVertex . Should ( ) . Be ( a ) ;
265+ shortestPathList [ 3 ] . ToString ( ) . Should ( )
266+ . Be ( $ "Vertex: { d } - Distance: { 1 } - Previous: { a } ") ;
267+
268+ shortestPathList [ 4 ] . Vertex . Should ( ) . Be ( e ) ;
269+ shortestPathList [ 4 ] . Distance . Should ( ) . Be ( 2 ) ;
270+ shortestPathList [ 4 ] . PreviousVertex . Should ( ) . Be ( d ) ;
271+ shortestPathList [ 4 ] . ToString ( ) . Should ( )
272+ . Be ( $ "Vertex: { e } - Distance: { 2 } - Previous: { d } ") ;
273+
274+ shortestPathList [ 5 ] . Vertex . Should ( ) . Be ( w ) ;
275+ shortestPathList [ 5 ] . Distance . Should ( ) . Be ( 50 ) ;
276+ shortestPathList [ 5 ] . PreviousVertex . Should ( ) . Be ( a ) ;
277+ shortestPathList [ 5 ] . ToString ( ) . Should ( )
278+ . Be ( $ "Vertex: { w } - Distance: { 50 } - Previous: { a } ") ;
279+
280+ shortestPathList [ 6 ] . Vertex . Should ( ) . Be ( z ) ;
281+ shortestPathList [ 6 ] . Distance . Should ( ) . Be ( 51 ) ;
282+ shortestPathList [ 6 ] . PreviousVertex . Should ( ) . Be ( w ) ;
283+ shortestPathList [ 6 ] . ToString ( ) . Should ( )
284+ . Be ( $ "Vertex: { z } - Distance: { 51 } - Previous: { w } ") ;
285+ }
286+
287+ [ Test ]
288+ public void DijkstraTest6_Success ( )
289+ {
290+ var graph = new DirectedWeightedGraph < char > ( 5 ) ;
291+ var a = graph . AddVertex ( 'A' ) ;
292+ var b = graph . AddVertex ( 'B' ) ;
293+ var c = graph . AddVertex ( 'C' ) ;
294+ var d = graph . AddVertex ( 'D' ) ;
295+
296+ graph . AddEdge ( a , b , 1 ) ;
297+ graph . AddEdge ( b , a , 1 ) ;
298+
299+ graph . AddEdge ( c , d , 5 ) ;
300+ graph . AddEdge ( d , c , 5 ) ;
301+
302+ var shortestPathList = DijkstraAlgorithm . GenerateShortestPath ( graph , a ) ;
303+
304+ shortestPathList . Length . Should ( ) . Be ( 4 ) ;
305+ shortestPathList [ 0 ] . Vertex . Should ( ) . Be ( a ) ;
306+ shortestPathList [ 0 ] . Distance . Should ( ) . Be ( 0 ) ;
307+ shortestPathList [ 0 ] . PreviousVertex . Should ( ) . Be ( a ) ;
308+ shortestPathList [ 0 ] . ToString ( ) . Should ( )
309+ . Be ( $ "Vertex: { a } - Distance: { 0 } - Previous: { a } ") ;
310+
311+ shortestPathList [ 1 ] . Vertex . Should ( ) . Be ( b ) ;
312+ shortestPathList [ 1 ] . Distance . Should ( ) . Be ( 1 ) ;
313+ shortestPathList [ 1 ] . PreviousVertex . Should ( ) . Be ( a ) ;
314+ shortestPathList [ 1 ] . ToString ( ) . Should ( )
315+ . Be ( $ "Vertex: { b } - Distance: { 1 } - Previous: { a } ") ;
316+
317+ shortestPathList [ 2 ] . Vertex . Should ( ) . Be ( c ) ;
318+ shortestPathList [ 2 ] . Distance . Should ( ) . Be ( double . MaxValue ) ;
319+ shortestPathList [ 2 ] . PreviousVertex . Should ( ) . BeNull ( ) ;
320+ shortestPathList [ 2 ] . ToString ( ) . Should ( )
321+ . Be ( $ "Vertex: { c } - Distance: { double . MaxValue } - Previous: { null } ") ;
322+
197323 shortestPathList [ 3 ] . Vertex . Should ( ) . Be ( d ) ;
198324 shortestPathList [ 3 ] . Distance . Should ( ) . Be ( double . MaxValue ) ;
199325 shortestPathList [ 3 ] . PreviousVertex . Should ( ) . BeNull ( ) ;
0 commit comments