//compare distance to target using the heuristic provided public int CompareTo(AStarWrap <T, W> other) { if (this == other) { return(0); } var result1 = heuristic.HueristicDistanceToTarget(Vertex, destinationVertex); var result2 = heuristic.HueristicDistanceToTarget(other.Vertex, destinationVertex); return(result1.CompareTo(result2)); }
/// <summary> /// Search path to target using the heuristic. /// </summary> public ShortestPathResult <T, W> FindShortestPath(WeightedDiGraph <T, W> graph, T source, T destination) { //regular argument checks if (graph?.FindVertex(source) == null || graph.FindVertex(destination) == null) { throw new ArgumentException(); } //track progress for distance to each Vertex from source var progress = new Dictionary <T, W>(); //trace our current path by mapping current vertex to its Parent var parentMap = new Dictionary <T, T>(); //min heap to pick next closest vertex var minHeap = new FibonacciHeap <AStarWrap <T, W> >(); //keep references of heap Node for decrement key operation var heapMapping = new Dictionary <T, AStarWrap <T, W> >(); //add vertices to min heap and progress map foreach (var vertex in graph.Vertices) { //init parent parentMap.Add(vertex.Key, default(T)); //init to max value progress.Add(vertex.Key, operators.MaxValue); if (vertex.Key.Equals(source)) { continue; } } //start from source vertex as current var current = new AStarWrap <T, W>(heuristic, destination) { Distance = operators.DefaultValue, Vertex = source }; //insert neighbour in heap minHeap.Insert(current); heapMapping[source] = current; //until heap is empty while (minHeap.Count > 0) { //next min vertex to visit current = minHeap.Extract(); heapMapping.Remove(current.Vertex); //no path exists, so return max value if (current.Distance.Equals(operators.MaxValue)) { return(new ShortestPathResult <T, W>(null, operators.MaxValue)); } //visit neighbours of current foreach (var neighbour in graph.Vertices[current.Vertex].OutEdges.Where(x => !x.Key.Value.Equals(source))) { //new distance to neighbour var newDistance = operators.Sum(current.Distance, graph.Vertices[current.Vertex].OutEdges[neighbour.Key]); //current distance to neighbour var existingDistance = progress[neighbour.Key.Value]; //update distance if new is better if (newDistance.CompareTo(existingDistance) < 0) { progress[neighbour.Key.Value] = newDistance; if (heapMapping.ContainsKey(neighbour.Key.Value)) { //decrement distance to neighbour in heap var decremented = new AStarWrap <T, W>(heuristic, destination) { Distance = newDistance, Vertex = neighbour.Key.Value }; minHeap.UpdateKey(heapMapping[neighbour.Key.Value], decremented); heapMapping[neighbour.Key.Value] = decremented; } else { //insert neighbour in heap var discovered = new AStarWrap <T, W>(heuristic, destination) { Distance = newDistance, Vertex = neighbour.Key.Value }; minHeap.Insert(discovered); heapMapping[neighbour.Key.Value] = discovered; } //trace parent parentMap[neighbour.Key.Value] = current.Vertex; } } } return(tracePath(graph, parentMap, source, destination)); }
/// <summary> /// Search path to target using the heuristic. /// </summary> public ShortestPathResult <T, W> FindShortestPath(IGraph <T> graph, T source, T destination) { if (this.@operator == null) { throw new ArgumentException("Provide an operator implementation for generic type W during initialization."); } if (!graph.IsWeightedGraph) { if ([email protected]() != typeof(int)) { throw new ArgumentException("Edges of unweighted graphs are assigned an imaginary weight of one (1)." + "Provide an appropriate IShortestPathOperators<int> operator implementation during initialization."); } } //regular argument checks if (graph?.GetVertex(source) == null || graph.GetVertex(destination) == null) { throw new ArgumentException(); } //track progress for distance to each Vertex from source var progress = new Dictionary <T, W>(); //trace our current path by mapping current vertex to its Parent var parentMap = new Dictionary <T, T>(); //min heap to pick next closest vertex var minHeap = new FibonacciHeap <AStarWrap <T, W> >(); //keep references of heap Node for decrement key operation var heapMapping = new Dictionary <T, AStarWrap <T, W> >(); //add vertices to min heap and progress map foreach (var vertex in graph.VerticesAsEnumberable) { //init parent parentMap.Add(vertex.Key, default(T)); //init to max value progress.Add(vertex.Key, @operator.MaxValue); if (vertex.Key.Equals(source)) { continue; } } //start from source vertex as current var current = new AStarWrap <T, W>(heuristic, destination) { Distance = @operator.DefaultValue, Vertex = source }; //insert neighbour in heap minHeap.Insert(current); heapMapping[source] = current; //until heap is empty while (minHeap.Count > 0) { //next min vertex to visit current = minHeap.Extract(); heapMapping.Remove(current.Vertex); //no path exists, so return max value if (current.Distance.Equals(@operator.MaxValue)) { return(new ShortestPathResult <T, W>(null, @operator.MaxValue)); } //visit neighbours of current foreach (var neighbour in graph.GetVertex(current.Vertex).Edges.Where(x => !x.TargetVertexKey.Equals(source))) { //new distance to neighbour var newDistance = @operator.Sum(current.Distance, graph.GetVertex(current.Vertex).GetEdge(neighbour.TargetVertex).Weight <W>()); //current distance to neighbour var existingDistance = progress[neighbour.TargetVertexKey]; //update distance if new is better if (newDistance.CompareTo(existingDistance) < 0) { progress[neighbour.TargetVertexKey] = newDistance; if (heapMapping.ContainsKey(neighbour.TargetVertexKey)) { //decrement distance to neighbour in heap var decremented = new AStarWrap <T, W>(heuristic, destination) { Distance = newDistance, Vertex = neighbour.TargetVertexKey }; minHeap.UpdateKey(heapMapping[neighbour.TargetVertexKey], decremented); heapMapping[neighbour.TargetVertexKey] = decremented; } else { //insert neighbour in heap var discovered = new AStarWrap <T, W>(heuristic, destination) { Distance = newDistance, Vertex = neighbour.TargetVertexKey }; minHeap.Insert(discovered); heapMapping[neighbour.TargetVertexKey] = discovered; } //trace parent parentMap[neighbour.TargetVertexKey] = current.Vertex; } } } return(tracePath(graph, parentMap, source, destination)); }