Skip to content

Commit 0815785

Browse files
authored
Merge pull request #16 from matiii/feature/immutable-graph
Feature/immutable graph
2 parents 39f4470 + 88d1ee6 commit 0815785

26 files changed

+466
-57
lines changed

src/Dijkstra.NET.Benchmark/BenchmarkIt.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
using BenchmarkDotNet.Attributes;
44
using BenchmarkDotNet.Attributes.Jobs;
55
using BenchmarkDotNet.Engines;
6+
using Dijkstra.NET.Extensions;
67

78
namespace Dijkstra.NET.Benchmark
89
{
910
[SimpleJob(RunStrategy.Monitoring, launchCount: 1, warmupCount: 2, targetCount: 3)]
11+
[MemoryDiagnoser]
1012
public class BenchmarkIt
1113
{
1214
private readonly DijkstraBenchmarkBase _dijkstra;
@@ -42,6 +44,14 @@ public int DijkstraBenchmark()
4244
return result.GetPath().Count();
4345
}
4446

47+
[Benchmark]
48+
public int DijkstraExtensionBenchmark()
49+
{
50+
var result = DijkstraBenchmarkBase.Graph.Dijkstra(DijkstraBenchmarkBase.From, DijkstraBenchmarkBase.To);
51+
52+
return result.GetPath().Count();
53+
}
54+
4555
[Benchmark]
4656
public int BfsBenchmark()
4757
{

src/Dijkstra.NET.Benchmark/Dijkstra.NET.Benchmark.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<DefineConstants>DEBUG;TRACE</DefineConstants>
2222
<ErrorReport>prompt</ErrorReport>
2323
<WarningLevel>4</WarningLevel>
24+
<LangVersion>7.3</LangVersion>
2425
</PropertyGroup>
2526
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2627
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -30,6 +31,7 @@
3031
<DefineConstants>TRACE</DefineConstants>
3132
<ErrorReport>prompt</ErrorReport>
3233
<WarningLevel>4</WarningLevel>
34+
<LangVersion>7.2</LangVersion>
3335
</PropertyGroup>
3436
<ItemGroup>
3537
<Reference Include="BenchmarkDotNet, Version=0.10.10.0, Culture=neutral, PublicKeyToken=aa0ca2f9092cefc4, processorArchitecture=MSIL">

src/Dijkstra.NET.Benchmark/DijkstraBenchmarkBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ namespace Dijkstra.NET.Benchmark
66
{
77
public abstract class DijkstraBenchmarkBase
88
{
9-
protected static readonly Graph<int, string> Graph = new Graph<int, string>();
9+
public static readonly Graph<int, string> Graph = new Graph<int, string>();
1010

1111
private const int Nodes = 10_000_000;
1212
private const int Connections = 1_000_000;
1313

14-
public uint From { get; private set; }
15-
public uint To { get; private set; }
14+
public static uint From { get; private set; }
15+
public static uint To { get; private set; }
1616

1717
public void Initialise()
1818
{

src/Dijkstra.NET.Tests/Dijkstra.NET.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

9+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
10+
<LangVersion>7.3</LangVersion>
11+
</PropertyGroup>
12+
913
<ItemGroup>
1014
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
1115
<PackageReference Include="xunit" Version="2.2.0" />
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
using System.Linq;
2+
using Dijkstra.NET.Extensions;
3+
using Dijkstra.NET.Model;
4+
using Xunit;
5+
6+
namespace Dijkstra.NET.Tests.Extensions
7+
{
8+
public class ShortestPathExtensionsTests
9+
{
10+
[Fact]
11+
public void DijkstraGraphShould_Find_Path_In_Multi_Paths_Graph()
12+
{
13+
var graph = new Graph<int, string>();
14+
15+
graph.AddNode(0);
16+
graph.AddNode(0);
17+
graph.AddNode(0);
18+
graph.AddNode(0);
19+
graph.AddNode(0);
20+
graph.AddNode(0);
21+
22+
graph.Connect(0, 1, 2, null);
23+
graph.Connect(0, 2, 3, null);
24+
graph.Connect(1, 3, 4, null);
25+
graph.Connect(2, 3, 2, null);
26+
graph.Connect(2, 4, 1, null);
27+
graph.Connect(3, 5, 6, null);
28+
29+
var result = graph.Dijkstra(0, 5);
30+
uint[] path = result.GetPath().ToArray();
31+
32+
Assert.Equal<uint>(0, path[0]);
33+
Assert.Equal<uint>(2, path[1]);
34+
Assert.Equal<uint>(3, path[2]);
35+
Assert.Equal<uint>(5, path[3]);
36+
37+
Assert.Equal(11, result.Distance);
38+
Assert.True(result.IsFounded);
39+
}
40+
41+
[Fact]
42+
public void DijkstraGraphShould_Find_Path_In_Override_Node()
43+
{
44+
var graph = new Graph<int, string>();
45+
46+
graph.AddNode(0);
47+
graph.AddNode(0);
48+
graph.AddNode(0);
49+
graph.AddNode(0);
50+
graph.AddNode(0);
51+
graph.AddNode(0);
52+
53+
graph.Connect(0, 1, 2, null);
54+
graph.Connect(0, 2, 6, null);
55+
graph.Connect(1, 2, 2, null);
56+
graph.Connect(2, 3, 1, null);
57+
graph.Connect(3, 4, 1, null);
58+
graph.Connect(0, 5, 5, null);
59+
60+
var result = graph.Dijkstra(0, 4);
61+
uint[] path = result.GetPath().ToArray();
62+
63+
Assert.Equal<uint>(0, path[0]);
64+
Assert.Equal<uint>(1, path[1]);
65+
Assert.Equal<uint>(2, path[2]);
66+
Assert.Equal<uint>(3, path[3]);
67+
Assert.Equal<uint>(4, path[4]);
68+
69+
Assert.Equal(6, result.Distance);
70+
Assert.True(result.IsFounded);
71+
}
72+
73+
[Fact]
74+
public void DijkstraGraphShould_Find_Path_With_One_Vertex_In_Graph()
75+
{
76+
var graph = new Graph<int, string>();
77+
graph.AddNode(0);
78+
79+
var result = graph.Dijkstra(0, 0);
80+
uint[] path = result.GetPath().ToArray();
81+
82+
Assert.Equal<uint>(0, path[0]);
83+
Assert.Equal(0, result.Distance);
84+
Assert.True(result.IsFounded);
85+
}
86+
87+
[Fact]
88+
public void DijkstraGraphShould_Find_Path_With_One_Vertex_And_One_Edge_In_Graph()
89+
{
90+
var graph = new Graph<int, string>();
91+
graph.AddNode(0);
92+
93+
graph.Connect(0, 0, 5, null);
94+
95+
var result = graph.Dijkstra(0, 0);
96+
uint[] path = result.GetPath().ToArray();
97+
98+
Assert.Equal<uint>(0, path[0]);
99+
Assert.Equal(0, result.Distance);
100+
Assert.True(result.IsFounded);
101+
}
102+
103+
[Fact]
104+
public void DijkstraGraphShould_Find_Path_In_Multi_Edges_Graph()
105+
{
106+
var graph = new Graph<int, string>();
107+
108+
graph.AddNode(0);
109+
graph.AddNode(0);
110+
graph.AddNode(0);
111+
graph.AddNode(0);
112+
graph.AddNode(0);
113+
graph.AddNode(0);
114+
115+
graph.Connect(0, 1, 2, null);
116+
graph.Connect(0, 2, 3, null);
117+
graph.Connect(1, 3, 4, null);
118+
graph.Connect(2, 3, 3, null);
119+
graph.Connect(2, 3, 2, null);
120+
graph.Connect(2, 3, 4, null);
121+
graph.Connect(2, 4, 1, null);
122+
graph.Connect(3, 5, 6, null);
123+
124+
var result = graph.Dijkstra(0, 5);
125+
uint[] path = result.GetPath().ToArray();
126+
127+
Assert.Equal<uint>(0, path[0]);
128+
Assert.Equal<uint>(2, path[1]);
129+
Assert.Equal<uint>(3, path[2]);
130+
Assert.Equal<uint>(5, path[3]);
131+
132+
Assert.Equal(11, result.Distance);
133+
Assert.True(result.IsFounded);
134+
}
135+
136+
[Fact]
137+
public void DijkstraGraphNot_Should_Find_Path_In_Graph()
138+
{
139+
var graph = new Graph<int, string>();
140+
141+
graph.AddNode(0);
142+
graph.AddNode(0);
143+
graph.AddNode(0);
144+
graph.AddNode(0);
145+
graph.AddNode(0);
146+
graph.AddNode(0);
147+
148+
graph.Connect(0, 1, 2, null);
149+
graph.Connect(0, 2, 3, null);
150+
graph.Connect(1, 3, 4, null);
151+
graph.Connect(2, 3, 2, null);
152+
graph.Connect(2, 4, 1, null);
153+
154+
var result = graph.Dijkstra(0, 5);
155+
156+
Assert.False(result.IsFounded);
157+
}
158+
}
159+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace Dijkstra.NET.Constants
4+
{
5+
internal class NodeConstants
6+
{
7+
public const int MaxSize = Int32.MaxValue / 4;
8+
}
9+
}

src/Dijkstra.NET/Contract/ICloneable.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
namespace Dijkstra.NET.Contract
1+
using System;
2+
3+
namespace Dijkstra.NET.Contract
24
{
5+
[Obsolete]
36
public interface ICloneable<out T>
47
{
58
T Clone();

src/Dijkstra.NET/Contract/INode.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
namespace Dijkstra.NET.Contract
2-
{
3-
using System;
4-
using System.Collections.Generic;
5-
using Model;
1+
using System;
2+
using Dijkstra.NET.Delegates;
63

4+
namespace Dijkstra.NET.Contract
5+
{
76
public interface INode<T,TEdgeCustom> where TEdgeCustom : IEquatable<TEdgeCustom>
87
{
9-
IList<Edge<T, TEdgeCustom>> Children { get; }
108
T Item { get; }
9+
1110
uint Key { get; }
11+
12+
void EachChild(ChildAction<T, TEdgeCustom> action);
13+
14+
[Obsolete]
1215
int Distance { get; set; }
1316
}
1417
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
namespace Dijkstra.NET.Contract
2-
{
3-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
43

4+
namespace Dijkstra.NET.Contract
5+
{
6+
[Obsolete]
57
public interface IShortestPathResult
68
{
79
IEnumerable<uint> GetReversePath();
10+
811
IEnumerable<uint> GetPath();
12+
913
bool IsFounded { get; }
14+
1015
int Distance { get; }
16+
1117
uint FromNode { get; }
18+
1219
uint ToNode { get; }
1320
}
1421
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
using Dijkstra.NET.Model;
3+
4+
namespace Dijkstra.NET.Delegates
5+
{
6+
public delegate void ChildAction<T, TEdgeCustom>(in Edge<T, TEdgeCustom> edge) where TEdgeCustom : IEquatable<TEdgeCustom>;
7+
}

0 commit comments

Comments
 (0)