Skip to content

Instantly share code, notes, and snippets.

View panyam's full-sized avatar

Sri Panyam panyam

View GitHub Profile
ShortestPath-Dijkstra(G,s,t)
known = {s}
for i = 1 to n, dist[i] = ∞
for each edge (s,v), dist[v] = w(s,v)
last = s
while (last != t)
select vnext, the unknown vertex minimizing dist[v]
for each edge (vnext,x), dist[x] = min[dist[x],dist[vnext] + w(vnext,x)]
last = vnext
known = known ∪ {vnext}
@panyam
panyam / dijkstra_shortest_path.py
Last active February 21, 2017 17:59
Dijkstra's shortest path algorithm - basic version
def shortest_path(source, target):
"""
Return the shortest path from the source to target.
"""
# Keeps track of the parent node for a node in the path between source and target.
parents = defaultdict(lambda: None)
# Keep track of the distance of each node to the source node
distances = {source: 0, target: INFINITY}