-
Notifications
You must be signed in to change notification settings - Fork 33
/
mappedGraph.go
312 lines (284 loc) · 7.35 KB
/
mappedGraph.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package dijkstra
import "errors"
type MappedGraph[T comparable] struct {
graph Graph
mapping map[T]int
}
// NewMappedGraph creates a new empty mapped graph
func NewMappedGraph[T comparable]() MappedGraph[T] {
return MappedGraph[T]{
graph: NewGraph(),
mapping: make(map[T]int),
}
}
// AddEmptyVertex adds a single empty vertex
func (mg *MappedGraph[T]) AddEmptyVertex(item T) error {
_, err := mg.addMap(item)
return err
}
// RemoveArc removes the arc between two vertices
func (mg *MappedGraph[T]) RemoveArc(src, dest T) error {
srcIndex, destIndex, err := mg.getMap2(src, dest)
if err != nil {
return err
}
return mg.graph.RemoveArc(srcIndex, destIndex)
}
// AddArc adds an arc between two vertices
func (mg *MappedGraph[T]) AddArc(src, dest T, distance uint64) error {
srcIndex, destIndex, err := mg.getMap2(src, dest)
if err != nil {
return err
}
return mg.graph.AddArc(srcIndex, destIndex, distance)
}
// GetArc gets the distance from src to dest
func (mg MappedGraph[T]) GetArc(src, dest T) (uint64, error) {
var err error
var dist uint64
srcIndex, destIndex, err := mg.getMap2(src, dest)
if err != nil {
return 0, err
}
dist, err = mg.graph.GetArc(srcIndex, destIndex)
if errors.Is(err, ErrArcNotFound) {
return dist, newErrArcNotFound(src, dest)
}
return dist, err
}
// AddVertex adds a vertex with specified arcs, if the destination of an arc
// does not exist, it will error. If arc destinations should be added, use
// AddVertexAndArcs instead
func (mg *MappedGraph[T]) AddVertex(item T, vertexArcs map[T]uint64) error {
index, err := mg.getMap(item)
if err != nil {
return err
}
mappedVertexArcs, err := mg.getMappedMap(vertexArcs)
if err != nil {
return err
}
err = mg.graph.AddVertex(index, mappedVertexArcs)
if errors.Is(err, ErrArcNotFound) {
//this should NEVER happen
return err
}
return err
}
// AddVertexAndArcs adds a vertex with specified arcs, if the destination of an
// arc does not exist, it will be added.
func (mg *MappedGraph[T]) AddVertexAndArcs(item T, vertexArcs map[T]uint64) error {
index, err := mg.getMap(item)
if err != nil {
return err
}
mappedVertexArcs, err := mg.getMappedMapCreate(vertexArcs)
if err != nil {
return err
}
return mg.graph.AddVertexAndArcs(index, mappedVertexArcs)
}
func (mg MappedGraph[T]) getMap2(a, b T) (int, int, error) {
var aid, bid int
var err error
aid, err = mg.getMap(a)
if err != nil {
return aid, bid, err
}
bid, err = mg.getMap(b)
return aid, bid, err
}
func (mg MappedGraph[T]) getMap(item T) (int, error) {
id, ok := mg.mapping[item]
if !ok {
return id, newErrMappedVertexNotFound(item)
}
return id, nil
}
func (mg MappedGraph[T]) getInverseMap(index int) (T, error) {
for k, v := range mg.mapping {
if v == index {
return k, nil
}
}
var t T
return t, newErrMapNotFound(index)
}
func (mg *MappedGraph[T]) addMap(item T) (int, error) {
var id int
var ok bool
id, ok = mg.mapping[item]
if ok {
return id, newErrMappedVertexAlreadyExists(item)
}
id = mg.graph.AddNewEmptyVertex()
mg.mapping[item] = id
return id, nil
}
// AddVertex adds a single vertex
func (mg *MappedGraph[T]) addVertex(item T, vertexArcs map[T]uint64) error {
index, ok := mg.mapping[item]
if ok {
return newErrMappedVertexAlreadyExists(item)
}
realVertex := make(map[int]uint64)
for toID, v := range vertexArcs {
toid, err := mg.getMap(toID)
if err != nil {
return err
}
realVertex[toid] = v
}
return mg.graph.AddVertex(index, realVertex)
}
// GetVertex gets the reference of the specified vertex. An error is thrown if
// there is no vertex with that index/ID.
func (mg *MappedGraph[T]) GetVertexArcs(item T) (map[T]uint64, error) {
var arcs map[int]uint64
id, err := mg.getMap(item)
if err != nil {
return nil, err
}
arcs, err = mg.graph.GetVertexArcs(id)
if err != nil {
return nil, err
}
return mg.getInverseMappedMap(arcs)
}
func (mg *MappedGraph[T]) RemoveVertexAndArcs(item T) error {
index, err := mg.getMap(item)
if err != nil {
return err
}
return mg.graph.RemoveVertexAndArcs(index)
}
func (mg *MappedGraph[T]) RemoveVertex(item T) error {
index, err := mg.getMap(item)
if err != nil {
return err
}
return mg.graph.RemoveVertex(index)
}
func (mg MappedGraph[T]) getInverseMappedMap(arcs map[int]uint64) (map[T]uint64, error) {
mappedArcs := make(map[T]uint64)
for k, v := range arcs {
name, err := mg.getInverseMap(k)
if err != nil {
return mappedArcs, err
}
mappedArcs[name] = v
}
return mappedArcs, nil
}
func (mg MappedGraph[T]) getMappedMap(arcs map[T]uint64) (map[int]uint64, error) {
mappedArcs := make(map[int]uint64)
for k, v := range arcs {
id, err := mg.getMap(k)
if err != nil {
return mappedArcs, err
}
mappedArcs[id] = v
}
return mappedArcs, nil
}
func (mg MappedGraph[T]) getMappedMapCreate(arcs map[T]uint64) (map[int]uint64, error) {
mappedArcs := make(map[int]uint64)
for k, v := range arcs {
if _, ok := mg.mapping[k]; !ok {
_, err := mg.addMap(k)
if err != nil {
return mappedArcs, err
}
}
id, err := mg.getMap(k)
if err != nil {
return mappedArcs, err
}
mappedArcs[id] = v
}
return mappedArcs, nil
}
func (mg MappedGraph[T]) validate() error {
for k, v := range mg.mapping {
if _, err := mg.graph.GetVertexArcs(v); err != nil {
return newErrMappedVertexNotFound(k)
}
}
return mg.graph.validate()
}
func (mg MappedGraph[T]) inverseMapPath(path []int) ([]T, error) {
var err error
var result []T = make([]T, len(path))
for i, v := range path {
result[i], err = mg.getInverseMap(v)
if err != nil {
return result, err
}
}
return result, err
}
func (mg MappedGraph[T]) toMappedBestPath(bp BestPath[int]) (BestPath[T], error) {
var err error
var result BestPath[T]
result.Distance = bp.Distance
result.Path, err = mg.inverseMapPath(bp.Path)
return result, err
}
func (mg MappedGraph[T]) toMappedBestPaths(bp BestPaths[int]) (BestPaths[T], error) {
var err error
var result BestPaths[T]
result.Paths = make([][]T, len(bp.Paths))
for i, v := range bp.Paths {
result.Paths[i], err = mg.inverseMapPath(v)
if err != nil {
return result, err
}
}
return result, err
}
func (mg MappedGraph[T]) evaluate(src, dest T, shortest bool) (BestPath[T], error) {
var bp BestPath[T]
var bpOriginal BestPath[int]
srcId, destId, err := mg.getMap2(src, dest)
if err != nil {
return bp, err
}
if shortest {
bpOriginal, err = mg.graph.Shortest(srcId, destId)
} else {
bpOriginal, err = mg.graph.Longest(srcId, destId)
}
if err != nil {
return bp, err
}
return mg.toMappedBestPath(bpOriginal)
}
func (mg MappedGraph[T]) Shortest(src, dest T) (BestPath[T], error) {
return mg.evaluate(src, dest, true)
}
func (mg MappedGraph[T]) Longest(src, dest T) (BestPath[T], error) {
return mg.evaluate(src, dest, false)
}
func (mg MappedGraph[T]) evaluateAll(src, dest T, shortest bool) (BestPaths[T], error) {
var bp BestPaths[T]
var bpOriginal BestPaths[int]
srcId, destId, err := mg.getMap2(src, dest)
if err != nil {
return bp, err
}
if shortest {
bpOriginal, err = mg.graph.ShortestAll(srcId, destId)
} else {
bpOriginal, err = mg.graph.LongestAll(srcId, destId)
}
if err != nil {
return bp, err
}
return mg.toMappedBestPaths(bpOriginal)
}
func (mg MappedGraph[T]) ShortestAll(src, dest T) (BestPaths[T], error) {
return mg.evaluateAll(src, dest, true)
}
func (mg MappedGraph[T]) LongestAll(src, dest T) (BestPaths[T], error) {
return mg.evaluateAll(src, dest, false)
}