From 483cb84d6f920ce06215290180548acc8a722011 Mon Sep 17 00:00:00 2001 From: mitchellri Date: Sun, 22 Mar 2020 22:04:55 -0700 Subject: [PATCH] Refactor heuristic node comparing --- src/Dijkstra.NET/ShortestPath/AStar.cs | 4 +-- .../ShortestPath/HeuristicNodeComparer.cs | 30 +++++++++++++++++++ src/Dijkstra.NET/ShortestPath/NodeComparer.cs | 21 ++++--------- 3 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 src/Dijkstra.NET/ShortestPath/HeuristicNodeComparer.cs diff --git a/src/Dijkstra.NET/ShortestPath/AStar.cs b/src/Dijkstra.NET/ShortestPath/AStar.cs index a2bbb50..32bfcb8 100644 --- a/src/Dijkstra.NET/ShortestPath/AStar.cs +++ b/src/Dijkstra.NET/ShortestPath/AStar.cs @@ -6,12 +6,12 @@ namespace Dijkstra.NET.ShortestPath { internal static class AStar { - public static ShortestPathResult GetShortestPath(IDijkstraGraph graph, uint from, uint to, Func heuristic, int depth) + public static ShortestPathResult GetShortestPath(IDijkstraGraph graph, uint from, uint to, int depth, Func heuristic) { var path = new Dictionary(); var distance = new Dictionary {[from] = 0}; var d = new Dictionary {[from] = 0}; - var q = new SortedSet(new[] {from}, new NodeComparer(distance, heuristic)); + var q = new SortedSet(new[] {from}, new HeuristicNodeComparer(distance, heuristic)); var current = new HashSet(); int Distance(uint key) diff --git a/src/Dijkstra.NET/ShortestPath/HeuristicNodeComparer.cs b/src/Dijkstra.NET/ShortestPath/HeuristicNodeComparer.cs new file mode 100644 index 0000000..03ca151 --- /dev/null +++ b/src/Dijkstra.NET/ShortestPath/HeuristicNodeComparer.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; + +namespace Dijkstra.NET.ShortestPath +{ + internal class HeuristicNodeComparer : NodeComparer + { + private readonly Func _heuristic; + + public HeuristicNodeComparer(IDictionary distance, Func 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; + } + } +} diff --git a/src/Dijkstra.NET/ShortestPath/NodeComparer.cs b/src/Dijkstra.NET/ShortestPath/NodeComparer.cs index 519f515..6400311 100644 --- a/src/Dijkstra.NET/ShortestPath/NodeComparer.cs +++ b/src/Dijkstra.NET/ShortestPath/NodeComparer.cs @@ -5,28 +5,17 @@ namespace Dijkstra.NET.ShortestPath { internal class NodeComparer : IComparer { - private readonly IDictionary _distance; - private readonly Func _heuristic; + protected readonly IDictionary _distance; public NodeComparer(IDictionary distance) { _distance = distance; - _heuristic = null; - } - public NodeComparer(IDictionary distance, Func 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); @@ -38,4 +27,4 @@ public int Compare(uint x, uint y) return comparer; } } -} +} \ No newline at end of file