def dijkstra(graph, start):visited = []index = startnodes = dict((i, INF) for i in graph)nodes[start] = 0while len(visited) < len(graph):visited.append(index)for i in graph[index]:new_cost = nodes[index] + graph[index][i]if new_cost < nodes[i]:nodes[i] = new_costnext = INFfor n in nodes:continueif nodes[n] < next:next = nodes[n]index = nreturn nodesINF = 9999graph = {'A':{'A':0, 'B':2, 'C':4},'B':{'B':0, 'C':7, 'E':6},'C':{'C':0, 'D':6, 'E':2},'D':{'D':0, 'E':8, 'G':4},'E':{'E':0, 'G':2},'G':{'G':0}}rtn = dijkstra(graph, 'A')print(rtn)
Result

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store