Skip to content

Commit

Permalink
Simplified Test Structure. Spanning tree function -> spanning_tree
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyT1994 committed Oct 30, 2023
1 parent cf9df1a commit 9c77ae5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 41 deletions.
10 changes: 5 additions & 5 deletions src/traversals/trees_and_forests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ end
return undirected_graph(dfs_tree(g, root_vertex))
end

#Given a graph, split it into its connected components, construct a spanning tree over each of them
#Given a graph, split it into its connected components, construct a spanning tree, using the function spanning_tree, over each of them
# and take the union.
function spanning_forest(g::AbstractNamedGraph; spanning_tree_function=spanning_tree)
return reduce(union, (spanning_tree_function(g[vs]) for vs in connected_components(g)))
function spanning_forest(g::AbstractNamedGraph; spanning_tree=spanning_tree)
return reduce(union, (spanning_tree(g[vs]) for vs in connected_components(g)))
end

#Given an undirected graph g with vertex set V, build a set of forests (each with vertex set V) which covers all edges in g
# (see https://en.wikipedia.org/wiki/Arboricity) We do not find the minimum but our tests show this algorithm performs well
function forest_cover(g::AbstractNamedGraph; spanning_tree_function=spanning_tree)
function forest_cover(g::AbstractNamedGraph; spanning_tree=spanning_tree)
edges_collected = edgetype(g)[]
remaining_edges = edges(g)
forests = NamedGraph[]
while !isempty(remaining_edges)
g_reduced = rem_edges(g, edges_collected)
g_reduced_spanning_forest = spanning_forest(g_reduced; spanning_tree_function)
g_reduced_spanning_forest = spanning_forest(g_reduced; spanning_tree)
push!(edges_collected, edges(g_reduced_spanning_forest)...)
push!(forests, g_reduced_spanning_forest)
setdiff!(remaining_edges, edges(g_reduced_spanning_forest))
Expand Down
59 changes: 23 additions & 36 deletions test/test_trees_and_forests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,30 @@ using NamedGraphs
using NamedGraphs:
hexagonal_lattice_graph, triangular_lattice_graph, forest_cover, spanning_tree

@testset "Test Spanning Trees" begin
gs = [
named_grid((6, 1)),
named_grid((3, 3, 3)),
hexagonal_lattice_graph(6, 6),
named_comb_tree((4, 4)),
named_grid((10, 10)),
triangular_lattice_graph(5, 5; periodic=true),
]
algs = [NamedGraphs.BFS(), NamedGraphs.DFS(), NamedGraphs.RandomBFS()]
for g in gs
for alg in algs
s_tree = spanning_tree(alg, g)
@test is_tree(s_tree)
@test Set(vertices(s_tree)) == Set(vertices(g))
@test issubset(Set(edges(s_tree)), Set(edges(g)))
end
end
gs = [
("Chain", named_grid((6, 1))),
("Cubic Lattice", named_grid((3, 3, 3))),
("Hexagonal Grid", hexagonal_lattice_graph(6, 6)),
("Comb Tree", named_comb_tree((4, 4))),
("Square lattice", named_grid((10, 10))),
("Triangular Grid", triangular_lattice_graph(5, 5; periodic=true)),
];
algs = [NamedGraphs.BFS(), NamedGraphs.DFS(), NamedGraphs.RandomBFS()];

@testset "Test Spanning Trees $g_string, $alg" for (g_string, g) in gs, alg in algs
s_tree = spanning_tree(alg, g)
@test is_tree(s_tree)
@test Set(vertices(s_tree)) == Set(vertices(g))
@test issubset(Set(edges(s_tree)), Set(edges(g)))
end

@testset "Test Forest Cover" begin
gs = [
named_grid((6, 1)),
named_grid((3, 3, 3)),
hexagonal_lattice_graph(6, 6),
named_comb_tree((4, 4)),
named_grid((10, 10)),
triangular_lattice_graph(5, 5; periodic=true),
]
for g in gs
cover = forest_cover(g)
cover_edges = reduce(vcat, edges.(cover))
@test issetequal(cover_edges, edges(g))
@test all(issetequal(vertices(forest), vertices(g)) for forest in cover)
for forest in cover
trees = NamedGraph[forest[vs] for vs in connected_components(forest)]
@test all(is_tree.(trees))
end
@testset "Test Forest Cover $g_string" for (g_string, g) in gs
cover = forest_cover(g)
cover_edges = reduce(vcat, edges.(cover))
@test issetequal(cover_edges, edges(g))
@test all(issetequal(vertices(forest), vertices(g)) for forest in cover)
for forest in cover
trees = NamedGraph[forest[vs] for vs in connected_components(forest)]
@test all(is_tree.(trees))
end
end

0 comments on commit 9c77ae5

Please sign in to comment.