¿Cómo encuentro los bordes de un vértice usando igraph y R?


Digamos que tengo este gráfico de ejemplo, quiero encontrar los bordes conectados al vértice ' 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)

Puedo encontrar fácilmente un vértice:

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

Pero necesito hacer referencia a todas las aristas conectadas al vértice 'a'.

 22
Author: tommy chheng, 2010-07-22

4 answers

Ver la documentación sobre iteradores igraph; en particular las funciones from() y to ().

En su ejemplo," a " es V (g) [0], por lo que para encontrar todas las aristas conectadas a "a":

E(g) [ from(0) ]

Resultado:

[0] b -- a
[1] c -- a
[2] d -- a
 25
Author: neilfws,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-22 05:14:44

Si no conoce el índice del vértice, puede encontrarlo usando match() antes de usar la función from ().

idx <- match("a", V(g)$name)
E(g) [ from(idx) ]
 4
Author: cannin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-10 11:02:09

Encontró una versión más simple que combina los dos esfuerzos anteriores que también puede ser útil.

E(g)[from(V(g)["name"])]
 3
Author: jtclaypool,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-12 20:09:45

Utilizo esta función para obtener el número de aristas para todos los nodos:

sapply(V(g)$name, function(x) length(E(g)[from(V(g)[x])]))
 1
Author: Dr_K_Lo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-17 15:04:39