Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boundary_partitionedges #51

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Graphs/partitionedgraphs/partitionedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,26 @@ function edges(pg::PartitionedGraph, partitionedges::Vector{<:PartitionEdge})
return unique(reduce(vcat, [edges(pg, pe) for pe in partitionedges]))
end

function boundary_partitionedges(
pg::PartitionedGraph, partitionvertices::Vector{<:PartitionVertex}; kwargs...
)
return PartitionEdge.(
boundary_edges(partitioned_graph(pg), parent.(partitionvertices); kwargs...)
)
end

function boundary_partitionedges(
pg::PartitionedGraph, partitionvertex::PartitionVertex; kwargs...
)
return boundary_partitionedges(pg, [partitionvertex]; kwargs...)
end

function copy(pg::PartitionedGraph)
return PartitionedGraph(
copy(unpartitioned_graph(pg)),
copy(partitioned_graph(pg)),
copy(partitioned_vertices(pg)),
copy(partitionvertex(pg)),
copy(which_partition(pg)),
)
end

Expand Down
19 changes: 16 additions & 3 deletions test/test_partitionedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using NamedGraphs:
forest_cover,
PartitionEdge,
PartitionVertex,
boundary_partitionedges,
parent,
default_root_vertex,
triangular_lattice_graph,
Expand All @@ -27,6 +28,8 @@ using Graphs
@test is_tree(partitioned_graph(pg))
@test nv(pg) == nx * ny
@test nv(partitioned_graph(pg)) == nx
pg_c = copy(pg)
@test pg_c == pg

#Same partitioning but with a dictionary constructor
partition_dict = Dictionary([first(partition) for partition in partitions], partitions)
Expand All @@ -41,6 +44,8 @@ using Graphs
@test is_tree(partitioned_graph(pg))
@test nv(pg) == nx * ny
@test nv(partitioned_graph(pg)) == nx
pg_c = copy(pg)
@test pg_c == pg

#Partition the whole thing into just 1 vertex
pg = PartitionedGraph([i for i in 1:nx])
Expand All @@ -49,6 +54,8 @@ using Graphs
@test nv(partitioned_graph(pg)) == nx
@test ne(pg) == 0
@test ne(partitioned_graph(pg)) == 0
pg_c = copy(pg)
@test pg_c == pg
end

@testset "Test Partitioned Graph Partition Edge and Vertex Finding" begin
Expand All @@ -65,11 +72,17 @@ end
@test partitionvertex(pg, (1, 1, 1)) == partitionvertex(pg, (1, 1, nz))
@test partitionvertex(pg, (2, 1, 1)) != partitionvertex(pg, (1, 1, nz))

@test partitionedge(pg, NamedEdge((1, 1, 1) => (2, 1, 1))) ==
partitionedge(pg, NamedEdge((1, 1, 2) => (2, 1, 2)))
inter_column_edges = NamedEdge.([(1, 1, i) => (2, 1, i) for i in 1:nz])
@test partitionedge(pg, (1, 1, 1) => (2, 1, 1)) ==
partitionedge(pg, (1, 1, 2) => (2, 1, 2))
inter_column_edges = [(1, 1, i) => (2, 1, i) for i in 1:nz]
@test length(partitionedges(pg, inter_column_edges)) == 1
@test length(partitionvertices(pg, [(1, 2, i) for i in 1:nz])) == 1

boundary_sizes = [length(boundary_partitionedges(pg, pv)) for pv in partitionvertices(pg)]
#Partitions into a square grid so each partition should have maximum 4 incoming edges and minimum 2
@test maximum(boundary_sizes) == 4
@test minimum(boundary_sizes) == 2
@test isempty(boundary_partitionedges(pg, partitionvertices(pg)))
end

@testset "Test Partitioned Graph Vertex/Edge Addition and Removal" begin
Expand Down
Loading