मैं बंदरगाहों के नेटवर्क के बीच सबसे कम पथों की गणना करने के लिए 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
यह पूरी तरह से काम करता है। जगह के लिए धन्यवाद। –
एक पूर्ण न्यूबी क्वेंस्टियन: इस मामले में "वजन" या "दूरी", क्या यह कम है या दूसरी तरफ आसान है? एक उच्च वजन एक पथ के लिए एक किनारे अधिक बेहतर बनाता है? – Jason