DSA Dijkstra’s Algorithm

DSA Tutorial

DSA Dijkstra’s Algorithm

(Concept • Steps • Example • Code • Complexity • When to Use)

Dijkstra’s Algorithm finds the shortest path from a single source to all other vertices in a weighted graph with non-negative edge weights.
It’s one of the most asked graph algorithms in DSA interviews.


1️⃣ What Problem Does Dijkstra Solve?

Given:

  • A weighted graph (directed or undirected)

  • No negative edge weights

  • A source vertex

Find:

  • The minimum distance from the source to every other vertex


2️⃣ When to Use Dijkstra (Very Important)

Graph TypeUse Dijkstra?
Unweighted graph❌ (Use BFS)
Weighted, all weights ≥ 0
Negative edge weights❌ (Use Bellman–Ford)
DAG❌ (Use Topo + Relaxation)

3️⃣ Core Idea (Greedy)

  • Keep a distance array

  • Start with dist[source] = 0

  • Repeatedly pick the unvisited node with the smallest distance

  • Relax its edges (try to improve neighbors’ distances)

 “Once the shortest distance to a node is finalized, it never changes.”


4️⃣ Key Terms

  • Relaxation:
    If dist[u] + weight(u,v) < dist[v], update dist[v]

  • Min-Heap / Priority Queue:
    Efficiently get the closest unvisited node


5️⃣ Step-by-Step Example

Graph (weights in brackets)

Source = 0

Initial

dist = [0, ∞, ∞, ∞]

After-processing 0

dist[1] = 4
dist[2] = 1

After- processing 2

dist[3] = 2 (1 + 1)

After-processing 3

dist[1] = 7? (2 + 5 = 7) ❌ (keep 4)

Final Distances

0 → 0
1 → 4
2 → 1
3 → 2

6️⃣ Dijkstra Algorithm (Steps)

  1. Initialize dist[] with ∞

  2. Set dist[source] = 0

  3. Push (0, source) into min-heap

  4. While heap not empty:

    • Pop node with minimum distance

    • Relax all adjacent edges

  5. End


7️⃣ C++ Implementation (Using Priority Queue)


 


8️⃣ Graph Representation Used

Adjacency List with weights:


9️⃣ Time & Space Complexity

 Time

  • With min-heap:

O((V + E) log V)

Space

O(V + E)

🔟 Why Dijkstra Fails with Negative Weights

Dijkstra assumes:

“The shortest distance to a visited node is final.”

Negative edges can break this assumption, giving wrong results.

 Use Bellman–Ford if negative edges exist.


1️⃣1️⃣ Dijkstra vs Other Algorithms

AlgorithmWeightsComplexityUse Case
BFSUnweightedO(V+E)Simple shortest path
Dijkstra≥ 0O(E log V)Fast weighted paths
Bellman-FordAnyO(VE)Negative edges
Floyd-WarshallAnyO(V³)All-pairs

1️⃣2️⃣ Real-Life Applications

✔ Google Maps & GPS
✔ Network routing (OSPF)
✔ Game AI pathfinding
✔ Logistics & supply chains
✔ Recommendation systems


Interview Questions (Dijkstra)

Q1. Can Dijkstra handle negative weights?
👉 ❌ No

Q2. Why priority queue is used?
👉 To get minimum distance node efficiently

Q3. Time complexity with min-heap?
👉 O((V+E) log V)

Q4. Difference between BFS and Dijkstra?
👉 BFS for unweighted, Dijkstra for weighted graphs


Final Summary

✔ Dijkstra finds shortest path from one source
✔ Works only with non-negative weights
✔ Uses greedy approach + priority queue
✔ Time complexity: O((V+E) log V)
✔ Extremely important for DSA interviews

You may also like...