DSA Crash Course
Advanced Data Structures

4.4 Advanced Graph Algorithms

Mark when done:

Deep Dive: Dijkstra Step by Step

Beginner tip: When graph edges have weights (like road distances), BFS no longer finds the shortest path. Dijkstra's algorithm does -- it always expands the closest unvisited node, like a GPS routing you through traffic. For interviews, master Dijkstra and topological sort. The rest (Bellman-Ford, Floyd-Warshall, Kruskal's) are good to know but rarely tested.

Network Delay Time

Graph: node 2 sends signal. Edges: 2->1 (1), 2->3 (1), 3->4 (1).

Start: dist = {2:0}, heap = [(0,2)]

Pop (0,2): visit node 2
  -> neighbor 1: 0+1=1, push (1,1). dist={2:0, 1:1}
  -> neighbor 3: 0+1=1, push (1,3). dist={2:0, 1:1, 3:1}

Pop (1,1): visit node 1. No outgoing edges.

Pop (1,3): visit node 3.
  -> neighbor 4: 1+1=2, push (2,4). dist={2:0, 1:1, 3:1, 4:2}

Pop (2,4): visit node 4. No outgoing edges.

All nodes visited. Max distance = 2. Answer: 2.

Topological Sort -- Alien Dictionary

Given sorted alien words: ["wrt", "wrf", "er", "ett", "rftt"]

Compare adjacent words to find edges:
  "wrt" vs "wrf": first diff at index 2: t->f
  "wrf" vs "er":  first diff at index 0: w->e
  "er"  vs "ett": first diff at index 1: r->t
  "ett" vs "rftt": first diff at index 0: e->r

Graph: t->f, w->e, r->t, e->r
Topo sort: w -> e -> r -> t -> f
Alien alphabet order: wertf

Before You Start -- How to Think About Advanced Graphs

When to Go Beyond BFS/DFS

BFS/DFS handle: unweighted shortest path, traversal, connectivity, cycle detection.

You need more when:
  - Edges have WEIGHTS -> Dijkstra
  - Need ALL-PAIRS shortest path -> Floyd-Warshall
  - Need DEPENDENCY ORDERING -> Topological Sort
  - Need to find BRIDGES / cut points -> Tarjan's
  - Need MINIMUM SPANNING TREE -> Kruskal's / Prim's

Dijkstra -- The One You Must Know

What: Shortest path from a source to all other nodes in a weighted graph (non-negative weights).
How: Greedy + min-heap. Always expand the closest unvisited node.
Complexity: O((V+E) log V)

Template:
  1. distances = {node: infinity for all nodes}, distances[start] = 0
  2. min-heap with (0, start)
  3. Pop smallest, update neighbors if shorter path found

Topological Sort -- For Dependencies

What: Order nodes so that for every edge A->B, A comes before B.
Only works on DAGs (directed acyclic graphs).

Kahn's Algorithm (BFS):
  1. Compute in-degree of every node
  2. Add all nodes with in-degree 0 to queue
  3. Process queue: remove node, decrement neighbors' in-degree
  4. If neighbor's in-degree becomes 0, add to queue
  5. If result length < total nodes -> cycle exists!

Interview Priority

AlgorithmInterview FrequencyPriority
DijkstraMedium-HighMust know
Topological SortMediumMust know
Bellman-FordRareKnow concept
Floyd-WarshallRareKnow concept
Kruskal's/Prim'sRareKnow concept
Tarjan'sRareBonus

Priority: Master Dijkstra and Topological Sort. Know the others exist.

Problems

#ProblemAlgorithmStatus
1Network Delay TimeDijkstra[ ]
2Cheapest Flights Within K StopsBellman-Ford[ ]
3Course Schedule IITopological Sort[ ]
4Alien DictionaryTopological Sort[ ]
5Min Cost to Connect All PointsPrim's/Kruskal's[ ]
6Swim in Rising WaterBS + BFS[ ]
7Critical Connections (Bridges)Tarjan's[ ]
8Is Graph Bipartite?BFS 2-coloring[ ]

Priority: Dijkstra is commonly tested. Others are bonus/optional.

Notes