Skip to content

Commit d3b2706

Browse files
Use GenericGraph for testing spanningtrees algorithms (#276)
1 parent 794beff commit d3b2706

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

test/spanningtrees/boruvka.jl

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414
max_vec_mst = Vector{Edge}([Edge(1, 4), Edge(2, 4), Edge(1, 3)])
1515
cost_max_vec_mst = sum(distmx[src(e), dst(e)] for e in max_vec_mst)
1616

17-
for g in testgraphs(g4)
17+
for g in test_generic_graphs(g4)
1818
# Testing Boruvka's algorithm
1919
res1 = boruvka_mst(g, distmx)
20-
g1t = SimpleGraph(res1.mst)
20+
edges1 = [Edge(src(e), dst(e)) for e in res1.mst]
21+
g1t = GenericGraph(SimpleGraph(edges1))
2122
@test res1.weight == cost_mst
2223
# acyclic graphs have n - c edges
2324
@test nv(g1t) - length(connected_components(g1t)) == ne(g1t)
2425
@test nv(g1t) == nv(g)
2526

2627
res2 = boruvka_mst(g, distmx; minimize=false)
27-
g2t = SimpleGraph(res2.mst)
28+
edges2 = [Edge(src(e), dst(e)) for e in res2.mst]
29+
g2t = GenericGraph(SimpleGraph(edges2))
2830
@test res2.weight == cost_max_vec_mst
2931
@test nv(g2t) - length(connected_components(g2t)) == ne(g2t)
3032
@test nv(g2t) == nv(g)
@@ -53,15 +55,17 @@
5355
])
5456
weight_max_vec2 = sum(distmx_sec[src(e), dst(e)] for e in max_vec2)
5557

56-
for g in testgraphs(gx)
58+
for g in test_generic_graphs(gx)
5759
res3 = boruvka_mst(g, distmx_sec)
58-
g3t = SimpleGraph(res3.mst)
60+
edges3 = [Edge(src(e), dst(e)) for e in res3.mst]
61+
g3t = GenericGraph(SimpleGraph(edges3))
5962
@test res3.weight == weight_vec2
6063
@test nv(g3t) - length(connected_components(g3t)) == ne(g3t)
6164
@test nv(g3t) == nv(gx)
6265

6366
res4 = boruvka_mst(g, distmx_sec; minimize=false)
64-
g4t = SimpleGraph(res4.mst)
67+
edges4 = [Edge(src(e), dst(e)) for e in res4.mst]
68+
g4t = GenericGraph(SimpleGraph(edges4))
6569
@test res4.weight == weight_max_vec2
6670
@test nv(g4t) - length(connected_components(g4t)) == ne(g4t)
6771
@test nv(g4t) == nv(gx)
@@ -114,15 +118,17 @@
114118
])
115119
weight_max_vec3 = sum(distmx_third[src(e), dst(e)] for e in max_vec3)
116120

117-
for g in testgraphs(gd)
121+
for g in test_generic_graphs(gd)
118122
res5 = boruvka_mst(g, distmx_third)
119-
g5t = SimpleGraph(res5.mst)
123+
edges5 = [Edge(src(e), dst(e)) for e in res5.mst]
124+
g5t = GenericGraph(SimpleGraph(edges5))
120125
@test res5.weight == weight_vec3
121126
@test nv(g5t) - length(connected_components(g5t)) == ne(g5t)
122127
@test nv(g5t) == nv(gd)
123128

124129
res6 = boruvka_mst(g, distmx_third; minimize=false)
125-
g6t = SimpleGraph(res6.mst)
130+
edges6 = [Edge(src(e), dst(e)) for e in res6.mst]
131+
g6t = GenericGraph(SimpleGraph(edges6))
126132
@test res6.weight == weight_max_vec3
127133
@test nv(g6t) - length(connected_components(g6t)) == ne(g6t)
128134
@test nv(g6t) == nv(gd)

test/spanningtrees/kruskal.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010

1111
vec_mst = Vector{Edge}([Edge(1, 2), Edge(3, 4), Edge(2, 3)])
1212
max_vec_mst = Vector{Edge}([Edge(2, 4), Edge(1, 4), Edge(1, 3)])
13-
for g in testgraphs(g4)
13+
for g in test_generic_graphs(g4)
1414
# Testing Kruskal's algorithm
1515
mst = @inferred(kruskal_mst(g, distmx))
16-
@test mst == vec_mst
17-
@test @inferred(kruskal_mst(g, distmx, minimize=false)) == max_vec_mst
16+
max_mst = @inferred(kruskal_mst(g, distmx, minimize=false))
17+
# GenericEdge currently does not implement any comparison operators
18+
# so instead we compare tuples of source and target vertices
19+
@test sort([(src(e), dst(e)) for e in mst]) == sort([(src(e), dst(e)) for e in vec_mst])
20+
@test sort([(src(e), dst(e)) for e in max_mst]) == sort([(src(e), dst(e)) for e in max_vec_mst])
1821
end
1922
# second test
2023
distmx_sec = [
@@ -35,9 +38,10 @@
3538
max_vec2 = Vector{Edge}([
3639
Edge(5, 7), Edge(1, 7), Edge(4, 7), Edge(3, 7), Edge(5, 8), Edge(2, 3), Edge(5, 6)
3740
])
38-
for g in testgraphs(gx)
41+
for g in test_generic_graphs(gx)
3942
mst2 = @inferred(kruskal_mst(g, distmx_sec))
40-
@test mst2 == vec2
41-
@test @inferred(kruskal_mst(g, distmx_sec, minimize=false)) == max_vec2
43+
max_mst2 = @inferred(kruskal_mst(g, distmx_sec, minimize=false))
44+
@test sort([(src(e), dst(e)) for e in mst2]) == sort([(src(e), dst(e)) for e in vec2])
45+
@test sort([(src(e), dst(e)) for e in max_mst2]) == sort([(src(e), dst(e)) for e in max_vec2])
4246
end
4347
end

test/spanningtrees/prim.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
]
1010

1111
vec_mst = Vector{Edge}([Edge(1, 2), Edge(2, 3), Edge(3, 4)])
12-
for g in testgraphs(g4)
12+
for g in test_generic_graphs(g4)
1313
# Testing Prim's algorithm
1414
mst = @inferred(prim_mst(g, distmx))
1515
@test mst == vec_mst
@@ -31,7 +31,7 @@
3131
Edge(8, 2), Edge(1, 3), Edge(3, 4), Edge(6, 5), Edge(8, 6), Edge(3, 7), Edge(1, 8)
3232
])
3333
gx = SimpleGraph(distmx_sec)
34-
for g in testgraphs(gx)
34+
for g in test_generic_graphs(gx)
3535
mst2 = @inferred(prim_mst(g, distmx_sec))
3636
@test mst2 == vec2
3737
end

0 commit comments

Comments
 (0)