Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.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
使用igraph/R查找所有最短路径_R_Graph_Igraph - Fatal编程技术网

使用igraph/R查找所有最短路径

使用igraph/R查找所有最短路径,r,graph,igraph,R,Graph,Igraph,首先,我对R不是很精通,但我有一个由100个节点组成的网络,我正在对其进行分析。我正在寻找网络中每一对节点之间的所有最短路径(100次计算),诀窍是将它们作为整数返回,路径长度(如果愿意) #construct a small sample of the graph g = graph.formula('insert graph') #use the function to retrieve shortest paths from a single vertex get.all.shorte

首先,我对R不是很精通,但我有一个由100个节点组成的网络,我正在对其进行分析。我正在寻找网络中每一对节点之间的所有最短路径(100次计算),诀窍是将它们作为整数返回,路径长度(如果愿意)

#construct a small sample of the graph
g = graph.formula('insert graph') 

#use the function to retrieve shortest paths from a single vertex
get.all.shortest.paths(g, 1, to = V(g))

#output
$res
$res[[1]]
[1] 1

$res[[2]]
[1] 1 2

$res[[3]]
[1] 1 2 3

$res[[4]]
[1] 1 2 3 4

$res[[5]]
[1] 1 2 3 4 5

$res[[6]]
[1]  1 10  9  8  7  6

$res[[7]]
[1] 1 2 3 4 5 6

$res[[8]]
[1]  1 10  9  8  7

$res[[9]]
[1]  1 10  9  8

$res[[10]]
[1]  1 10  9

$res[[11]]
[1]  1 10


$nrgeo
 [1] 1 1 1 1 1 2 1 1 1 1
虽然这很好,但手动迭代所有100个节点并不实际。有没有一种自动化的方法?问题的第二个部分是,我希望将每条路径作为数字输出,比如路径长度,而不是给定实际路径。我想做的另一件事是计算模式路径长度(或网络中更常见的路径长度)。查看100个数字是不可行的,因此必须实现自动化


如果您需要任何帮助,我们将不胜感激,这可能看起来像是一个新手问题,我是一个新手。

如果您只需要长度,您应该使用
path.length.hist
。要获得模式,只需使用

which.max(path.length.hist(g)$res)

您可以使用
最短路径
,返回每个节点之间的距离矩阵:

distMatrix <- shortest.paths(g, v=V(g), to=V(g))
而且它非常容易访问:

# e.g. shortest path length between the second node and the fifth
distMatrix[2,5]
>
[1] 3

# or using node names:
distMatrix["ste20", "mth1"]
>
[1] 3

极好的这给了我模式,但我将使用原始数据进行额外的分析。当然可以,谢谢。有没有办法对每个节点自动执行此操作?此函数生成一个包含所有节点之间所有距离的矩阵。。。不仅仅是从=1到所有顶点…非常感谢,这确实做到了所需要的。我用它来绘制直方图,它也给了我模式@codeBarer:您应该使用
get.shortest.paths
函数,而不是
shortest.paths
,因为后者提供路径,而不仅仅是距离@digEmAll谢谢,我使用了get.shortest.paths,它返回了路径上遇到的所有顶点的id/索引。这帮了大忙!:)
# e.g. shortest path length between the second node and the fifth
distMatrix[2,5]
>
[1] 3

# or using node names:
distMatrix["ste20", "mth1"]
>
[1] 3