-
Notifications
You must be signed in to change notification settings - Fork 33
/
graph.go
227 lines (211 loc) · 5.46 KB
/
graph.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
package dijkstra
// Graph contains all the graph details (verticies and arcs)
type Graph struct {
//slice of all verticies available
// maps need to be initialised so if vertexArcs[i] == nil then vertex i
// does not exist
vertexArcs []map[int]uint64
}
// NewGraph creates a new empty graph
func NewGraph() Graph {
new := Graph{}
return new
}
// AddNewVertex adds a new vertex at the next available index
func (g *Graph) AddNewEmptyVertex() (index int) {
for i, v := range g.vertexArcs {
if v == nil {
g.vertexArcs[i] = map[int]uint64{}
return i
}
}
index = len(g.vertexArcs)
//only error can be id colision
_ = g.AddEmptyVertex(index)
return index
}
// AddVertex adds a single vertex at the specified index
func (g *Graph) AddEmptyVertex(index int) error {
if err := g.vertexAvailable(index); err != nil {
return err
}
g.addVertex(index, map[int]uint64{})
return nil
}
func (g Graph) vertexOK(index int) error {
if index < 0 {
return newErrVertexNegative(index)
}
return nil
}
func (g Graph) vertexExists(index int) error {
if index >= len(g.vertexArcs) ||
g.vertexArcs[index] == nil {
return newErrVertexNotFound(index)
}
return nil
}
func (g Graph) vertexAvailable(index int) error {
if err := g.vertexOK(index); err != nil {
return err
}
if index < len(g.vertexArcs) && g.vertexArcs[index] != nil {
return newErrVertexAlreadyExists(index)
}
return nil
}
func (g Graph) vertexValid(index int) error {
if err := g.vertexOK(index); err != nil {
return err
}
if err := g.vertexExists(index); err != nil {
return err
}
return nil
}
// GetArc gets the arc distance from src to dest
func (g Graph) GetArc(src, dest int) (uint64, error) {
err := g.vertexValid(src)
if err != nil {
return 0, err
}
err = g.vertexValid(dest)
if err != nil {
return 0, err
}
got, ok := g.vertexArcs[src][dest]
if !ok {
return 0, newErrArcNotFound(src, dest)
}
return got, nil
}
// AddVertex adds or overwrites 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 (g *Graph) AddVertex(index int, vertexArcs map[int]uint64) error {
if err := g.vertexOK(index); err != nil {
return err
}
for to := range vertexArcs {
if err := g.vertexOK(to); err != nil {
return err
}
if err := g.vertexExists(to); err != nil {
return newErrArcNotFound(index, to)
}
}
g.addVertex(index, vertexArcs)
return nil
}
// AddVertexAndArcs adds or overwrites a vertex with specified arcs, if the destination of an
// arc does not exist, it will be added.
func (g *Graph) AddVertexAndArcs(index int, vertexArcs map[int]uint64) error {
var err error
if err = g.vertexOK(index); err != nil {
return err
}
for to := range vertexArcs {
if err = g.vertexOK(to); err != nil {
return err
}
if err = g.vertexExists(to); err != nil {
if err = g.AddEmptyVertex(to); err != nil {
return err
}
}
}
g.addVertex(index, vertexArcs)
return nil
}
func (g *Graph) addVertex(index int, vertexArcs map[int]uint64) {
if index >= len(g.vertexArcs) {
if index == len(g.vertexArcs) {
g.vertexArcs = append(g.vertexArcs, vertexArcs)
} else {
g.vertexArcs = append(g.vertexArcs, make([]map[int]uint64, index-len(g.vertexArcs)+1)...)
}
}
g.vertexArcs[index] = vertexArcs
}
// AddArc adds an arc from src to dest with the specified distance
// will error if the arc already exists (remove then add)
func (g *Graph) AddArc(src, dest int, distance uint64) error {
if src >= len(g.vertexArcs) || g.vertexArcs[src] == nil {
return newErrVertexNotFound(src)
}
if src < 0 {
return newErrVertexNegative(src)
}
if dest >= len(g.vertexArcs) || g.vertexArcs[dest] == nil {
return newErrVertexNotFound(dest)
}
if dest < 0 {
return newErrVertexNegative(dest)
}
g.vertexArcs[src][dest] = distance
return nil
}
// RemoveArc removes the arc from src to dest
func (g *Graph) RemoveArc(src, dest int) error {
if src >= len(g.vertexArcs) || g.vertexArcs[src] == nil {
return newErrVertexNotFound(src)
}
if src < 0 {
return newErrVertexNegative(src)
}
if dest >= len(g.vertexArcs) || g.vertexArcs[dest] == nil {
return newErrVertexNotFound(dest)
}
if dest < 0 {
return newErrVertexNegative(dest)
}
if _, ok := g.vertexArcs[src][dest]; !ok {
return newErrArcNotFound(src, dest)
}
delete(g.vertexArcs[src], dest)
return nil
}
// GetVertexArcs gets all the arcs from vertex index
func (g Graph) GetVertexArcs(index int) (map[int]uint64, error) {
if err := g.vertexValid(index); err != nil {
return nil, err
}
return g.vertexArcs[index], nil
}
// RemoveVertexAndArcs removes the vertex at index and all arcs pointing to it
func (g *Graph) RemoveVertexAndArcs(index int) error {
if err := g.vertexValid(index); err != nil {
return err
}
for i := range g.vertexArcs {
delete(g.vertexArcs[i], index)
}
g.vertexArcs[index] = nil
return nil
}
// RemoveVertex removes the vertex at index, fails if there are still
// arcs pointing to the vertex. To remove all arcs also, use RemoveVertexAndArcs
func (g *Graph) RemoveVertex(index int) error {
if err := g.vertexValid(index); err != nil {
return err
}
for i, v := range g.vertexArcs {
for to := range v {
if to == index {
return newArcHanging(i, index)
}
}
}
g.vertexArcs[index] = nil
return nil
}
func (g Graph) validate() error {
for vId, vertex := range g.vertexArcs {
for k := range vertex {
if k >= len(g.vertexArcs) || k < 0 {
return newErrGraphNotValid(vId, k)
}
}
}
return nil
}