473,624 Members | 2,612 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_Mod elling\Lab4\Lab _Transportation \shayslab4.py", line 104, in <module>
path1, dist1 = dijkstra_epp.sh ortestPath(newl ist, 'JuncID1027', 'n')
File "K:\Spatial_Mod elling\Lab4\Lab _Transportation \dijkstra_epp.p y", line 80, in shortestPath
D,P = Dijkstra(G,star t,end)
File "K:\Spatial_Mod elling\Lab4\Lab _Transportation \dijkstra_epp.p y", line 59, in Dijkstra
for w in G[v]:
TypeError: list indices must be integers, not str
Oct 30 '11 #1
2 2314
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
1568
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 (particularly number theory/artificial intelligence/genetic algorithms) I can't think why anyone would be proposing to do away with it. Sometimes an anonymous function is just what you need and surely it just reflects the python philosophy of...
3
2106
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 determine where in the picture this recangle shape is and how big it is? best regards /Lars
4
6994
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
2506
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
1472
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 codes given on above website are based on - Arrays
9
6372
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
1882
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 to vector3; //this conversion just returns positon Vector3 position;
5
2348
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
8246
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8490
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.