Skip to content

Commit f6a440f

Browse files
committed
implementation of depth
1 parent 616df54 commit f6a440f

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

src/Dijkstra.NET.Tests/Extensions/ShortestPathExtensionsTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,31 @@ public void DijkstraGraphNot_Should_Find_Path_In_Graph()
155155

156156
Assert.False(result.IsFounded);
157157
}
158+
159+
[Fact]
160+
public void Dijkstra_Should_Concern_Depth_In_Graph()
161+
{
162+
var graph = new Graph<int, string>();
163+
164+
graph.AddNode(0);
165+
graph.AddNode(0);
166+
graph.AddNode(0);
167+
graph.AddNode(0);
168+
169+
graph.Connect(0, 1, 1, null);
170+
graph.Connect(0, 2, 1, null);
171+
graph.Connect(0, 3, 5, null);
172+
graph.Connect(2, 3, 2, null);
173+
174+
var result = graph.Dijkstra(0, 3, 1);
175+
176+
Assert.True(result.IsFounded);
177+
Assert.Equal(5, result.Distance);
178+
179+
uint[] path = result.GetPath().ToArray();
180+
181+
Assert.Equal((uint)0, path[0]);
182+
Assert.Equal((uint)3, path[1]);
183+
}
158184
}
159185
}

src/Dijkstra.NET/Contract/IGraph.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
namespace Dijkstra.NET.Contract
2-
{
3-
using System;
1+
using System;
42

3+
namespace Dijkstra.NET.Contract
4+
{
55
public interface IGraph<T, TEdgeCustom> where TEdgeCustom : IEquatable<TEdgeCustom>
66
{
77
INode<T, TEdgeCustom> this[uint node] { get; }

src/Dijkstra.NET/Extensions/ShortestPathExtensions.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,23 @@ public static class ShortestPathExtensions
1616
/// <param name="to">End node</param>
1717
/// <returns>Value with path</returns>
1818
public static ShortestPathResult Dijkstra<T, TEdgeCustom>(this IGraph<T, TEdgeCustom> graph, uint from, uint to)
19+
where TEdgeCustom : IEquatable<TEdgeCustom> => Dijkstra(graph, from, to, Int32.MaxValue);
20+
21+
/// <summary>
22+
/// Get path from @from to @to
23+
/// </summary>
24+
/// <param name="graph">Source graph</param>
25+
/// <param name="from">Start node</param>
26+
/// <param name="to">End node</param>
27+
/// <param name="depth">Depth of path</param>
28+
/// <returns>Value with path</returns>
29+
public static ShortestPathResult Dijkstra<T, TEdgeCustom>(this IGraph<T, TEdgeCustom> graph, uint from, uint to, int depth)
1930
where TEdgeCustom : IEquatable<TEdgeCustom>
2031
{
2132
var path = new Dictionary<uint, uint>();
22-
var distance = new Dictionary<uint, int> {[from] = 0};
23-
var q = new SortedSet<uint>(new[] {from}, new NodeComparer(distance));
33+
var distance = new Dictionary<uint, int> { [from] = 0 };
34+
var d = new Dictionary<uint, int> { [from] = 0 };
35+
var q = new SortedSet<uint>(new[] { from }, new NodeComparer(distance));
2436
var current = new HashSet<uint>();
2537

2638
int Distance(uint key)
@@ -39,6 +51,11 @@ int Distance(uint key)
3951

4052
current.Remove(u);
4153

54+
if (depth == d[u])
55+
{
56+
continue;
57+
}
58+
4259
graph[u].EachChild((in Edge<T, TEdgeCustom> e) =>
4360
{
4461
if (Distance(e.Node.Key) > Distance(u) + e.Cost)
@@ -52,6 +69,7 @@ int Distance(uint key)
5269
q.Add(e.Node.Key);
5370
current.Add(e.Node.Key);
5471
path[e.Node.Key] = u;
72+
d[e.Node.Key] = d[u] + 1;
5573
}
5674
});
5775
}

src/Dijkstra.NET/Model/Graph.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ public class Graph<T, TEdgeCustom>: IGraph<T, TEdgeCustom>, IEnumerable<INode<T,
1010
{
1111
private readonly IDictionary<uint, Node<T, TEdgeCustom>> _nodes = new Dictionary<uint, Node<T, TEdgeCustom>>();
1212

13-
public void AddNode(T item)
13+
/// <summary>
14+
/// Add node to graph
15+
/// </summary>
16+
/// <param name="item">Node</param>
17+
/// <returns>Key of node</returns>
18+
public uint AddNode(T item)
1419
{
1520
uint key = (uint) _nodes.Count;
1621
AddNode(key, item);
22+
return key;
1723
}
1824

1925
protected void AddNode(uint key, T item)
@@ -24,6 +30,14 @@ protected void AddNode(uint key, T item)
2430
_nodes.Add(key, new Node<T, TEdgeCustom>(key, item));
2531
}
2632

33+
/// <summary>
34+
/// Connect node from with node to
35+
/// </summary>
36+
/// <param name="from">First node</param>
37+
/// <param name="to">Second node</param>
38+
/// <param name="cost">Cost of connection</param>
39+
/// <param name="custom">Information saved in edge</param>
40+
/// <returns>Returns true if nodes connected</returns>
2741
public bool Connect(uint from, uint to, int cost, TEdgeCustom custom)
2842
{
2943
if (!_nodes.ContainsKey(from) || !_nodes.ContainsKey(to))
@@ -38,6 +52,7 @@ public bool Connect(uint from, uint to, int cost, TEdgeCustom custom)
3852
}
3953

4054
public IEnumerator<INode<T, TEdgeCustom>> GetEnumerator() => _nodes.Select(x => x.Value).GetEnumerator();
55+
4156
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
4257

4358
public INode<T, TEdgeCustom> this[uint node] => _nodes[node];

0 commit comments

Comments
 (0)