DSA Shortest Path

DSA Tutorial

DSA Shortest Path – Complete Beginner Guide

(Concept • Algorithms • When to Use What • Examples • Code • Complexity)

Shortest Path algorithms help find the minimum distance / cost from one node to another in a graph.
This is a core DSA topic and a favorite in interviews.


1️⃣ What is the Shortest Path Problem?

Given:

  • A graph (directed / undirected)

  • A source vertex

Find:

  • The minimum distance from the source to all other vertices
    (or to a specific destination)


2️⃣ Types of Shortest Path Problems

CaseExample
Single Source → AllGoogle Maps from one location
Single Source → OneShortest route A → B
All PairsNetwork routing tables

3️⃣ Which Shortest Path Algorithm to Use?  (VERY IMPORTANT)

Graph TypeAlgorithm
Unweighted GraphBFS
Weighted (No negative)Dijkstra
Weighted (Negative edges)Bellman-Ford
All-Pairs Shortest PathFloyd–Warshall
DAGTopological Sort + Relaxation

 PART 1: Shortest Path in Unweighted Graph (BFS)


4️⃣ BFS Shortest Path

 Why BFS?

  • BFS explores level by level

  • First time reaching a node = shortest path


 Algorithm (BFS)

  1. Initialize distance array with

  2. Distance[source] = 0

  3. Use queue

  4. For each neighbor:

    • If unvisited, update distance


 C++ Code (Unweighted Graph)


 


 Time Complexity

O(V + E)

PART 2: Shortest Path in Weighted Graph (Dijkstra)


5️⃣ Dijkstra’s Algorithm

 Conditions

✔ Graph with non-negative weights
❌ Does NOT work with negative edges


 Core Idea

  • Greedy algorithm

  • Always picks closest unvisited node

  • Uses priority queue (min-heap)


 Steps

  1. Distance[source] = 0

  2. Push (distance, node) into min-heap

  3. Relax edges

  4. Repeat until heap empty


 C++ Code (Dijkstra)


 


 Time Complexity

O((V + E) log V)

 PART 3: Shortest Path with Negative Weights


6️⃣ Bellman–Ford Algorithm

 Features

✔ Handles negative edges
✔ Detects negative weight cycles
❌ Slower than Dijkstra


 Idea

  • Relax all edges V−1 times

  • One more pass to check negative cycle


 Time Complexity

O(V × E)

7️⃣ All-Pairs Shortest Path

 Floyd–Warshall Algorithm

  • Dynamic Programming

  • Finds shortest paths between every pair

 Complexity:

O(V³)

 Used when graph is small.


8️⃣ Shortest Path in DAG

For Directed Acyclic Graph:

  1. Topological sort

  2. Relax edges in topo order

 Complexity:

O(V + E)

9️⃣ Comparison Summary

AlgorithmWeightsCyclesComplexity
BFSNoYesO(V+E)
Dijkstra+ve onlyYesO(E log V)
Bellman-FordAnyYesO(VE)
Floyd-WarshallAnyYesO(V³)

🔟 Real-Life Applications

✔ Google Maps & GPS
✔ Network routing
✔ Game AI pathfinding
✔ Social networks
✔ Logistics & supply chain


Interview Questions (Shortest Path)

Q1. Why BFS gives shortest path in unweighted graph?
👉 Level-wise traversal

Q2. Why Dijkstra fails with negative weights?
👉 Greedy assumption breaks

Q3. Which algorithm detects negative cycle?
👉 Bellman-Ford


 Final Summary

✔ Shortest path finds minimum distance
✔ Algorithm depends on graph type
✔ BFS → Unweighted
✔ Dijkstra → Positive weights
✔ Bellman-Ford → Negative weights
✔ Extremely important for DSA interviews

You may also like...