🛤

Shortest Path

Find shortest paths using BFS (unweighted), Dijkstra (weighted), and Bellman-Ford.

0 Problems3-4 hours

Overview

Shortest path algorithms find the minimum cost path between nodes. BFS finds the shortest path in unweighted graphs in O(V+E). Dijkstra uses a priority queue for weighted graphs with non-negative edges in O((V+E) log V). Bellman-Ford handles negative weights in O(VE).

Complexity Summary

Time Complexity

BFS O(V+E) · Dijkstra O((V+E) log V) · Bellman-Ford O(VE)

Space Complexity

O(V) for distance array

Key Patterns & Techniques

Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.

1

BFS shortest path

Concept

works for unweighted graphs; each level = one step

Practice questions for this pattern
2

Dijkstra

Concept

greedy algorithm; always expand the cheapest unvisited node

Practice questions for this pattern
3

Min-heap

Concept

use a priority queue keyed by distance for Dijkstra

Practice questions for this pattern
4

Relaxation

Concept

update dist[v] if dist[u] + weight(u,v) < dist[v]

Practice questions for this pattern
5

Bellman-Ford

Concept

relax all edges V-1 times; detects negative cycles

Practice questions for this pattern
6

Floyd-Warshall

Concept

all-pairs shortest path in O(V³)

Practice questions for this pattern
DSA Practice Online - 150+ Coding Interview Questions | LeetCode Alternative | InstaMock - AI Mock Interview