Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-igraph中方向子图的连接_R_Connection_Igraph_Shortest Path - Fatal编程技术网

R-igraph中方向子图的连接

R-igraph中方向子图的连接,r,connection,igraph,shortest-path,R,Connection,Igraph,Shortest Path,我有一个方向加权图,它由两个或多个断开连接的子图组成,除了权重之外,还有一些属性: 我的最终目标是找到最短路径,但这只能对连通图完成 因此,我需要连接这两个子图,边在3和4之间,权重可能很大,所以最后我只有一个图。通常,顶点名称是定义从小到大方向的日期。可能存在多个间隙。在您的情况下,您可以执行以下操作: 谢谢你的评论 在您的情况下,您可以: 谢谢你的评论 如果有多个间隙,即两个以上的簇,可以尝试下面的代码 e <- c(sapply(decompose(g),function(x) na

我有一个方向加权图,它由两个或多个断开连接的子图组成,除了权重之外,还有一些属性:

我的最终目标是找到最短路径,但这只能对连通图完成


因此,我需要连接这两个子图,边在3和4之间,权重可能很大,所以最后我只有一个图。通常,顶点名称是定义从小到大方向的日期。可能存在多个间隙。

在您的情况下,您可以执行以下操作: 谢谢你的评论


在您的情况下,您可以: 谢谢你的评论


如果有多个间隙,即两个以上的簇,可以尝试下面的代码

e <- c(sapply(decompose(g),function(x) names(V(x))[degree(x)==1]))
G <- g %>% 
  add.edges(e[2:(length(e)-1)],weight = vcount(g))
资料


如果有多个间隙,即两个以上的簇,可以尝试下面的代码

e <- c(sapply(decompose(g),function(x) names(V(x))[degree(x)==1]))
G <- g %>% 
  add.edges(e[2:(length(e)-1)],weight = vcount(g))
资料


我们也可以在add.edges中加入weight=vcountg。谢谢,这有助于我理解如何添加边。我们也可以在add.edges中加入weight=vcountg。谢谢,这有助于我理解如何添加边。谢谢,我只是修改了一行,因为我的图的度数通常不等于1:Ethak you,我只是修改了一行,因为我的图的度数通常不等于一:e
e <- c(sapply(decompose(g),function(x) names(V(x))[degree(x)==1]))
G <- g %>% 
  add.edges(e[2:(length(e)-1)],weight = vcount(g))
> get.data.frame(G)
  from to weight attr
1    1  2      1  0.1
2    2  3      1  0.1
3    4  5      1  0.1
4    5  6      1  0.1
5    7  8      1  0.1
6    8  9      1  0.1
7    3  4      9   NA
8    6  7      9   NA
df <-
  data.frame(
    from = c(1, 2, 4, 5, 7, 8),
    to = c(2, 3, 5, 6, 8, 9),
    weight = c(1, 1, 1, 1, 1, 1),
    attr = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
  )