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

Commit

Permalink
Use matrices in FloydWarshallState
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahao committed Apr 4, 2015
1 parent 19be9c7 commit 74386dc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
40 changes: 19 additions & 21 deletions src/floyd-warshall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
# > WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


type FloydWarshallState<:AbstractPathState
dists::Vector{Vector{Float64}}
parents::Vector{Vector{Int}}
type FloydWarshallState{T<:Real} <: AbstractPathState
dists::Matrix{T}
parents::Matrix{Int}
end

# @doc doc"""
Expand All @@ -36,17 +36,17 @@ end
# Note that it is possible to consume large amounts of memory as the
# space required for the FloydWarshallState is O(n^2).
# """ ->
function floyd_warshall_shortest_paths(
function floyd_warshall_shortest_paths{T<:Real}(
g::AbstractGraph;
edge_dists::AbstractArray{Float64, 2} = Array(Float64,(0,0))
edge_dists::AbstractArray{T,2} = zeros(0,0)
)

use_dists = issparse(edge_dists)? nnz(edge_dists > 0) : !isempty(edge_dists)
n_v = nv(g)
dists = fill(convert(Float64,Inf), (n_v,n_v))
parents = zeros(Int, (n_v,n_v))
S = typeof(one(T) + one(T))
dists = fill(typemax(S), n_v, n_v)
parents = zeros(Int, n_v, n_v)

fws = FloydWarshallState(Vector{Float64}[], Vector{Int}[])
for v in 1:n_v
dists[v,v] = 0.0
end
Expand All @@ -67,23 +67,21 @@ function floyd_warshall_shortest_paths(
end
end
for w in vertices(g), u in vertices(g), v in vertices(g)
if dists[u,v] > dists[u,w] + dists[w,v]
dists[u,v] = dists[u,w] + dists[w,v]
if dists[u,w] == typemax(S) || dists[w,v] == typemax(S)
continue
end
d = dists[u,w] + dists[w,v]
if dists[u,v] > d
dists[u,v] = d
parents[u,v] = parents[w,v]
end
end
for r in 1:size(parents,1) # row by row
push!(fws.parents, vec(parents[r,:]))
end
for r in 1:size(dists,1)
push!(fws.dists, vec(dists[r,:]))
end

return fws
return FloydWarshallState{S}(dists, parents)
end

function enumerate_paths(s::FloydWarshallState, v::Integer)
pathinfo = s.parents[v]
function enumerate_paths(s::FloydWarshallState, v::Int)
pathinfo = slice(s.parents, v, :)
paths = Vector{Int}[]
for i in 1:length(pathinfo)
if i == v
Expand All @@ -101,5 +99,5 @@ function enumerate_paths(s::FloydWarshallState, v::Integer)
return paths
end

enumerate_paths(s::FloydWarshallState) = [enumerate_paths(s, v) for v in 1:length(s.parents)]
enumerate_paths(st::FloydWarshallState, s::Integer, d::Integer) = enumerate_paths(st, s)[d]
enumerate_paths(s::FloydWarshallState) = [enumerate_paths(s, v) for v in 1:size(s.parents, 1)]
enumerate_paths(st::FloydWarshallState, s::Int, d::Int) = enumerate_paths(st, s)[d]
23 changes: 19 additions & 4 deletions test/floyd-warshall.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
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 = floyd_warshall_shortest_paths(g3; edge_dists=d)
@test z.dists[3] == [7, 6, 0, 11, 27]
@test z.parents[3] == [2, 3, 0, 3, 4]
d = [ 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]

for T in (Int8, Int16, Int32, Int64, Int128,
Float16, Float32, Float64)

z = floyd_warshall_shortest_paths(g3; edge_dists=convert(Matrix{T}, d))
@test z.dists == [
0 1 7 18 34
1 0 6 17 33
7 6 0 11 27
18 17 11 0 16
34 33 27 16 0]
@test z.parents == [
0 1 2 3 4
2 0 2 3 4
2 3 0 3 4
2 3 4 0 4
2 3 4 5 0]
end

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

0 comments on commit 74386dc

Please sign in to comment.