menu_book Course Curriculum (Data Structures & Algorithms) expand_more
Complexity & Foundations
Arrays & Strings
Linked Lists
Stacks & Queues
Hashing
Trees & Heaps
Graphs
Dijkstra shortest path
Learn Dijkstra shortest path in Data Structures & Algorithms with a focused explanation, a topic-specific example, edge guidance, and a mastery check.
12 min read
•
Advanced
data-structures-algorithms-068.py
python
import heapq
g={"A":[("B",4),("C",1)],"C":[("B",2)],"B":[("D",1)],"D":[]};dist={"A":0};q=[(0,"A")]
while q:
d,u=heapq.heappop(q)
if d!=dist[u]:continue
for v,w in g[u]:
nd=d+w
if nd<dist.get(v,float("inf")):dist[v]=nd;heapq.heappush(q,(nd,v))
print(dist)
What to watch
Content source & attribution
Original Syntaxis Content · Original Syntaxis Content