/// <summary> /// Get shortest distance to target. /// </summary> public ShortestPathResult <T, W> FindShortestPath(IGraph <T> graph, T source, T destination) { //regular argument checks if (graph?.GetVertex(source) == null || graph.GetVertex(destination) == null) { throw new ArgumentException(); } 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."); } } //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 <MinHeapWrap <T, W> >(); //keep references of heap Node for decrement key operation var heapMapping = new Dictionary <T, MinHeapWrap <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 MinHeapWrap <T, W>() { Distance = @operator.DefaultValue, Vertex = source }; minHeap.Insert(current); heapMapping.Add(current.Vertex, 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)) { var wrap = new MinHeapWrap <T, W>() { Distance = newDistance, Vertex = neighbour.TargetVertexKey }; minHeap.Insert(wrap); heapMapping.Add(neighbour.TargetVertexKey, wrap); } else { //decrement distance to neighbour in heap var decremented = new MinHeapWrap <T, W>() { Distance = newDistance, Vertex = neighbour.TargetVertexKey }; minHeap.UpdateKey(heapMapping[neighbour.TargetVertexKey], decremented); heapMapping[neighbour.TargetVertexKey] = decremented; } //trace parent parentMap[neighbour.TargetVertexKey] = current.Vertex; } } } return(tracePath(graph, parentMap, source, destination)); }
/// <summary> /// Get shortest distance to target. /// </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 FibornacciHeap <MinHeapWrap <T, W> >(); //keep references of heap Node for decrement key operation var heapMapping = new Dictionary <T, MinHeapWrap <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 MinHeapWrap <T, W>() { Distance = operators.DefaultValue, Vertex = source }; minHeap.Insert(current); heapMapping.Add(current.Vertex, 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)) { var wrap = new MinHeapWrap <T, W>() { Distance = newDistance, Vertex = neighbour.Key.Value }; minHeap.Insert(wrap); heapMapping.Add(neighbour.Key.Value, wrap); } else { //decrement distance to neighbour in heap var decremented = new MinHeapWrap <T, W>() { Distance = newDistance, Vertex = neighbour.Key.Value }; minHeap.UpdateKey(heapMapping[neighbour.Key.Value], decremented); heapMapping[neighbour.Key.Value] = decremented; } //trace parent parentMap[neighbour.Key.Value] = current.Vertex; } } } return(tracePath(graph, parentMap, source, destination)); }
/// <summary> /// Get shortest distance to target /// </summary> /// <param name="graph"></param> /// <param name="source"></param> /// <param name="destination"></param> /// <returns></returns> public ShortestPathResult <T, W> GetShortestPath(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 System.Collections.Generic.Dictionary <T, W>(); //trace our current path by mapping current vertex to its Parent var parentMap = new System.Collections.Generic.Dictionary <T, T>(); //min heap to pick next closest vertex var minHeap = new FibornacciMinHeap <MinHeapWrap <T, W> >(); //keep references of heap Node for decrement key operation var heapMapping = new System.Collections.Generic.Dictionary <T, FibornacciHeapNode <MinHeapWrap <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; } //construct heap for all vertices with Max Distance as default var wrap = new MinHeapWrap <T, W>() { Distance = operators.MaxValue, Target = vertex.Key }; var heapNode = minHeap.Insert(wrap); heapMapping.Add(vertex.Key, heapNode); } //start from source vertex as current var current = new MinHeapWrap <T, W>() { Distance = operators.DefaultValue, Target = source }; //until heap is empty while (minHeap.Count > 0) { //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.Target].OutEdges) { //new distance to neighbour var newDistance = operators.Sum(current.Distance, graph.Vertices[current.Target].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; heapMapping[neighbour.Key.Value].Value.Distance = newDistance; //decrement distance to neighbour in heap minHeap.DecrementKey(heapMapping[neighbour.Key.Value]); //trace parent parentMap[neighbour.Key.Value] = current.Target; } } //next min vertex to visit current = minHeap.ExtractMin(); } return(tracePath(graph, parentMap, source, destination)); }
public int CompareTo(MinHeapWrap <T, W> other) { return(Distance.CompareTo(other.Distance)); }