-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add spanning tree and forest cover functions
- Loading branch information
Showing
5 changed files
with
109 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
abstract type SpanningTreeAlgorithm end | ||
|
||
struct BFS <: SpanningTreeAlgorithm end | ||
struct RandomBFS <: SpanningTreeAlgorithm end | ||
struct DFS <: SpanningTreeAlgorithm end | ||
|
||
default_spanning_tree_alg() = BFS() | ||
|
||
default_root_vertex(g) = last(findmax(eccentricities(g))) | ||
|
||
function spanning_tree( | ||
g::AbstractNamedGraph; alg=default_spanning_tree_alg(), root_vertex=default_root_vertex(g) | ||
) | ||
return spanning_tree(alg, g; root_vertex) | ||
end | ||
|
||
@traitfn function spanning_tree( | ||
::BFS, g::AbstractNamedGraph::(!IsDirected); root_vertex=default_root_vertex(g) | ||
) | ||
return undirected_graph(bfs_tree(g, root_vertex)) | ||
end | ||
|
||
@traitfn function spanning_tree( | ||
::RandomBFS, g::AbstractNamedGraph::(!IsDirected); root_vertex=default_root_vertex(g) | ||
) | ||
return undirected_graph(random_bfs_tree(g, root_vertex)) | ||
end | ||
|
||
@traitfn function spanning_tree( | ||
::DFS, g::AbstractNamedGraph::(!IsDirected); root_vertex=default_root_vertex(g) | ||
) | ||
return undirected_graph(dfs_tree(g, root_vertex)) | ||
end | ||
|
||
#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=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=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) | ||
push!(edges_collected, edges(g_reduced_spanning_forest)...) | ||
push!(forests, g_reduced_spanning_forest) | ||
setdiff!(remaining_edges, edges(g_reduced_spanning_forest)) | ||
end | ||
|
||
return forests | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Test | ||
using Graphs | ||
using NamedGraphs | ||
using NamedGraphs: forest_cover, spanning_tree | ||
|
||
module TestTreesAndForests | ||
using NamedGraphs | ||
using NamedGraphs: hexagonal_lattice_graph, triangular_lattice_graph | ||
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()] | ||
end | ||
|
||
@testset "Test Spanning Trees $g_string, $alg" for (g_string, g) in TestTreesAndForests.gs, | ||
alg in TestTreesAndForests.algs | ||
|
||
s_tree = spanning_tree(g; alg) | ||
@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 $g_string" for (g_string, g) in TestTreesAndForests.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 |