This repository has been archived by the owner on Apr 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
johnson_apsp.go
146 lines (125 loc) · 3.28 KB
/
johnson_apsp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright ©2015 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package search
import (
"math"
"math/rand"
"github.com/gonum/graph"
"github.com/gonum/graph/concrete"
)
// JohnsonAllPaths returns a shortest-path tree for shortest paths in the graph g.
// If weight is nil and the graph does not implement graph.Coster, UniformCost is used.
//
// The time complexity of JohnsonAllPaths is O(|V|.|E|+|V|^2.log|V|).
func JohnsonAllPaths(g graph.Graph, weight graph.CostFunc) (paths AllShortest, ok bool) {
jg := johnsonWeightAdjuster{
g: g,
from: g.Neighbors,
to: g.Neighbors,
weight: weight,
}
switch g := g.(type) {
case graph.DirectedGraph:
jg.from = g.Successors
jg.to = g.Predecessors
jg.edgeTo = g.EdgeTo
default:
jg.edgeTo = g.EdgeBetween
}
if jg.weight == nil {
if g, ok := g.(graph.Coster); ok {
jg.weight = g.Cost
} else {
jg.weight = UniformCost
}
}
paths = newAllShortest(g.NodeList(), false)
sign := -1
for {
// Choose a random node ID until we find
// one that is not in g.
jg.q = sign * rand.Int()
if _, exists := paths.indexOf[jg.q]; !exists {
break
}
sign *= -1
}
jg.bellmanFord = true
jg.adjustBy, ok = BellmanFordFrom(johnsonGraphNode(jg.q), jg, nil)
if !ok {
return paths, false
}
jg.bellmanFord = false
dijkstraAllPaths(jg, nil, paths)
for i, u := range paths.nodes {
hu := jg.adjustBy.WeightTo(u)
for j, v := range paths.nodes {
if i == j {
continue
}
hv := jg.adjustBy.WeightTo(v)
paths.dist.Set(i, j, paths.dist.At(i, j)-hu+hv)
}
}
return paths, ok
}
type johnsonWeightAdjuster struct {
q int
g graph.Graph
from, to func(graph.Node) []graph.Node
edgeTo func(graph.Node, graph.Node) graph.Edge
weight graph.CostFunc
bellmanFord bool
adjustBy Shortest
}
var (
_ graph.DirectedGraph = johnsonWeightAdjuster{}
_ graph.Coster = johnsonWeightAdjuster{}
)
func (g johnsonWeightAdjuster) NodeExists(n graph.Node) bool {
if g.bellmanFord && n.ID() == g.q {
return true
}
return g.g.NodeExists(n)
}
func (g johnsonWeightAdjuster) NodeList() []graph.Node {
if g.bellmanFord {
return append(g.g.NodeList(), johnsonGraphNode(g.q))
}
return g.g.NodeList()
}
func (g johnsonWeightAdjuster) Successors(n graph.Node) []graph.Node {
if g.bellmanFord && n.ID() == g.q {
return g.g.NodeList()
}
return g.from(n)
}
func (g johnsonWeightAdjuster) EdgeTo(u, v graph.Node) graph.Edge {
if g.bellmanFord && u.ID() == g.q && g.g.NodeExists(v) {
return concrete.Edge{johnsonGraphNode(g.q), v}
}
return g.edgeTo(u, v)
}
func (g johnsonWeightAdjuster) Cost(e graph.Edge) float64 {
if g.bellmanFord {
switch g.q {
case e.From().ID():
return 0
case e.To().ID():
return math.Inf(1)
default:
return g.weight(e)
}
}
return g.weight(e) + g.adjustBy.WeightTo(e.From()) - g.adjustBy.WeightTo(e.To())
}
func (johnsonWeightAdjuster) Neighbors(graph.Node) []graph.Node {
panic("search: unintended use of johnsonWeightAdjuster")
}
func (johnsonWeightAdjuster) EdgeBetween(_, _ graph.Node) graph.Edge {
panic("search: unintended use of johnsonWeightAdjuster")
}
func (johnsonWeightAdjuster) Predecessors(graph.Node) []graph.Node {
panic("search: unintended use of johnsonWeightAdjuster")
}