473,499 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dijkstra's Algoithm

1 New Member
I Am having problems using Dijkstra's algorithm to calculate path and distance from a favorite point to certain junctions. This is my code:
Expand|Select|Wrap|Line Numbers
  1. import dijkstra_epp
  2.  
  3. file_loc = 'join_intersect.txt'
  4.  
  5. # create the file handle
  6. file = open(file_loc, 'r')
  7.  
  8. # put all lines from file in a list
  9. lines = file.readlines()
  10.  
  11. # dump/pop the header row
  12. header = lines.pop(0)
  13.  
  14. tokenlist = []              # initialize new list
  15.  
  16. # tokenizing the list
  17. for n in lines:
  18.     newline = n.split('\t')
  19.     tokenlist.append(newline)
  20.  
  21. # cleaning up the number element
  22. for n in tokenlist:
  23.     n[2] = float(n[2])
  24.  
  25. # extract edges and lengths from dataset
  26. edges = {}
  27.  
  28. # traverse the tokenlist and add edge weights to dict.
  29. for n in tokenlist:
  30.  
  31.     edge = n[1]
  32.     weight = n[2]
  33.  
  34.     if not edges.has_key(edge):
  35.         edges[edge] = weight    
  36.  
  37.  
  38. # Build adj. list structure for storing graph
  39. #
  40.  
  41.  
  42. # populate dictionary of edges
  43. edgList = {}
  44. for n in tokenlist:
  45.     if not edgList.has_key(n[0]):
  46.         edgList[n[1]] = []
  47. ##
  48.  
  49.  
  50. # determine which edges are associated with which nodes
  51. for n in tokenlist:
  52.     edgList[n[1]].append(n[0])
  53.  
  54.  
  55.  
  56. # populate shell of adjacency list
  57. adjList = {}
  58.  
  59. for n in edgList.keys():
  60.     nodes = edgList[n]
  61.     if not adjList.has_key(nodes[0]):
  62.         adjList[nodes[0]] = {}
  63.     if not adjList.has_key(nodes[1]):
  64.         adjList[nodes[1]] = {}
  65.  
  66. for n in edgList.keys():
  67.     nodes = edgList[n]
  68.  
  69.     adjList[nodes[0]][nodes[1]] = edges[n]
  70.     adjList[nodes[1]][nodes[0]] = edges[n]
  71.  
  72.  
  73. #
  74. # begin dijkstra
  75. #
  76.  
  77. path1, dist1 = dijkstra_epp.shortestPath(adjList, 'J2', 'J14')
  78.  
  79. #initializes a new list without my favorite place
  80.  
  81. newlist = [] 
  82.  
  83. # creates a new list that appends the adjList so that my favorite place is removed
  84.  
  85. for n in adjList:  
  86.     n != "JuncID1072"
  87.     newlist.append(adjList)
  88.  
  89. favoriteplace = []
  90.  
  91. for n in adjList:
  92.     n = "JuncID1072"
  93.     favoriteplace.append(adjList)
  94.  
  95. travelcosts = {}
  96. for n in newlist:
  97.  
  98.     path1, dist1 = dijkstra_epp.shortestPath(newlist, 'JuncID1027', 'n')
This is the error: any ideas?

Traceback (most recent call last):
File "K:\Spatial_Modelling\Lab4\Lab_Transportation\shay slab4.py", line 104, in <module>
path1, dist1 = dijkstra_epp.shortestPath(newlist, 'JuncID1027', 'n')
File "K:\Spatial_Modelling\Lab4\Lab_Transportation\dijk stra_epp.py", line 80, in shortestPath
D,P = Dijkstra(G,start,end)
File "K:\Spatial_Modelling\Lab4\Lab_Transportation\dijk stra_epp.py", line 59, in Dijkstra
for w in G[v]:
TypeError: list indices must be integers, not str
Oct 30 '11 #1
2 2299
Glenton
391 Recognized Expert Contributor
Hi

You're not showing your graph class (G, I assume).

But there's a list being used somewhere where you're feeding it a string instead of an integer.

I'd be willing to bet that it's the v in G[v] in dijkstra_epp.py that is a string instead of an integer.
Oct 31 '11 #2
Glenton
391 Recognized Expert Contributor
Incidentally, I implemented a dijkstra algorithm some time back and wrote about it here:
http://bytes.com/topic/python/insigh...shortest-route
Oct 31 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
1559
by: Philip Smith | last post by:
I've read with interest the continuing debate about 'lambda' and its place in Python. Just to say that personally I think its an elegant and useful construct for many types of programming task...
3
2092
by: Lars Netzel | last post by:
Hi, I have by reading thru the bytes of a bitmap and added an edge detection filter, created an image with more distinct edges. From this I would like to find a recangle shaped object. How can I...
4
6980
by: thomasau | last post by:
Hi everyone ! Can someone point me to a good and simple implementation of the Dijkstras algorithm using C . Thanks! -Thomas
4
2501
bucketbot
by: bucketbot | last post by:
Can anyone explain how to code a graph based on a priority queue? As in, what would I need to do in order to successfully implement a graph? thanks
0
1465
by: georgerxz | last post by:
Download source code in C for learning Data structure implementation from http://zsoftwares.googlepages.com/CPrograms.html and http://zsoftwares.googlepages.com/DSFPrograms.htm Source...
9
6361
by: X106 | last post by:
how to using the technique of Array so that I can input 5 values and then the program will find out which value is the large one. Who can help me???? THANK!!!!!
10
1872
by: colin | last post by:
Hi, I profile my code and find its spending a lot of time doing implicit conversions from similar structures. the conversions are mainly things like this class Point { implicit conversion...
5
2333
by: onkardesai | last post by:
#include<stdio.h> int main() { int a,b,w,v,e,i; int g,visited,d,p; printf("enter the number of vertices"); scanf("%d",&v); printf("enter the number of edges"); scanf("%d",&e);
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.