2015-11-24 76 views
-1

我试图读取一个文件与源目标体重值分别找到最短的路径和打印与最终重量的路径。我使用Networkx来使用Dijkstra函数。然而,当我执行,我收到此通知:
<function shortest_path at 0x7f9a13c39230> <function bidirectional_dijkstra at 0x7f9a13c48140>Dijkstra的算法与Networkx的实现,显示打印错误

我最初使用只是shortest_path功能思维不知何故,我搞砸了使用它,因此利用bidirectional_dijkstra功能。

有问题的文件虚拟数据example.txt的标题为: D1 D5 7 D1 D2 6 D5 D4 7 D5 D3 7 D5 D3 3 D5 D2 4 D2 D2 1 D4 D3 1

我的方法在Python:

#!/usr/bin/env python 
import os.path 
import networkx as nx 
from sys import argv 


#!script, filename = argv 
#assigns example to open the file example.txt 
example = open("example.txt", "r") 

print("\n This is what we have in our file:\n") 

#prints the open file 
print example.read() 

G =nx.Graph() 

for line in example: 
    source, target, weight = line.split() 
    nx.shortest_path(G,[source, target, weight]) 

print (nx.shortest_path) 
print (nx.bidirectional_dijkstra) 
#for some reason, this is printing: 
# 
#<function shortest_path at 0x7fe480d56230> 
#<function bidirectional_dijkstra at 0x7fe480d65140> 
+0

你实际上没有调用该函数。即'nx.shortest_path'是实际的函数,'nx.shortest_path()'会给你返回值。 –

+0

所以,我应该将'nx.shortest_path'改为'nx.shortest_path()'? – Atychip

+0

那么你想要打印什么?看起来像你在while循环中正确调用它,但没有捕获返回值 –

回答

0

所以,我收到了,因为我没有正确定义的边缘的消息。 这里的更新代码:

import os.path 
    import networkx as nx 
    from sys import argv 


    #!script, filename = argv 
    #assigns example to open the file example.txt 
    example = open("/example.txt", "r") 


    G =nx.Graph() 

    for line in example: 
     line = line.split() 
     G.add_edge(line[0], line[1], weight=line[2]) 
     #nx.shortest_path(G,[source, target, weight]) 

    print nx.shortest_path(G) 
    print nx.average_shortest_path_length(G)