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

AStar add #45

Open
wants to merge 2 commits 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
60 changes: 60 additions & 0 deletions src/Dijkstra.NET/ShortestPath/AStar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using Dijkstra.NET.Graph;

namespace Dijkstra.NET.ShortestPath
{
internal static class AStar
{
public static ShortestPathResult GetShortestPath(IDijkstraGraph graph, uint from, uint to, Func<uint,uint,int> heuristic, int depth)
{
var path = new Dictionary<uint, uint>();
var distance = new Dictionary<uint, int> {[from] = 0};
var d = new Dictionary<uint, int> {[from] = 0};
var q = new SortedSet<uint>(new[] {from}, new HeuristicNodeComparer(distance, heuristic));
var current = new HashSet<uint>();

int Distance(uint key)
{
return distance.ContainsKey(key) ? distance[key] : Int32.MaxValue;
}

do
{
uint u = q.Deque();

if (u == to)
{
return new ShortestPathResult(from, to, distance[u], path);
}

current.Remove(u);

if (depth == d[u])
{
continue;
}

graph[u]((node, cost) =>
{
if (Distance(node) > Distance(u) + cost)
{
if (current.Contains(node))
{
q.Remove(node);
}

distance[node] = Distance(u) + cost;
q.Add(node);
current.Add(node);
path[node] = u;
d[node] = d[u] + 1;
}
});

} while (q.Count > 0 && depth > 0);

return new ShortestPathResult(from, to);
}
}
}
23 changes: 23 additions & 0 deletions src/Dijkstra.NET/ShortestPath/DijkstraExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public static class DijkstraExtensions
public static ShortestPathResult Dijkstra(this IDijkstraGraph graph, uint from, uint to)
=> Dijkstra(graph, from, to, Int32.MaxValue);

/// <summary>
/// Get path from @from to @to
/// </summary>
/// <param name="graph">Source graph</param>
/// <param name="from">Start node</param>
/// <param name="to">End node</param>
/// <returns>Value with path</returns>
public static ShortestPathResult AStar(this IDijkstraGraph graph, uint from, uint to, Func<uint,uint,int> heuristic)
=> AStar(graph, from, to, heuristic, Int32.MaxValue);

/// <summary>
/// Get path from @from to @to
/// </summary>
Expand All @@ -29,5 +39,18 @@ public static ShortestPathResult Dijkstra(this IDijkstraGraph graph, uint from,
{
return ShortestPath.Dijkstra.GetShortestPath(graph, from, to, depth);
}

/// <summary>
/// Get path from @from to @to
/// </summary>
/// <param name="graph">Source graph</param>
/// <param name="from">Start node</param>
/// <param name="to">End node</param>
/// <param name="depth">Depth of path</param>
/// <returns>Value with path</returns>
public static ShortestPathResult AStar(this IDijkstraGraph graph, uint from, uint to, Func<uint, uint, int> heuristic, int depth)
{
return ShortestPath.AStar.GetShortestPath(graph, from, to, heuristic, depth);
}
}
}
30 changes: 30 additions & 0 deletions src/Dijkstra.NET/ShortestPath/HeuristicNodeComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;

namespace Dijkstra.NET.ShortestPath
{
internal class HeuristicNodeComparer : NodeComparer
{
private readonly Func<uint, uint, int> _heuristic;

public HeuristicNodeComparer(IDictionary<uint, int> distance, Func<uint, uint, int> heuristic) : base(distance)
{
_heuristic = heuristic;
}

public override int Compare(uint x, uint y)
{
int xDistance = _distance.ContainsKey(x) ? _distance[x] + _heuristic(x, y) : Int32.MaxValue;
int yDistance = _distance.ContainsKey(y) ? _distance[y] + _heuristic(y, x) : Int32.MaxValue;

int comparer = xDistance.CompareTo(yDistance);

if (comparer == 0)
{
return x.CompareTo(y);
}

return comparer;
}
}
}
6 changes: 3 additions & 3 deletions src/Dijkstra.NET/ShortestPath/NodeComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace Dijkstra.NET.ShortestPath
{
internal class NodeComparer : IComparer<uint>
{
private readonly IDictionary<uint, int> _distance;
protected readonly IDictionary<uint, int> _distance;

public NodeComparer(IDictionary<uint, int> distance)
{
_distance = distance;
}

public int Compare(uint x, uint y)
public virtual int Compare(uint x, uint y)
{
int xDistance = _distance.ContainsKey(x) ? _distance[x] : Int32.MaxValue;
int yDistance = _distance.ContainsKey(y) ? _distance[y] : Int32.MaxValue;
Expand All @@ -27,4 +27,4 @@ public int Compare(uint x, uint y)
return comparer;
}
}
}
}