Ok, so I've implemented Djakstra's shortest path algorithm in Python and it is working properly (the distances are calculated correctly), but I want it to return the new optimized graph also. Here's my work:
nodes = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I')
graph = {'A': {'B': 4, 'H': 8},
'B': {'A': 4, 'C': 8, 'H': 11},
'C': {'B': 8, 'D': 7, 'I': 2, 'F': 4},
'D': {'C': 7, 'E': 9, 'F': 14},
'E': {'D': 9, 'F': 10},
'F': {'E': 10, 'D': 14, 'C': 4, 'G': 2},
'G': {'F': 2, 'H': 1, 'I': 6},
'H': {'A': 8, 'B': 11, 'I': 7, 'G': 1},
'I': {'C': 2, 'H': 7, 'G':6}}
inf = 5000 #truncating inf as 5000
sptset = {} #shortest path tree set (SPT)
main = {} #main dict with nodes and their distances
source = 'A'
#initialization of source (starting point)
for node in nodes:
main[node] = inf
main[source] = 0
sptset[source] = 0
temp_set = {source: 0}
path = {source: None}
current = None
while len(sptset)<len(nodes):
path[min(temp_set, key=temp_set.get)] = current
#keeping track of the path
current = min(temp_set, key=temp_set.get)
#choosing the next node to check as the minimum of the mainset
#(IF node IS NOT in stpset)
sptset[current] = main.get(current)
#adding the current node to stpset
for neighbor in graph[current].keys():
#for every adjacent node of the current
alt = main.get(current) + graph[current].get(neighbor)
#calculate the distance of current node(from the mainset) + wight to neighbor(from graph)
if alt < main.get(neighbor):
#if that sum is less than the distance of neighbor (from the mainset), then update it in mainset
main[neighbor] = alt
temp_set = {}
for key in main.keys():
if key not in sptset.keys():
temp_set[key] = main.get(key)
#this dictionary is being used to reconstruct the mainset WITHOUT the nodes already in stpset
#(this is done so that the algorithm can be reiterated and a new current node can be chosen)
print path
print sptset
Now after the path gets printed, you can clearly that's just the algorithm path's, not the true path of the nodes. How can I figure out the true path of the nodes based on the distances I just calculated ?