Skip to content

Commit 15e6640

Browse files
authored
Improve docstrings for all shortest path states (#302)
1 parent 6e47549 commit 15e6640

File tree

8 files changed

+60
-35
lines changed

8 files changed

+60
-35
lines changed

src/shortestpaths/astar.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ end
6161
6262
Compute a shortest path using the [A* search algorithm](http://en.wikipedia.org/wiki/A%2A_search_algorithm).
6363
64+
Return a vector of edges.
65+
6466
# Arguments
6567
- `g::AbstractGraph`: the graph
6668
- `s::Integer`: the source vertex
6769
- `t::Integer`: the target vertex
6870
- `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`](@ref)).
6971
- `heuristic`: 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). Note that the heuristic values should have the same type as the edge weights!
70-
- `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`.
72+
- `edgetype_to_return::Type{E}`: the type `E<:AbstractEdge` of the edges in the return vector. 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`.
7173
"""
7274
function a_star(
7375
g::AbstractGraph{U}, # the g

src/shortestpaths/bellman-ford.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ struct NegativeCycleError <: Exception end
1717
BellmanFordState{T, U}
1818
1919
An `AbstractPathState` designed for Bellman-Ford shortest-paths calculations.
20+
21+
# Fields
22+
23+
- `parents::Vector{U}`: `parents[v]` is the predecessor of vertex `v` on the shortest path from the source to `v`
24+
- `dists::Vector{T}`: `dists[v]` is the length of the shortest path from the source to `v`
2025
"""
2126
struct BellmanFordState{T<:Real,U<:Integer} <: AbstractPathState
2227
parents::Vector{U}
@@ -29,7 +34,8 @@ end
2934
3035
Compute shortest paths between a source `s` (or list of sources `ss`) and all
3136
other nodes in graph `g` using the [Bellman-Ford algorithm](http://en.wikipedia.org/wiki/Bellman–Ford_algorithm).
32-
Return a [`Graphs.BellmanFordState`](@ref) with relevant traversal information.
37+
38+
Return a [`Graphs.BellmanFordState`](@ref) with relevant traversal information (try querying `state.parents` or `state.dists`).
3339
"""
3440
function bellman_ford_shortest_paths(
3541
graph::AbstractGraph{U},

src/shortestpaths/desopo-pape.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
struct DEposoPapeState{T, U}
33
44
An [`AbstractPathState`](@ref) designed for D`Esopo-Pape shortest-path calculations.
5+
6+
# Fields
7+
8+
- `parents::Vector{U}`: `parents[v]` is the predecessor of vertex `v` on the shortest path from the source to `v`
9+
- `dists::Vector{T}`: `dists[v]` is the length of the shortest path from the source to `v`
510
"""
611
struct DEsopoPapeState{T<:Real,U<:Integer} <: AbstractPathState
712
parents::Vector{U}
@@ -13,7 +18,7 @@ end
1318
1419
Compute shortest paths between a source `src` and all
1520
other nodes in graph `g` using the [D'Esopo-Pape algorithm](http://web.mit.edu/dimitrib/www/SLF.pdf).
16-
Return a [`Graphs.DEsopoPapeState`](@ref) with relevant traversal information.
21+
Return a [`Graphs.DEsopoPapeState`](@ref) with relevant traversal information (try querying `state.parents` or `state.dists`).
1722
1823
# Examples
1924
```jldoctest

src/shortestpaths/dijkstra.jl

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
struct DijkstraState{T, U}
33
44
An [`AbstractPathState`](@ref) designed for Dijkstra shortest-paths calculations.
5+
6+
# Fields
7+
8+
- `parents::Vector{U}`
9+
- `dists::Vector{T}`
10+
- `predecessors::Vector{Vector{U}}`: 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.
11+
- `pathcounts::Vector{Float64}`: 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`.
12+
- `closest_vertices::Vector{U}`: a vector of all vertices in the graph ordered from closest to farthest.
513
"""
614
struct DijkstraState{T<:Real,U<:Integer} <: AbstractPathState
715
parents::Vector{U}
@@ -16,23 +24,12 @@ end
1624
1725
Perform [Dijkstra's algorithm](http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)
1826
on a graph, computing shortest distances between `srcs` and all other vertices.
19-
Return a [`Graphs.DijkstraState`](@ref) that contains various traversal information.
27+
Return a [`Graphs.DijkstraState`](@ref) that contains various traversal information (try querying `state.parents` or `state.dists`).
2028
2129
2230
### Optional Arguments
23-
* `allpaths=false`: If true,
24-
25-
`state.predecessors` holds a vector, indexed by vertex,
26-
of all the predecessors discovered during shortest-path calculations.
27-
This keeps track of all parents when there are multiple shortest paths available from the source.
28-
29-
`state.pathcounts` holds a vector, indexed by vertex, of the number of shortest paths from the source to that vertex.
30-
The path count of a source vertex is always `1.0`. The path count of an unreached vertex is always `0.0`.
31-
32-
* `trackvertices=false`: If true,
33-
34-
`state.closest_vertices` holds a vector of all vertices in the graph ordered from closest to farthest.
35-
31+
* `allpaths=false`: If true, `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`.
32+
* `trackvertices=false`: If true, `state.closest_vertices` holds a vector of all vertices in the graph ordered from closest to farthest.
3633
* `maxdist` (default: `typemax(T)`) specifies the maximum path distance beyond which all path distances are assumed to be infinite (that is, they do not exist).
3734
3835
### Performance
@@ -75,9 +72,8 @@ function dijkstra_shortest_paths(
7572
distmx::AbstractMatrix{T}=weights(g);
7673
allpaths=false,
7774
trackvertices=false,
78-
maxdist=typemax(T)
79-
) where T <: Real where U <: Integer
80-
75+
maxdist=typemax(T),
76+
) where {T<:Real} where {U<:Integer}
8177
nvg = nv(g)
8278
dists = fill(typemax(T), nvg)
8379
parents = zeros(U, nvg)
@@ -163,7 +159,7 @@ function dijkstra_shortest_paths(
163159
distmx::AbstractMatrix=weights(g);
164160
allpaths=false,
165161
trackvertices=false,
166-
maxdist=typemax(eltype(distmx))
162+
maxdist=typemax(eltype(distmx)),
167163
)
168164
return dijkstra_shortest_paths(
169165
g, [src;], distmx; allpaths=allpaths, trackvertices=trackvertices, maxdist=maxdist

src/shortestpaths/floyd-warshall.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
struct FloydWarshallState{T, U}
55
66
An [`AbstractPathState`](@ref) designed for Floyd-Warshall shortest-paths calculations.
7+
8+
# Fields
9+
10+
- `dists::Matrix{T}`: `dists[u, v]` is the length of the shortest path from `u` to `v`
11+
- `parents::Matrix{U}`: `parents[u, v]` is the predecessor of vertex `v` on the shortest path from `u` to `v`
712
"""
813
struct FloydWarshallState{T,U<:Integer} <: AbstractPathState
914
dists::Matrix{T}
@@ -15,11 +20,10 @@ end
1520
1621
Use the [Floyd-Warshall algorithm](http://en.wikipedia.org/wiki/Floyd–Warshall_algorithm)
1722
to compute the shortest paths between all pairs of vertices in graph `g` using an
18-
optional distance matrix `distmx`. Return a [`Graphs.FloydWarshallState`](@ref) with relevant
19-
traversal information.
23+
optional distance matrix `distmx`. Return a [`Graphs.FloydWarshallState`](@ref) with relevant traversal information (try querying `state.parents` or `state.dists`).
2024
2125
### Performance
22-
Space complexity is on the order of ``\\mathcal{O}(|V|^2)``.
26+
Space complexity is on the order of `O(|V|^2)`.
2327
"""
2428
function floyd_warshall_shortest_paths(
2529
g::AbstractGraph{U}, distmx::AbstractMatrix{T}=weights(g)

src/shortestpaths/johnson.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
struct JohnsonState{T, U}
44
55
An [`AbstractPathState`](@ref) designed for Johnson shortest-paths calculations.
6+
7+
# Fields
8+
9+
- `dists::Matrix{T}`: `dists[u, v]` is the length of the shortest path from `u` to `v`
10+
- `parents::Matrix{U}`: `parents[u, v]` is the predecessor of vertex `v` on the shortest path from `u` to `v`
611
"""
712
struct JohnsonState{T<:Real,U<:Integer} <: AbstractPathState
813
dists::Matrix{T}
@@ -17,18 +22,19 @@ to compute the shortest paths between all pairs of vertices in graph `g` using a
1722
optional distance matrix `distmx`.
1823
1924
Return a [`Graphs.JohnsonState`](@ref) with relevant
20-
traversal information.
25+
traversal information (try querying `state.parents` or `state.dists`).
2126
2227
### Performance
23-
Complexity: O(|V|*|E|)
28+
Complexity: `O(|V|*|E|)`
2429
"""
2530
function johnson_shortest_paths(
2631
g::AbstractGraph{U}, distmx::AbstractMatrix{T}=weights(g)
2732
) where {T<:Real} where {U<:Integer}
2833
nvg = nv(g)
2934
type_distmx = typeof(distmx)
3035
# Change when parallel implementation of Bellman Ford available
31-
wt_transform = bellman_ford_shortest_paths(g, collect_if_not_vector(vertices(g)), distmx).dists
36+
wt_transform =
37+
bellman_ford_shortest_paths(g, collect_if_not_vector(vertices(g)), distmx).dists
3238

3339
@compat if !ismutable(distmx) && type_distmx != Graphs.DefaultDistance
3440
distmx = sparse(distmx) # Change reference, not value

src/shortestpaths/spfa.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Compute shortest paths between a source `s` and all
1515
other nodes in graph `g` using the [Shortest Path Faster Algorithm]
1616
(https://en.wikipedia.org/wiki/Shortest_Path_Faster_Algorithm).
1717
18+
Return a vector of distances to the source.
19+
1820
# Examples
1921
2022
```jldoctest

src/shortestpaths/yen.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# TODO this algorithm does not work with abitrary AbstractGraph yet,
22
# as it relies on rem_edge! and deepcopy
33

4-
54
"""
65
struct YenState{T, U}
76
87
Designed for yen k-shortest-paths calculations.
8+
9+
# Fields
10+
11+
- `dists::Vector{T}`: `dists[k]` is the length of the `k`-th shortest path from the source to the target
12+
- `paths::Vector{Vector{U}}`: `paths[k]` is the description of the `k`-th shortest path (as a sequence of vertices) from the source to the target
913
"""
1014
struct YenState{T,U<:Integer} <: AbstractPathState
1115
dists::Vector{T}
@@ -25,8 +29,8 @@ function yen_k_shortest_paths(
2529
target::U,
2630
distmx::AbstractMatrix{T}=weights(g),
2731
K::Int=1;
28-
maxdist=typemax(T)) where {T<:Real} where {U<:Integer}
29-
32+
maxdist=typemax(T),
33+
) where {T<:Real} where {U<:Integer}
3034
source == target && return YenState{T,U}([U(0)], [[source]])
3135

3236
dj = dijkstra_shortest_paths(g, source, distmx; maxdist)
@@ -39,7 +43,7 @@ function yen_k_shortest_paths(
3943
B = PriorityQueue()
4044
gcopy = deepcopy(g)
4145

42-
for k in 1:(K-1)
46+
for k in 1:(K - 1)
4347
for j in 1:length(A[k])
4448
# Spur node is retrieved from the previous k-shortest path, k − 1
4549
spurnode = A[k][j]
@@ -52,7 +56,7 @@ function yen_k_shortest_paths(
5256
for ppath in A
5357
if length(ppath) > j && rootpath == ppath[1:j]
5458
u = ppath[j]
55-
v = ppath[j+1]
59+
v = ppath[j + 1]
5660
if has_edge(gcopy, u, v)
5761
rem_edge!(gcopy, u, v)
5862
push!(edgesremoved, (u, v))
@@ -62,7 +66,7 @@ function yen_k_shortest_paths(
6266

6367
# Remove node of root path and calculate dist of it
6468
distrootpath = zero(T)
65-
for n in 1:(length(rootpath)-1)
69+
for n in 1:(length(rootpath) - 1)
6670
u = rootpath[n]
6771
nei = copy(neighbors(gcopy, u))
6872
for v in nei
@@ -71,7 +75,7 @@ function yen_k_shortest_paths(
7175
end
7276

7377
# Evaluate distance of root path
74-
v = rootpath[n+1]
78+
v = rootpath[n + 1]
7579
distrootpath += distmx[u, v]
7680
end
7781

@@ -80,7 +84,7 @@ function yen_k_shortest_paths(
8084
spurpath = enumerate_paths(djspur)[target]
8185
if !isempty(spurpath)
8286
# Entire path is made up of the root path and spur path
83-
pathtotal = [rootpath[1:(end-1)]; spurpath]
87+
pathtotal = [rootpath[1:(end - 1)]; spurpath]
8488
distpath = distrootpath + djspur.dists[target]
8589
# Add the potential k-shortest path to the heap
8690
if !haskey(B, pathtotal)

0 commit comments

Comments
 (0)