From 9c77ae585798f45a7fb3aa19d5aadc789aa26eee Mon Sep 17 00:00:00 2001 From: Joseph Tindall Date: Mon, 30 Oct 2023 15:19:53 -0400 Subject: [PATCH] Simplified Test Structure. Spanning tree function -> spanning_tree --- src/traversals/trees_and_forests.jl | 10 ++--- test/test_trees_and_forests.jl | 59 +++++++++++------------------ 2 files changed, 28 insertions(+), 41 deletions(-) diff --git a/src/traversals/trees_and_forests.jl b/src/traversals/trees_and_forests.jl index 3ebe95a..2fb8e0e 100644 --- a/src/traversals/trees_and_forests.jl +++ b/src/traversals/trees_and_forests.jl @@ -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)) diff --git a/test/test_trees_and_forests.jl b/test/test_trees_and_forests.jl index 222d794..72c0e45 100644 --- a/test/test_trees_and_forests.jl +++ b/test/test_trees_and_forests.jl @@ -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