Skip to content
This repository has been archived by the owner on Apr 26, 2019. It is now read-only.

Commit

Permalink
search: roll distance matrix into paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed May 18, 2015
1 parent 43972a1 commit 495ea35
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions search/floydwarshall.go
Expand Up @@ -46,30 +46,29 @@ func FloydWarshall(g graph.Graph, weight graph.CostFunc) (paths ShortestPaths, o
for i := range dist {
dist[i] = math.Inf(1)
}
m := mat64.NewDense(len(nodes), len(nodes), dist)
paths = ShortestPaths{
nodes: nodes,
indexOf: indexOf,

dist: m,
dist: mat64.NewDense(len(nodes), len(nodes), dist),
next: make([][]int, len(nodes)*len(nodes)),
}
for i, u := range nodes {
m.Set(i, i, 0)
paths.dist.Set(i, i, 0)
for _, v := range from(u) {
j := indexOf[v.ID()]
m.Set(i, j, weight(edgeTo(u, v)))
paths.dist.Set(i, j, weight(edgeTo(u, v)))
paths.set(i, j, j)
}
}

for k := range nodes {
for i := range nodes {
for j := range nodes {
ij := m.At(i, j)
joint := m.At(i, k) + m.At(k, j)
ij := paths.dist.At(i, j)
joint := paths.dist.At(i, k) + paths.dist.At(k, j)
if ij > joint {
m.Set(i, j, joint)
paths.dist.Set(i, j, joint)
paths.set(i, j, paths.at(i, k)...)
} else if i != k && k != j && ij-joint == 0 {
paths.add(i, j, paths.at(i, k)...)
Expand All @@ -80,7 +79,7 @@ func FloydWarshall(g graph.Graph, weight graph.CostFunc) (paths ShortestPaths, o

ok = true
for i := range nodes {
if m.At(i, i) < 0 {
if paths.dist.At(i, i) < 0 {
ok = false
break
}
Expand Down

0 comments on commit 495ea35

Please sign in to comment.