By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,275 Members | 1,745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,275 IT Pros & Developers. It's quick & easy.

How Can I Set First Number on Each Text File Line to be Initial Node in Dijkstra?

P: 2
Hello,

I am attempting to recreate Dijkstra's algorithm in Python but everything is going wrong.
Here is my code:

Expand|Select|Wrap|Line Numbers
  1. def twoDArray():
  2.     network = []
  3.     sourceNode[]       
  4.     f = open('twoDArray.txt', 'r')
  5.     #network = []  
  6.     for line in f:
  7.                 sourceNode.append(line.split(',') [0])
  8.     line = line = line.rstrip('\n')
  9.     row = line.split(',')
  10.     irow = [int(item) for item in row]
  11.         network.append(irow)
  12.  
  13.     print "Network = "
  14.     print network
  15.  
  16. #def main():
  17.  
  18. if __name__ == "__main__":
  19.     twoDArray()
  20.  
I have a text file containing the distances between nodes that looks similar to this:

Expand|Select|Wrap|Line Numbers
  1. 0,1,2,3,4,5,6,7,8,9,10,11,12
  2. 1,0,3,6,2,7,4,8,6,9,12,10,12
What I am trying to do is to populate a 2D array, which works OK, but then for each line set an initial node so that the distance from the initial node to another node can be calculated, though it isn't working and I don't know what to try next
Feb 22 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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