473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Calculating Distance From Start Node - Dijkstra's Algorithm

Hello,

I am having some trouble in working out the distance of each node from the starting node in my Dijkstra Algorithm code. Here is my code with the section i'm stuck on in bold:

Expand|Select|Wrap|Line Numbers
  1. infinity = 1000000
  2. invalid_node = -1
  3. startNode = 0
  4.  
  5. class Node:
  6.      distFromSource = infinity
  7.      previous = invalid_node
  8.      visited = False
  9.  
  10. def populateNodeTable(): 
  11.     nodeTable = []
  12.     index =0
  13.     f = open('route.txt', 'r')
  14.     for line in f: 
  15.       node = map(int, line.split(',')) 
  16.       nodeTable.append(Node()) 
  17.       print nodeTable[index].previous 
  18.       print nodeTable[index].distFromSource 
  19.       index +=1
  20.     nodeTable[startNode].distFromSource = 0 
  21.  
  22.     return nodeTable
  23.  
  24. def tentativeDistance(currentNode, nodeTable):
  25.     nearestNeighbour = []
  26.     for currentNode in nodeTable:
  27. #         if Node[currentNode].distFromSource + 
  28.      currentDistance = startNode + currentNode
  29. #      currentDistance = currentNode.distFromSource + nodeTable.currentNode 
  30.          currentNode.previous = currentNode
  31.          currentNode.length = currentDistance
  32.          currentNode.visited = True
  33.          currentNode +=1
  34.          nearestNeighbour.append(currentNode)
  35.          print nearestNeighbour
  36.  
  37.     return nearestNeighbour
  38.  
  39. currentNode = startNode
  40.  
  41. if __name__ == "__main__":
  42.     populateNodeTable()
  43.     tentativeDistance(currentNode,populateNodeTable())
I've no idea on where I am going wrong, i've tried a few things now but nothing seems to work
Mar 8 '11 #1
1 2898
dwblas
626 Expert 512MB
You are using currentNode for two different variables.
Expand|Select|Wrap|Line Numbers
  1. def tentativeDistance(currentNode, nodeTable):
  2.     # and
  3.     for currentNode in nodeTable: 
Print the values so you know what they are. For example:
Expand|Select|Wrap|Line Numbers
  1. print "startNode =", startNode, type(startNode)
  2. print "currentNode =", currentNode, type(currentNode)
  3. #
  4. #Node[currentNode].distFromSource + 
  5. #      currentDistance = startNode + currentNode 
Perhaps a dictionary of classes would be easier to understand than a list of classes.
Expand|Select|Wrap|Line Numbers
  1. class Node:
  2.     def __init__(self, previous):
  3.        infinity = 1000000
  4.        invalid_node = -1
  5.        self.distFromSource = infinity
  6.        self.previous = previous
  7.        self.visited = False
  8.  
  9.  
  10. def populateNodeTable(): 
  11.     startNode = 0
  12.     ## dictionary with key = record or node number
  13.     nodeTable = {}
  14. ##    f = open('route.txt', 'r')
  15.     f = range(10)     ## test data
  16.     for index, line in enumerate(f): 
  17.         nodeTable[index] = Node(index-1)      ## index-1 = previous record
  18.     nodeTable[startNode].distFromSource = 0
  19.     nodeTable[startNode].previous = -1
  20.  
  21.     ## print ascending
  22.     for node in range(0, len(nodeTable)):
  23.         ## add to distFromSource
  24.         nodeTable[node].distFromSource += node
  25.         print node, nodeTable[node].distFromSource
  26.  
  27.     ## print descending using another method
  28.     print "-"*30
  29.     index = 9     ## last node number 
  30.     while index > -1:
  31.         print index, nodeTable[index].distFromSource, \
  32.               "previous =", nodeTable[index].previous
  33.         index = nodeTable[index].previous
  34.  
  35.  
  36. populateNodeTable() 
Mar 8 '11 #2

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

Similar topics

0
by: Der Andere | last post by:
Do you know of an efficient implementation (priority queues) of Dijkstra's algorithm for calculating the shortest distances between vertices in a graph? Cheers, Matthias -- Für emails...
3
by: A_StClaire_ | last post by:
implemented Dijkstra's algorithm as follows. plz pour on the negative criticism cuz I know I have a ton to learn. oh, before you flame me regarding the language, my class was told to do this in...
2
by: A_StClaire_ | last post by:
hi, I am trying to do this thing in C# and was wondering if it's customary for the algorithm to backtrack. is its purpose only to find the shortest path from a to z, or does one normally...
3
by: Ook | last post by:
This is probably a bit OT, as I'm not looking for a c++ implementaiton of Dijkstra's algorithm, rather I'm just trying to understand it (is there a better place then here to ask this question?)....
9
by: Josh Zenker | last post by:
I've been working on an implementation of Dijkstra's algorithm on and off for the past few days. I thought I was close to being finished, but clearly I've done something wrong. I'm not a very...
8
by: abhradwip | last post by:
I want to write a program which will find the shortest path between n no. of cities using dijkstra's algorithm ...... but could not do it..... i have written a program which will give the shortest...
0
momotaro
by: momotaro | last post by:
am having a final tomorrow am not yet done with dijkstra!!! plz help this is my code waht is wrong with it! void Dijkstra(char *root, char *destination, GraphNode *head) { GraphNode *Root,...
3
by: victorporton | last post by:
D.K. is traveling from City A to City B. He can stop at some designated spots only. I am trying to use Dijkstra’s algorithm to determine the “spot-to-spot” path that will get D.K. from City A to...
2
by: shashankbs | last post by:
Given a topology and a certain node X, find the shortest path tree with X as the root. * Input: a topology file similar to the following (which is a three-node ring) ...
1
by: Glenton | last post by:
Hi All Here is a very simple little class for finding a shortest route on a network, following Dijkstra's Algorithm: #!/usr/bin/env python #This is meant to solve a maze with Dijkstra's...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.