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

Commit

Permalink
search: combine reconstruction recursions
Browse files Browse the repository at this point in the history
I don't think the same work improves readability for the iterative case.
  • Loading branch information
kortschak committed Jun 3, 2015
1 parent 9040bc5 commit 843b492
Showing 1 changed file with 23 additions and 37 deletions.
60 changes: 23 additions & 37 deletions search/shortest.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,62 +226,48 @@ func (p ShortestPaths) AllBetween(u, v graph.Node) (paths [][]graph.Node, weight
return nil, math.Inf(1)
}

switch {
case p.forward:
// Do forward path reconstruction.
seen := make([]bool, len(p.nodes))
paths = p.allBetweenForward(from, to, seen, []graph.Node{p.nodes[from]}, nil)

default:
// Do reverse path reconstruction.
seen := make([]bool, len(p.nodes))
paths = p.allBetweenReverse(from, to, seen, []graph.Node{p.nodes[to]}, nil)
var n graph.Node
if p.forward {
n = u
} else {
n = v
}
seen := make([]bool, len(p.nodes))
paths = p.allBetween(from, to, seen, []graph.Node{n}, nil)

return paths, p.dist.At(from, to)
}

func (p ShortestPaths) allBetweenForward(from, to int, seen []bool, path []graph.Node, paths [][]graph.Node) [][]graph.Node {
seen[from] = true
if from == to {
if path == nil {
return paths
}
return append(paths, path)
func (p ShortestPaths) allBetween(from, to int, seen []bool, path []graph.Node, paths [][]graph.Node) [][]graph.Node {
if p.forward {
seen[from] = true
} else {
seen[to] = true
}
first := true
for _, from := range p.at(from, to) {
if seen[from] {
continue
}
if first {
path = append([]graph.Node(nil), path...)
first = false
}
paths = p.allBetweenForward(from, to, append([]bool(nil), seen...), append(path, p.nodes[from]), paths)
}
return paths
}

func (p ShortestPaths) allBetweenReverse(from, to int, seen []bool, path []graph.Node, paths [][]graph.Node) [][]graph.Node {
seen[to] = true
if from == to {
if path == nil {
return paths
}
reverse(path)
if !p.forward {
reverse(path)
}
return append(paths, path)
}
first := true
for _, to := range p.at(from, to) {
if seen[to] {
for _, n := range p.at(from, to) {
if seen[n] {
continue
}
if first {
path = append([]graph.Node(nil), path...)
first = false
}
paths = p.allBetweenReverse(from, to, append([]bool(nil), seen...), append(path, p.nodes[to]), paths)
if p.forward {
from = n
} else {
to = n
}
paths = p.allBetween(from, to, append([]bool(nil), seen...), append(path, p.nodes[n]), paths)
}
return paths
}
Expand Down

0 comments on commit 843b492

Please sign in to comment.