Skip to content

Commit 788a168

Browse files
committed
throw argument error and explicitly test enumerate_paths!
1 parent 057d7ed commit 788a168

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/Graphs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ export
253253
has_negative_edge_cycle_spfa,
254254
has_negative_edge_cycle,
255255
enumerate_paths,
256+
enumerate_paths!,
256257
johnson_shortest_paths,
257258
floyd_warshall_shortest_paths,
258259
transitiveclosure!,

src/shortestpaths/bellman-ford.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ single destinations, the path is represented by a single vector of vertices,
107107
and will be length 0 if the path does not exist.
108108
109109
### Implementation Notes
110+
110111
For Floyd-Warshall path states, please note that the output is a bit different,
111112
since this algorithm calculates all shortest paths for all pairs of vertices:
112113
`enumerate_paths(state)` will return a vector (indexed by source vertex) of
@@ -130,7 +131,10 @@ end
130131
131132
In-place version of [`enumerate_paths`](@ref).
132133
133-
`paths` must be a `Vector{Vectors{eltype(state.parents)}}`
134+
`paths` must be a `Vector{Vectors{eltype(state.parents)}}`, `state` an `AbstractPathState`,
135+
and `vs`` an AbstractRange or other AbstractVector of `Int`.
136+
137+
See the `enumerate_paths` documentation for details.
134138
135139
`enumerate_paths!` should be more efficient when used in a loop,
136140
as the same memory can be used for each iteration.
@@ -142,7 +146,8 @@ function enumerate_paths!(
142146
)
143147
Base.require_one_based_indexing(all_paths)
144148
Base.require_one_based_indexing(vs)
145-
@assert length(all_paths) == length(vs)
149+
length(all_paths) == length(vs) ||
150+
throw(ArgumentError("length of destination paths $(length(vs)) deos not match length of vs $(length(all_paths))"))
146151

147152
parents = state.parents
148153
T = eltype(state.parents)
@@ -161,4 +166,4 @@ function enumerate_paths!(
161166
end
162167
end
163168
return all_paths
164-
end
169+
end

test/shortestpaths/bellman-ford.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@testset "Bellman Ford" begin
1+
# @testset "Bellman Ford" begin
22
g4 = path_digraph(5)
33

44
d1 = float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0])
@@ -56,12 +56,15 @@
5656

5757
d3 = [CustomReal(i, 3) for i in d1]
5858
d4 = sparse(d3)
59+
g = first(test_generic_graphs(g4))
5960
for g in test_generic_graphs(g4)
6061
y = @inferred(bellman_ford_shortest_paths(g, 2, d3))
6162
z = @inferred(bellman_ford_shortest_paths(g, 2, d4))
6263
@test getfield.(y.dists, :val) == getfield.(z.dists, :val) == [Inf, 0, 6, 17, 33]
6364
@test @inferred(enumerate_paths(z))[2] == []
6465
@test @inferred(enumerate_paths(z))[4] == enumerate_paths(z, 4) == [2, 3, 4]
66+
@test @inferred(enumerate_paths!([[0]], z, 4:4))[1] == [2, 3, 4]
67+
@test_throws ArgumentError enumerate_paths!([[0, 0], [0, 0]], z, 4:4)
6568
@test @inferred(!has_negative_edge_cycle(g))
6669
@test @inferred(!has_negative_edge_cycle(g, d3))
6770

0 commit comments

Comments
 (0)