Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3. moves = ((-1, 0), (0, -1), (0, 1), (1, 0))
  4.  
  5.  
  6. def add_edge(graph, a, b):
  7.     graph[a].add(b)
  8.     graph[b].add(a)
  9.  
  10.  
  11. def create_graph(row, col, labyrinth):
  12.     graph = defaultdict(set)
  13.     for i in range(row):
  14.         for j in range(col):
  15.             for new_row, new_col in allowed_moves(i, j, row, col, labyrinth):
  16.                 add_edge(graph, (i, j), (new_row, new_col))
  17.  
  18.     print("Graph:")
  19.  
  20.     answer = defaultdict()
  21.     for key in graph:
  22.         points = []
  23.         for point in graph[key]:
  24.             points.append(point[0] * col + point[1])
  25.         answer[key[0] * col + key[1]] = sorted(points)
  26.  
  27.     count = 0
  28.     for key in sorted(answer):
  29.         while key != count:
  30.             print(count, "- None")
  31.             count += 1
  32.         print("%s - %s " % (key, str(answer[key]).replace("[", "").replace("]", "").replace(",", "")))
  33.         count += 1
  34.     while count < row_size*col_size:
  35.         print(count, "- None")
  36.         count += 1
  37.  
  38.     return graph
  39.  
  40.  
  41. def allowed_moves(x, y, row, col, labyrinth):
  42.     if labyrinth[x][y] != "#":
  43.         for row_offset, col_offset in moves:
  44.             new_row, new_col = x + row_offset, y + col_offset
  45.             if 0 <= new_row < row and 0 <= new_col < col and labyrinth[new_row][new_col] != '#':
  46.                 yield new_row, new_col
  47.  
  48.  
  49.  
  50.  
  51. # for adjacency matrix representation of the graph
  52. import sys
  53.  
  54. class Graph:
  55.  
  56.     def __init__(self, vertices):
  57.         self.V = vertices
  58.         self.graph = [[0 for column in range(vertices)] for row in range(vertices)]
  59.  
  60.     def print_result(self, dist):
  61.         print("Vertex tDistance from Source")
  62.         for node in range(self.V):
  63.             print(node, "t", dist[node])
  64.  
  65.     # A utility function to find the vertex with minimum distance value, from the set of vertices
  66.     # not yet included in shortest path tree
  67.     def min_distance(self, dist, sptSet):
  68.  
  69.         # Initilaize minimum distance for next node
  70.         min = sys.maxsize
  71.  
  72.         # Search not nearest vertex not in the shortest path tree
  73.         for v in range(self.V):
  74.             if dist[v] < min and sptSet[v] == False:
  75.                 min = dist[v]
  76.                 min_index = v
  77.  
  78.         return min_index
  79.  
  80.     def dijkstra(self, src):
  81.  
  82.         dist = [sys.maxsize] * self.V
  83.         dist[src] = 0
  84.         sptSet = [False] * self.V
  85.  
  86.         for vertex in range(self.V):
  87.  
  88.             # Pick the minimum distance vertex from the set of vertices not yet processed.
  89.             u = self.min_distance(dist, sptSet)
  90.  
  91.             # Put the minimum distance vertex in the shotest path tree
  92.             sptSet[u] = True
  93.  
  94.             # Update dist value of the adjacent vertices of the picked vertex only if the current
  95.             # distance is greater than new distance and the vertex in not in the shotest path tree
  96.             for v in range(self.V):
  97.                 if self.graph[u][v] > 0 and sptSet[v] == False and dist[v] > dist[u] + self.graph[u][v]:
  98.                     dist[v] = dist[u] + self.graph[u][v]
  99.  
  100.         self.print_result(dist)
  101.  
  102.  
  103. g = Graph(9)
  104. g.graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0],
  105.            [4, 0, 8, 0, 0, 0, 0, 11, 0],
  106.            [0, 8, 0, 7, 0, 4, 0, 0, 2],
  107.            [0, 0, 7, 0, 9, 14, 0, 0, 0],
  108.            [0, 0, 0, 9, 0, 10, 0, 0, 0],
  109.            [0, 0, 4, 14, 10, 0, 2, 0, 0],
  110.            [0, 0, 0, 0, 0, 2, 0, 1, 6],
  111.            [8, 11, 0, 0, 0, 0, 1, 0, 7],
  112.            [0, 0, 2, 0, 0, 0, 6, 7, 0]
  113.            ]
  114.  
  115. g.dijkstra(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement