Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function mindist(dist, sptset)
- min = Inf # Initialize minimum distance for next node
- minindex = 0
- # Search smallest value nearest vertex not in the
- # shortest path tree
- for i in 1:length(G.V)
- if dist[i] < min && sptset[i] == false
- min = dist[i]
- minindex = i
- end
- end
- println("\nNext Node to be processed: ", minindex)
- return minindex
- end
- function dijkstra(G, initial_node)
- println("Source Node:", initial_node)
- println("Graph's Adjacency Matrix:\n", G)
- TreeSet = [false for i in 1:length(G.V) ] # step 1
- dist = [Inf for i in 1:length(G.V) ] # step 2
- dist[initial_node] = 0
- for i in 1:length(G.V)
- # Pick the minimum distance vertex from
- # the set of vertices not yet processed.
- x = mindist(dist, TreeSet) # step 3
- println("Before relaxation: ", dist)
- for j in 1:length(G.V)
- if (G.V[x] > 0 && TreeSet[j] == false && dist[j] > dist[x] + G.V[x])
- dist[j] = dist[x] + G.V[x]
- end
- end
- println("After relaxation: ", dist)
- # Put the minimum distance vertex in the
- # shortest path tree
- TreeSet[x] = true # step 4
- end
- println("v | d[v] ")
- println("---------")
- for (i , j) in enumerate(dist)
- println(i, " | ", j)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement