2013-01-16 28 views
7

मैं बंदरगाहों के नेटवर्क के बीच सबसे कम पथों की गणना करने के लिए Python 2.7 Enthought distribution में networkx पैकेज का उपयोग कर रहा हूं। यह dijkstra_path_length का उपयोग करके दूरी की गणना करने के लिए ठीक काम कर रहा है, लेकिन मुझे यह भी पता होना चाहिए कि dijkstra_path (एक तरफ के रूप में, मुझे लगता है कि अगर मैं पहले पथ की गणना करता हूं तो इसे चलाने के लिए तेज़ होना चाहिए, फिर पथ से लंबाई की गणना करें एक ही डेटा पर दो बार डिजस्ट्रा के एल्गोरिदम को चलाने के बजाए)। हालांकि पथ कार्य विफल रहा है, list indices must be integers, not str कह रहा है।नेटवर्कएक्स का उपयोग करके भारित ग्राफ में सबसे कम पथ कैसे खोजें?

यहां कोड है जो त्रुटि उत्पन्न करता है। क्या कोई मुझे बता सकता है कि मैं क्या गलत कर रहा हूं?

import networkx as nx 

# Create graph 
network_graph = nx.Graph() 
f_routes = open('routes-list.txt', 'rb') 
# Assign list items to variables 
for line in f_routes: 
    route_list = line.split(",") 
    orig = route_list[0] 
    dest = route_list[1] 
    distance = float(route_list[2]) 
    # Add route as an edge to the graph 
    network_graph.add_edge(orig, dest, distance=(distance)) 

# Loop through all destination and origin pairs 
for destination in network_graph: 
    for origin in network_graph: 
     # This line works 
     length = nx.dijkstra_path_length(network_graph, origin, destination, "distance") 
     # This line fails 
     path = nx.dijkstra_path(network_graph, origin, destination, "distance") 

मुझे ट्रेसबैक में निम्नलिखित मिल रहा है।

Traceback (most recent call last): 
    File "C:\Users\jamie.bull\workspace\Shipping\src\shortest_path.py", line 67, in <module> 
    path = nx.dijkstra_path(network_graph, origin, destination, "distance") 
    File "C:\Enthought\Python27\lib\site-packages\networkx\algorithms\shortest_paths\weighted.py", line 74, in dijkstra_path 
    return path[target] 
TypeError: list indices must be integers, not str 

उत्तर

13

एक सा प्रयोग, ऐसा लगता है जब मूल और गंतव्य नोड्स एक ही हैं कि nx.dijkstra_path एक भ्रामक अपवाद को जन्म देती है:

>>> import networkx as nx 
>>> g = nx.Graph() 
>>> g.add_edge('a', 'b', distance=0.3) 
>>> g.add_edge('a', 'c', distance=0.7) 
>>> nx.dijkstra_path_length(g, 'b', 'c', 'distance') 
1.0 
>>> nx.dijkstra_path(g, 'b', 'c', 'distance') 
['b', 'a', 'c'] 
>>> nx.dijkstra_path_length(g, 'b', 'b', 'distance') 
0 
>>> nx.dijkstra_path(g, 'b', 'b', 'distance') 
Traceback (most recent call last): 
    File "<pyshell#7>", line 1, in <module> 
    nx.dijkstra_path(g, 'b', 'b', 'distance') 
    File "C:\Users\barberm\AppData\Roaming\Python\Python27\site-packages\networkx\algorithms\shortest_paths\weighted.py", line 74, in dijkstra_path 
    return path[target] 
TypeError: list indices must be integers, not str 

तो बस एक स्पष्ट परीक्षण है कि क्या destination और origin एक ही कर रहे हैं, और जब वे हैं तो इसे अलग से संभाल लें।

+0

यह पूरी तरह से काम करता है। जगह के लिए धन्यवाद। –

+0

एक पूर्ण न्यूबी क्वेंस्टियन: इस मामले में "वजन" या "दूरी", क्या यह कम है या दूसरी तरफ आसान है? एक उच्च वजन एक पथ के लिए एक किनारे अधिक बेहतर बनाता है? – Jason

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^