Skip to content

Instantly share code, notes, and snippets.

@wotori
Created November 21, 2018 16:14
Show Gist options
  • Save wotori/5ba523d744c0a17b28069def51ec1a43 to your computer and use it in GitHub Desktop.
Save wotori/5ba523d744c0a17b28069def51ec1a43 to your computer and use it in GitHub Desktop.
Dijkstra
def Dijkstra(N, S, matrix):
valid = [True] * N
weight = [1000000] * N
weight[S] = 0
for i in range(N):
min_weight = 1000001
ID_min_weight = -1
for i in range(len(weight)):
if valid[i] and weight[i] < min_weight:
min_weight = weight[i]
ID_min_weight = i
for i in range(N):
if weight[ID_min_weight] + matrix[ID_min_weight][i] < weight[i]:
weight[i] = weight[ID_min_weight] + matrix[ID_min_weight][i]
valid[ID_min_weight] = False
return weight
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment