Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Subgraph #18

Merged
merged 3 commits into from Mar 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/operators.md
Expand Up @@ -23,3 +23,10 @@ Produces a graph with edges from graph *g* that do not exist in graph *h*, and v

`compose(g, h)`
Merges graphs *g* and *h* by taking the set union of all vertices and edges.

`function inducedsubgraph(g::AbstractGraph, iter)`
Filter *g* to include only the vertices present in iter which should not have duplicates.
Returns the subgraph of *g* induced by set(iter) along with the mapping from the old vertex names to the new vertex names.

`inducedsubgraph!(h, g, newvid)`
Inplace filtering for preallocated output, edge iterable, vertex mapping.
1 change: 1 addition & 0 deletions src/LightGraphs.jl
Expand Up @@ -20,6 +20,7 @@ module LightGraphs
# operators
complement, reverse, reverse!, union, intersect,
difference, symmetric_difference, compose,
inducedsubgraph,

# graph visit
AbstractGraphVisitor, TrivialGraphVisitor, LogGraphVisitor,
Expand Down
2 changes: 2 additions & 0 deletions src/graph.jl
Expand Up @@ -107,3 +107,5 @@ degree(g::Graph, v::Int) = indegree(g,v)
# union(neighbors(g,v), [e.dst for e in g.binclist[v]])
# )
density(g::Graph) = (2*ne(g)) / (nv(g) * (nv(g)-1))


26 changes: 26 additions & 0 deletions src/operators.jl
Expand Up @@ -114,3 +114,29 @@ function compose{T<:AbstractGraph}(g::T, h::T)
end
return r
end

#@doc "filter g to include only the vertices present in iter which should not have duplicates
#returns the subgraph of g induced by set(iter) along with the mapping from the old vertex names to the new vertex names" ->
function inducedsubgraph(g::AbstractGraph, iter)
n = length(iter)
h = Graph(n)
newvid = Dict()
i=1
for v in iter
newvid[v] = i
i +=1
end
inducedsubgraph!(h,edges(g),newvid)
return h, newvid
end

#@doc "inplace filtering for preallocated output, edge iterable, vertex mapping" ->
function inducedsubgraph!(h::AbstractGraph, edges, newvid)
for edg in edges
newsrc = get(newvid, src(edg), 0)
newdst = get(newvid, dst(edg), 0)
if newsrc > 0 && newdst > 0
add_edge!(h, newsrc, newdst)
end
end
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Expand Up @@ -53,7 +53,8 @@ tests = [
"maxadjvisit",
"centrality/betweenness",
"centrality/closeness",
"centrality/degree"
"centrality/degree",
"subgraphs"
]


Expand Down
17 changes: 17 additions & 0 deletions test/subgraphs.jl
@@ -0,0 +1,17 @@
using LightGraphs
import LightGraphs: inducedsubgraph
using Base.Test

g = BullGraph()
n = 3
h, vmap = inducedsubgraph(g, [1:n])
@test nv(h) == n
@test ne(h) == 3

h, vmap = inducedsubgraph(g, [1,2,4])
@test nv(h) == n
@test ne(h) == 2

h, vmap = inducedsubgraph(g, [1,5])
@test nv(h) == 2
@test ne(h) == 0