存在任何形式来计算分隔2个节点的边数,例如,如果我们有节点“a”、“b”、“c”和“d”,形式为“a”-“b”-“c”-“d”(其中“-”是边),我需要计算“a”和“d”之间的边。在
真正的例子如下。我有一个很大的图,但是在这个链接中你可以看到一个图片https://drive.google.com/file/d/0B7GayK8MGGtCcVhRMncyM0VMc2c/view?usp=sharing
在本例中,图有2806个节点,我需要知道例如,有多少条边将616的节点608分开。我原以为number_of_edges函数可以帮助我,但现在我认为只有2个节点连接或不连接时才会返回(因为在这样的代码中返回1或0)
for k in id1: #id1 is a list of nodes
for l in id2: #id2 is a list of nodes
print graph.number_of_edges(k,l)
在不知道您尝试了什么,也没有示例图的情况下,我将给您一个简单的示例。它可能会帮你解决问题。在
我将使用newtworkx和numpy从adjacency matrix生成一个4节点4边的图。在
这将打印我们的图表:
^{2}$现在将这个邻接矩阵输入networkx:
绘制:
现在,我们可以通过以下方式查看哪些节点相互连接:
正如预期的那样:
所以如果你从},那么a和b不是相邻的(不是它们之间的边)。在
number_of_edges(a, b)
得到{[编辑:如果我们想找到2和0之间的所有路径,可以执行以下操作
或寻找最短路径:
在这种情况下,你可以说:
]
相关问题 更多 >
编程相关推荐