Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
PROBLEM-SOLVING TECHNIQUES

Shortest Path Algorithms

Cheapest routes in weighted graphs - Dijkstra, Bellman-Ford and 0-1 BFS.

Advanced7 problems3 interactive subtopicsO((V + E) log V) time · O(V + E) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/5Start at A
start at A
41251
A
B
C
D
0
MIN-HEAP (DIST, NODE)
(0, A)
  • waiting / queued
  • active this step
  • current
  • visited / done
  • result
  • removed / popped
Space Play←→ StepR Replay
1 / 5
PYTHONA: (B, 4), (C, 1) · C: (B, 2), (D, 5) · B: (D, 1) - from A
1import heapq
2
3def dijkstra(graph, start):
4 dist = {start: 0}
▶5 heap = [(0, start)]
6 while heap:
7 d, node = heapq.heappop(heap)
8 if d > dist[node]:
9 continue
10 for nxt, w in graph[node]:
11 nd = d + w
12 if nd < dist.get(nxt, float("inf")):
13 dist[nxt] = nd
14 heapq.heappush(heap, (nd, nxt))
15 return dist
STEP 1 OF 5Setup

Start at A

A costs 0 and is the only entry in the heap.

VARIABLES

settled
-
dist
{A: 0}
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

Dijkstra repeatedly settles the closest unsettled node using a min-heap and needs non-negative weights. Bellman-Ford handles negative weights by relaxing every edge up to V − 1 times.

Why it matters

Routing, network delay and cheapest-flight problems need the minimum total weight, not the fewest edges.

Think of it like this

Planning a road trip by always extending the route to the nearest town you haven’t finalized yet.

Use it when

  • Weighted edges with non-negative costs (Dijkstra)
  • Negative edges, or a limit on edges used (Bellman-Ford)
  • Weights of only 0 or 1 (0-1 BFS with a deque)

Avoid it when

  • Unweighted graphs - plain BFS is simpler
  • Negative weights with Dijkstra - it can finalize nodes too early

Complexity cheatsheet

OperationTimeSpace
Dijkstra with a binary heapO((V + E) log V)O(V + E)
Bellman-FordO(V × E)O(V)
0-1 BFSO(V + E)O(V)

Often confused with: BFS - it minimizes the number of edges; Dijkstra minimizes total weight.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

weightedminimum costnetwork delaycheapest flightat most k stops
  • “At most k stops” suggests Bellman-Ford limited to k + 1 rounds
  • Skip stale heap entries whose distance is worse than the best known
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Using Dijkstra when some edge weights are negative
  • Not skipping stale heap entries - wasted work
  • Treating a node as final when it’s pushed rather than when it’s popped

Edge cases to test

  • Unreachable nodes - report infinity or -1
  • Several edges between the same pair
  • Zero-weight edges
Interview tip: Say why Dijkstra needs non-negative weights: once a node is popped, no later path to it can be cheaper.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Why can Dijkstra give wrong answers with negative edge weights?

06

Practice

Apply what you just watched on real interview problems.

Generate Graph Theory problem in AI DSA Practice →
  • BeginnerNetwork Delay TimeLeetCode 743 ↗ (opens in a new tab)
  • IntermediateCheapest Flights Within K StopsLeetCode 787 ↗ (opens in a new tab)
  • AdvancedMinimum Cost to Make at Least One Valid Path in a GridLeetCode 1368 ↗ (opens in a new tab)
Union FindBit Manipulation