Mr_HO1A

Dijkstra Algorithm

Oct 8th, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.62 KB | None | 0 0
  1. # Python 2.7.X Implementation For Dijksta Algo
  2.  
  3. # Importing Sys Lib just to include maxInt function
  4. # That returns the highest Integer that machine can hold
  5. import sys
  6. import time
  7. from os import system
  8. # Creating a main class to hold all functions
  9.  
  10.  
  11. class Dijksta:
  12.     # Creating Default constructer to init our graph and object
  13.     # Vertices/Number of nodes in our graph
  14.     def __init__(self, Vertices):
  15.         # Setting value of vertices to init out graph
  16.         self.vertices = Vertices
  17.         # Makeing our defaut graph to hold value 0 initallly
  18.         self.graph = [[0 for column in range(Vertices)]
  19.                       for row in range(Vertices)]
  20.  
  21.     # Function to add nodes when you dont want to supply Matrix form
  22.     def addNode(self, srcNode, dstNode, weight):
  23.         self.graph[srcNode][dstNode] = weight
  24.         self.graph[dstNode][srcNode] = weight
  25.  
  26.     # A printing function that will print all distances
  27.     def printDistance(self, DistArray):
  28.         print "Node \t Distance"
  29.         for node in range(self.vertices):
  30.             print "{} \t {}".format(node, DistArray[node])
  31.         print "Shortest Path For All Nodes"
  32.         # Commented Below will be used later to see how this works
  33.         # time.sleep(3)
  34.         # system("cls")
  35.  
  36.     # A function that returns the minimum distance index from the given set of vertex
  37.     # wich in not included in shortest path tree set
  38.     def minDistance(self, dist, sptSet):
  39.         # Var which stores minimum distance init let us consider this value as infinite
  40.         minDis = sys.maxint
  41.         for v in range(self.vertices):
  42.             if dist[v] < minDis and sptSet[v] == False:
  43.                 minDis = dist[v]
  44.                 min_dist_index = v
  45.         return min_dist_index
  46.  
  47.     # Fucntion to implement dijkstra algo
  48.     # src is the source node
  49.     def calculateDijkstra(self, src):
  50.         """
  51.        Paramater Src : Source Node Value
  52.        """
  53.         # Initlize all distance as infinte first and make array equal to
  54.         # Number of vertices/nodes
  55.         dist = [sys.maxint] * self.vertices
  56.         # Distance form the souce node is always equal to 0
  57.         dist[src] = 0
  58.         # Shortest Path Tree Set i.e this array will hold the true if
  59.         # the value of shortest distance form src node to node i is finalized
  60.         sptSet = [False] * self.vertices
  61.  
  62.         # Now Itterate over the graph
  63.         for _ in range(self.vertices):
  64.             # Now let us pick the shortest distance "index" from the dist array
  65.             # The shortest distance in first ittration is always 0 as Distance from Source Node is 0
  66.             # And it is not yet processed hence it is also not included in sptSet
  67.             # We are going to use our utility function here
  68.             # SDI = Shortest Distance Index
  69.             SDI = self.minDistance(dist, sptSet)
  70.  
  71.             # As we have found the shortest distance index we are going to
  72.             # update sptSet array to state it is the finalized shortest distance
  73.             sptSet[SDI] = True
  74.  
  75.             # Now we are going to update the distances in Dist Array
  76.             # Only if
  77.             # 1. It is connected to some other node i.e distance to other node is not 0
  78.             #    if it is 0 then node is not connected with each other
  79.             # 2. The shortest distance is not yet finalized ie index value in sptSet is not True
  80.             #    if it finalized no need to update value
  81.             # 3. And if its value in dist array is more then previous Min Distance plus the disstance
  82.             #    to the next connected node
  83.             # Repeat this for all the vertex connected to the current node ie node with minimum distance
  84.             for noVertex in range(self.vertices):
  85.                 if self.graph[SDI][noVertex] > 0 and sptSet[noVertex] == False and dist[noVertex] > dist[SDI] + self.graph[SDI][noVertex]:
  86.                     # Update all the min distance values exit loop 2
  87.                     dist[noVertex] = dist[SDI] + self.graph[SDI][noVertex]
  88.                 # Exit loop
  89.             time.sleep(1)
  90.             print dist
  91.             print sptSet
  92.         # Now let us print all the minimum distances in dist array
  93.         self.printDistance(dist)
  94.  
  95.  
  96. # Now let's fire the functions
  97. Graph = Dijksta(9)
  98. Graph.graph = [
  99.     [0, 4, 0, 0, 0, 0, 0, 8, 0],
  100.     [4, 0, 8, 0, 0, 0, 0, 11, 0],
  101.     [0, 8, 0, 7, 0, 4, 0, 0, 2],
  102.     [0, 0, 7, 0, 9, 14, 0, 0, 0],
  103.     [0, 0, 0, 9, 0, 10, 0, 0, 0],
  104.     [0, 0, 4, 14, 10, 0, 2, 0, 0],
  105.     [0, 0, 0, 0, 0, 2, 0, 1, 6],
  106.     [8, 11, 0, 0, 0, 0, 1, 0, 7],
  107.     [0, 0, 2, 0, 0, 0, 6, 7, 0]
  108. ]
  109. Graph.calculateDijkstra(0)
Add Comment
Please, Sign In to add comment