GateMCQSCIENTIFIC PREP LAB
PlanSign Up
INTERMEDIATECore Topic 4 min read 19 views

Shortest Path & MST Algorithms: Dijkstra, Bellman-Ford, Prim & Kruskal

Edge relaxation, negative edge traps, Disjoint-Set Union Kruskal complexity, and MST uniqueness theorem.

Shortest Path & MST Master Comparison

AlgorithmProblemNegative Weights Allowed?Time Complexity
BFSUnweighted Shortest PathN/AO(V+E)\mathcal{O}(V + E)
DAG RelaxationSingle-Source on DAGYesO(V+E)\mathcal{O}(V + E)
DijkstraSingle-Source Shortest PathNoO((V+E)log⁡V)\mathcal{O}((V+E)\log V)
Bellman-FordSingle-Source + Neg Cycle CheckYesO(V⋅E)\mathcal{O}(V \cdot E)
Floyd-WarshallAll-Pairs Shortest PathYesO(V3)\mathcal{O}(V^3)
Kruskal / PrimMinimum Spanning TreeYes! (MST works on negative edges)O(Elog⁡V)\mathcal{O}(E \log V)
Important

MST Uniqueness Theorem:

  • If all edge weights in GG are distinct, the Minimum Spanning Tree is guaranteed to be unique!
  • Conversely, a unique MST does not require all edge weights to be distinct (e.g., a tree with equal weights has a unique MST).
Key Formulas Summary
Dijkstra & Prim (Binary Heap)
O((V+E)log⁡V)=O(Elog⁡V) for connected graphs\mathcal{O}((V + E)\log V) = \mathcal{O}(E \log V) \text{ for connected graphs}
Note: With Fibonacci Heap: O(E + V log V)
Bellman-Ford & Kruskal
Bellman-Ford: O(V⋅E),Kruskal: O(Elog⁡E)=O(Elog⁡V)\text{Bellman-Ford: } \mathcal{O}(V \cdot E), \qquad \text{Kruskal: } \mathcal{O}(E \log E) = \mathcal{O}(E \log V)
Note: DAG Shortest Path (even with negative weights): O(V + E)
#Graphs#Greedy#DP#Dijkstra#MST

Practice Questions on This Chapter (3)

Q1GATE CS AlgorithmsMCQMEDIUM
+2M • -0.67

What is the worst-case time complexity of Dijkstra’s single-source shortest path algorithm on a graph G=(V,E)G = (V, E) implemented using a Binary Min-Heap adjacency list?

⚡ Click an option above to verify instantly
Q2GATE CS AlgorithmsMCQMEDIUM
+2M • -0.67

Five characters have frequencies 5,9,12,13,165, 9, 12, 13, 16. Using Huffman optimal prefix coding, what is the total weighted number of bits required to encode all 5555 characters?

⚡ Click an option above to verify instantly
Q3GATE CS 2020NATMEDIUM
+2M • 0 Neg

The complete undirected graph K6K_6 (66 labeled vertices) has how many distinct Spanning Trees (by Cayley’s Formula)?

Previous TopicAsymptotic Notation & Master Theorem for Divide-and-ConquerNext Topic Dynamic Programming Masterclass: 0/1 Knapsack, LCS & Matrix Chain Multiplication