Repository navigation
Expand file tree
/
Copy pathcomponents_test.go
More file actions
300 lines (274 loc) · 7.99 KB
/
Copy pathcomponents_test.go
File metadata and controls
300 lines (274 loc) · 7.99 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
package gograph
import (
"fmt"
"math/rand/v2"
"reflect"
"slices"
"testing"
)
func labelsOf[T comparable](components [][]*Vertex[T]) [][]T {
labels := make([][]T, len(components))
for i, component := range components {
labels[i] = make([]T, len(component))
for j, v := range component {
labels[i][j] = v.label
}
}
return labels
}
func graphWithEdges(directed bool, vertices int, edges [][2]int) Graph[int] {
var g Graph[int]
if directed {
g = New[int](Directed())
} else {
g = New[int]()
}
for i := 1; i <= vertices; i++ {
g.AddVertexByLabel(i)
}
for _, e := range edges {
_, _ = g.AddEdge(g.GetVertexByID(e[0]), g.GetVertexByID(e[1]))
}
return g
}
func TestConnectedComponents(t *testing.T) {
tests := []struct {
name string
directed bool
vertices int
edges [][2]int
want [][]int
}{
{
name: "undirected, in breadth-first order",
vertices: 6,
edges: [][2]int{{1, 3}, {4, 5}, {3, 2}, {1, 6}},
want: [][]int{{1, 3, 6, 2}, {4, 5}},
},
{
name: "directed, weak, in insertion order",
directed: true,
vertices: 6,
edges: [][2]int{{3, 1}, {2, 3}, {5, 4}},
want: [][]int{{1, 2, 3}, {4, 5}, {6}},
},
{
name: "undirected self-loops join nothing",
vertices: 2,
edges: [][2]int{{1, 1}, {2, 2}},
want: [][]int{{1}, {2}},
},
{
name: "directed self-loops join nothing",
directed: true,
vertices: 2,
edges: [][2]int{{1, 1}, {2, 2}},
want: [][]int{{1}, {2}},
},
{
name: "directed cycle",
directed: true,
vertices: 3,
edges: [][2]int{{2, 3}, {3, 1}, {1, 2}},
want: [][]int{{1, 2, 3}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := graphWithEdges(tt.directed, tt.vertices, tt.edges)
if got := labelsOf(ConnectedComponents(g)); !reflect.DeepEqual(got, tt.want) {
t.Fatalf("ConnectedComponents() = %v, want %v", got, tt.want)
}
})
}
}
func TestConnectedComponentsEmpty(t *testing.T) {
for _, g := range []Graph[int]{New[int](), New[int](Directed())} {
if got := ConnectedComponents(g); got == nil || len(got) != 0 {
t.Fatalf("ConnectedComponents() = %#v, want an empty slice", got)
}
}
}
func TestConnectedComponentsAfterRemovals(t *testing.T) {
for _, directed := range []bool{false, true} {
g := graphWithEdges(directed, 5, [][2]int{{1, 2}, {2, 3}, {3, 4}, {4, 5}})
g.RemoveVertices(g.GetVertexByID(3))
g.AddVertexByLabel(3)
_, _ = g.AddEdge(g.GetVertexByID(5), g.GetVertexByID(3))
want := [][]int{{1, 2}, {4, 5, 3}}
if got := labelsOf(ConnectedComponents(g)); !reflect.DeepEqual(got, want) {
t.Fatalf("directed=%v: ConnectedComponents() = %v, want %v", directed, got, want)
}
}
}
func TestConnectedComponentsStoredVertices(t *testing.T) {
for _, directed := range []bool{false, true} {
g := graphWithEdges(directed, 3, [][2]int{{1, 2}})
components := ConnectedComponents(g)
for _, component := range components {
for _, v := range component {
if v != g.GetVertexByID(v.label) {
t.Fatalf("directed=%v: vertex %d is a copy", directed, v.label)
}
}
}
_ = append(components[0], NewVertex(99))
if components[1][0].label != 3 {
t.Fatalf("directed=%v: appending to a component changed the next one", directed)
}
}
}
type otherGraph[T comparable] struct{ Graph[T] }
func TestConnectedComponentsOtherGraph(t *testing.T) {
for _, directed := range []bool{false, true} {
g := graphWithEdges(directed, 6, [][2]int{{1, 2}, {2, 3}, {4, 5}})
got := labelsOf(ConnectedComponents(otherGraph[int]{g}))
want := labelsOf(ConnectedComponents(g))
if !reflect.DeepEqual(got, want) {
t.Fatalf("directed=%v: ConnectedComponents(wrapper) = %v, want %v", directed, got, want)
}
}
}
func TestConnectedComponentsOtherDirectedGraphPreservesVertexOrder(t *testing.T) {
g := New[int](Directed())
for _, label := range []int{1, 2, 3} {
g.AddVertexByLabel(label)
}
_, _ = g.AddEdge(g.GetVertexByID(1), g.GetVertexByID(3))
_, _ = g.AddEdge(g.GetVertexByID(3), g.GetVertexByID(2))
got := labelsOf(ConnectedComponents(otherGraph[int]{g}))
want := [][]int{{1, 2, 3}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("ConnectedComponents(wrapper) = %v, want %v", got, want)
}
}
func TestConnectedComponentsOtherDirectedGraphAppend(t *testing.T) {
g := graphWithEdges(true, 3, [][2]int{{1, 2}})
components := ConnectedComponents(otherGraph[int]{g})
_ = append(components[0], NewVertex(99))
if components[1][0].label != 3 {
t.Fatal("appending to a component changed the next one")
}
}
type distinctEndpointsGraph[T comparable] struct {
Graph[T]
}
func (g distinctEndpointsGraph[T]) AllEdges() []*Edge[T] {
edges := g.Graph.AllEdges()
distinct := make([]*Edge[T], len(edges)+1)
for i, e := range edges {
distinct[i] = NewEdge(NewVertex(e.Source().Label()), NewVertex(e.Destination().Label()))
}
distinct[len(edges)] = nil
return distinct
}
func TestConnectedComponentsOtherDirectedGraphDistinctEndpoints(t *testing.T) {
g := graphWithEdges(true, 3, [][2]int{{1, 2}, {2, 3}})
got := labelsOf(ConnectedComponents(distinctEndpointsGraph[int]{g}))
want := [][]int{{1, 2, 3}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("ConnectedComponents(distinct endpoints) = %v, want %v", got, want)
}
}
func TestConnectedComponentsPanics(t *testing.T) {
for _, g := range []Graph[int]{nil, (*baseGraph[int])(nil)} {
func() {
defer func() {
if recover() == nil {
t.Fatal("expected panic")
}
}()
_ = ConnectedComponents(g)
}()
}
}
// referenceComponents is the breadth-first search over EdgesOf that
// GirvanNewman used before ConnectedComponents. For directed graphs, it puts
// the vertices of each component in GetAllVertices order.
func referenceComponents(g Graph[int]) [][]int {
position := map[int]int{}
for i, v := range g.GetAllVertices() {
position[v.label] = i
}
visited := map[*Vertex[int]]bool{}
components := [][]int{}
for _, root := range g.GetAllVertices() {
if visited[root] {
continue
}
visited[root] = true
queue := []*Vertex[int]{root}
var component []int
for len(queue) > 0 {
v := queue[0]
queue = queue[1:]
component = append(component, v.label)
for _, e := range g.EdgesOf(v) {
if w := e.OtherVertex(v.label); w != nil && !visited[w] {
visited[w] = true
queue = append(queue, w)
}
}
}
if g.IsDirected() {
slices.SortFunc(component, func(a, b int) int { return position[a] - position[b] })
}
components = append(components, component)
}
return components
}
// randomTestGraph has up to 9 vertices, self-loops, and vertices removed and
// added again, which leave gaps in the insertion order.
func randomTestGraph(r *rand.Rand, directed bool) Graph[int] {
n := 1 + r.IntN(9)
g := graphWithEdges(directed, n, nil)
for range r.IntN(2*n + 1) {
a, b := 1+r.IntN(n), 1+r.IntN(n)
if a == b && r.IntN(4) != 0 {
continue
}
_, _ = g.AddEdge(g.GetVertexByID(a), g.GetVertexByID(b))
}
for range r.IntN(3) {
if v := g.GetVertexByID(1 + r.IntN(n)); v != nil {
g.RemoveVertices(v)
}
}
if r.IntN(2) == 0 {
v := g.AddVertexByLabel(n + 1)
for range r.IntN(3) {
if w := g.GetVertexByID(1 + r.IntN(n)); w != nil {
_, _ = g.AddEdge(v, w)
}
}
}
return g
}
func TestConnectedComponentsRandom(t *testing.T) {
r := rand.New(rand.NewPCG(111, 250)) //nolint:gosec // seeded so failures are reproducible
for i := range 4000 {
g := randomTestGraph(r, i%2 == 0)
want := referenceComponents(g)
if got := labelsOf(ConnectedComponents(g)); !reflect.DeepEqual(got, want) {
t.Fatalf("graph %d, directed=%v: ConnectedComponents() = %v, want %v", i, g.IsDirected(), got, want)
}
}
}
func ExampleConnectedComponents() {
g := New[string]()
_, _ = g.AddEdge(NewVertex("a"), NewVertex("b"))
_, _ = g.AddEdge(NewVertex("b"), NewVertex("c"))
_, _ = g.AddEdge(NewVertex("d"), NewVertex("e"))
g.AddVertexByLabel("f")
for _, component := range ConnectedComponents(g) {
var labels []string
for _, v := range component {
labels = append(labels, v.Label())
}
fmt.Println(labels)
}
// Output:
// [a b c]
// [d e]
// [f]
}