Exclude unconnected nodes in LightGraphs

133 Views Asked by At

I have generated a network via random draws. Now when I plot the network via LightGraphs.jl I also get the unconnected nodes:

However, I wish to exclude those nodes from the graph. Is this possible?

1

There are 1 best solutions below

0
Bogumił Kamiński On

Yes, use the getindex or induced_subgraph functions (I assume you want to exclude 0-degree nodes):

julia> Random.seed!(123);

julia> g = erdos_renyi(10, 0.1)
{10, 7} undirected simple Int64 graph

julia> degree(g)
10-element Vector{Int64}:
 1
 4
 1
 1
 1
 2
 0
 2
 2
 0

julia> sg = g[findall(>(0), degree(g))]
{8, 7} undirected simple Int64 graph

julia> degree(sg)
8-element Vector{Int64}:
 1
 4
 1
 1
 1
 2
 2
 2

julia> sg2 = induced_subgraph(g, findall(>(0), degree(g)))
({8, 7} undirected simple Int64 graph, [1, 2, 3, 4, 5, 6, 8, 9])

julia> sg2[1] == sg
true

The benefit of induced_sugraph is that it also returns a vector mapping the new vertices to the old ones.