Skip to content

Commit

Permalink
Define permute_vertices for SimpleGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Apr 26, 2024
1 parent dbb6c70 commit 03e46bf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/abstractnamedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/lib/GraphsExtensions/src/simplegraph.jl
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
25 changes: 24 additions & 1 deletion src/lib/GraphsExtensions/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using Graphs:
add_vertex!,
edges,
edgetype,
has_edge,
inneighbors,
is_cyclic,
is_directed,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 03e46bf

Please sign in to comment.