sam
sam

Reputation: 87

count internal and external edges of a subgraph

Given a non-directed graph g and a subset of vertices v,

I want to count:

Is there an easy way to do this using an igraph function in R?

Reproducible example:

library(igraph)
g <- sample_gnp(100,0.5,directed = TRUE)
v = V(g)[1:10]

Upvotes: 0

Views: 286

Answers (1)

G5W
G5W

Reputation: 37641

You can use the ends function to find the vertices adjacent to an edge. If both ends of an edge are in v, it is an internal edge. If an edge has one but not both ends in v, it is external.

E1 = which(sapply(E(g), function(e) ends(g, e)[1]) %in% v)
E2 = which(sapply(E(g), function(e) ends(g, e)[2]) %in% v)

Internal = intersect(E1, E2)
External = setdiff(union(E1,E2), Internal)

## Spot check answer
 ends(g, Internal[10])
     [,1] [,2]
[1,]    6    3
> ends(g, External[20])
     [,1] [,2]
[1,]    7   13

Upvotes: 1

Related Questions