This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
/
weighted.go
497 lines (409 loc) · 11.7 KB
/
weighted.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
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
package gogl
import (
"sync"
"gopkg.in/fatih/set.v0"
)
// This is implemented as an adjacency list, because those are simple.
type baseWeighted struct {
list map[Vertex]map[Vertex]float64
size int
mu sync.RWMutex
}
/* baseWeighted shared methods */
// Traverses the graph's vertices in random order, passing each vertex to the
// provided closure.
func (g *baseWeighted) EachVertex(f VertexLambda) {
g.mu.RLock()
defer g.mu.RUnlock()
for v := range g.list {
if f(v) {
return
}
}
}
// Indicates whether or not the given vertex is present in the graph.
func (g *baseWeighted) HasVertex(vertex Vertex) (exists bool) {
g.mu.RLock()
defer g.mu.RUnlock()
exists = g.hasVertex(vertex)
return
}
// Indicates whether or not the given vertex is present in the graph.
func (g *baseWeighted) hasVertex(vertex Vertex) (exists bool) {
_, exists = g.list[vertex]
return
}
// Returns the order (number of vertices) in the graph.
func (g *baseWeighted) Order() int {
g.mu.RLock()
defer g.mu.RUnlock()
return len(g.list)
}
// Returns the size (number of edges) in the graph.
func (g *baseWeighted) Size() int {
return g.size
}
// Adds the provided vertices to the graph. If a provided vertex is
// already present in the graph, it is a no-op (for that vertex only).
func (g *baseWeighted) EnsureVertex(vertices ...Vertex) {
if len(vertices) == 0 {
return
}
g.mu.Lock()
defer g.mu.Unlock()
g.ensureVertex(vertices...)
}
// Adds the provided vertices to the graph. If a provided vertex is
// already present in the graph, it is a no-op (for that vertex only).
func (g *baseWeighted) ensureVertex(vertices ...Vertex) {
for _, vertex := range vertices {
if !g.hasVertex(vertex) {
// TODO experiment with different lengths...possibly by analyzing existing density?
g.list[vertex] = make(map[Vertex]float64, 10)
}
}
return
}
/* DirectedWeighted implementation */
type weightedDirected struct {
baseWeighted
}
// Returns the outdegree of the provided vertex. If the vertex is not present in the
// graph, the second return value will be false.
func (g *weightedDirected) OutDegreeOf(vertex Vertex) (degree int, exists bool) {
g.mu.RLock()
defer g.mu.RUnlock()
if exists = g.hasVertex(vertex); exists {
degree = len(g.list[vertex])
}
return
}
// Returns the indegree of the provided vertex. If the vertex is not present in the
// graph, the second return value will be false.
//
// Note that getting indegree is inefficient for directed adjacency lists; it requires
// a full scan of the graph's edge set.
func (g *weightedDirected) InDegreeOf(vertex Vertex) (degree int, exists bool) {
g.mu.RLock()
defer g.mu.RUnlock()
return inDegreeOf(g, vertex)
}
// Returns the degree of the given vertex, counting both in and out-edges.
func (g *weightedDirected) DegreeOf(vertex Vertex) (degree int, exists bool) {
g.mu.RLock()
defer g.mu.RUnlock()
indegree, exists := inDegreeOf(g, vertex)
outdegree, exists := g.OutDegreeOf(vertex)
return indegree + outdegree, exists
}
// Enumerates the set of all edges incident to the provided vertex.
func (g *weightedDirected) EachEdgeIncidentTo(v Vertex, f EdgeLambda) {
g.mu.RLock()
defer g.mu.RUnlock()
eachEdgeIncidentToDirected(g, v, f)
}
// Enumerates the vertices adjacent to the provided vertex.
func (g *weightedDirected) EachAdjacentTo(start Vertex, f VertexLambda) {
g.mu.RLock()
defer g.mu.RUnlock()
g.EachEdgeIncidentTo(start, func(e Edge) bool {
u, v := e.Both()
if u == start {
return f(v)
} else {
return f(u)
}
})
}
// Enumerates the set of out-edges for the provided vertex.
func (g *weightedDirected) EachArcFrom(v Vertex, f EdgeLambda) {
g.mu.RLock()
defer g.mu.RUnlock()
if !g.hasVertex(v) {
return
}
for adjacent, weight := range g.list[v] {
if f(NewWeightedEdge(v, adjacent, weight)) {
return
}
}
}
// Enumerates the set of in-edges for the provided vertex.
func (g *weightedDirected) EachArcTo(v Vertex, f EdgeLambda) {
g.mu.RLock()
defer g.mu.RUnlock()
if !g.hasVertex(v) {
return
}
for candidate, adjacent := range g.list {
for target, weight := range adjacent {
if target == v {
if f(NewWeightedEdge(candidate, target, weight)) {
return
}
}
}
}
}
// Traverses the set of edges in the graph, passing each edge to the
// provided closure.
func (g *weightedDirected) EachEdge(f EdgeLambda) {
g.mu.RLock()
defer g.mu.RUnlock()
for source, adjacent := range g.list {
for target, weight := range adjacent {
if f(NewWeightedEdge(source, target, weight)) {
return
}
}
}
}
// Indicates whether or not the given edge is present in the graph. It matches
// based solely on the presence of an edge, disregarding edge weight.
func (g *weightedDirected) HasEdge(edge Edge) bool {
g.mu.RLock()
defer g.mu.RUnlock()
_, exists := g.list[edge.Source()][edge.Target()]
return exists
}
// Indicates whether or not the given weighted edge is present in the graph.
// It will only match if the provided WeightedEdge has the same weight as
// the edge contained in the graph.
func (g *weightedDirected) HasWeightedEdge(edge WeightedEdge) bool {
g.mu.RLock()
defer g.mu.RUnlock()
if weight, exists := g.list[edge.Source()][edge.Target()]; exists {
return weight == edge.Weight()
}
return false
}
// Returns the density of the graph. Density is the ratio of edge count to the
// number of edges there would be in complete graph (maximum edge count).
func (g *weightedDirected) Density() float64 {
g.mu.RLock()
defer g.mu.RUnlock()
order := g.Order()
return float64(g.Size()) / float64(order*(order-1))
}
// Removes a vertex from the graph. Also removes any edges of which that
// vertex is a member.
func (g *weightedDirected) RemoveVertex(vertices ...Vertex) {
if len(vertices) == 0 {
return
}
g.mu.Lock()
defer g.mu.Unlock()
for _, vertex := range vertices {
if g.hasVertex(vertex) {
g.size -= len(g.list[vertex])
delete(g.list, vertex)
for _, adjacent := range g.list {
if _, has := adjacent[vertex]; has {
delete(adjacent, vertex)
g.size--
}
}
}
}
return
}
// Adds edges to the graph.
func (g *weightedDirected) AddEdges(edges ...WeightedEdge) {
if len(edges) == 0 {
return
}
g.mu.Lock()
defer g.mu.Unlock()
g.addEdges(edges...)
}
// Adds a new edge to the graph.
func (g *weightedDirected) addEdges(edges ...WeightedEdge) {
for _, edge := range edges {
g.ensureVertex(edge.Source(), edge.Target())
if _, exists := g.list[edge.Source()][edge.Target()]; !exists {
g.list[edge.Source()][edge.Target()] = edge.Weight()
g.size++
}
}
}
// Removes edges from the graph. This does NOT remove vertex members of the
// removed edges.
func (g *weightedDirected) RemoveEdges(edges ...WeightedEdge) {
if len(edges) == 0 {
return
}
g.mu.Lock()
defer g.mu.Unlock()
for _, edge := range edges {
s, t := edge.Both()
if _, exists := g.list[s][t]; exists {
delete(g.list[s], t)
g.size--
}
}
}
func (g *weightedDirected) Transpose() Digraph {
g.mu.RLock()
defer g.mu.RUnlock()
g2 := &weightedDirected{}
g2.list = make(map[Vertex]map[Vertex]float64)
// Guess at average indegree by looking at ratio of edges to vertices, use that to initially size the adjacency maps
startcap := int(g.Size() / g.Order())
for source, adjacent := range g.list {
if !g2.hasVertex(source) {
g2.list[source] = make(map[Vertex]float64, startcap+1)
}
for target, weight := range adjacent {
if !g2.hasVertex(target) {
g2.list[target] = make(map[Vertex]float64, startcap+1)
}
g2.list[target][source] = weight
}
}
return g2
}
/* UndirectedWeighted implementation */
type weightedUndirected struct {
baseWeighted
}
// Returns the degree of the provided vertex. If the vertex is not present in the
// graph, the second return value will be false.
func (g *weightedUndirected) DegreeOf(vertex Vertex) (degree int, exists bool) {
g.mu.RLock()
defer g.mu.RUnlock()
if exists = g.hasVertex(vertex); exists {
degree = len(g.list[vertex])
}
return
}
// Traverses the set of edges in the graph, passing each edge to the
// provided closure.
func (g *weightedUndirected) EachEdge(f EdgeLambda) {
g.mu.RLock()
defer g.mu.RUnlock()
visited := set.NewNonTS()
var e WeightedEdge
for source, adjacent := range g.list {
for target, weight := range adjacent {
e = NewWeightedEdge(source, target, weight)
if !visited.Has(NewEdge(e.Both())) {
visited.Add(NewEdge(target, source))
if f(e) {
return
}
}
}
}
}
// Enumerates the set of all edges incident to the provided vertex.
func (g *weightedUndirected) EachEdgeIncidentTo(v Vertex, f EdgeLambda) {
g.mu.RLock()
defer g.mu.RUnlock()
if !g.hasVertex(v) {
return
}
for adjacent, weight := range g.list[v] {
if f(NewWeightedEdge(v, adjacent, weight)) {
return
}
}
}
// Enumerates the vertices adjacent to the provided vertex.
func (g *weightedUndirected) EachAdjacentTo(vertex Vertex, f VertexLambda) {
g.mu.RLock()
defer g.mu.RUnlock()
eachAdjacentToUndirected(g.list, vertex, f)
}
// Indicates whether or not the given edge is present in the graph. It matches
// based solely on the presence of an edge, disregarding edge weight.
func (g *weightedUndirected) HasEdge(edge Edge) bool {
g.mu.RLock()
defer g.mu.RUnlock()
// Spread it into two expressions to avoid evaluating the second if possible
if _, exists := g.list[edge.Source()][edge.Target()]; exists {
return true
} else if _, exists := g.list[edge.Target()][edge.Source()]; exists {
return true
}
return false
}
// Indicates whether or not the given weighted edge is present in the graph.
// It will only match if the provided WeightedEdge has the same weight as
// the edge contained in the graph.
func (g *weightedUndirected) HasWeightedEdge(edge WeightedEdge) bool {
g.mu.RLock()
defer g.mu.RUnlock()
// Spread it into two expressions to avoid evaluating the second if possible
if weight, exists := g.list[edge.Source()][edge.Target()]; exists {
return edge.Weight() == weight
} else if weight, exists := g.list[edge.Target()][edge.Source()]; exists {
return edge.Weight() == weight
}
return false
}
// Returns the density of the graph. Density is the ratio of edge count to the
// number of edges there would be in complete graph (maximum edge count).
func (g *weightedUndirected) Density() float64 {
g.mu.RLock()
defer g.mu.RUnlock()
order := g.Order()
return 2 * float64(g.Size()) / float64(order*(order-1))
}
// Removes a vertex from the graph. Also removes any edges of which that
// vertex is a member.
func (g *weightedUndirected) RemoveVertex(vertices ...Vertex) {
if len(vertices) == 0 {
return
}
g.mu.Lock()
defer g.mu.Unlock()
for _, vertex := range vertices {
if g.hasVertex(vertex) {
eachAdjacentToUndirected(g.list, vertex, func(adjacent Vertex) (terminate bool) {
delete(g.list[adjacent], vertex)
return
})
g.size -= len(g.list[vertex])
delete(g.list, vertex)
}
}
return
}
// Adds edges to the graph.
func (g *weightedUndirected) AddEdges(edges ...WeightedEdge) {
if len(edges) == 0 {
return
}
g.mu.Lock()
defer g.mu.Unlock()
g.addEdges(edges...)
}
// Adds a new edge to the graph.
func (g *weightedUndirected) addEdges(edges ...WeightedEdge) {
for _, edge := range edges {
g.ensureVertex(edge.Source(), edge.Target())
if _, exists := g.list[edge.Source()][edge.Target()]; !exists {
w := edge.Weight()
g.list[edge.Source()][edge.Target()] = w
g.list[edge.Target()][edge.Source()] = w
g.size++
}
}
}
// Removes edges from the graph. This does NOT remove vertex members of the
// removed edges.
func (g *weightedUndirected) RemoveEdges(edges ...WeightedEdge) {
if len(edges) == 0 {
return
}
g.mu.Lock()
defer g.mu.Unlock()
for _, edge := range edges {
s, t := edge.Both()
if _, exists := g.list[s][t]; exists {
delete(g.list[s], t)
delete(g.list[t], s)
g.size--
}
}
}