Shortest Path Problem

2 min read Updated Fri Apr 24 2026 07:36:29 GMT+0000 (Coordinated Universal Time)

The task of finding a path between 2 vertices such that the sum of edge weights is minimized. There might multiple shortest path trees for a given graph and a source vertex.

Used in routing in computer networks, GPS navigation and resource optimization.

Shortest Path Tree

A tree that represents shortest paths from a source to all vertices. Source vertex is the root. Edges correspond to shortest paths.

Relaxation

Relaxation updates distances when a shorter path is found:

dist(v)=dist(u)+w(u,v)\text{dist}(v) = \text{dist}(u) + w(u,v)

Dijkstra’s Algorithm

A greedy method for finding shortest paths from a single source in graphs with non-negative weights. Guaranteed optimal solution. Efficient for sparse graphs. High memory usage for large grahps. Does not support negative weights.

  1. Initialize:

    • S=S = \emptyset
    • dist[source]=0\text{dist}[\text{source}] = 0
    • dist[others]=\text{dist}[\text{others}] = \infty
  2. Repeat until all vertices processed:

    • pick vertex with minimum distance not in SS
    • add it to SS
    • update (relax) distances of neighbors

Dijkstra’s Algorithm was covered in S2 as well.

Bellman–Ford Algorithm

A dynamic programming algorithm for single-source shortest paths. Repeatedly relax all edges to progressively improve distance estimates.

Supports negative weights. Detects negative cycles. Guaranteed to be optimal. Slower than Dijkstra.

A negative cycle is a cycle whose total edge weight is negative. A negative cycle causes the shortest path to be undefined.

  1. Initialize:

    • dist[source]=0\text{dist}[\text{source}] = 0
    • dist[others]=\text{dist}[\text{others}] = \infty
  2. Repeat relaxation:
    For each edge (u,v)(u,v), if dist(u)+w(u,v)<dist(v)\text{dist}(u) + w(u,v) < \text{dist}(v), update the distance to be the smaller one.

n1n-1 iterations are enough for bellman-form to become stable, where nn is the number of vertices. If nn-th iteration causes distance changes, it means there are negative cycles.

Bellman-Ford Algorithm was covered in S2 as well.