Skip to content

Commit cf77b53

Browse files
cecileanegdalle
andauthored
induced_subgraph: new method based on a subset of edges (#72)
* induced_subgraph from an edge set * fixed test_broken for compatibility with julia 1.6 * Remove duplicate code * Add compat entries for new Aqua tests --------- Co-authored-by: Guillaume Dalle <[email protected]>
1 parent 5a255a4 commit cf77b53

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

Project.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
88
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
99

1010
[compat]
11+
Aqua = "0.8"
12+
BenchmarkTools = "1"
13+
Documenter = "1"
1114
Graphs = "1.4.1"
15+
InteractiveUtils = "1"
1216
JLD2 = "0.1.11, 0.2, 0.3, 0.4"
17+
JuliaFormatter = "1"
18+
MetaGraphs = "0.7"
1319
SimpleTraits = "0.9"
20+
Test = "1"
1421
julia = "1.6"
1522

1623
[extras]

src/graphs.jl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function Graphs.outneighbors(meta_graph::MetaGraph, code::Integer)
4444
end
4545

4646
function Base.issubset(meta_graph::MetaGraph, h::MetaGraph)
47+
# no checking of: matching vertex label, or matching edge data
4748
return issubset(meta_graph.graph, h.graph)
4849
end
4950

@@ -267,10 +268,8 @@ function Base.zero(meta_graph::MetaGraph)
267268
)
268269
end
269270

270-
function Graphs.induced_subgraph(
271-
meta_graph::MetaGraph, vertex_codes::AbstractVector{<:Integer}
272-
)
273-
inducedgraph, code_map = induced_subgraph(meta_graph.graph, vertex_codes)
271+
function meta_induced_subgraph(meta_graph::MetaGraph, selector)
272+
inducedgraph, code_map = induced_subgraph(meta_graph.graph, selector)
274273
new_graph = MetaGraph(
275274
inducedgraph,
276275
empty(meta_graph.vertex_labels),
@@ -284,6 +283,19 @@ function Graphs.induced_subgraph(
284283
return new_graph, code_map
285284
end
286285

286+
function Graphs.induced_subgraph(
287+
meta_graph::MetaGraph, vertex_codes::AbstractVector{<:Integer}
288+
)
289+
return meta_induced_subgraph(meta_graph, vertex_codes)
290+
end
291+
292+
function Graphs.induced_subgraph(
293+
meta_graph::MetaGraph, edges::AbstractVector{<:AbstractEdge}
294+
)
295+
# separate method to avoid dispatch ambiguity
296+
return meta_induced_subgraph(meta_graph, edges)
297+
end
298+
287299
@traitfn function Graphs.reverse(meta_graph::MG) where {MG <: MetaGraph; IsDirected{MG}}
288300
edge_data = meta_graph.edge_data
289301
reverse_edge_data = empty(edge_data)

test/tutorial/2_graphs.jl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ haskey(rock_paper_scissors, :scissors, :rock)
134134
haskey(reverse(rock_paper_scissors), :scissors, :rock)
135135
@test haskey(reverse(rock_paper_scissors), :scissors, :rock) #src
136136

137-
# Finally, let us take a subgraph:
137+
# Let us take a subgraph induced by a subset of vertices:
138138

139139
rock_paper, _ = induced_subgraph(rock_paper_scissors, [1, 2])
140140
@test @inferred induced_subgraph(rock_paper_scissors, [1, 2])[1] == rock_paper #src
@@ -147,3 +147,25 @@ haskey(rock_paper, :paper, :rock)
147147
#-
148148
haskey(rock_paper, :rock, :scissors)
149149
@test !haskey(rock_paper, :rock, :scissors) #src
150+
151+
# Subgraphs can also be induced by a subset of edges.
152+
153+
subtree_edges = collect(edges(rock_paper_scissors))[2:3]
154+
rock_paper_scissors_subtree, _ = induced_subgraph(rock_paper_scissors, subtree_edges)
155+
issubset(rock_paper_scissors_subtree, rock_paper_scissors)
156+
ne(rock_paper_scissors_subtree)
157+
@test ne(rock_paper_scissors_subtree) == 2 #src
158+
#-
159+
nv(rock_paper_scissors_subtree)
160+
@test nv(rock_paper_scissors_subtree) == 3 #src
161+
#-
162+
[rock_paper_scissors_subtree[e...] for e in edge_labels(rock_paper_scissors_subtree)]
163+
164+
# Checking that a graph is a subset of another is not supported yet. For example, an induced subgraph may appear as not a subset of the original graph, if vertex codes were modified.
165+
166+
issubset(rock_paper_scissors_subtree, rock_paper_scissors)
167+
@test_broken issubset(rock_paper_scissors_subtree, rock_paper_scissors) #src
168+
#-
169+
rock_scissors, _ = induced_subgraph(rock_paper_scissors, [1, 3])
170+
issubset(rock_scissors, rock_paper_scissors)
171+
@test_broken issubset(rock_scissors, rock_paper_scissors) #src

0 commit comments

Comments
 (0)