Buthetleon
Buthetleon

Reputation: 1305

connected length

is there a function in igraph to calculate the no. and size of each of connected component based on attributes. ie. subsets of vertices "connected" by their attributes.

library(igraph)
## create example graph
g1 <- graph.full(5)
V(g1)$name <- 1:5    
g2 <- graph.full(5)
V(g2)$name <- 6:10
g3 <- graph.ring(5)
V(g3)$name <- 11:15
g <- g1 + g2 + g3 + edge('1', '6') + edge('1', '11')

##defining attribute to subset of vertices
hh<-1:vcount(g) %in% c(7,6,1,13,14)
g<-set.vertex.attribute(g, name="selected" ,value=0)
g<-set.vertex.attribute(g, name="selected",hh, value=1)

#plotted out
vc="[<-"(rep('black',vcount(g)),c(7,6,1,13,14),'red')
plot(g,vertex.color=vc, vertex.label.color="white")

what i can think of at this moment is to regraph using only the selected vertices and run the clusters function. Is there a simpler method?

Upvotes: 1

Views: 119

Answers (1)

agstudy
agstudy

Reputation: 121568

You can use induced.subgraph or subgraph depend in your igraph verion

     clusters(induced.subgraph(g,V(g)[V(g)$selected == 1]))

 $membership
[1] 1 1 1 2 2

$csize
[1] 3 2

$no
[1] 2

Upvotes: 2

Related Questions