Skip to content

Commit 5174535

Browse files
committed
i20 simple graph implementation
1 parent f2c1a66 commit 5174535

File tree

5 files changed

+145
-8
lines changed

5 files changed

+145
-8
lines changed

src/Dijkstra.NET.Tests/ShortestPath/DijkstraExtensionsTests.cs

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq;
22
using Dijkstra.NET.Graph;
3+
using Dijkstra.NET.Graph.Simple;
34
using Dijkstra.NET.ShortestPath;
45
using Xunit;
56

@@ -31,6 +32,40 @@ public void DijkstraGraphShould_Find_Path_In_Multi_Paths_Graph()
3132
Assert.True(result.IsFounded);
3233
}
3334

35+
[Fact]
36+
public void DijkstraSimpleGraphShould_Find_Path_In_Multi_Paths_Graph()
37+
{
38+
var graph = new Graph.Simple.Graph() + 6;
39+
40+
var connected = graph >> 1 >> 2 ^ 2;
41+
Assert.True(connected);
42+
connected = graph >> 1 >> 3 ^ 3;
43+
Assert.True(connected);
44+
45+
connected = graph >> 2 >> 4 ^ 4;
46+
Assert.True(connected);
47+
48+
connected = graph >> 3 >> 4 ^ 2;
49+
Assert.True(connected);
50+
51+
connected = graph >> 3 >> 5 ^ 1;
52+
Assert.True(connected);
53+
54+
connected = graph >> 4 >> 6 ^ 6;
55+
Assert.True(connected);
56+
57+
var result = graph.Dijkstra(1, 6);
58+
uint[] path = result.GetPath().ToArray();
59+
60+
Assert.Equal<uint>(1, path[0]);
61+
Assert.Equal<uint>(3, path[1]);
62+
Assert.Equal<uint>(4, path[2]);
63+
Assert.Equal<uint>(6, path[3]);
64+
65+
Assert.Equal(11, result.Distance);
66+
Assert.True(result.IsFounded);
67+
}
68+
3469
[Fact]
3570
public void DijkstraGraphShould_Find_Path_In_Override_Node()
3671
{
@@ -109,6 +144,19 @@ public void DijkstraGraphShould_Find_Path_With_One_Vertex_In_Graph()
109144
Assert.True(result.IsFounded);
110145
}
111146

147+
[Fact]
148+
public void DijkstraSimpleGraphShould_Find_Path_With_One_Vertex_In_Graph()
149+
{
150+
var graph = 1.ToGraph();
151+
152+
var result = graph.Dijkstra(1, 1);
153+
uint[] path = result.GetPath().ToArray();
154+
155+
Assert.Equal<uint>(1, path[0]);
156+
Assert.Equal(0, result.Distance);
157+
Assert.True(result.IsFounded);
158+
}
159+
112160
[Fact]
113161
public void DijkstraGraphShould_Find_Path_With_One_Vertex_In_Graph_And_Depth_Zero()
114162
{
@@ -123,6 +171,19 @@ public void DijkstraGraphShould_Find_Path_With_One_Vertex_In_Graph_And_Depth_Zer
123171
Assert.True(result.IsFounded);
124172
}
125173

174+
[Fact]
175+
public void DijkstraSimpleGraphShould_Find_Path_With_One_Vertex_In_Graph_And_Depth_Zero()
176+
{
177+
var graph = 1.ToGraph();
178+
179+
var result = graph.Dijkstra(1, 1, 0);
180+
uint[] path = result.GetPath().ToArray();
181+
182+
Assert.Equal<uint>(1, path[0]);
183+
Assert.Equal(0, result.Distance);
184+
Assert.True(result.IsFounded);
185+
}
186+
126187
[Fact]
127188
public void DijkstraGraphShould_Find_Path_With_One_Vertex_And_One_Edge_In_Graph()
128189
{
@@ -139,6 +200,21 @@ public void DijkstraGraphShould_Find_Path_With_One_Vertex_And_One_Edge_In_Graph(
139200
Assert.True(result.IsFounded);
140201
}
141202

203+
[Fact]
204+
public void DijkstraSimpleGraphShould_Find_Path_With_One_Vertex_And_One_Edge_In_Graph()
205+
{
206+
var graph = 1.ToGraph();
207+
208+
graph.Connect(1, 1, 5);
209+
210+
var result = graph.Dijkstra(1, 1);
211+
uint[] path = result.GetPath().ToArray();
212+
213+
Assert.Equal<uint>(1, path[0]);
214+
Assert.Equal(0, result.Distance);
215+
Assert.True(result.IsFounded);
216+
}
217+
142218
[Fact]
143219
public void DijkstraGraphShould_Find_Path_In_Multi_Edges_Graph()
144220
{
@@ -173,7 +249,7 @@ public void DijkstraGraphShould_Find_Path_In_Multi_Edges_Graph()
173249
}
174250

175251
[Fact]
176-
public void DijkstraGraphShould_Find_Path_In_Multi_Edges_SimpleGraph()
252+
public void DijkstraSimpleGraphShould_Find_Path_In_Multi_Edges()
177253
{
178254
var graph = new Graph.Simple.Graph();
179255

@@ -229,7 +305,7 @@ public void DijkstraGraphNot_Should_Find_Path_In_Graph()
229305
}
230306

231307
[Fact]
232-
public void DijkstraGraphNot_Should_Find_Path_In_SimpleGraph()
308+
public void DijkstraSimpleGraphNot_Should_Find_Path_In_Graph()
233309
{
234310
var graph = new Graph.Simple.Graph();
235311

src/Dijkstra.NET/Graph/EdgeTemp.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public static EdgeTemp<T, TCustom> operator>>(EdgeTemp<T, TCustom> edge, int cos
4747
return edge.Graph.Connect(edge.NodeFrom, edge.NodeTo, edge.Cost, edgeCustom);
4848
}
4949

50-
public uint NodeFrom { get; }
50+
internal uint NodeFrom { get; }
5151

52-
public uint NodeTo { get; }
52+
internal uint NodeTo { get; }
5353

54-
public Graph<T, TCustom> Graph { get; }
54+
internal Graph<T, TCustom> Graph { get; }
5555

56-
public int Cost { get; }
56+
internal int Cost { get; }
5757
}
5858
}

src/Dijkstra.NET/Graph/Exceptions/EdgeNotFoundException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Dijkstra.NET.Graph.Exceptions
55
public class EdgeNotFoundException: Exception
66
{
77
internal EdgeNotFoundException(uint node)
8-
:base($"Edge with {node} nod key doesn't exist.")
8+
:base($"Edge with {node} node key doesn't exist.")
99
{
1010
}
1111
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,52 @@
1+
using System;
2+
13
namespace Dijkstra.NET.Graph.Simple
24
{
35
public readonly struct EdgeTemp
46
{
7+
internal EdgeTemp(Graph graph, uint nodeFrom)
8+
: this(graph, nodeFrom, default)
9+
{
10+
}
11+
12+
internal EdgeTemp(Graph graph, uint nodeFrom, uint nodeTo)
13+
{
14+
Graph = graph;
15+
NodeFrom = nodeFrom;
16+
NodeTo = nodeTo;
17+
}
18+
19+
internal Graph Graph { get; }
20+
21+
internal uint NodeFrom { get; }
22+
23+
internal uint NodeTo { get; }
24+
25+
/// <summary>
26+
/// Connect two nodes in graph
27+
/// </summary>
28+
/// <param name="edge">Node from</param>
29+
/// <param name="node">Node to</param>
30+
/// <returns>Temporary edge</returns>
31+
public static EdgeTemp operator >>(EdgeTemp edge, int node)
32+
{
33+
return new EdgeTemp(edge.Graph, edge.NodeFrom, (uint)node);
34+
}
35+
36+
/// <summary>
37+
/// Create edge between two nodes
38+
/// </summary>
39+
/// <param name="edge">Temporary edge</param>
40+
/// <param name="cost">Connection cost</param>
41+
/// <returns>True if connected</returns>
42+
public static bool operator^(EdgeTemp edge, int cost)
43+
{
44+
if (edge.NodeTo == default)
45+
{
46+
throw new InvalidOperationException("Destination node is not defined. Use >> operator to define it.");
47+
}
548

49+
return edge.Graph.Connect(edge.NodeFrom, edge.NodeTo, cost);
50+
}
651
}
752
}

src/Dijkstra.NET/Graph/Simple/Graph.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using Dijkstra.NET.Extensions;
56
using Dijkstra.NET.PageRank;
67
using Dijkstra.NET.ShortestPath;
@@ -23,7 +24,22 @@ public class Graph: IDijkstraGraph, IPageRankGraph
2324
/// <returns>Temporary edge</returns>
2425
public static EdgeTemp operator >>(Graph graph, int node)
2526
{
26-
return new EdgeTemp();
27+
return new EdgeTemp(graph, (uint)node);
28+
}
29+
30+
/// <summary>
31+
/// Add nodes to graph
32+
/// </summary>
33+
/// <param name="graph">Graph</param>
34+
/// <param name="numberOfNodes">Number of nodes</param>
35+
/// <returns></returns>
36+
public static Graph operator +(Graph graph, int numberOfNodes)
37+
{
38+
Enumerable
39+
.Range(0, numberOfNodes)
40+
.Each(_ => graph.AddNode());
41+
42+
return graph;
2743
}
2844

2945
/// <summary>

0 commit comments

Comments
 (0)