Advertisement
Guest User

ucs

a guest
Mar 24th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None | 0 0
  1. import copy
  2. import queue as Q
  3.  
  4.  
  5. class Graph:
  6.     def __init__(self):
  7.         self.graph = dict()
  8.         self.cost_dict = dict()
  9.         self.final_dict = dict()
  10.  
  11.  
  12. # u and v are nodes: edge u-->v with cost c
  13.     def addEdge(self, u, v, c):
  14.         if u not in self.graph:
  15.             qu = Q.PriorityQueue()
  16.             self.graph.update({u: qu})
  17.  
  18.         self.graph[u].put(v)
  19.         self.cost_dict.update({(u, v): c})
  20.  
  21.  
  22. # Makes a list to keep track of visited nodes
  23.     def tnode(self, n):
  24.         self.visited = [False] * n
  25.  
  26.  
  27.     def UCS_util(self, s, visited, path, goal, value):
  28.     # Appending node to the current path
  29.         path.append(s)
  30.     # Marking that node is visited
  31.         visited[s] = True
  32.  
  33.     # If goal node is reached save the path and return
  34.         if goal == s:
  35.             self.final_dict.update({tuple(path): value})
  36.             return
  37.  
  38.     # Checking if the adjacent node is been visited and explore the new path if haven't
  39.         for i in self.graph[s].queue:
  40.             if visited[i] == False:
  41.             # When new path is being explored add the cost of that path to cost of entire course traversal
  42.             # Send a copy of path list to avoid sending it by reference
  43.                 self.UCS_util(i, copy.deepcopy(visited), copy.deepcopy(path), goal, value + self.cost_dict[s, i])
  44.  
  45.  
  46.     def UCS(self, s, goal):
  47.         self.visited[s] = True
  48.     # List to hold all the nodes visited in path from start node to goal node
  49.         path = [s]
  50.  
  51.         for i in self.graph[s].queue:
  52.             if self.visited[i] == False:
  53.             # Make a variable to hold the cost of traversal
  54.                 value = self.cost_dict[s, i]
  55.                 self.UCS_util(i, copy.deepcopy(self.visited), copy.deepcopy(path), goal, value)
  56.  
  57.  
  58. # Display all the paths that is been discovered from start node to Goal node
  59.     def all_paths(self):
  60.         # Check if there is any path
  61.         if bool(self.final_dict):
  62.             print("All the paths: ")
  63.             for i in self.final_dict:
  64.                 print("path: ", i, "cost: ", self.final_dict[i])
  65.         else:
  66.             print("No Path exist between start and goal node")
  67.  
  68.  
  69. # Find the most optimal path between start node to goal node
  70.     def optimal_path(self):
  71.         if bool(self.final_dict):
  72.             print("best path: ", min(self.final_dict, key=self.final_dict.get))
  73.         else:
  74.             print("No Path exist between start and goal node")
  75.  
  76.  
  77. g = Graph() #constractor
  78. g.tnode(10) #total node
  79. #edges are connected via weighted
  80. g.addEdge(0, 1, 1); g.addEdge(0, 2, 1); g.addEdge(1, 3, 3);
  81. g.addEdge(2, 5, 2); g.addEdge(3, 6, 4); g.addEdge(3, 5, 2);
  82. g.addEdge(4, 6, 1); g.addEdge(4, 7, 5); g.addEdge(5, 4, 4);
  83. g.addEdge(6, 7, 1); g.addEdge(5, 0, 3); g.addEdge(5, 8, 1);
  84. g.addEdge(8, 4, 1); g.addEdge(8, 9, 3); g.addEdge(9, 7, 1);
  85.  
  86. g.UCS(0,7) #start node and goal node
  87. g.optimal_path() #solution
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement