Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
fix bug in shortest path algorithms with use_dists; abstract that fun…
Browse files Browse the repository at this point in the history
…ction into a (non-exported) has_distance() function in distance.jl

forgot about dijkstra_with_pred; added tests
  • Loading branch information
sbromberger committed Apr 6, 2015
1 parent 14b1ef5 commit 1d722bd
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/astar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function a_star_impl!(
heuristic::Function, # heuristic fn (under)estimating distance to target
t::Int) # the end vertex

use_dists = issparse(edge_dists)? nnz(edge_dists > 0) : !isempty(edge_dists)
# has_distances in distance.jl
use_dists = LightGraphs.has_distances(edge_dists)

while !isempty(frontier)
(cost_so_far, path, u) = dequeue!(frontier)
Expand Down
3 changes: 2 additions & 1 deletion src/bellman-ford.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ function bellman_ford_shortest_paths!(
sources::AbstractVector{Int},
state::BellmanFordState)

use_dists = issparse(edge_dists)? nnz(edge_dists > 0) : !isempty(edge_dists)
# has_distances in distance.jl
use_dists = has_distances(edge_dists)

active = Set{Int}()
for v in sources
Expand Down
7 changes: 5 additions & 2 deletions src/dijkstra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ function process_neighbors!(
hmap::Vector{Int} = state.hmap
dv::Float64 = zero(Float64)

use_dists = issparse(edge_dists)? nnz(edge_dists > 0) : !isempty(edge_dists)
# has_distances in distance.jl
use_dists = has_distances(edge_dists)

for e in out_edges(graph, u)
v::Int = dst(e)
v_color::Int = colormap[v]
Expand Down Expand Up @@ -284,7 +286,8 @@ function process_neighbors_with_pred!(
hmap::Vector{Int} = state.hmap
dv::Float64 = zero(Float64)

use_dists = issparse(edge_dists)? nnz(edge_dists > 0) : !isempty(edge_dists)
# has_distances in distance.jl
use_dists = has_distances(edge_dists)

for e in out_edges(graph, u)
v::Int = dst(e)
Expand Down
4 changes: 4 additions & 0 deletions src/distance.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# used in shortest path calculations
has_distances{T}(edge_dists::AbstractArray{T,2}) =
issparse(edge_dists)? (nnz(edge_dists) > 0) : !isempty(edge_dists)

function eccentricity(
g::AbstractGraph,
v::Int;
Expand Down
4 changes: 3 additions & 1 deletion src/floyd-warshall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ function floyd_warshall_shortest_paths(
edge_dists::AbstractArray{Float64, 2} = Array(Float64,(0,0))
)

use_dists = issparse(edge_dists)? nnz(edge_dists > 0) : !isempty(edge_dists)
# has_distances in distance.jl
use_dists = has_distances(edge_dists)

n_v = nv(g)
dists = fill(convert(Float64,Inf), (n_v,n_v))
parents = zeros(Int, (n_v,n_v))
Expand Down
7 changes: 5 additions & 2 deletions test/astar.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
d = 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])
@test a_star(g3, 1, 4; edge_dists=d) == a_star(g4, 1, 4; edge_dists=d)
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])
d2 = sparse(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]))
@test a_star(g3, 1, 4; edge_dists=d1) ==
a_star(g4, 1, 4; edge_dists=d1) ==
a_star(g3, 1, 4, edge_dists=d2)
@test a_star(g4, 4, 1) == nothing
8 changes: 5 additions & 3 deletions test/bellman-ford.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
d = 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])
z = bellman_ford_shortest_paths(g4, 2; edge_dists=d)
@test z.dists == [Inf, 0, 6, 17, 33]
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])
d2 = sparse(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]))
y = bellman_ford_shortest_paths(g4, 2; edge_dists=d1)
z = bellman_ford_shortest_paths(g4, 2; edge_dists=d2)
@test y.dists == z.dists == [Inf, 0, 6, 17, 33]
@test enumerate_paths(z)[2] == []
@test enumerate_paths(z)[4] == enumerate_paths(z,4) == [2,3,4]
@test !has_negative_edge_cycle(g4)
22 changes: 14 additions & 8 deletions test/dijkstra.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
d = 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])
z = dijkstra_shortest_paths(g4, 2; edge_dists=d)
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])
d2 = sparse(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]))

@test z.parents == [0, 0, 2, 3, 4]
@test z.dists == [Inf, 0, 6, 17, 33]
y = dijkstra_shortest_paths(g4, 2; edge_dists=d1)
z = dijkstra_shortest_paths(g4, 2; edge_dists=d2)

z = dijkstra_predecessor_and_distance(g4, 2; edge_dists=d)
@test z.predecessors[3] == [2]
@test y.parents == z.parents == [0, 0, 2, 3, 4]
@test y.dists == z.dists == [Inf, 0, 6, 17, 33]

@test enumerate_paths(z)[2] == []
@test enumerate_paths(z)[4] == enumerate_paths(z,4) == [2,3,4]
y = dijkstra_predecessor_and_distance(g4, 2; edge_dists=d1)
z = dijkstra_predecessor_and_distance(g4, 2; edge_dists=d2)
@test z.predecessors[3] == y.predecessors[3] == [2]

@test enumerate_paths(z) == enumerate_paths(y)
@test enumerate_paths(z)[4] ==
enumerate_paths(z,4) ==
enumerate_paths(y,4) == [2,3,4]

0 comments on commit 1d722bd

Please sign in to comment.