Shortest paths
Graphs.jl includes standard algorithms for shortest paths.
Index
Graphs.BellmanFordStateGraphs.DEsopoPapeStateGraphs.DijkstraStateGraphs.FloydWarshallStateGraphs.JohnsonStateGraphs.YenStateGraphs.a_starGraphs.bellman_ford_shortest_pathsGraphs.desopo_pape_shortest_pathsGraphs.dijkstra_shortest_pathsGraphs.enumerate_pathsGraphs.floyd_warshall_shortest_pathsGraphs.has_negative_edge_cycle_spfaGraphs.johnson_shortest_pathsGraphs.spfa_shortest_pathsGraphs.yen_k_shortest_paths
Full docs
Graphs.a_star — Methoda_star(g, s, t[, distmx][, heuristic][, edgetype_to_return])Compute a shortest path using the A* search algorithm.
Arguments
g::AbstractGraph: the graphs::Integer: the source vertext::Integer: the target vertexdistmx::AbstractMatrix: an optional (possibly sparse)n × nmatrix of edge weights. It is set toweights(g)by default (which itself falls back onGraphs.DefaultDistance).heuristic::Function: an optional function mapping each vertex to a lower estimate of the remaining distance fromvtot. It is set tov -> 0by default (which corresponds to Dijkstra's algorithm)edgetype_to_return::Type{E}: the eltypeE<:AbstractEdgeof the vector of edges returned. It is set toedgetype(g)by default. Note that the two-argument constructorE(u, v)must be defined, even for weighted edges: if it isn't, consider usingE = Graphs.SimpleEdge.
Graphs.BellmanFordState — TypeBellmanFordState{T, U}An AbstractPathState designed for Bellman-Ford shortest-paths calculations.
Graphs.bellman_ford_shortest_paths — Methodbellman_ford_shortest_paths(g, s, distmx=weights(g))
bellman_ford_shortest_paths(g, ss, distmx=weights(g))Compute shortest paths between a source s (or list of sources ss) and all other nodes in graph g using the Bellman-Ford algorithm. Return a Graphs.BellmanFordState with relevant traversal information.
Graphs.enumerate_paths — Methodenumerate_paths(state[, vs])Given a path state state of type AbstractPathState, return a vector (indexed by vertex) of the paths between the source vertex used to compute the path state and a single destination vertex, a list of destination vertices, or the entire graph. For multiple destination vertices, each path is represented by a vector of vertices on the path between the source and the destination. Nonexistent paths will be indicated by an empty vector. For single destinations, the path is represented by a single vector of vertices, and will be length 0 if the path does not exist.
Implementation Notes
For Floyd-Warshall path states, please note that the output is a bit different, since this algorithm calculates all shortest paths for all pairs of vertices: enumerate_paths(state) will return a vector (indexed by source vertex) of vectors (indexed by destination vertex) of paths. enumerate_paths(state, v) will return a vector (indexed by destination vertex) of paths from source v to all other vertices. In addition, enumerate_paths(state, v, d) will return a vector representing the path from vertex v to vertex d.
Graphs.DEsopoPapeState — Typestruct DEposoPapeState{T, U}An AbstractPathState designed for D`Esopo-Pape shortest-path calculations.
Graphs.desopo_pape_shortest_paths — Methoddesopo_pape_shortest_paths(g, src, distmx=weights(g))Compute shortest paths between a source src and all other nodes in graph g using the D'Esopo-Pape algorithm. Return a Graphs.DEsopoPapeState with relevant traversal information.
Examples
julia> using Graphs
julia> ds = desopo_pape_shortest_paths(cycle_graph(5), 2);
julia> ds.dists
5-element Array{Int64,1}:
1
0
1
2
2
julia> ds = desopo_pape_shortest_paths(path_graph(5), 2);
julia> ds.dists
5-element Array{Int64,1}:
1
0
1
2
3Graphs.DijkstraState — Typestruct DijkstraState{T, U}An AbstractPathState designed for Dijkstra shortest-paths calculations.
Graphs.dijkstra_shortest_paths — Methoddijkstra_shortest_paths(g, srcs, distmx=weights(g));Perform Dijkstra's algorithm on a graph, computing shortest distances between srcs and all other vertices. Return a Graphs.DijkstraState that contains various traversal information.
Optional Arguments
allpaths=false: If true,
state.predecessors holds a vector, indexed by vertex, of all the predecessors discovered during shortest-path calculations. This keeps track of all parents when there are multiple shortest paths available from the source.
state.pathcounts holds a vector, indexed by vertex, of the number of shortest paths from the source to that vertex. The path count of a source vertex is always 1.0. The path count of an unreached vertex is always 0.0.
trackvertices=false: If true,
state.closest_vertices holds a vector of all vertices in the graph ordered from closest to farthest.
Performance
If using a sparse matrix for distmx, you may achieve better performance by passing in a transpose of its sparse transpose. That is, assuming D is the sparse distance matrix:
D = transpose(sparse(transpose(D)))Be aware that realizing the sparse transpose of D incurs a heavy one-time penalty, so this strategy should only be used when multiple calls to dijkstra_shortest_paths with the distance matrix are planned.
Examples
julia> using Graphs
julia> ds = dijkstra_shortest_paths(cycle_graph(5), 2);
julia> ds.dists
5-element Array{Int64,1}:
1
0
1
2
2
julia> ds = dijkstra_shortest_paths(path_graph(5), 2);
julia> ds.dists
5-element Array{Int64,1}:
1
0
1
2
3Graphs.FloydWarshallState — Typestruct FloydWarshallState{T, U}An AbstractPathState designed for Floyd-Warshall shortest-paths calculations.
Graphs.floyd_warshall_shortest_paths — Methodfloyd_warshall_shortest_paths(g, distmx=weights(g))Use the Floyd-Warshall algorithm to compute the shortest paths between all pairs of vertices in graph g using an optional distance matrix distmx. Return a Graphs.FloydWarshallState with relevant traversal information.
Performance
Space complexity is on the order of $\mathcal{O}(|V|^2)$.
Graphs.JohnsonState — Typestruct JohnsonState{T, U}An AbstractPathState designed for Johnson shortest-paths calculations.
Graphs.johnson_shortest_paths — Methodjohnson_shortest_paths(g, distmx=weights(g))Use the Johnson algorithm to compute the shortest paths between all pairs of vertices in graph g using an optional distance matrix distmx.
Return a Graphs.JohnsonState with relevant traversal information.
Performance
Complexity: O(|V|*|E|)
Graphs.has_negative_edge_cycle_spfa — MethodFunction which returns true if there is any negative weight cycle in the graph.
Examples
julia> g = complete_graph(3);
julia> d = [1 -3 1; -3 1 1; 1 1 1];
julia> has_negative_edge_cycle_spfa(g, d)
true
julia> g = complete_graph(4);
julia> d = [1 1 -1 1; 1 1 -1 1; 1 1 1 1; 1 1 1 1];
julia> has_negative_edge_cycle_spfa(g, d);
falseGraphs.spfa_shortest_paths — Methodspfa_shortest_paths(g, s, distmx=weights(g))Compute shortest paths between a source s and all other nodes in graph g using the [Shortest Path Faster Algorithm] (https://en.wikipedia.org/wiki/ShortestPathFaster_Algorithm).
Examples
julia> g = complete_graph(3);
julia> d = [1 -3 1; -3 1 1; 1 1 1];
julia> spfa_shortest_paths(g, 1, d)
ERROR: Graphs.NegativeCycleError()
julia> g = complete_graph(4);
julia> d = [1 1 -1 1; 1 1 -1 1; 1 1 1 1; 1 1 1 1];
julia> spfa_shortest_paths(gx, 1, d)
4-element Array{Int64,1}:
0
0
-1
0Graphs.YenState — Typestruct YenState{T, U}Designed for yen k-shortest-paths calculations.
Graphs.yen_k_shortest_paths — Methodyen_k_shortest_paths(g, source, target, distmx=weights(g), K=1; maxdist=Inf);Perform Yen's algorithm on a graph, computing k-shortest distances between source and target other vertices. Return a YenState that contains distances and paths.