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

Partitioned graphs bug fix #87

Closed
wants to merge 3 commits into from
Closed
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
10 changes: 5 additions & 5 deletions src/lib/PartitionedGraphs/src/partitionedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ function partitionedges(pg::PartitionedGraph)
end

function Graphs.edges(pg::PartitionedGraph, partitionedge::PartitionEdge)
psrc_vs = vertices(pg, PartitionVertex(src(partitionedge)))
pdst_vs = vertices(pg, PartitionVertex(dst(partitionedge)))
psrc_subgraph = subgraph(unpartitioned_graph(pg), psrc_vs)
pdst_subgraph = subgraph(pg, pdst_vs)
full_subgraph = subgraph(pg, vcat(psrc_vs, pdst_vs))
psrc_vs = vertices(pg, src(partitionedge))
pdst_vs = vertices(pg, dst(partitionedge))
psrc_subgraph, _ = induced_subgraph(unpartitioned_graph(pg), psrc_vs)
pdst_subgraph, _ = induced_subgraph(pg, pdst_vs)
full_subgraph, _ = induced_subgraph(pg, vcat(psrc_vs, pdst_vs))

return setdiff(edges(full_subgraph), vcat(edges(psrc_subgraph), edges(pdst_subgraph)))
end
Expand Down
14 changes: 13 additions & 1 deletion src/steiner_tree.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Graphs: Graphs, IsDirected, nv, steiner_tree
using SimpleTraits: SimpleTraits, Not, @traitfn

@traitfn function Graphs.steiner_tree(
@traitfn function namedgraph_steiner_tree(
g::AbstractNamedGraph::(!IsDirected), term_vert, distmx=weights(g)
)
position_tree = steiner_tree(
Expand All @@ -15,3 +15,15 @@ using SimpleTraits: SimpleTraits, Not, @traitfn
end
return tree
end

@traitfn function Graphs.steiner_tree(
g::AbstractNamedGraph::(!IsDirected), term_vert, args...
)
return namedgraph_steiner_tree(g, term_vert, args...)
end

@traitfn function Graphs.steiner_tree(
g::AbstractNamedGraph::(!IsDirected), term_vert::Vector{<:Integer}, args...
)
return namedgraph_steiner_tree(g, term_vert, args...)
end
10 changes: 10 additions & 0 deletions test/test_namedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,16 @@ end
for e in es
@test has_edge(st, e)
end

g = named_path_graph(4)
terminal_vertices = [1, 3]
st = steiner_tree(g, terminal_vertices)
es = [1 => 2, 2 => 3]
@test ne(st) == 2
@test nv(st) == 3
for e in es
@test has_edge(st, e)
end
end
@testset "topological_sort_by_dfs" begin
g = NamedDiGraph(["A", "B", "C", "D", "E", "F", "G"])
Expand Down
6 changes: 4 additions & 2 deletions test/test_partitionedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using NamedGraphs.GraphsExtensions:
vertextype
using NamedGraphs.NamedGraphGenerators:
named_comb_tree, named_grid, named_triangular_lattice_graph
using NamedGraphs.OrderedDictionaries: OrderedDictionary
using NamedGraphs.PartitionedGraphs:
PartitionEdge,
PartitionedGraph,
Expand All @@ -52,7 +53,7 @@ using Test: @test, @testset
pg = PartitionedGraph(g, partitions)
@test vertextype(partitioned_graph(pg)) == Int64
@test vertextype(unpartitioned_graph(pg)) == vertextype(g)
@test isa(partitionvertices(pg), Dictionary{Int64,PartitionVertex{Int64}})
@test isa(partitionvertices(pg), OrderedDictionary{Int64,PartitionVertex{Int64}})
@test isa(partitionedges(pg), Vector{PartitionEdge{Int64,NamedEdge{Int64}}})
@test is_tree(partitioned_graph(pg))
@test nv(pg) == nx * ny
Expand All @@ -67,7 +68,7 @@ using Test: @test, @testset
@test vertextype(unpartitioned_graph(pg)) == vertextype(g)
@test isa(
partitionvertices(pg),
Dictionary{Tuple{Int64,Int64},PartitionVertex{Tuple{Int64,Int64}}},
OrderedDictionary{Tuple{Int64,Int64},PartitionVertex{Tuple{Int64,Int64}}},
)
@test isa(
partitionedges(pg),
Expand Down Expand Up @@ -103,6 +104,7 @@ end
@test !is_self_loop(partitionedge(pg, (1, 2, 1) => (1, 1, 1)))
@test partitionvertex(pg, (1, 1, 1)) == partitionvertex(pg, (1, 1, nz))
@test partitionvertex(pg, (2, 1, 1)) != partitionvertex(pg, (1, 1, nz))
@test all([length(edges(pg, pe)) == nz for pe in partitionedges(pg)])

@test partitionedge(pg, (1, 1, 1) => (2, 1, 1)) ==
partitionedge(pg, (1, 1, 2) => (2, 1, 2))
Expand Down
Loading