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: - infinity = 1000000
-
invalid_node = -1
-
startNode = 0
-
-
class Node:
-
distFromSource = infinity
-
previous = invalid_node
-
visited = False
-
-
def populateNodeTable():
-
nodeTable = []
-
index =0
-
f = open('route.txt', 'r')
-
for line in f:
-
node = map(int, line.split(','))
-
nodeTable.append(Node())
-
print nodeTable[index].previous
-
print nodeTable[index].distFromSource
-
index +=1
-
nodeTable[startNode].distFromSource = 0
-
-
return nodeTable
-
-
def tentativeDistance(currentNode, nodeTable):
-
nearestNeighbour = []
-
for currentNode in nodeTable:
- # if Node[currentNode].distFromSource +
-
currentDistance = startNode + currentNode
-
# currentDistance = currentNode.distFromSource + nodeTable.currentNode
-
currentNode.previous = currentNode
-
currentNode.length = currentDistance
-
currentNode.visited = True
-
currentNode +=1
-
nearestNeighbour.append(currentNode)
-
print nearestNeighbour
-
-
return nearestNeighbour
-
-
currentNode = startNode
-
-
if __name__ == "__main__":
-
populateNodeTable()
-
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
1 2898
You are using currentNode for two different variables. - def tentativeDistance(currentNode, nodeTable):
-
# and
-
for currentNode in nodeTable:
Print the values so you know what they are. For example: - print "startNode =", startNode, type(startNode)
-
print "currentNode =", currentNode, type(currentNode)
-
#
-
#Node[currentNode].distFromSource +
-
# currentDistance = startNode + currentNode
Perhaps a dictionary of classes would be easier to understand than a list of classes. - class Node:
-
def __init__(self, previous):
-
infinity = 1000000
-
invalid_node = -1
-
self.distFromSource = infinity
-
self.previous = previous
-
self.visited = False
-
-
-
def populateNodeTable():
-
startNode = 0
-
## dictionary with key = record or node number
-
nodeTable = {}
-
## f = open('route.txt', 'r')
-
f = range(10) ## test data
-
for index, line in enumerate(f):
-
nodeTable[index] = Node(index-1) ## index-1 = previous record
-
nodeTable[startNode].distFromSource = 0
-
nodeTable[startNode].previous = -1
-
-
## print ascending
-
for node in range(0, len(nodeTable)):
-
## add to distFromSource
-
nodeTable[node].distFromSource += node
-
print node, nodeTable[node].distFromSource
-
-
## print descending using another method
-
print "-"*30
-
index = 9 ## last node number
-
while index > -1:
-
print index, nodeTable[index].distFromSource, \
-
"previous =", nodeTable[index].previous
-
index = nodeTable[index].previous
-
-
-
populateNodeTable()
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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?)....
|
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...
|
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...
|
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,...
|
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...
|
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)
...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
| |