R 子集有向图

R 子集有向图,r,igraph,network-analysis,R,Igraph,Network Analysis,我在igraph中使用有向网络。以下是生成此类网络的一些代码: # 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) #示例

我在igraph中使用有向网络。以下是生成此类网络的一些代码:

# 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)
#示例图
#install.packages(c(“igraph”),dependencies=TRUE)
图书馆(igraph)
种子(1)

g看来,
edge\u connectivity
函数是这个任务的一个很好的候选者。它具有<代码>源<代码>和<代码>目标< /代码>参数,考虑边缘的方向性:

 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)

除了@42-,我认为还可以使用

> 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
到其他节点具有非零和有限距离的顶点索引。

您可以使用
make\u ego\u graph
获得特定节点的邻域图。将顺序设置为顶点数(或n-1),以允许遍历整个图形

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

我认为这里给出的三个答案都很有用,但@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)
d=距离(g,到'e',模式'out')
删除
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)