DSA Mastery

Dijkstra's Algorithm: Shortest path in weighted graphs

1 Views Updated 5/4/2026

Dijkstra's Algorithm

BFS finds the shortest path in unweighted graphs. But what if connections have different costs (e.g., distance or traffic)? Dijkstra's Algorithm is the world's most famous solution for finding the shortest path in Weighted Graphs.

1. How it Works (Greedy + DP)

Dijkstra uses a Priority Queue (Min-Heap).

  1. Set distance to all nodes to Infinity (except Start = 0).
  2. Pick the unvisited node with the smallest distance.
  3. For each neighbor, calculate NewDistance = CurrentDistance + EdgeWeight.
  4. If NewDistance < ExistingDistance, update the neighbor and add it to the Priority Queue.

2. The "No Negative Weights" Rule

Dijkstra is a greedy algorithm. It assumes that once it "relaxes" a node, it has found the absolute shortest path. This only works if all edge weights are positive. If you have negative weights (e.g., a "Cashback" on a path), you must use the Bellman-Ford algorithm instead.

4. Interview Mastery

Q: "Where is Dijkstra used in modern software architecture?"

Architect Answer: "It is the heart of **OSPF (Open Shortest Path First)** routing protocols that power the internet. It is also used by **Google Maps** (though with more advanced heuristics like A*) to find the fastest route between two cities. In microservices, it can be used for 'Service Discovery' to find the instance with the lowest latency."

DSA Mastery
1. Algorithmic Foundations
Big O Notation: Analyzing Time and Space Complexity Memory Management: Stack vs Heap in C# Recursion: The foundation of modern algorithms
2. Linear Data Structures
Arrays Deep Dive: Static vs Dynamic (List<T> Internals) Linked Lists: Singly, Doubly, and Circular Stacks and Queues: Implementing Undo/Redo & Message Buffers Hash Tables: Handling Collisions like a Pro
3. Non-Linear Data Structures
Binary Trees & BST: Searching at Log(N) speed Balanced Trees: AVL and Red-Black Trees Internals Heaps: Implementing a Priority Queue Tries (Prefix Trees): Optimizing Auto-complete features Graphs (Part 1): Representation (Matrix vs List) Graphs (Part 2): BFS vs DFS Traversal
4. Searching & Sorting
Binary Search: The power of Divide & Conquer Elementary Sorts: Bubble, Selection, and Insertion Merge Sort: Stable sorting for massive datasets Quick Sort: In-place sorting and Pivot selection Heap Sort: Leveraging the Priority Queue
5. Algorithmic Patterns
Sliding Window Pattern: Optimizing array performance Two Pointers Pattern: Reversing and Finding cycles Fast & Slow Pointers (Hare & Tortoise) Backtracking: Solving Sudoku and N-Queens
6. Dynamic Programming (DP)
Memoization vs Tabulation: Top-down vs Bottom-up The Knapsack Problem: 0/1 DP optimization Longest Common Subsequence (LCS) Matrix Chain Multiplication
7. Advanced Graphs & Interview
Dijkstra's Algorithm: Shortest path in weighted graphs Prim's and Kruskal's: Minimum Spanning Trees Disjoint Set Union (DSU) / Union-Find DSA Interview: FAANG Style Coding Challenges