diff --git a/src/abstractnamedgraph.jl b/src/abstractnamedgraph.jl index d94e410..654f724 100644 --- a/src/abstractnamedgraph.jl +++ b/src/abstractnamedgraph.jl @@ -51,7 +51,7 @@ Graphs.add_vertex!(graph::AbstractNamedGraph, vertex) = not_implemented() GraphsExtensions.rename_vertices(f::Function, g::AbstractNamedGraph) = not_implemented() -# TODO: Is this a good definition? +# TODO: Is this a good definition? Maybe make it generic to any graph? function GraphsExtensions.permute_vertices(graph::AbstractNamedGraph, permutation) return subgraph(graph, map(v -> ordered_vertices(graph)[v], permutation)) end diff --git a/src/lib/GraphsExtensions/src/simplegraph.jl b/src/lib/GraphsExtensions/src/simplegraph.jl index af3fbf2..df53d92 100644 --- a/src/lib/GraphsExtensions/src/simplegraph.jl +++ b/src/lib/GraphsExtensions/src/simplegraph.jl @@ -1,5 +1,9 @@ using Graphs.SimpleGraphs: AbstractSimpleGraph +function permute_vertices(graph::AbstractSimpleGraph, permutation) + return graph[permutation] +end + # https://github.com/JuliaGraphs/Graphs.jl/issues/365 function graph_from_vertices(graph_type::Type{<:AbstractSimpleGraph}, vertices) @assert vertices == Base.OneTo(length(vertices)) diff --git a/src/lib/GraphsExtensions/test/runtests.jl b/src/lib/GraphsExtensions/test/runtests.jl index 34c0ec2..b645b64 100644 --- a/src/lib/GraphsExtensions/test/runtests.jl +++ b/src/lib/GraphsExtensions/test/runtests.jl @@ -16,6 +16,7 @@ using Graphs: add_vertex!, edges, edgetype, + has_edge, inneighbors, is_cyclic, is_directed, @@ -177,7 +178,29 @@ using Test: @test, @test_broken, @test_throws, @testset # permute_vertices g = path_graph(4) - @test_broken permute_vertices(g, [2, 1, 4, 3]) + g_perm = permute_vertices(g, [2, 1, 4, 3]) + @test nv(g_perm) == 4 + @test ne(g_perm) == 3 + @test vertices(g_perm) == 1:4 + @test has_edge(g_perm, 1 => 2) + @test has_edge(g_perm, 2 => 1) + @test has_edge(g_perm, 1 => 4) + @test has_edge(g_perm, 4 => 1) + @test has_edge(g_perm, 3 => 4) + @test has_edge(g_perm, 4 => 3) + @test !has_edge(g_perm, 2 => 3) + @test !has_edge(g_perm, 3 => 2) + g = path_digraph(4) + g_perm = permute_vertices(g, [2, 1, 4, 3]) + @test nv(g_perm) == 4 + @test ne(g_perm) == 3 + @test vertices(g_perm) == 1:4 + @test has_edge(g_perm, 2 => 1) + @test !has_edge(g_perm, 1 => 2) + @test has_edge(g_perm, 1 => 4) + @test !has_edge(g_perm, 4 => 1) + @test has_edge(g_perm, 4 => 3) + @test !has_edge(g_perm, 3 => 4) # all_edges g = path_graph(4)