计算最短路径似乎不适用于我:
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
没有输出。
答案 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]
产生,