【问题标题】:Faster way to calculate the number of shortest paths a vertex belongs to using Networkx使用 Networkx 更快地计算顶点所属的最短路径数
【发布时间】:2013-06-13 16:33:25
【问题描述】:

我正在考虑顶点i应力i所属的所有顶点对之间的最短路径数。

我正在尝试使用 Networkx 进行计算,到目前为止我已经通过三种方式进行了计算。 可读dirtydirtiest,但没有一个是快速的。实际上,我希望它比 Networkx 上的 betweenness (source) 更快。有没有更好的计算方法?提前感谢提供任何建议、回答或评论。下面看看我到目前为止做了什么:

Ps.:Here 是一个馅饼,如果你想尝试一下,代码已经准备好了,再次感谢。

这是所有版本的共同部分:

import networkx as nx
from collections import defaultdict

最脏的,振作起来:

def stress_centrality_dirtiest(g):

  stress = defaultdict(int)

  for a in nx.nodes_iter(g):
    for b in nx.nodes_iter(g):
      if a==b:
        continue
      # pred = nx.predecessor(G,b)  # for unweighted graphs
      pred, distance = nx.dijkstra_predecessor_and_distance(g,b)  # for weighted graphs
      if not pred.has_key(a):
        return [] 
      path = [[a,0]] 
      path_length = 1
      index = 0
      while index >= 0: 
        n,i = path[index] 
        if n == b: 
          for vertex in map(lambda x:x[0], path[:index+1])[1:-1]:
            stress[vertex] += 1
        if len(pred[n]) > i: 
          index += 1 
          if index == path_length: 
            path.append([pred[n][i],0]) 
            path_length += 1 
          else: 
            path[index] = [pred[n][i],0] 
        else: 
          index -= 1 
          if index >= 0: 
            path[index][4] += 1 
  return stress

def stress_centrality_dirty(g):

  stress = defaultdict(int)

  paths = nx.all_pairs_dijkstra_path(g)
  for item in paths.values():
    for element in item.values():
      if len(element) > 2:
        for vertex in element[1:-1]:
          stress[vertex] += 1
  return stress

可读

def stress_centrality_readable(g):

  stress = defaultdict(int)

  paths = nx.all_pairs_dijkstra_path(g)
  for source in nx.nodes_iter(g):
    for end in nx.nodes_iter(g):
      if source == end:
        continue
      path = paths[source][end]
      if len(path) > 2:                                         # path must contains at least 3 vertices source - another node - end
        for vertex in path[1:-1]:                               # when counting the number of occurrencies, exclude source and end vertices
          stress[vertex] += 1
  return stress

【问题讨论】:

    标签: python performance graph shortest-path networkx


    【解决方案1】:

    您在 NetworkX 中指向的中介代码几乎可以满足您的需求,并且可以轻松调整。

    在中间函数中,如果您在“累积”阶段调用以下函数(而不是 _accumulate_basic),它应该计算应力中心性(未经测试)

    def _accumulate_stress(betweenness,S,P,sigma,s):
        delta = dict.fromkeys(S,0)
        while S:
            w = S.pop()
            for v in P[w]:
                delta[v] += (1.0+delta[w])
            if w != s:
                betweenness[w] += sigma[w]*delta[w]
        return betweenness
    

    请参阅论文 Ulrik Brandes:关于最短路径中介中心性及其通用计算的变体。社交网络 30(2):136-145, 2008。http://www.inf.uni-konstanz.de/algo/publications/b-vspbc-08.pdf

    应力中心算法是算法 12。

    【讨论】:

      【解决方案2】:

      根据here 得到的答案,我尝试做同样的事情。

      我的尝试围绕着使用nx.all_shortest_paths(G,source,target) 函数,它产生了一个生成器:

      counts={}
      for n in G.nodes(): counts[n]=0
      for n in G.nodes():
          for j in G.nodes():
              if (n!=j):
                  gener=nx.all_shortest_paths(G,source=n,target=j) #A generator
                  print('From node '+str(n)+' to '+str(j))
                  for p in gener:
                      print(p) 
                      for v in p: counts[v]+=1
                  print('------')
      

      我用100 节点的NxN 网格网络测试了这段代码,我花了大约 168 秒得到结果。现在我知道这不是最好的答案,因为这段代码没有优化,但我想你可能想知道它。希望我能得到一些关于如何改进我的代码的指导。

      【讨论】:

        猜你喜欢
        • 2020-01-20
        • 2014-06-21
        • 2016-03-26
        • 1970-01-01
        • 1970-01-01
        • 2016-10-30
        • 2023-04-11
        • 1970-01-01
        • 2014-10-16
        相关资源
        最近更新 更多