Skip to content

Commit a95925c

Browse files
authored
Merge pull request #7 from matiii/build
bug fix
2 parents 8b06cbf + 029293f commit a95925c

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

src/Dijkstra.NET/Dijkstra.NET.Tests/DijkstraTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public void Dijkstra_Should_Find_Path_In_Multi_Paths_Graph()
2424
_fixture.Algorithm_Should_Find_Path_In_Multi_Paths_Graph(_dijkstraFactory);
2525
}
2626

27+
[TestMethod, Timeout(2500)]
28+
public void Dijkstra_Should_Find_Path_In_Override_Node()
29+
{
30+
_fixture.Algorithm_Should_Find_Path_In_Override_Node(_dijkstraFactory);
31+
}
32+
2733
[TestMethod]
2834
public void Dijkstra_Should_Find_Path_With_One_Vertex_In_Graph()
2935
{

src/Dijkstra.NET/Dijkstra.NET.Tests/TheShortestPathFixture.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,40 @@ public void Algorithm_Should_Find_Path_In_Multi_Paths_Graph(Func<Graph<int, stri
4141
Assert.IsTrue(result.IsFounded);
4242
}
4343

44+
public void Algorithm_Should_Find_Path_In_Override_Node(Func<Graph<int, string>, Dijkstra<int, string>> algorithm)
45+
{
46+
var graph = new Graph<int, string>();
47+
48+
graph.AddNode(0);
49+
graph.AddNode(0);
50+
graph.AddNode(0);
51+
graph.AddNode(0);
52+
graph.AddNode(0);
53+
graph.AddNode(0);
54+
55+
graph.Connect(0, 1, 2, null);
56+
graph.Connect(0, 2, 6, null);
57+
graph.Connect(1, 2, 2, null);
58+
graph.Connect(2, 3, 1, null);
59+
graph.Connect(3, 4, 1, null);
60+
graph.Connect(0, 5, 5, null);
61+
62+
var dijkstra = algorithm(graph);
63+
var result = dijkstra.Process(0, 4);
64+
uint[] path = result.GetPath().ToArray();
65+
66+
Dispose(dijkstra);
67+
68+
Assert.AreEqual<uint>(0, path[0]);
69+
Assert.AreEqual<uint>(1, path[1]);
70+
Assert.AreEqual<uint>(2, path[2]);
71+
Assert.AreEqual<uint>(3, path[3]);
72+
Assert.AreEqual<uint>(4, path[4]);
73+
74+
Assert.AreEqual(6, result.Distance);
75+
Assert.IsTrue(result.IsFounded);
76+
}
77+
4478
public void Algorithm_Should_Find_Path_With_One_Vertex_In_Graph(Func<Graph<int, string>, Dijkstra<int, string>> algorithm)
4579
{
4680
var graph = new Graph<int, string>();

src/Dijkstra.NET/Dijkstra.NET/Model/Node.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using Contract;
6+
67
public class Node<T, TEdgeCustom>: INode<T, TEdgeCustom> where TEdgeCustom: IEquatable<TEdgeCustom>
78
{
89
public Node(uint key, T item)
@@ -16,5 +17,16 @@ public Node(uint key, T item)
1617
public uint Key { get; }
1718
public T Item { get; }
1819
public int Distance { get; set; }
20+
21+
//public override int GetHashCode() => Key.GetHashCode();
22+
//public override bool Equals(object obj)
23+
//{
24+
// var that = obj as Node<T, TEdgeCustom>;
25+
26+
// if (that == null || that.Key != Key)
27+
// return false;
28+
29+
// return true;
30+
//}
1931
}
2032
}

src/Dijkstra.NET/Dijkstra.NET/ShortestPath/Dijkstra.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ public virtual IShortestPathResult Process(uint from, uint to)
2727
var result = new DijkstraResult(from, to);
2828
Graph[from].Distance = 0;
2929
var q = new SortedSet<INode<T, TEdgeCustom>>(new[] { Graph[from]}, new NodeComparer<T, TEdgeCustom>());
30+
var current = new HashSet<uint>();
3031

3132
while (q.Count > 0)
3233
{
3334
INode<T, TEdgeCustom> u = q.Deque();
35+
current.Remove(u.Key);
3436

3537
if (u.Key == to)
3638
{
@@ -44,8 +46,12 @@ public virtual IShortestPathResult Process(uint from, uint to)
4446

4547
if (e.Node.Distance > u.Distance + e.Cost)
4648
{
49+
if (current.Contains(e.Node.Key))
50+
q.Remove(e.Node);
51+
4752
e.Node.Distance = u.Distance + e.Cost;
4853
q.Add(e.Node);
54+
current.Add(e.Node.Key);
4955
result.Path[e.Node.Key] = u.Key;
5056
}
5157
}

0 commit comments

Comments
 (0)