Skip to content

Commit

Permalink
Refactor heuristic node comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellri committed Mar 23, 2020
1 parent e0ad6d0 commit 483cb84
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Dijkstra.NET/ShortestPath/AStar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ 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)
public static ShortestPathResult GetShortestPath(IDijkstraGraph graph, uint from, uint to, int depth, Func<uint,uint,int> heuristic)
{
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 NodeComparer(distance, heuristic));
var q = new SortedSet<uint>(new[] {from}, new HeuristicNodeComparer(distance, heuristic));
var current = new HashSet<uint>();

int Distance(uint key)
Expand Down
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;
}
}
}
21 changes: 5 additions & 16 deletions src/Dijkstra.NET/ShortestPath/NodeComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,17 @@ namespace Dijkstra.NET.ShortestPath
{
internal class NodeComparer : IComparer<uint>
{
private readonly IDictionary<uint, int> _distance;
private readonly Func<uint, uint, int> _heuristic;
protected readonly IDictionary<uint, int> _distance;

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

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

int comparer = xDistance.CompareTo(yDistance);

Expand All @@ -38,4 +27,4 @@ public int Compare(uint x, uint y)
return comparer;
}
}
}
}

0 comments on commit 483cb84

Please sign in to comment.