Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the edges of a vertex using igraph and R?

Tags:

r

igraph

Say I have this example graph, i want to find the edges connected to vertex 'a'

 d <- data.frame(p1=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd'),                  p2=c('b', 'c', 'd', 'c', 'd', 'e', 'd', 'e', 'e'))  library(igraph) g <- graph.data.frame(d, directed=FALSE) print(g, e=TRUE, v=TRUE) 

I can easily find a vertex:

 V(g)[V(g)$name == 'a' ] 

But i need to reference all the edges connected to the vertex 'a'.

like image 393
tommy chheng Avatar asked Jul 22 '10 03:07

tommy chheng


People also ask

How do you find the edges of a vertex?

The sum of the vertex degree values is twice the number of edges, because each of the edges has been counted from both ends. In your case 6 vertices of degree 4 mean there are (6×4)/2=12 edges.

How do you delete edges in Igraph?

The minus operator (' - ') can be used to remove vertices or edges from the graph. The operation performed is selected based on the type of the right hand side argument: If it is an igraph graph object, then the difference of the two graphs is calculated, see difference .


2 Answers

See the documentation on igraph iterators; in particular the from() and to() functions.

In your example, "a" is V(g)[0], so to find all edges connected to "a":

E(g) [ from(0) ] 

Result:

[0] b -- a [1] c -- a [2] d -- a 
like image 180
neilfws Avatar answered Sep 29 '22 22:09

neilfws


If you do not know the index of the vertex, you can find it using match() before using the from() function.

idx <- match("a", V(g)$name) E(g) [ from(idx) ] 
like image 37
cannin Avatar answered Sep 29 '22 21:09

cannin