I Am having problems using Dijkstra's algorithm to calculate path and distance from a favorite point to certain junctions. This is my code: - import dijkstra_epp
-
-
file_loc = 'join_intersect.txt'
-
-
# create the file handle
-
file = open(file_loc, 'r')
-
-
# put all lines from file in a list
-
lines = file.readlines()
-
-
# dump/pop the header row
-
header = lines.pop(0)
-
-
tokenlist = [] # initialize new list
-
-
# tokenizing the list
-
for n in lines:
-
newline = n.split('\t')
-
tokenlist.append(newline)
-
-
# cleaning up the number element
-
for n in tokenlist:
-
n[2] = float(n[2])
-
-
# extract edges and lengths from dataset
-
edges = {}
-
-
# traverse the tokenlist and add edge weights to dict.
-
for n in tokenlist:
-
-
edge = n[1]
-
weight = n[2]
-
-
if not edges.has_key(edge):
-
edges[edge] = weight
-
-
-
# Build adj. list structure for storing graph
-
#
-
-
-
# populate dictionary of edges
-
edgList = {}
-
for n in tokenlist:
-
if not edgList.has_key(n[0]):
-
edgList[n[1]] = []
-
##
-
-
-
# determine which edges are associated with which nodes
-
for n in tokenlist:
-
edgList[n[1]].append(n[0])
-
-
-
-
# populate shell of adjacency list
-
adjList = {}
-
-
for n in edgList.keys():
-
nodes = edgList[n]
-
if not adjList.has_key(nodes[0]):
-
adjList[nodes[0]] = {}
-
if not adjList.has_key(nodes[1]):
-
adjList[nodes[1]] = {}
-
-
for n in edgList.keys():
-
nodes = edgList[n]
-
-
adjList[nodes[0]][nodes[1]] = edges[n]
-
adjList[nodes[1]][nodes[0]] = edges[n]
-
-
-
#
-
# begin dijkstra
-
#
-
-
path1, dist1 = dijkstra_epp.shortestPath(adjList, 'J2', 'J14')
-
-
#initializes a new list without my favorite place
-
-
newlist = []
-
-
# creates a new list that appends the adjList so that my favorite place is removed
-
-
for n in adjList:
-
n != "JuncID1072"
-
newlist.append(adjList)
-
-
favoriteplace = []
-
-
for n in adjList:
-
n = "JuncID1072"
-
favoriteplace.append(adjList)
-
-
travelcosts = {}
-
for n in newlist:
-
-
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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
|
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
|
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
| |
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!!!!!
|
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;
|
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);
|
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...
|
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,...
|
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,...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |