Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- graph = {'s': {'v':1,'w':4 },
- 'v': {'w':2,'t':6},
- 'w': {'t':3},
- 't': {'': None}
- }
- def dj(graph, start):
- Q = []# queue to hold all the vertices
- dist = {} #dictionary to hold all the distance
- prev = {} #dictionary to hold all the previous node visited
- for key in graph.keys():
- dist[key] = 1000
- prev[key] = ""
- Q.append(key)
- dist[start] = 0
- #I had to have another distance data structure dst
- # to store all the distance covered
- # I am not fond of it
- dst = []
- while Q:
- u = min(dist, key = dist.get)#Here I get the minimum distance
- Q.remove(u)#remove the min one from Q
- for v in graph[u]:
- #I also want to improvise this condition block
- #It is because of 't': {'': None}
- if graph[u][v] is None:
- continue
- alt = dist[u] + graph[u][v]
- if alt < dist[v]:
- dist[v] = alt
- prev[v] = u
- d = (dist.pop(u))
- dst.append(d)
- return dst,prev
- print(dj(graph,'s'))
Add Comment
Please, Sign In to add comment