Jump to content

Implementing Dijkstra's Algorithm

- - - - -

  • Please log in to reply
1 reply to this topic

#1
fyo

fyo

    Newbie

  • Members
  • Pip
  • 4 posts
Hello,

I am struggling on what is theoretically a simple problem, much to my frustration.
Essentially, I need to be able to create Dijkstra's Algorithm so that:

  • I have a 2D array holding distances between the network nodes
  • I have a 2D array holding predecessor, length & visited/unvisited status
  • I have a 1D array holding the optimum path from source to destination

Here is my code so far:

#usr/bin/python


#Declarations

max_nodes = 1000

infinity = -1

network = [max_nodes][max_nodes]

state = [max_nodes] 

path = []

int k, i, j


def Dijkstra (g, start, end)


    #For each node in graph

    for n in g:

        length = infinity #Set length to all nodes to infinity

        previousNode = none #Set previous node to none

        length[startingNode] = 0 #Set starting node length to 0


   #For each edge outgoing from u

    for edge in g[n]:

        currentDistance = state[i] + network[k][i] #Set the current distance from each node

        if currentDistance < #If current distance is smaller than ??

        

        

As you can see it's terrible, i'm getting confused on how to work with the arrays and just need some guidance on how I can improve on the rubbish I have written so far .

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
Not sure if this is legal in Python:

network = [max_nodes][max_nodes]


You probably want nested lists

network = [ [0 for j in range(max_nodes)] for i in range(max_nodes) ]

This will create "2D array" with elements set to 0.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users