Advertisement
PlotnikovPhilipp

Untitled

Nov 17th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. def deikstra(start_node, finish_node, graph):
  2.     distance = {str(i): float('inf') for i in range(1, len(graph) + 1)}
  3.     visited = []
  4.     current_node = start_node
  5.     distance[str(current_node)] = 0
  6.     while finish_node not in visited:
  7.         visited.append(current_node)
  8.         for arr in matrix[current_node]:
  9.             n = arr[0]
  10.             w = arr[1]
  11.             if n in visited:
  12.                 continue
  13.             if distance[str(current_node)] + w < distance[str(n)]:
  14.                 distance[str(n)] = distance[str(current_node)] + w
  15.         minWeight = 19000000
  16.         for i in range(1, length + 1):
  17.             if (i not in visited) and (distance[str(i)] < minWeight):
  18.                 current_node = i
  19.                 minWeight = distance[str(i)]
  20.     return distance[str(finish_node)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement