# Shortest paths

Graphs.jl includes standard algorithms for shortest paths.

## Full docs

Graphs.a_starMethod
a_star(g, s, t[, distmx][, heuristic][, edgetype_to_return])

Compute a shortest path using the A* search algorithm.

Arguments

• g::AbstractGraph: the graph
• s::Integer: the source vertex
• t::Integer: the target vertex
• distmx::AbstractMatrix: an optional (possibly sparse) n × n matrix of edge weights. It is set to weights(g) by default (which itself falls back on Graphs.DefaultDistance).
• heuristic::Function: an optional function mapping each vertex to a lower estimate of the remaining distance from v to t. It is set to v -> 0 by default (which corresponds to Dijkstra's algorithm)
• edgetype_to_return::Type{E}: the eltype E<:AbstractEdge of the vector of edges returned. It is set to edgetype(g) by default. Note that the two-argument constructor E(u, v) must be defined, even for weighted edges: if it isn't, consider using E = Graphs.SimpleEdge.
source
Graphs.enumerate_pathsMethod
enumerate_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.

source
Graphs.desopo_pape_shortest_pathsMethod
desopo_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
3
source
Graphs.dijkstra_shortest_pathsMethod
dijkstra_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
3
source
Graphs.has_negative_edge_cycle_spfaMethod

Function 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);
false
source
Graphs.spfa_shortest_pathsMethod
spfa_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.

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
0
source