-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkstar.go
106 lines (94 loc) · 2.17 KB
/
kstar.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
package kstar
type kstar struct {
pg *pathGraph
as *astar
d *dijkstra
paths [][]Edge
asExhausted bool
}
func newKstar(g *Graph, pg *pathGraph) kstar {
return kstar{
pg: pg,
as: newAstar(*g),
d: newDijkstra(&pg.r),
paths: make([][]Edge, 0),
}
}
// Run returns the k shortest paths given a Graph implementation and k.
func Run(g Graph, k int) (paths [][]Edge) {
pg := newPathGraph()
ks := newKstar(&g, pg)
paths = make([][]Edge, 0)
tReached := ks.startAstar()
if tReached {
for len(paths) < k {
sigmaPath, empty := ks.d.step()
edgeSeq := buildSeq(sigmaPath)
path := buildPath(edgeSeq, ks.as.searchTreeParents, g.S(), g.T())
paths = append(paths, path)
if empty {
if ks.asExhausted {
break
}
ks.resumeAstar()
end := ks.d.resume()
if end {
break
}
}
}
}
return paths
}
func (ks *kstar) startAstar() (tReached bool) {
newEdges, end := ks.as.run()
if !end {
ks.pg.updateHinNodes(newEdges, ks.as)
ks.pg.generateHts(ks.as)
tHt := ks.pg.ht[ks.as.g.T()]
ks.pg.r.tHt = tHt
}
return !end
}
func (ks *kstar) resumeAstar() {
newEdges, end := ks.as.run()
ks.asExhausted = end
ks.pg.updateHinNodes(newEdges, ks.as)
}
// Transforms a dijkstra path into a sequence of sidetrack edges
func buildSeq(pgPath []*dijkstraNode) (seq []Edge) {
seq = make([]Edge, 0)
if len(pgPath) < 2 {
// length 1 is just R
return seq
}
vdn := pgPath[len(pgPath)-1]
u, v, i := (*vdn).n.EdgeKeys()
seq = append(seq, Edge{U: u, V: v, I: i})
for j := len(pgPath) - 1; j > 0; j-- {
udn := pgPath[j-1]
if vdn.isCross && !udn.isR {
u, v, i = (*udn).n.EdgeKeys()
seq = append(seq, Edge{U: u, V: v, I: i})
}
vdn = udn
}
return seq
}
// Adds tree nodes to the sidetrack edges to complete the path
func buildPath(seq []Edge, spTree map[int]Edge, s, t int) (path []Edge) {
path = make([]Edge, 0)
current := t
for current != s || len(seq) != 0 {
if len(seq) > 0 && seq[len(seq)-1].V == current {
e := seq[len(seq)-1]
path = append(path, e)
seq = seq[:len(seq)-1]
current = e.U
} else {
path = append(path, spTree[current])
current = spTree[current].U
}
}
return path
}