@@ -16,11 +16,23 @@ public static class ShortestPathExtensions
1616 /// <param name="to">End node</param>
1717 /// <returns>Value with path</returns>
1818 public static ShortestPathResult Dijkstra < T , TEdgeCustom > ( this IGraph < T , TEdgeCustom > graph , uint from , uint to )
19+ where TEdgeCustom : IEquatable < TEdgeCustom > => Dijkstra ( graph , from , to , Int32 . MaxValue ) ;
20+
21+ /// <summary>
22+ /// Get path from @from to @to
23+ /// </summary>
24+ /// <param name="graph">Source graph</param>
25+ /// <param name="from">Start node</param>
26+ /// <param name="to">End node</param>
27+ /// <param name="depth">Depth of path</param>
28+ /// <returns>Value with path</returns>
29+ public static ShortestPathResult Dijkstra < T , TEdgeCustom > ( this IGraph < T , TEdgeCustom > graph , uint from , uint to , int depth )
1930 where TEdgeCustom : IEquatable < TEdgeCustom >
2031 {
2132 var path = new Dictionary < uint , uint > ( ) ;
22- var distance = new Dictionary < uint , int > { [ from ] = 0 } ;
23- var q = new SortedSet < uint > ( new [ ] { from } , new NodeComparer ( distance ) ) ;
33+ var distance = new Dictionary < uint , int > { [ from ] = 0 } ;
34+ var d = new Dictionary < uint , int > { [ from ] = 0 } ;
35+ var q = new SortedSet < uint > ( new [ ] { from } , new NodeComparer ( distance ) ) ;
2436 var current = new HashSet < uint > ( ) ;
2537
2638 int Distance ( uint key )
@@ -39,6 +51,11 @@ int Distance(uint key)
3951
4052 current . Remove ( u ) ;
4153
54+ if ( depth == d [ u ] )
55+ {
56+ continue ;
57+ }
58+
4259 graph [ u ] . EachChild ( ( in Edge < T , TEdgeCustom > e ) =>
4360 {
4461 if ( Distance ( e . Node . Key ) > Distance ( u ) + e . Cost )
@@ -52,6 +69,7 @@ int Distance(uint key)
5269 q. Add ( e . Node . Key ) ;
5370 current. Add ( e . Node . Key ) ;
5471 path[ e . Node . Key ] = u ;
72+ d[ e . Node . Key ] = d [ u ] + 1 ;
5573 }
5674 } ) ;
5775 }
0 commit comments