-
Notifications
You must be signed in to change notification settings - Fork 49
/
test_graph.jl
371 lines (325 loc) · 14.8 KB
/
test_graph.jl
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
Graphs.flip_direction(edge::Edge{Int32}) = Edge(-edge.data)
@testset "graphs" begin
@testset "disconnected" begin
Random.seed!(8)
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
verts = [Vertex(rand(Int64)) for i = 1 : 10]
for v in verts
add_vertex!(graph, v)
end
@test num_vertices(graph) == length(verts)
@test num_edges(graph) == 0
for v in verts
@test length(in_edges(v, graph)) == 0
@test length(out_edges(v, graph)) == 0
@test length(in_neighbors(v, graph)) == 0
@test length(out_neighbors(v, graph)) == 0
end
@test isempty(setdiff(vertices(graph), verts))
@test isempty(edges(graph))
show(devnull, graph)
end
@testset "tree graph" begin
Random.seed!(9)
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
root = Vertex(rand(Int64))
add_vertex!(graph, root)
nedges = 15
for i = 1 : nedges
parent = rand(vertices(graph))
child = Vertex(rand(Int64))
edge = Edge(rand())
add_edge!(graph, parent, child, edge)
end
@test num_vertices(graph) == nedges + 1
@test num_edges(graph) == nedges
for v in vertices(graph)
if v == root
@test length(in_edges(v, graph)) == 0
@test length(out_edges(v, graph)) > 0
@test length(in_neighbors(v, graph)) == 0
@test length(out_neighbors(v, graph)) > 0
else
@test length(in_edges(v, graph)) == 1
@test length(in_neighbors(v, graph)) == 1
end
end
show(devnull, graph)
end
@testset "remove_vertex!" begin
Random.seed!(10)
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
edge = Edge(rand())
add_edge!(graph, Vertex(rand(Int64)), Vertex(rand(Int64)), edge)
for v in vertices(graph)
@test_throws ErrorException remove_vertex!(graph, v)
end
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
for i = 1 : 100
add_vertex!(graph, Vertex(i))
end
for i = 1 : num_vertices(graph) - 1
add_edge!(graph, rand(vertices(graph)), rand(vertices(graph)), Edge(Float64(i)))
end
original = deepcopy(graph)
vertex = rand(collect(filter(v -> isempty(in_edges(v, graph)) && isempty(out_edges(v, graph)), vertices(graph))))
remove_vertex!(graph, vertex)
@test vertex ∉ vertices(graph)
for v in vertices(graph)
v_orig = vertices(original)[findfirst(v_orig -> v.data == v_orig.data, vertices(original))]
for (e, e_orig) in zip(in_edges(v, graph), in_edges(v_orig, original))
@test e.data == e_orig.data
end
for (e, e_orig) in zip(out_edges(v, graph), out_edges(v_orig, original))
@test e.data == e_orig.data
end
end
end
@testset "remove_edge!" begin
Random.seed!(11)
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
for i = 1 : 100
add_vertex!(graph, Vertex(i))
end
for i = 1 : num_vertices(graph) - 1
add_edge!(graph, rand(vertices(graph)), rand(vertices(graph)), Edge(Float64(i)))
end
original = deepcopy(graph)
edge = rand(edges(graph))
remove_edge!(graph, edge)
@test edge ∉ edges(graph)
@test num_edges(graph) == num_edges(original) - 1
for v in vertices(graph)
@test edge ∉ in_edges(v, graph)
@test edge ∉ out_edges(v, graph)
end
for e in edges(graph)
e_orig = edges(original)[findfirst(e_orig -> e.data == e_orig.data, edges(original))]
@test source(e, graph).data == source(e_orig, original).data
@test target(e, graph).data == target(e_orig, original).data
end
end
@testset "rewire!" begin
Random.seed!(12)
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
for i = 1 : 100
add_vertex!(graph, Vertex(i))
end
for i = 1 : num_vertices(graph) - 1
add_edge!(graph, rand(vertices(graph)), rand(vertices(graph)), Edge(Float64(i)))
end
original = deepcopy(graph)
edge = rand(edges(graph))
oldsource = source(edge, graph)
oldtarget = target(edge, graph)
non_source_vertices = delete!(Set(vertices(graph)), oldsource)
non_target_vertices = delete!(Set(vertices(graph)), oldtarget)
newsource = rand(collect(non_source_vertices))
newtarget = rand(collect(non_target_vertices))
rewire!(graph, edge, newsource, newtarget)
@test source(edge, graph) == newsource
@test target(edge, graph) == newtarget
@test edge ∈ out_edges(newsource, graph)
@test edge ∈ in_edges(newtarget, graph)
@test edge ∉ out_edges(oldsource, graph)
@test edge ∉ in_edges(oldtarget, graph)
@test map(x -> x.data, vertices(original)) == map(x -> x.data, vertices(graph))
for e in filter(e -> e != edge, edges(graph))
e_orig = edges(original)[findfirst(e_orig -> e.data == e_orig.data, edges(original))]
@test source(e, graph).data == source(e_orig, original).data
@test target(e, graph).data == target(e_orig, original).data
end
end
@testset "replace_edge!" begin
Random.seed!(13)
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
for i = 1 : 100
add_vertex!(graph, Vertex(i))
end
for i = 1 : num_vertices(graph) - 1
add_edge!(graph, rand(vertices(graph)), rand(vertices(graph)), Edge(Float64(i)))
end
original = deepcopy(graph)
for i = 1 : 10
old_edge = rand(edges(graph))
src = source(old_edge, graph)
dest = target(old_edge, graph)
new_edge = Edge(NaN)
replace_edge!(graph, old_edge, new_edge)
@test Graphs.edge_index(old_edge) == -1
@test all(map(x -> x.data, vertices(graph)) .== map(x -> x.data, vertices(original)))
@test new_edge ∈ in_edges(dest, graph)
@test old_edge ∉ in_edges(dest, graph)
@test new_edge ∈ out_edges(src, graph)
@test old_edge ∉ out_edges(src, graph)
@test source(new_edge, graph) == src
@test target(new_edge, graph) == dest
@test isnan(new_edge.data)
end
end
@testset "SpanningTree" begin
Random.seed!(14)
rootdata = 0
# graph1: tree grown incrementally
graph1 = DirectedGraph{Vertex{Int64}, Edge{Int32}}()
root1 = Vertex(rootdata)
add_vertex!(graph1, root1)
tree1 = SpanningTree(graph1, root1)
# graph2: tree constructed after graph is built
graph2 = DirectedGraph{Vertex{Int64}, Edge{Int32}}()
root2 = Vertex(rootdata)
add_vertex!(graph2, root2)
nedges = 15
for i = 1 : nedges
parentind = rand(1 : num_vertices(graph1))
childdata = i
edgedata = Int32(i + 3)
add_edge!(tree1, vertices(tree1)[parentind], Vertex(childdata), Edge(edgedata))
add_edge!(graph2, vertices(graph2)[parentind], Vertex(childdata), Edge(edgedata))
end
tree2 = SpanningTree(graph2, root2)
@test all(map(x -> x.data, vertices(tree1)) == map(x -> x.data, vertices(tree2)))
for (v1, v2) in zip(vertices(tree1), vertices(tree2))
if v1 == root(tree1)
@test v2 == root(tree2)
else
@test edge_to_parent(v1, tree1).data == edge_to_parent(v2, tree2).data
outedgedata1 = map(x -> x.data, collect(edges_to_children(v1, tree1)))
outedgedata2 = map(x -> x.data, collect(edges_to_children(v2, tree2)))
@test isempty(setdiff(outedgedata1, outedgedata2))
@test isempty(setdiff(out_edges(v1, graph1), edges_to_children(v1, tree1)))
@test isempty(setdiff(out_edges(v2, graph2), edges_to_children(v2, tree2)))
end
end
tree = tree1
show(devnull, tree)
@test_throws AssertionError add_edge!(tree, rand(vertices(tree)), rand(vertices(tree)), Edge(rand(Int32)))
for src in vertices(tree)
for dest in vertices(tree)
src_ancestors = ancestors(src, tree)
dest_ancestors = ancestors(dest, tree)
lca = lowest_common_ancestor(src, dest, tree)
p = TreePath(src, dest, tree)
show(devnull, p)
@inferred collect(p)
@test source(p) == src
@test target(p) == dest
source_to_lca = collect(edge for edge in p if direction(edge, p) == PathDirections.up)
target_to_lca = reverse!(collect(edge for edge in p if direction(edge, p) == PathDirections.down))
for (v, v_ancestors, pathsegment) in [(src, src_ancestors, source_to_lca); (dest, dest_ancestors, target_to_lca)]
if v == root(tree)
@test tree_index(v, tree) == 1
@test lca == v
@test length(v_ancestors) == 1
@test isempty(pathsegment)
end
for v_ancestor in v_ancestors
@test tree_index(v_ancestor, tree) <= tree_index(v, tree)
end
@test lca ∈ v_ancestors
if v != lca
@test source(last(pathsegment), tree) == lca
end
end
for v in intersect(src_ancestors, dest_ancestors)
@test tree_index(v, tree) <= tree_index(lca, tree)
if v != lca
@test v ∉ (v -> source(v, tree)).(source_to_lca)
@test v ∉ (v -> source(v, tree)).(target_to_lca)
end
end
for v in setdiff(src_ancestors, dest_ancestors)
@test tree_index(v, tree) > tree_index(lca, tree)
end
end
end
for i = 1 : 10
old_edge = rand(edges(tree))
src = source(old_edge, tree)
dest = target(old_edge, tree)
old_id = Graphs.edge_index(old_edge)
# make sure that replacing edge with itself doesn't mess with anything
replace_edge!(tree, old_edge, old_edge)
@test source(old_edge, tree) == src
@test target(old_edge, tree) == dest
@test Graphs.edge_index(old_edge) == old_id
# replace with a new edge
d = Int32(-10 * i)
new_edge = Edge(d)
replace_edge!(tree, old_edge, new_edge)
@test Graphs.edge_index(old_edge) == -1
@test new_edge ∈ in_edges(dest, tree)
@test old_edge ∉ in_edges(dest, tree)
@test new_edge ∈ out_edges(src, tree)
@test old_edge ∉ out_edges(src, tree)
@test source(new_edge, tree) == src
@test target(new_edge, tree) == dest
@test new_edge.data == d
@test edge_to_parent(dest, tree) == new_edge
@test new_edge ∈ edges_to_children(src, tree)
end
original = deepcopy(graph2)
edgemap = Dict(zip(edges(original), edges(graph2)))
newroot = rand(setdiff(vertices(graph2), [root2]))
flipped_edge_map = Dict{Edge{Int32}, Edge{Int32}}()
newtree = SpanningTree(graph2, newroot, flipped_edge_map)
@test !isempty(flipped_edge_map)
for (oldedge, newedge) in edgemap
flipped = haskey(flipped_edge_map, newedge)
if flipped
newedge = flipped_edge_map[newedge]
end
old_source_ind = Graphs.vertex_index(source(oldedge, original))
old_target_ind = Graphs.vertex_index(target(oldedge, original))
new_source_ind = Graphs.vertex_index(source(newedge, graph2))
new_target_ind = Graphs.vertex_index(target(newedge, graph2))
if flipped
@test oldedge.data == -newedge.data
@test new_source_ind == old_target_ind
@test new_target_ind == old_source_ind
else
@test oldedge.data == newedge.data
@test new_source_ind == old_source_ind
@test new_target_ind == old_target_ind
end
end
end
@testset "reindex!" begin
Random.seed!(15)
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
for i = 1 : 100
add_vertex!(graph, Vertex(i))
end
for i = 1 : num_vertices(graph) - 1
add_edge!(graph, rand(vertices(graph)), rand(vertices(graph)), Edge(Float64(i)))
end
newvertices = shuffle(vertices(graph))
newedges = shuffle(edges(graph))
reindex!(graph, newvertices, newedges)
@test all(vertices(graph) .== newvertices)
@test all(edges(graph) .== newedges)
@test all(Graphs.vertex_index.(vertices(graph)) .== 1 : num_vertices(graph))
@test all(Graphs.edge_index.(edges(graph)) .== 1 : num_edges(graph))
end
@testset "map-like constructor" begin
Random.seed!(16)
graph = DirectedGraph{Vertex{Int32}, Edge{Float32}}()
for i = Int32(1) : Int32(100)
add_vertex!(graph, Vertex(i))
end
for i = 1 : num_vertices(graph) - 1
add_edge!(graph, rand(vertices(graph)), rand(vertices(graph)), Edge(Float32(i)))
end
mappedgraph = DirectedGraph(x -> Vertex(Int64(x.data), x.id), x -> Edge(Float64(x.data), x.id), graph)
@test vertextype(mappedgraph) == Vertex{Int64}
@test edgetype(mappedgraph) == Edge{Float64}
@test all(v1.data == v2.data for (v1, v2) in zip(vertices(graph), vertices(mappedgraph)))
@test all(e1.data == e2.data for (e1, e2) in zip(edges(graph), edges(mappedgraph)))
@test all(source(e1, graph).data == source(e2, mappedgraph).data for (e1, e2) in zip(edges(graph), edges(mappedgraph)))
@test all(target(e1, graph).data == target(e2, mappedgraph).data for (e1, e2) in zip(edges(graph), edges(mappedgraph)))
inedgesmatch(v1, v2) = all(e1.data == e2.data for (e1, e2) in zip(in_edges(v1, graph), in_edges(v2, mappedgraph)))
@test all(inedgesmatch(v1, v2) for (v1, v2) in zip(vertices(graph), vertices(mappedgraph)))
outedgesmatch(v1, v2) = all(e1.data == e2.data for (e1, e2) in zip(out_edges(v1, graph), out_edges(v2, mappedgraph)))
@test all(outedgesmatch(v1, v2) for (v1, v2) in zip(vertices(graph), vertices(mappedgraph)))
end
end