1

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?

Daniël
  • 111
  • 3
  • It looks like you meant to paste something in as an example, which isn't quite showing up if so – cbk Jul 06 '21 at 03:09

1 Answers1

0

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.

Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107