3
votes

I ultimately wish to get a subset of my graph by removing connected components with 2 vertices (i.e. both vertices have an edge between them and) You could rephrase this question as:

 given an edge e = (s, d) if degree(s) == degree(d) == 1 then delete edge e

I am using R and Igraph, how would I do this? I know I can subset my graph to remove all nodes with zero degree by doing the following:

g = some_graph()
ldegs <- V(g)[degree(g) < 1]
g = delete.vertices(g, ldegs)

Thanks in advance!

1

1 Answers

5
votes

I don't think this is too hard, you just find the list of nodes with degree == 1, find their neighbours, and if any of the neighbours are in the list, they're the ones to delete:

library(igraph)
g = erdos.renyi.game(100, 0.02)
ones = V(g)[degree(g) == 1]
one_ns = sapply(ones, neighbors, graph=g)
# If any of the neighbours are in ones, we
# can delete these
to_delete = one_ns[one_ns %in% ones]
# Visualize:
plot(g, mark.groups=to_delete)