Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShortestPath extension to getting custom info chain implemented #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Dijkstra.NET.Tests/EdgeTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Dijkstra.NET.Graph;
using Dijkstra.NET.ShortestPath;
using System.Linq;
using Xunit;

namespace Dijkstra.NET.Tests
Expand Down Expand Up @@ -119,5 +121,32 @@ public void Edges_Get_TEdgeCustom_Should_Work()

Assert.True(act);
}
[Fact]
public void Edges_Get_Full_Path_Custom()
{
var g = new Graph<int, string>();
g.AddNode(1);
g.AddNode(2);
g.AddNode(3);

g.Connect(1, 2, 1, "First");
g.Connect(2, 3, 1, "Second");

var path = g.Dijkstra(1, 3);
var pathCustom = path.GetPathEdgesCustom(g);
var pathCustomReversed = path.GetReversedPathEdgesCustom(g);

bool act = pathCustom.Count() == 2;
bool act2 = pathCustomReversed.Count() == 2;
bool act3 = pathCustom.First() == "First";
bool act4 = pathCustomReversed.First() == "Second";

Assert.True(act);
Assert.True(act2);
Assert.True(act3);
Assert.True(act3);

}

}
}
3 changes: 1 addition & 2 deletions src/Dijkstra.NET/Graph/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public void EachEdge(Edge edge)
public TEdgeCustom GetFirstEdgeCustom(int nodeEdgeKey)
{
return Edges.First(c => c.Node.Key == nodeEdgeKey).Item;
}

}

internal void AddEdge(in Edge<T, TEdgeCustom> edge)
{
Expand Down
45 changes: 45 additions & 0 deletions src/Dijkstra.NET/ShortestPath/ShortestPathExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Dijkstra.NET.Graph;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Dijkstra.NET.ShortestPath
{
public static class ShortestPathExtensions
{
public static IEnumerable<TEdgeCustom> GetReversedPathEdgesCustom<T, TEdgeCustom>(this ShortestPathResult result, Graph<T, TEdgeCustom> graph) where TEdgeCustom : IEquatable<TEdgeCustom>
{
return result.GetPathEdgesCustom(graph).Reverse();
}

public static IEnumerable<TEdgeCustom> GetPathEdgesCustom<T, TEdgeCustom>(this ShortestPathResult result, Graph<T, TEdgeCustom> graph) where TEdgeCustom : IEquatable<TEdgeCustom>
{
if (result.IsFounded)
{
using (var iterator = result.GetPath().GetEnumerator())
{
if (!iterator.MoveNext())
{
yield break;
}
var currentNode = iterator.Current;
while (iterator.MoveNext())
{
yield return GetFirstEdgeCustom(graph, currentNode, iterator.Current);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if between two nodes will bo more than one edge ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello. Mea culpa. I implemented this method to work with my custom aggregated info (GROUP BY from,to) so I have only one groupped edge between nodes in path. I will take a look what will be good realization of this veriant

currentNode = iterator.Current;
}
}
}
else
throw new System.Exception("path not founded");
}

static TEdgeCustom GetFirstEdgeCustom<T, TEdgeCustom>(Graph<T, TEdgeCustom> graph, uint currentNodeKey, uint edgeNodeKey) where TEdgeCustom : IEquatable<TEdgeCustom>
{
var currentNode = graph >> (int)currentNodeKey;
if (currentNode == null)
throw new ArgumentException("node not founded at graph");
return currentNode.GetFirstEdgeCustom((int)edgeNodeKey);
}
}
}
3 changes: 2 additions & 1 deletion src/Samples/Dijkstra.Net40/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ static void Main(string[] args)
{
var graph = new Graph<int, string>() + 1 + 2;

bool connected = graph >> 1 >> 2 >> 5 ^ "edge information";
bool connected = graph >> 1 >> 2 >> 5 ^ "edge information";

var immutablePath = graph.Dijkstra(1, 2);
var immutablePathDescriptions = immutablePath.GetPathEdgesCustom(graph);
}
}
}