Skip to content

Commit

Permalink
Kj/induced subgraphs (#4)
Browse files Browse the repository at this point in the history
* Create LabelledGraph package

* Add LightGraphs dependency

* Implement induced subgraph construction
  • Loading branch information
dexter2206 committed Mar 16, 2021
1 parent 0077669 commit bcdc7ea
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/LabelledGraphs.jl
Expand Up @@ -217,6 +217,13 @@ module LabelledGraphs
has_prop(lg.inner_graph, prop)
end

function LightGraphs.induced_subgraph(
lg::LabelledGraph{S, T}, vertices::Vector{T}
) where {S, T}
sub_ig, _vmap = induced_subgraph(lg.inner_graph, [lg.reverse_label_map[v] for v in vertices])
LabelledGraph(vertices, sub_ig), vertices
end

# --- Default-type aliases ---
LabelledGraph(labels::Vector{T}) where T = LabelledGraph{SimpleGraph}(labels)
LabelledDiGraph(labels::Vector{T}) where T = LabelledGraph{SimpleDiGraph}(labels)
Expand Down
35 changes: 35 additions & 0 deletions test/runtests.jl
Expand Up @@ -278,3 +278,38 @@ for graph_type ∈ (MetaGraph, MetaDiGraph)
@test !has_prop(lg, :title)
end
end

for graph_type (SimpleGraph, SimpleDiGraph, MetaGraph, MetaDiGraph)
@testset "Vertices labels are preserved when constructing induced subgraph of LabelledGraph{$graph_type}" begin
lg = LabelledGraph{graph_type}([10, 20, 40, 50])
add_edge!(lg, 40, 10)
add_edge!(lg, 50, 10)
add_edge!(lg, 20, 40)

sub_lg, vmap = induced_subgraph(lg, [10, 20, 40])

@test nv(sub_lg) == 3
@test vertices(sub_lg) == [10, 20, 40]

@test ne(sub_lg) == 2
@test has_edge(sub_lg, 20, 40)
@test has_edge(sub_lg, 40, 10)
@test vmap == [10, 20, 40]
end
end

for graph_type (MetaGraph, MetaDiGraph)
@testset "Metainformation are correctly preserved when constructing induced subgraph of LabelledGraph{$graph_type}" begin
lg = LabelledGraph{graph_type}([5, 10, 15])
add_edge!(lg, 5, 10)

set_prop!(lg, :name, "The Ising model")
set_prop!(lg, 5, :x, 20)
set_prop!(lg, 5, 10, :y, 30)

sub_ig, vmap = induced_subgraph(lg, [10, 5])
@test get_prop(sub_ig, :name) == "The Ising model"
@test get_prop(sub_ig, 5, :x) == 20
@test get_prop(sub_ig, 5, 10, :y) == 30
end
end

0 comments on commit bcdc7ea

Please sign in to comment.