Skip to content

Commit 38560e4

Browse files
authored
Merge pull request #4 from matiii/mapreduce
Mapreduce implementation
2 parents a5fe3d2 + 7c1143c commit 38560e4

23 files changed

+639
-151
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Dijkstra.NET.Benchmark
2+
{
3+
using Contract;
4+
using Model;
5+
using ShortestPath;
6+
7+
public class BfsParallelBenchmark: DijkstraBenchmarkBase
8+
{
9+
public BfsParallelBenchmark()
10+
{
11+
}
12+
13+
public BfsParallelBenchmark(Graph<int, string> graph, uint from, uint to): base(graph, from, to)
14+
{
15+
}
16+
17+
public override IShortestPathResult GetPath()
18+
{
19+
var bfs = new BfsParallel<int, string>(_graph);
20+
return bfs.Process(_from, _to);
21+
}
22+
}
23+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<Reference Include="System.Xml" />
6565
</ItemGroup>
6666
<ItemGroup>
67+
<Compile Include="BfsParallelBenchmark.cs" />
6768
<Compile Include="DijkstraBenchmark.cs" />
6869
<Compile Include="DijkstraBenchmarkBase.cs" />
6970
<Compile Include="Program.cs" />

src/Dijkstra.NET/Dijkstra.NET.Benchmark/DijkstraBenchmark.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
namespace Dijkstra.NET.Benchmark
22
{
3+
using Contract;
34
using Model;
5+
using ShortestPath;
46

57
public class DijkstraBenchmark: DijkstraBenchmarkBase
68
{
7-
public DijkstraResult GetPath()
9+
10+
public DijkstraBenchmark()
11+
{
12+
}
13+
14+
public DijkstraBenchmark(Graph<int, string> graph, uint from, uint to) : base(graph, from, to)
15+
{
16+
}
17+
18+
public override IShortestPathResult GetPath()
819
{
920
var dijkstra = new Dijkstra<int, string>(_graph);
10-
return dijkstra.Process(@from, to);
21+
return dijkstra.Process(_from, _to);
1122
}
1223

1324
}
Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
11
namespace Dijkstra.NET.Benchmark
22
{
33
using System;
4+
using Contract;
45
using Model;
56

6-
public class DijkstraBenchmarkBase
7+
public abstract class DijkstraBenchmarkBase
78
{
8-
protected readonly Graph<int, string> _graph = new Graph<int, string>();
9+
protected readonly Graph<int, string> _graph;
910

10-
private const int Nodes = 1000000;
11+
private const int Nodes = 10000000;
1112
private const int Connections = 10000000;
1213

13-
protected uint from;
14-
protected uint to;
14+
private const int ConnectionLevel = 300000;
15+
16+
protected uint _from;
17+
protected uint _to;
18+
19+
private bool _graphFromCtor;
20+
21+
protected DijkstraBenchmarkBase()
22+
{
23+
_graph = new Graph<int, string>();
24+
}
25+
26+
protected DijkstraBenchmarkBase(Graph<int, string> graph, uint from, uint to)
27+
{
28+
_graph = graph;
29+
_from = from;
30+
_to = to;
31+
_graphFromCtor = true;
32+
}
33+
34+
public Graph<int, string> Graph => _graph;
35+
public uint From => _from;
36+
public uint To => _to;
37+
1538

1639
public void Setup()
1740
{
41+
if (_graphFromCtor)
42+
{
43+
_graph.Reset();
44+
return;
45+
}
46+
1847
for (int i = 0; i < Nodes; i++)
1948
_graph.AddNode(i);
2049

@@ -24,24 +53,50 @@ public void Setup()
2453
{
2554
int node1 = random.Next(1, Nodes);
2655
int node2 = random.Next(1, Nodes);
27-
int cost = random.Next(1, 50);
56+
int cost = random.Next(15, 50);
2857

29-
_graph.Connect((uint) node1, (uint) node2, (uint) cost, null);
58+
_graph.Connect((uint) node1, (uint) node2, cost, null);
3059
}
3160

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;
61+
for (uint i = 0; i < ConnectionLevel; i++)
62+
{
63+
int cost = random.Next(1, 5);
64+
_graph.Connect(0, i + 1, cost, null);
65+
}
66+
67+
for (uint i = 0; i < ConnectionLevel; i++)
68+
{
69+
int node1 = random.Next(1, ConnectionLevel);
70+
int node2 = random.Next(ConnectionLevel, ConnectionLevel*2);
71+
int cost = random.Next(1, 5);
72+
73+
_graph.Connect((uint) node1, (uint) node2, cost, null);
74+
}
75+
76+
for (uint i = 0; i < ConnectionLevel; i++)
77+
{
78+
int node1 = random.Next(ConnectionLevel, 2*ConnectionLevel);
79+
int node2 = random.Next(2*ConnectionLevel, 3*ConnectionLevel);
80+
int cost = random.Next(1, 5);
81+
82+
_graph.Connect((uint) node1, (uint) node2, cost, null);
83+
}
84+
85+
_graph.Connect(0, 5, 10000000, null);
86+
_graph.Connect(5, 121, 10000000, null);
87+
_graph.Connect(121, 115, 10000000, null);
88+
_graph.Connect(115, 300, 10000000, null);
89+
_graph.Connect(300, 855, 10000000, null);
90+
_graph.Connect(855, 1600, 10000000, null);
91+
_graph.Connect(1600, 5000, 10000000, null);
92+
_graph.Connect(5000, 50, 10000000, null);
93+
_graph.Connect(50, 21, 10000000, null);
94+
_graph.Connect(21, Nodes-1, 10000000, null);
95+
96+
_from = 0;
97+
_to = Nodes-1;
4598
}
99+
100+
public abstract IShortestPathResult GetPath();
46101
}
47102
}

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,37 @@
33
using System;
44
using System.Diagnostics;
55
using System.Linq;
6-
using Model;
6+
using Contract;
77

88
class Program
99
{
1010
static void Main()
1111
{
12-
var normal = new DijkstraBenchmark();
13-
normal.Setup();
12+
var bfs = new BfsParallelBenchmark();
13+
Measure(bfs);
1414

15-
var stopWatch = Stopwatch.StartNew();
16-
DijkstraResult result = normal.GetPath();
15+
GC.Collect();
16+
17+
Measure(new DijkstraBenchmark(bfs.Graph, bfs.From, bfs.To));
18+
Console.ReadKey();
19+
}
20+
21+
static void Measure(DijkstraBenchmarkBase benchmark)
22+
{
23+
string benchmarkName = benchmark.GetType().Name;
24+
25+
Console.WriteLine($"--- Start {benchmarkName} benchmark. --- \r\n");
26+
benchmark.Setup();
27+
28+
var stopWatch = Stopwatch.StartNew();
29+
IShortestPathResult result = benchmark.GetPath();
1730
stopWatch.Stop();
1831

1932
uint[] path = result.GetPath().ToArray();
2033

21-
Console.WriteLine($"Dijkstra takes {stopWatch.ElapsedMilliseconds} ms. Length of path is {path.Length}.");
34+
Console.WriteLine($"{benchmarkName} takes {stopWatch.ElapsedMilliseconds} ms. Length of path is {path.Length}.");
2235
Console.WriteLine($"Path: {path.Select(x => x.ToString()).Aggregate((a, b) => a + " -> " + b)}");
23-
24-
Console.ReadKey();
36+
Console.WriteLine($"--- End {benchmarkName} benchmark. --- \r\n");
2537
}
2638
}
2739
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
namespace Dijkstra.NET.Tests
2+
{
3+
using System;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Model;
6+
using ShortestPath;
7+
8+
[TestClass]
9+
public class BfsParallelTest
10+
{
11+
private TheShortestPathFixture _fixture;
12+
private readonly Func<Graph<int, string>, Dijkstra<int, string>> _dijkstraFactory = g => new BfsParallel<int, string>(g);
13+
14+
[TestInitialize]
15+
public void Initialise()
16+
{
17+
_fixture = new TheShortestPathFixture();
18+
}
19+
20+
21+
[TestMethod]
22+
public void BfsParallel_Should_Find_Path_In_Multi_Paths_Graph()
23+
{
24+
_fixture.Algorithm_Should_Find_Path_In_Multi_Paths_Graph(_dijkstraFactory);
25+
}
26+
27+
[TestMethod]
28+
public void Guard_In_BfsParallel_Should_Always_Works()
29+
{
30+
for (int i = 0; i < 500; i++)
31+
_fixture.Algorithm_Should_Find_Path_In_Multi_Paths_Graph(g => new BfsParallel<int, string>(g));
32+
}
33+
34+
[TestMethod]
35+
public void BfsParallel_Should_Find_Path_With_One_Vertex_In_Graph()
36+
{
37+
_fixture.Algorithm_Should_Find_Path_With_One_Vertex_In_Graph(_dijkstraFactory);
38+
}
39+
40+
[TestMethod]
41+
public void BfsParallel_Should_Find_Path_With_One_Vertex_And_One_Edge_In_Graph()
42+
{
43+
_fixture.Algorithm_Should_Find_Path_With_One_Vertex_And_One_Edge_In_Graph(_dijkstraFactory);
44+
}
45+
46+
[TestMethod]
47+
public void BfsParallel_Should_Find_Path_In_Multi_Edges_Graph()
48+
{
49+
_fixture.Algorithm_Should_Find_Path_In_Multi_Edges_Graph(_dijkstraFactory);
50+
}
51+
52+
[TestMethod]
53+
public void BfsParallel_Not_Should_Find_Path_In_Graph()
54+
{
55+
_fixture.Algorithm_Not_Should_Find_Path_In_Graph(_dijkstraFactory);
56+
}
57+
}
58+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@
7070
</Otherwise>
7171
</Choose>
7272
<ItemGroup>
73+
<Compile Include="BfsParallelTest.cs" />
7374
<Compile Include="DijkstraTest.cs" />
7475
<Compile Include="EdgeTest.cs" />
7576
<Compile Include="Properties\AssemblyInfo.cs" />
77+
<Compile Include="TheShortestPathFixture.cs" />
7678
</ItemGroup>
7779
<ItemGroup>
7880
<ProjectReference Include="..\Dijkstra.NET\Dijkstra.NET.csproj">

0 commit comments

Comments
 (0)