Skip to content

Commit 7716f78

Browse files
authored
Merge pull request #2 from matiii/priorityqueue
algorithm improved
2 parents 260647b + df11bca commit 7716f78

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

src/Dijkstra.NET/Dijkstra.NET.Benchmark/DijkstraBenchmarkBase.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@ public void Setup()
2929
_graph.Connect((uint) node1, (uint) node2, (uint) cost, null);
3030
}
3131

32-
@from = (uint) random.Next(0, Nodes-1);
33-
to = (uint) random.Next(0, Nodes-1);
32+
_graph.Connect(0, 5, 10, null);
33+
_graph.Connect(5, 121, 2, null);
34+
_graph.Connect(121, 115, 1, null);
35+
_graph.Connect(115, 300, 4, null);
36+
_graph.Connect(300, 855, 2, null);
37+
_graph.Connect(855, 1600, 1, null);
38+
_graph.Connect(1600, 5000, 4, null);
39+
_graph.Connect(5000, 50, 1, null);
40+
_graph.Connect(50, 21, 1, null);
41+
_graph.Connect(21, Nodes-1, 1, null);
42+
43+
@from = 0;
44+
to = Nodes-1;
3445
}
3546
}
3647
}

src/Dijkstra.NET/Dijkstra.NET.Benchmark/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Program
99
{
10-
static void Main(string[] args)
10+
static void Main()
1111
{
1212
var normal = new DijkstraBenchmark();
1313
normal.Setup();
@@ -16,7 +16,10 @@ static void Main(string[] args)
1616
DijkstraResult result = normal.GetPath();
1717
stopWatch.Stop();
1818

19-
Console.WriteLine($"Dijkstra takes {stopWatch.Elapsed.TotalSeconds:F} sec. Length of path is {result.GetPath().Count()}.");
19+
uint[] path = result.GetPath().ToArray();
20+
21+
Console.WriteLine($"Dijkstra takes {stopWatch.ElapsedMilliseconds} ms. Length of path is {path.Length}.");
22+
Console.WriteLine($"Path: {path.Select(x => x.ToString()).Aggregate((a, b) => a + " -> " + b)}");
2023

2124
Console.ReadKey();
2225
}

src/Dijkstra.NET/Dijkstra.NET/Contract/IGraph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
using System.Collections.Generic;
44

5-
public interface IGraph<T, TEdgeCustom> : IEnumerable<INode<T, TEdgeCustom>> where TEdgeCustom: class
5+
public interface IGraph<T, TEdgeCustom> where TEdgeCustom: class
66
{
77
INode<T, TEdgeCustom> this[uint node] { get; }
88
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public DijkstraResult Process(uint from, uint to)
1919
{
2020
var result = new DijkstraResult(from, to);
2121
_graph[from].Distance = 0;
22-
var q = new SortedSet<INode<T, TEdgeCustom>>(_graph, new NodeComparer<T, TEdgeCustom>());
22+
var q = new SortedSet<INode<T, TEdgeCustom>>(new[] {_graph[from]}, new NodeComparer<T, TEdgeCustom>());
2323

2424
while (q.Count > 0)
2525
{
@@ -37,7 +37,6 @@ public DijkstraResult Process(uint from, uint to)
3737

3838
if (e.Node.Distance > u.Distance + e.Cost)
3939
{
40-
q.Remove(e.Node);
4140
e.Node.Distance = u.Distance + e.Cost;
4241
q.Add(e.Node);
4342
result.Path[e.Node.Key] = u.Key;

src/Dijkstra.NET/Dijkstra.NET/Model/Graph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using Contract;
8-
public class Graph<T, TEdgeCustom>: IGraph<T, TEdgeCustom>, ICloneable where TEdgeCustom: class
8+
public class Graph<T, TEdgeCustom>: IGraph<T, TEdgeCustom>, IEnumerable<INode<T, TEdgeCustom>>, ICloneable where TEdgeCustom: class
99
{
1010
private readonly IDictionary<uint, INode<T, TEdgeCustom>> _nodes = new Dictionary<uint, INode<T, TEdgeCustom>>();
1111

0 commit comments

Comments
 (0)