Skip to content

Commit e2207f0

Browse files
committed
include one vertex corner case
1 parent f6a440f commit e2207f0

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/Dijkstra.NET.Tests/Extensions/ShortestPathExtensionsTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ public void DijkstraGraphShould_Find_Path_With_One_Vertex_In_Graph()
8484
Assert.True(result.IsFounded);
8585
}
8686

87+
[Fact]
88+
public void DijkstraGraphShould_Find_Path_With_One_Vertex_In_Graph_And_Depth_Zero()
89+
{
90+
var graph = new Graph<int, string>();
91+
graph.AddNode(0);
92+
93+
var result = graph.Dijkstra(0, 0, 0);
94+
uint[] path = result.GetPath().ToArray();
95+
96+
Assert.Equal<uint>(0, path[0]);
97+
Assert.Equal(0, result.Distance);
98+
Assert.True(result.IsFounded);
99+
}
100+
87101
[Fact]
88102
public void DijkstraGraphShould_Find_Path_With_One_Vertex_And_One_Edge_In_Graph()
89103
{

src/Dijkstra.NET/Extensions/ShortestPathExtensions.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,22 @@ public static ShortestPathResult Dijkstra<T, TEdgeCustom>(this IGraph<T, TEdgeCu
2626
/// <param name="to">End node</param>
2727
/// <param name="depth">Depth of path</param>
2828
/// <returns>Value with path</returns>
29-
public static ShortestPathResult Dijkstra<T, TEdgeCustom>(this IGraph<T, TEdgeCustom> graph, uint from, uint to, int depth)
29+
public static ShortestPathResult Dijkstra<T, TEdgeCustom>(this IGraph<T, TEdgeCustom> graph, uint from, uint to,
30+
int depth)
3031
where TEdgeCustom : IEquatable<TEdgeCustom>
3132
{
3233
var path = new Dictionary<uint, uint>();
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));
34+
var distance = new Dictionary<uint, int> {[from] = 0};
35+
var d = new Dictionary<uint, int> {[from] = 0};
36+
var q = new SortedSet<uint>(new[] {from}, new NodeComparer(distance));
3637
var current = new HashSet<uint>();
3738

3839
int Distance(uint key)
3940
{
4041
return distance.ContainsKey(key) ? distance[key] : Int32.MaxValue;
4142
}
4243

43-
while (q.Count > 0)
44+
do
4445
{
4546
uint u = q.Deque();
4647

@@ -72,7 +73,8 @@ int Distance(uint key)
7273
d[e.Node.Key] = d[u] + 1;
7374
}
7475
});
75-
}
76+
77+
} while (q.Count > 0 && depth > 0);
7678

7779
return new ShortestPathResult(from, to);
7880
}

0 commit comments

Comments
 (0)