-
Notifications
You must be signed in to change notification settings - Fork 5
/
bidirectional_ch_one_to_n.go
229 lines (190 loc) · 7.36 KB
/
bidirectional_ch_one_to_n.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package ch
import (
"container/heap"
)
func (graph *Graph) initShortestPathOneToMany() (estimateAll []float64, pathAll [][]int64, prev map[int64]int64, prevReverse map[int64]int64, queryDist, revQueryDist []float64, forwProcessed, revProcessed []int64) {
estimateAll = []float64{}
pathAll = [][]int64{}
prev = make(map[int64]int64)
prevReverse = make(map[int64]int64)
queryDist = make([]float64, len(graph.Vertices))
revQueryDist = make([]float64, len(graph.Vertices))
forwProcessed = make([]int64, len(graph.Vertices))
revProcessed = make([]int64, len(graph.Vertices))
return
}
// ShortestPathOneToMany computes and returns shortest paths and theirs's costs (extended Dijkstra's algorithm) between single source and multiple targets
//
// If there are some errors then function returns '-1.0' as cost and nil as shortest path
//
// source - user's definied ID of source vertex
// targets - set of user's definied IDs of target vertices
func (graph *Graph) ShortestPathOneToMany(source int64, targets []int64) ([]float64, [][]int64) {
estimateAll, pathAll, prev, prevReverse, queryDist, revQueryDist, forwProcessed, revProcessed := graph.initShortestPathOneToMany()
var ok bool
if source, ok = graph.mapping[source]; !ok {
estimateAll = append(estimateAll, -1.0)
pathAll = append(pathAll, nil)
return estimateAll, pathAll
}
for idx, target := range targets {
nextQueue := int64(idx) + 1
if source == target {
estimateAll = append(estimateAll, 0)
pathAll = append(pathAll, []int64{source})
continue
}
var ok bool
if target, ok = graph.mapping[target]; !ok {
estimateAll = append(estimateAll, -1.0)
pathAll = append(pathAll, nil)
continue
}
forwProcessed[source] = nextQueue
revProcessed[target] = nextQueue
queryDist[source] = 0
revQueryDist[target] = 0
forwQ := &vertexDistHeap{}
backwQ := &vertexDistHeap{}
heap.Init(forwQ)
heap.Init(backwQ)
heapSource := &vertexDist{
id: source,
dist: 0,
}
heapTarget := &vertexDist{
id: target,
dist: 0,
}
heap.Push(forwQ, heapSource)
heap.Push(backwQ, heapTarget)
estimate, path := graph.shortestPathOneToManyCore(nextQueue, prev, prevReverse, queryDist, revQueryDist, forwProcessed, revProcessed, forwQ, backwQ)
estimateAll = append(estimateAll, estimate)
pathAll = append(pathAll, path)
}
return estimateAll, pathAll
}
func (graph *Graph) shortestPathOneToManyCore(nextQueue int64, prev map[int64]int64, prevReverse map[int64]int64, queryDist, revQueryDist []float64, forwProcessed, revProcessed []int64, forwQ *vertexDistHeap, backwQ *vertexDistHeap) (float64, []int64) {
estimate := Infinity
var middleID int64
for forwQ.Len() != 0 || backwQ.Len() != 0 {
if forwQ.Len() != 0 {
vertex1 := heap.Pop(forwQ).(*vertexDist)
if vertex1.dist <= estimate {
forwProcessed[vertex1.id] = nextQueue
graph.relaxEdgesBiForwardOneToMany(vertex1, forwQ, prev, queryDist, nextQueue, forwProcessed)
}
if revProcessed[vertex1.id] == nextQueue {
if vertex1.dist+revQueryDist[vertex1.id] < estimate {
middleID = vertex1.id
estimate = vertex1.dist + revQueryDist[vertex1.id]
}
}
}
if backwQ.Len() != 0 {
vertex2 := heap.Pop(backwQ).(*vertexDist)
if vertex2.dist <= estimate {
revProcessed[vertex2.id] = nextQueue
graph.relaxEdgesBiBackwardOneToMany(vertex2, backwQ, prevReverse, revQueryDist, nextQueue, revProcessed)
}
if forwProcessed[vertex2.id] == nextQueue {
if vertex2.dist+queryDist[vertex2.id] < estimate {
middleID = vertex2.id
estimate = vertex2.dist + queryDist[vertex2.id]
}
}
}
}
if estimate == Infinity {
return -1, nil
}
return estimate, graph.ComputePath(middleID, prev, prevReverse)
}
// ShortestPathOneToManyWithAlternatives Computes and returns shortest path and it's cost (extended Dijkstra's algorithm) between single source and multiple targets
// with multiple alternatives for source and target vertices with additional distances to reach the vertices
// (useful if source and target are outside of the graph)
//
// If there are some errors then function returns '-1.0' as cost and nil as shortest path
//
// sourceAlternatives - user's definied ID of source vertex with additional penalty
// targetsAlternatives - set of user's definied IDs of target vertices with additional penalty
func (graph *Graph) ShortestPathOneToManyWithAlternatives(sourceAlternatives []VertexAlternative, targetsAlternatives [][]VertexAlternative) ([]float64, [][]int64) {
estimateAll, pathAll, prev, prevReverse, queryDist, revQueryDist, forwProcessed, revProcessed := graph.initShortestPathOneToMany()
sourceAlternativesInternal := graph.vertexAlternativesToInternal(sourceAlternatives)
for idx, targetAlternatives := range targetsAlternatives {
nextQueue := int64(idx) + 1
targetAlternativesInternal := graph.vertexAlternativesToInternal(targetAlternatives)
forwQ := &vertexDistHeap{}
backwQ := &vertexDistHeap{}
heap.Init(forwQ)
heap.Init(backwQ)
for _, sourceAlternative := range sourceAlternativesInternal {
if sourceAlternative.vertexNum == vertexNotFound {
continue
}
forwProcessed[sourceAlternative.vertexNum] = nextQueue
queryDist[sourceAlternative.vertexNum] = sourceAlternative.additionalDistance
heapSource := &vertexDist{
id: sourceAlternative.vertexNum,
dist: sourceAlternative.additionalDistance,
}
heap.Push(forwQ, heapSource)
}
for _, targetAlternative := range targetAlternativesInternal {
if targetAlternative.vertexNum == vertexNotFound {
continue
}
revProcessed[targetAlternative.vertexNum] = nextQueue
revQueryDist[targetAlternative.vertexNum] = targetAlternative.additionalDistance
heapTarget := &vertexDist{
id: targetAlternative.vertexNum,
dist: targetAlternative.additionalDistance,
}
heap.Push(backwQ, heapTarget)
}
estimate, path := graph.shortestPathOneToManyCore(nextQueue, prev, prevReverse, queryDist, revQueryDist, forwProcessed, revProcessed, forwQ, backwQ)
estimateAll = append(estimateAll, estimate)
pathAll = append(pathAll, path)
}
return estimateAll, pathAll
}
func (graph *Graph) relaxEdgesBiForwardOneToMany(vertex *vertexDist, forwQ *vertexDistHeap, prev map[int64]int64, queryDist []float64, cid int64, forwProcessed []int64) {
vertexList := graph.Vertices[vertex.id].outIncidentEdges
for i := range vertexList {
temp := vertexList[i].vertexID
cost := vertexList[i].weight
if graph.Vertices[vertex.id].orderPos < graph.Vertices[temp].orderPos {
alt := queryDist[vertex.id] + cost
if forwProcessed[temp] != cid || queryDist[temp] > alt {
queryDist[temp] = alt
prev[temp] = vertex.id
forwProcessed[temp] = cid
node := &vertexDist{
id: temp,
dist: alt,
}
heap.Push(forwQ, node)
}
}
}
}
func (graph *Graph) relaxEdgesBiBackwardOneToMany(vertex *vertexDist, backwQ *vertexDistHeap, prev map[int64]int64, revQueryDist []float64, cid int64, revProcessed []int64) {
vertexList := graph.Vertices[vertex.id].inIncidentEdges
for i := range vertexList {
temp := vertexList[i].vertexID
cost := vertexList[i].weight
if graph.Vertices[vertex.id].orderPos < graph.Vertices[temp].orderPos {
alt := revQueryDist[vertex.id] + cost
if revProcessed[temp] != cid || revQueryDist[temp] > alt {
revQueryDist[temp] = alt
prev[temp] = vertex.id
revProcessed[temp] = cid
node := &vertexDist{
id: temp,
dist: alt,
}
heap.Push(backwQ, node)
}
}
}
}