Subscript Editor

I work with a directional network in graphics. Here is some code to create such a network:

# example graph
# install.packages(c("igraph"), dependencies = TRUE)
library(igraph)
set.seed(1)
g <- erdos.renyi.game(20, 1/20,directed=TRUE,loops=FALSE)
V(g)$name <- letters[1:20]
par(mar=rep(0,4))
plot(g)

I would like to extract subsets of this network that include an arbitrary vertex and all edges and vertices that are directed to this vertex regardless of the degree or distance of this connection.

Here is an example of Photoshop that I would like to extract using the vertex "E" in this case. I would like to retrieve a network that includes all the vertices marked with blue and connected edges. Network subset

+4
source share
4 answers

In addition, that @ 42-, I think, distancecan also be used.

> d = distances(g, to='e', mode='out')
> V(g)[which(!is.infinite(d) & d >0)]
+ 4/20 vertices, named:
[1] a n r t

, , e .

+3

, edge_connectivity . source target, :

 sapply(V(g) , function(v) if( v != 5){edge_connectivity(g, source=v, target=5)} else NA )
 a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t 
 1  0  0  0 NA  0  0  0  0  0  0  0  0  1  0  0  0  1  0  1 
 # set the retruned vector to the value named `e_con`

 #then select the vertices
 V(g)[ which(e_con >0)]
+ 4/20 vertices, named, from ba45ff0:
[1] a n r t

:

small_g <- delete_vertices( g, !V(g) %in% c(5, V(g)[ which(econ >0)] ))

plot( small_g)

enter image description here

:

edges(small_g)
[[1]]
IGRAPH 11d63f6 DN-- 5 4 -- Erdos renyi (gnp) graph
+ attr: name (g/c), type (g/c), loops (g/l), p (g/n), name (v/c)
+ edges from 11d63f6 (vertex names):
[1] n->a t->a a->e r->e
+2

node (s) make_ego_graph. ( n-1), .

make_ego_graph(g, order=length(V(g)), nodes="e", mode="in")

enter image description here

+1

, , @Zhiya - , (, -, ).

, @42-, . , :

d = distances(g, to='e', mode='out')
vertices.to.delete <- row.names(d)[which(is.infinite(d))]
g1 <- igraph::delete.vertices(g, vertices.to.delete)
0

Source: https://habr.com/ru/post/1692384/


All Articles