/// <summary> /// Returns an ordered IEnumerable of Cells representing the shortest path from a specified source Cell to a destination Cell /// </summary> /// <param name="source">The Cell which is at the start of the path</param> /// <param name="destination">The Cell which is at the end of the path</param> /// <returns>Returns an ordered IEnumerable of Cells representing the shortest path from a specified source Cell to a destination Cell</returns> public IEnumerable<Cell> ShortestPath( Cell source, Cell destination ) { var dsp = new DijkstraShortestPath( _graph, IndexFor( source ) ); IEnumerable<DirectedEdge> path = dsp.PathTo( IndexFor( destination ) ); if ( path == null ) { yield return null; } else { foreach ( DirectedEdge edge in path ) { yield return CellFor( edge.To ); } } }
/// <summary> /// Returns an IEnumerable of DirectedEdges representing a shortest path from the specified sourceVertex to the specified destinationVertex /// This is more efficient than creating a new DijkstraShortestPath instance and calling PathTo( destinationVertex ) when we only /// want a single path from Source to Destination and don't want many paths from the source to multiple different destinations. /// </summary> /// <param name="graph">The edge-weighted directed graph</param> /// <param name="sourceVertex">The source vertex to find a shortest path from</param> /// <param name="destinationVertex">The destination vertex to find a shortest path to</param> /// <returns>IEnumerable of DirectedEdges representing a shortest path from the sourceVertex to the specified destinationVertex</returns> public static IEnumerable <DirectedEdge> FindPath(EdgeWeightedDigraph graph, int sourceVertex, int destinationVertex) { var dijkstraShortestPath = new DijkstraShortestPath(graph, sourceVertex, destinationVertex); return(dijkstraShortestPath.PathTo(destinationVertex)); }