无法从networkx中的边缘列表中读取

时间:2017-02-10 14:23:27

标签: python networkx digraphs

计算最短路径似乎不适用于我:

import networkx as nx
G = nx.read_edgelist(filename, delimiter=",", create_using=nx.DiGraph(), nodetype=int)
print G 
print nx.shortest_path(G, 1, 5)

我正在阅读的文件是:

1,2
2,3
3,4
4,5

没有输出。

1 个答案:

答案 0 :(得分:0)

您的代码适合我。

import networkx as nx
import matplotlib.pyplot as plt

filename = 'test.txt'
G = nx.read_edgelist(filename, delimiter=",", create_using=nx.DiGraph(), nodetype=int)

nx.draw_networkx(G, with_labels=True)
plt.axis('off')
plt.show()

print(type(G), G)
# (<class 'networkx.classes.digraph.DiGraph'>, <networkx.classes.digraph.DiGraph object at 0x10e29c150>)

print(nx.shortest_path(G, 1, 5))
#[1, 2, 3, 4, 5]

产生,

enter image description here