DSA for Interviews
Back to Trees, Graphs, and Recursion

Graph Algorithms (Dijkstra, Topological Sort)

Weighted shortest path (Dijkstra) and dependency ordering (topo sort). The two named algorithms most likely to appear. FIND_VIDEO: search 'dijkstra topological sort algorithm explained' — recommended channel: NeetCode / Abdul Bari. Aim for 11 min or under.

3 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.
Loading progress...

Key moments

  1. Algorithm Foundation and Scope — Dijkstra calculates single-source shortest paths on weighted graphs and differs from minimum spanning tree algorithms.
  2. Graph Setup and Distance Initialization — The source vertex is assigned a distance of zero while all other unvisited nodes are set to infinity.
  3. Edge Relaxation and Node Selection — Outgoing edges from the starting node are relaxed and the unvisited node with the lowest tentative cost is selected.
  4. Iterative Path Optimization — Visited nodes are closed and neighbor distances are iteratively relaxed to discover shorter intermediate paths.
  5. Algorithm Termination — The traversal concludes when all reachable nodes are settled and optimal distances are finalized in the table.
  6. Complexity and Pseudocode Analysis — Time complexity bounds are established based on heap data structures and formal pseudocode mechanics.
PDF notes

Frequently asked questions

Why does Dijkstra's algorithm fail when negative edge weights are present?

Dijkstra permanently freezes a node's distance once popped; negative edges can later produce a shorter path, violating the greedy assumption.

How does Dijkstra differ fundamentally from Prim's or Kruskal's algorithms?

Dijkstra minimizes cumulative distance from one start node, whereas Prim's and Kruskal's minimize the total edge weight connecting all vertices.

Why do unvisited nodes start with a distance of infinity?

Infinity represents unreachable state, allowing any real calculated edge cost to trigger the relaxation condition upon first discovery.

What is the optimal time complexity using a standard binary heap versus a Fibonacci heap?

Binary min-heaps run in $O((V + E) \log V)$ time, whereas Fibonacci heaps achieve $O(E + V \log V)$ via $O(1)$ amortized decrease-key operations.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.