Skip to content

Commit

Permalink
boundary_partitionedges, fix copy(pg::partitionedgraph) (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyT1994 authored Mar 6, 2024
1 parent 47487b0 commit 749dc45
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/Graphs/partitionedgraphs/partitionedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ 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)),
Expand Down
17 changes: 14 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 Down Expand Up @@ -43,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 @@ -51,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 @@ -67,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

0 comments on commit 749dc45

Please sign in to comment.