1

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

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

没有输出。

4

1 回答 1

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]

产生,

在此处输入图像描述

于 2017-02-10T15:09:26.157 回答