简体   繁体   English

查找节点 u 和 v 之间通过节点 g 的最短路径总数

[英]Find total number of shortest paths between nodes u and v that pass through node g

I have the following matrix that generates an undirected network diagram:我有以下生成无向网络图的矩阵:

  a b c d e f g h i j
a 0 1 1 0 0 0 0 0 0 0
b 1 0 1 0 0 0 0 0 0 0
c 1 1 0 1 1 0 1 0 0 0
d 0 0 1 0 1 0 0 0 0 0
e 0 0 1 1 0 1 0 0 0 0
f 0 0 0 0 1 0 1 0 0 0
g 0 0 1 0 0 1 0 1 0 0
h 0 0 0 0 0 0 1 0 1 1
i 0 0 0 0 0 0 0 1 0 0
j 0 0 0 0 0 0 0 1 0 0

m <- structure(c(0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 
0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 
0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, 0L, 0L), .Dim = c(10L, 10L), .Dimnames = list(
    c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), c("a", 
    "b", "c", "d", "e", "f", "g", "h", "i", "j")))

library(igraph)
g3n <- graph.adjacency(m)

I'm interested in manually calculating the betweeness of node 'g', which requires the finding of shortest paths among all possible nodes as the denominator and the numerator as the number of shortest paths contain node 'g'.我对手动计算节点“g”的介数很感兴趣,这需要在所有可能的节点中找到最短路径作为分母和分子,因为最短路径的数量包含节点“g”。

I used the following code to generate the lengths of shortest paths among all nodes:我使用以下代码生成所有节点之间最短路径的长度:

shortest.paths(g3n, v=V(g3n), to=V(g3n))

Shortest path matrix:最短路径矩阵:

  a b c d e f g h i j
a 0 1 1 2 2 3 2 3 4 4
b 1 0 1 2 2 3 2 3 4 4
c 1 1 0 1 1 2 1 2 3 3
d 2 2 1 0 1 2 2 3 4 4
e 2 2 1 1 0 1 2 3 4 4
f 3 3 2 2 1 0 1 2 3 3
g 2 2 1 2 2 1 0 1 2 2
h 3 3 2 3 3 2 1 0 1 1
i 4 4 3 4 4 3 2 1 0 2
j 4 4 3 4 4 3 2 1 2 0

Is there a way to count the number of times shortest path between 2 nodes contains node 'g' as a matrix or just in any other ways in R?有没有办法计算两个节点之间的最短路径包含节点“g”作为矩阵或仅以 R 中的任何其他方式包含的次数?

So, I'm not sure how elegant this solution is, but the following should work:所以,我不确定这个解决方案有多优雅,但以下应该有效:

#initialize a list to populate with all the shortest paths in the graphy
allpaths <- list()


#Assuming this is an undirected graph, we don't want to calculate both a %--% b and b %--% a  
for(x in V(g3n)$name){
  for(y in V(g3n)$name){
    if(x < y){
      shortest_path_options <- all_shortest_paths(g3n, x, y)$res

      #sometimes there are multiple shortest paths, we will include them all
      for(z in shortest_path_options){
        allpaths[[length(allpaths)+1]] <- z$name
      }
    }
}

#create a boolean of whether a shortest path contains 'g' or not
allpaths_bool <- sapply(allpaths, function(x){
  ('g' %in% x) & (head(x, 1) != 'g') & (tail(x, 1) != 'g')
  })

#Show all the paths that contain 'g'
allpaths[allpaths_bool]

You can calculate this for every vertex by wrapping everything into a sapply function.您可以通过将所有内容包装到sapply函数中来为每个顶点计算此值。

sapply(V(g3n)$name, function(x){
  temp_bool <- sapply(allpaths, function(y){
    (x %in% y) & (head(y, 1) != x) & (tail(y, 1) != x)
  })
  length(allpaths[temp_bool])
})

}

There has to be an easier way, but I'm not really sure of it.必须有一种更简单的方法,但我不太确定。 There might be a way to infer this information by using the betweeness function which provides the betweeness centrality measurements, but I'm not that well read up on graph theory.可能有一种方法可以通过使用betweeness函数来推断此信息,该函数提供了 betweeness 中心性测量,但我对图论的了解不是很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在R中使用igraph根据某些边缘属性找到两个节点之间的最短路径? - How to find shortest paths between two nodes based on certain edge attribute using igraph in R? 使用 R / igraph,有没有办法在考虑到唯一节点属性的计数的情况下找到节点之间的最短路径? - Using R / igraph, is there a way to find a shortest path between nodes taking the count of unique node attributes into account? 查找两个顶点(节点)之间的所有路径 - Find all paths between two vertices (nodes) 从get.shortest.paths()查找与路线距离有关的第二个变量的总数 - Find total of second variable related to the distance of route from get.shortest.paths() 计算相邻时间点之间的距离,并通过所有时间点找到“n”个最短路径 - Compute the distances between points of neighboring time points and find the `n` shortest paths though all time points 使用igraph / R查找所有最短路径 - Find All Shortest Paths using igraph/R 获取n个节点之间最短路径的子图 - Get subgraph of shortest path between n nodes 在R中的igraph网络中查找节点之间的连接数 - Find number of connections between nodes in igraph network in R xpathApply:如何传递多个路径或节点? - xpathApply: How to pass multiple paths or nodes? 从get.shortest.paths()中找出路线的距离 - Find distance of route from get.shortest.paths()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM