-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbase.go
453 lines (372 loc) · 11 KB
/
base.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package gograph
import "sync/atomic"
// baseGraph represents a basic implementation of Graph interface. It
// supports multiple types of graph.
//
// This implementation is not safe for concurrent read/write from different
// goroutines. If two goroutines try to modify the same graph it raises panic.
type baseGraph[T comparable] struct {
// vertices is a map of vertices of the graph. the key of the map
// is the vertex label.
vertices map[T]*Vertex[T]
// edges represents the edge between two vertices. The key of the
// first map is the label of source vertex and the key of the inner
// map is the label of destination vertex.
edges map[T]map[T]*Edge[T]
properties GraphProperties
verticesCount uint32
edgesCount uint32
}
func newBaseGraph[T comparable](properties GraphProperties) *baseGraph[T] {
return &baseGraph[T]{
vertices: make(map[T]*Vertex[T]),
edges: make(map[T]map[T]*Edge[T]),
properties: properties,
}
}
// addToEdgeMap creates a new edge struct and adds it to the edges map inside
// the baseGraph struct. Note that it doesn't add the neighbor to the source vertex.
//
// It returns the created edge.
func (g *baseGraph[T]) addToEdgeMap(from, to *Vertex[T], options ...EdgeOptionFunc) *Edge[T] {
edge := NewEdge(from, to, options...)
if _, ok := g.edges[from.label]; !ok {
g.edges[from.label] = map[T]*Edge[T]{to.label: edge}
} else {
g.edges[from.label][to.label] = edge
}
atomic.AddUint32(&g.edgesCount, 1)
return edge
}
// AddEdge adds and edge from the vertex with the 'from' label to
// the vertex with the 'to' label by appending the 'to' vertex to the
// 'neighbors' slice of the 'from' vertex, in directed graph.
//
// In undirected graph, it creates edges in both directions between
// the specified vertices.
//
// It creates the input vertices if they don't exist in the graph.
// If any of the specified vertices is nil, returns nil.
// If edge already exist, returns error.
func (g *baseGraph[T]) AddEdge(from, to *Vertex[T], options ...EdgeOptionFunc) (*Edge[T], error) {
if from == nil || to == nil {
return nil, ErrNilVertices
}
if g.findVertex(from.label) == nil {
g.AddVertex(from)
}
if g.findVertex(to.label) == nil {
g.AddVertex(to)
}
// prevent edge-multiplicity
if g.ContainsEdge(from, to) {
return nil, ErrEdgeAlreadyExists
}
from = g.vertices[from.label]
to = g.vertices[to.label]
from.neighbors = append(from.neighbors, to)
to.inDegree++
// prevent cycle creation, if graph is acyclic
if g.properties.isAcyclic {
// If topological sort returns an error, new edges created a cycle
_, err := TopologySort[T](g)
if err != nil {
// Remove the new edges
from.neighbors = from.neighbors[:len(from.neighbors)-1]
to.inDegree--
return nil, ErrDAGCycle
}
}
// add "from" to the "to" vertex neighbor slice, if graph is undirected.
if !g.properties.isDirected {
to.neighbors = append(to.neighbors, from)
from.inDegree++
g.addToEdgeMap(to, from, options...)
}
return g.addToEdgeMap(from, to, options...), nil
}
// AddVertexByLabel adds a new vertex with the given label to the graph.
// Label of the vertex is a comparable type. This method also accepts the
// vertex properties such as weight.
//
// If there is a vertex with the same label in the graph, returns nil.
// Otherwise, returns the created vertex.
func (g *baseGraph[T]) AddVertexByLabel(label T, options ...VertexOptionFunc) *Vertex[T] {
var properties VertexProperties
for _, option := range options {
option(&properties)
}
v := g.addVertex(&Vertex[T]{label: label, properties: properties})
return v
}
// AddVertex adds the input vertex to the graph. It doesn't add
// vertex to the graph if the input vertex label is already exists
// in the graph.
func (g *baseGraph[T]) AddVertex(v *Vertex[T]) {
if v == nil {
return
}
g.addVertex(v)
}
func (g *baseGraph[T]) addVertex(v *Vertex[T]) *Vertex[T] {
if _, ok := g.vertices[v.label]; ok {
return nil
}
g.vertices[v.label] = v
atomic.AddUint32(&g.verticesCount, 1)
return v
}
func (g *baseGraph[T]) findVertex(label T) *Vertex[T] {
return g.vertices[label]
}
// GetAllEdges returns a slice of all edges connecting source vertex to
// target vertex if such vertices exist in this graph.
//
// In directed graph, it returns a single edge.
//
// If any of the specified vertices is nil, returns nil.
// If any of the vertices does not exist, returns nil.
// If both vertices exist but no edges found, returns an empty set.
func (g *baseGraph[T]) GetAllEdges(from, to *Vertex[T]) []*Edge[T] {
if from == nil || to == nil {
return nil
}
if g.findVertex(from.label) == nil {
return nil
}
if g.findVertex(to.label) == nil {
return nil
}
var edges []*Edge[T]
if destMap, ok := g.edges[from.label]; ok {
if edge, ok := destMap[to.label]; ok {
edges = append(edges, edge)
}
}
if !g.IsDirected() {
if destMap, ok := g.edges[to.label]; ok {
if edge, ok := destMap[from.label]; ok {
edges = append(edges, edge)
}
}
}
return edges
}
// GetEdge returns an edge connecting source vertex to target vertex
// if such vertices and such edge exist in this graph.
//
// In undirected graph, returns only the edge from the "from" vertex to
// the "to" vertex.
//
// If any of the specified vertices is nil, returns nil.
// If edge does not exist, returns nil.
func (g *baseGraph[T]) GetEdge(from, to *Vertex[T]) *Edge[T] {
if from == nil || to == nil {
return nil
}
if g.findVertex(from.label) == nil {
return nil
}
if g.findVertex(to.label) == nil {
return nil
}
if destMap, ok := g.edges[from.label]; ok {
return destMap[to.label]
}
return nil
}
// EdgesOf returns a slice of all edges touching the specified vertex.
// If no edges are touching the specified vertex returns an empty slice.
//
// If the input vertex is nil, returns nil.
// If the input vertex does not exist, returns nil.
func (g *baseGraph[T]) EdgesOf(v *Vertex[T]) []*Edge[T] {
if v == nil {
return nil
}
if g.findVertex(v.label) == nil {
return nil
}
var edges []*Edge[T]
// find all the edges that start from the input vertex
if destMap, ok := g.edges[v.label]; ok {
for destID := range destMap {
edges = append(edges, destMap[destID])
}
}
// find all the edges that the input vertex is the
// destination of the edge
for sourceID, destMap := range g.edges {
if sourceID == v.label {
continue
}
for destID := range destMap {
if destID == v.label {
edges = append(edges, destMap[destID])
}
}
}
return edges
}
// RemoveEdges removes input edges from the graph from the specified
// slice of edges, if they exist.
func (g *baseGraph[T]) RemoveEdges(edges ...*Edge[T]) {
for i := range edges {
g.removeAllEdges(edges[i])
}
}
// removeAllEdges removes edges in both directions between the
// source and dest vertices in the specified edge, if the graph
// is undirected. Otherwise, removes the edge from the source to
// the dest only.
func (g *baseGraph[T]) removeAllEdges(edge *Edge[T]) {
if edge == nil {
return
}
if edge.source == nil || g.findVertex(edge.source.label) == nil {
return
}
if edge.dest == nil || g.findVertex(edge.dest.label) == nil {
return
}
g.removeEdge(edge)
if !g.IsDirected() {
g.removeEdge(NewEdge(edge.dest, edge.source))
}
}
// removeEdge removes the edge from edges destination map, if size of
// the internal map is zero, removes the source label from the edges.
func (g *baseGraph[T]) removeEdge(edge *Edge[T]) {
if destMap, ok := g.edges[edge.source.label]; ok {
delete(destMap, edge.dest.label)
// remove the neighbor vertex from the source neighbors slice.
g.removeNeighbor(edge.source.label, edge.dest.label)
// remove the source vertex label from the edge map, if it
// doesn't have any edges.
if len(destMap) == 0 {
delete(g.edges, edge.source.label)
}
atomic.AddUint32(&g.edgesCount, ^(uint32(1) - 1))
}
}
func (g *baseGraph[T]) removeNeighbor(sourceID, neighborLbl T) {
source := g.findVertex(sourceID)
for i := range source.neighbors {
if source.neighbors[i].label == neighborLbl {
source.neighbors[i].inDegree--
if i == 0 {
source.neighbors = source.neighbors[1:]
} else if i == len(source.neighbors)-1 {
source.neighbors = source.neighbors[:len(source.neighbors)-1]
} else {
source.neighbors = append(source.neighbors[:i], source.neighbors[i+1:]...)
}
break
}
}
}
// GetVertexByID returns the vertex with the input label.
//
// If vertex doesn't exist, returns nil.
func (g *baseGraph[T]) GetVertexByID(label T) *Vertex[T] {
return g.findVertex(label)
}
// GetAllVerticesByID returns a slice of vertices with the specified label list.
//
// If vertex doesn't exist, doesn't add nil to the output list.
func (g *baseGraph[T]) GetAllVerticesByID(idList ...T) []*Vertex[T] {
var vertices []*Vertex[T]
for _, label := range idList {
v := g.GetVertexByID(label)
if v != nil {
vertices = append(vertices, v)
}
}
return vertices
}
// GetAllVertices returns a slice of all existing vertices in the graph.
func (g *baseGraph[T]) GetAllVertices() []*Vertex[T] {
var vertices []*Vertex[T]
for _, vertex := range g.vertices {
vertices = append(vertices, vertex)
}
return vertices
}
// RemoveVertices removes all the specified vertices from this graph including
// all its touching edges if present.
func (g *baseGraph[T]) RemoveVertices(vertices ...*Vertex[T]) {
for i := range vertices {
g.removeVertex(vertices[i])
}
}
func (g *baseGraph[T]) removeVertex(in *Vertex[T]) {
if in == nil {
return
}
v := g.findVertex(in.label)
if v == nil {
return
}
if g.IsDirected() {
for i := range v.neighbors {
v.neighbors[i].inDegree--
}
}
for sourceID := range g.edges {
if edge, ok := g.edges[sourceID][v.label]; ok {
g.removeAllEdges(edge)
delete(g.edges[sourceID], v.label)
}
}
delete(g.edges, v.label)
delete(g.vertices, v.label)
atomic.AddUint32(&g.verticesCount, ^(uint32(1) - 1))
}
// ContainsEdge returns 'true' if and only if this graph contains an edge
// going from the source vertex to the target vertex.
//
// If any of the specified vertices does not exist in the graph, or if is nil,
// returns 'false'.
func (g *baseGraph[T]) ContainsEdge(from, to *Vertex[T]) bool {
if from == nil || to == nil {
return false
}
if g.findVertex(from.label) == nil {
return false
}
if g.findVertex(to.label) == nil {
return false
}
if destMap, ok := g.edges[from.label]; ok {
if _, ok = destMap[to.label]; ok {
return true
}
}
return false
}
// ContainsVertex returns 'true' if this graph contains the specified vertex.
//
// If the specified vertex is nil, returns 'false'.
func (g *baseGraph[T]) ContainsVertex(v *Vertex[T]) bool {
if v == nil {
return false
}
return g.findVertex(v.label) != nil
}
func (g *baseGraph[T]) AllEdges() []*Edge[T] {
var out []*Edge[T]
for _, dest := range g.edges {
for _, edge := range dest {
out = append(out, edge)
}
}
return out
}
// Order returns the number of vertices in the graph.
func (g *baseGraph[T]) Order() uint32 {
return atomic.LoadUint32(&g.verticesCount)
}
// Size returns the number of edges in the graph
func (g *baseGraph[T]) Size() uint32 {
return atomic.LoadUint32(&g.edgesCount)
}