public async Task <ShortestPathResult> GetShortestPathAsync(IDijkstraGraph graph, uint from, uint to, 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 NodeComparer(distance)); var current = new HashSet <uint>(); int Distance(uint key) { return(distance.ContainsKey(key) ? distance[key] : Int32.MaxValue); } do { uint u = q.Deque(); boardState.visitNode(u); if (u % 5 is 0) { await Task.Delay(1); } 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)); }
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)); }
/// <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)); }
/// <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 Dijkstra(this IDijkstraGraph graph, uint from, uint to, int depth) { 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> /// <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> /// <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 Dijkstra(this IDijkstraGraph graph, uint from, uint to) => Dijkstra(graph, from, to, Int32.MaxValue);