Repository navigation
Expand file tree
/
Copy pathbase.go
More file actions
603 lines (511 loc) · 15.8 KB
/
Copy pathbase.go
File metadata and controls
603 lines (511 loc) · 15.8 KB
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
package gograph
import (
"cmp"
"math"
"slices"
"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. Use a lock around every call when any goroutine modifies the
// graph; otherwise the results are undefined and the program may crash.
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]
// order keeps the vertices in the order they were added. A removed
// vertex leaves a nil slot, and removedCount counts those slots
// until removeFromOrder drops them.
order []*Vertex[T]
removedCount int
properties GraphProperties
verticesCount uint32
edgesCount uint32
nextEdge int
// search numbers the searches that mark vertices, and stack, behind and
// places are their reused work lists.
search uint32
stack []*Vertex[T]
behind []*Vertex[T]
places []int
// ord is the topological order of an acyclic graph, indexed by vertex
// position: every edge u -> v has ord[u.position] < ord[v.position].
// It is in use while it has one entry per slot of order, and is built
// again otherwise. first and last are at or beyond the lowest and the
// highest place in use, and the order is built again before either one
// could overflow.
ord []int
first, last int
}
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...)
g.nextEdge++
edge.added = g.nextEdge
dests := g.edges[from.label]
if dests == nil {
dests = make(map[T]*Edge[T])
g.edges[from.label] = dests
}
dests[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. A self-loop is stored once.
//
// It creates the input vertices if they don't exist in the graph, the
// same way AddVertex does, so a vertex from another graph is copied.
// 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
}
from = g.storedVertex(from)
to = g.storedVertex(to)
// prevent edge-multiplicity
if _, ok := g.edges[from.label][to.label]; ok {
return nil, ErrEdgeAlreadyExists
}
// in an acyclic graph, place the new edge in the topological order, or
// reject it if it closes a cycle
if g.properties.isAcyclic && !g.placeEdge(from, to) {
return nil, ErrDAGCycle
}
// add the edge from "to" to "from" too, if graph is undirected.
if !g.properties.isDirected && from != to {
g.link(to, from, options...)
}
return g.link(from, to, options...), nil
}
// link stores the edge from "from" to "to" and returns it.
func (g *baseGraph[T]) link(from, to *Vertex[T], options ...EdgeOptionFunc) *Edge[T] {
from.neighbors = append(from.neighbors, to)
edge := g.addToEdgeMap(from, to, options...)
to.incoming = append(to.incoming, edge)
return edge
}
// 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] {
v := g.addVertex(NewVertex(label, options...))
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.
//
// A vertex belongs to one graph. If the input vertex has already been
// added to a graph, including this one before it was removed, the graph
// stores a copy with the same label, weight and metadata, and no edges.
func (g *baseGraph[T]) AddVertex(v *Vertex[T]) {
if v == nil {
return
}
g.addVertex(v)
}
// storedVertex returns the vertex of the graph with the label of v. If there
// isn't one, it adds v the same way AddVertex does.
func (g *baseGraph[T]) storedVertex(v *Vertex[T]) *Vertex[T] {
if stored := g.vertices[v.label]; stored != nil {
return stored
}
return g.addVertex(v)
}
func (g *baseGraph[T]) addVertex(v *Vertex[T]) *Vertex[T] {
if _, ok := g.vertices[v.label]; ok {
return nil
}
if v.stored {
v = &Vertex[T]{label: v.label, properties: v.properties, metadata: v.metadata}
}
v.stored = true
g.vertices[v.label] = v
v.position = len(g.order)
g.order = append(g.order, v)
if g.properties.isAcyclic && len(g.ord) == v.position && g.last < math.MaxInt {
g.last++
g.ord = append(g.ord, g.last)
}
atomic.AddUint32(&g.verticesCount, 1)
return v
}
// removeFromOrder clears the slot of v in the insertion order. Once more
// than half of the slots are empty, it moves the remaining vertices to the
// front, so removals stay O(1) amortized.
func (g *baseGraph[T]) removeFromOrder(v *Vertex[T]) {
g.order[v.position] = nil
g.removedCount++
if g.removedCount <= len(g.order)/2 {
return
}
n := 0
ordered := len(g.ord) == len(g.order)
for i, u := range g.order {
if u != nil {
u.position = n
g.order[n] = u
if ordered {
g.ord[n] = g.ord[i]
}
n++
}
}
clear(g.order[n:])
g.order = g.order[:n]
if ordered {
g.ord = g.ord[:n]
} else {
g.ord = nil
}
g.removedCount = 0
}
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, or if both vertices are the same, 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 nil.
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() && from.label != to.label {
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 nil.
//
// The edges that start from the vertex come first, in the order they were
// added. The edges that end at the vertex follow, in the order of their
// source vertices in GetAllVertices.
//
// 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
}
vertex := g.findVertex(v.label)
if vertex == nil {
return nil
}
n := len(vertex.neighbors) + len(vertex.incoming)
if n == 0 {
return nil
}
// find all the edges that start from the input vertex
edges := g.outgoingEdges(vertex, make([]*Edge[T], 0, n))
// the edges that end at the input vertex, except a self-loop, which is
// already listed, in the order of their source vertices
out := len(edges)
for _, edge := range vertex.incoming {
if edge.source != vertex {
edges = append(edges, edge)
}
}
slices.SortFunc(edges[out:], func(a, b *Edge[T]) int {
return cmp.Compare(a.source.position, b.source.position)
})
return edges
}
// outgoingEdges appends the edges that start from v to out, in the order
// of its neighbors, and returns the result.
func (g *baseGraph[T]) outgoingEdges(v *Vertex[T], out []*Edge[T]) []*Edge[T] {
destMap := g.edges[v.label]
selfLoop := false
for _, neighbor := range v.neighbors {
// list a self-loop once, even if v is in its own neighbor list twice
if neighbor.label == v.label {
if selfLoop {
continue
}
selfLoop = true
}
if edge, ok := destMap[neighbor.label]; ok {
out = append(out, edge)
}
}
return out
}
// 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() && edge.source.label != edge.dest.label {
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.
// It does nothing if the graph doesn't contain the edge.
func (g *baseGraph[T]) removeEdge(edge *Edge[T]) {
destMap, ok := g.edges[edge.source.label]
if !ok {
return
}
stored, ok := destMap[edge.dest.label]
if !ok {
return
}
g.unlink(stored)
}
// unlink removes a stored edge from the edge map, from the neighbors of its
// source and from the incoming edges of its destination.
func (g *baseGraph[T]) unlink(edge *Edge[T]) {
destMap := g.edges[edge.source.label]
delete(destMap, 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))
removeNeighbor(edge.source, edge.dest)
removeIncoming(edge)
}
// removeNeighbor removes neighbor from the neighbors of source, keeping the
// order of the others.
func removeNeighbor[T comparable](source, neighbor *Vertex[T]) {
source.neighbors = removeItem(source.neighbors, neighbor)
}
// removeIncoming removes edge from the incoming edges of its destination.
func removeIncoming[T comparable](edge *Edge[T]) {
edge.dest.incoming = removeItem(edge.dest.incoming, edge)
}
// removeItem removes the first x from list, keeping the order of the others,
// and clears the freed slot. It checks both ends first, so removing the items
// in the order they were added, or in reverse, takes O(1) time per item.
func removeItem[E comparable](list []E, x E) []E {
var zero E
switch n := len(list); {
case n == 0:
return list
case list[0] == x:
list[0] = zero
return list[1:]
case list[n-1] == x:
list[n-1] = zero
return list[:n-1]
}
if i := slices.Index(list, x); i >= 0 {
return slices.Delete(list, i, i+1)
}
return list
}
// 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, in
// the order they were added. A vertex that is removed and added again
// moves to the end.
func (g *baseGraph[T]) GetAllVertices() []*Vertex[T] {
if len(g.vertices) == 0 {
return nil
}
vertices := make([]*Vertex[T], 0, len(g.vertices))
for _, vertex := range g.order {
if vertex != nil {
vertices = append(vertices, vertex)
}
}
return vertices
}
// RemoveVertices removes all the specified vertices from this graph including
// all its touching edges if present. A removed vertex reports no neighbors
// and an in-degree of 0.
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
}
g.removeFromOrder(v)
// the edges that end at v, except a self-loop, which starts at v too
for _, edge := range v.incoming {
if edge.source != v {
destMap := g.edges[edge.source.label]
delete(destMap, v.label)
if len(destMap) == 0 {
delete(g.edges, edge.source.label)
}
removeNeighbor(edge.source, v)
atomic.AddUint32(&g.edgesCount, ^(uint32(1) - 1))
}
}
// the edges that start from v
for _, edge := range g.edges[v.label] {
if edge.dest != v {
removeIncoming(edge)
}
atomic.AddUint32(&g.edgesCount, ^(uint32(1) - 1))
}
delete(g.edges, v.label)
delete(g.vertices, v.label)
atomic.AddUint32(&g.verticesCount, ^(uint32(1) - 1))
v.neighbors = nil
v.incoming = nil
}
// 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
}
// AllEdges returns all the edges in the graph. The edges are grouped by
// source vertex in the order of GetAllVertices, and the edges of each
// source vertex are in the order they were added.
func (g *baseGraph[T]) AllEdges() []*Edge[T] {
var out []*Edge[T]
for _, vertex := range g.order {
if vertex != nil {
out = g.outgoingEdges(vertex, out)
}
}
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)
}