Advanced Graphs — Master Recap and Algorithm Selection Guide

Sanjeev SharmaSanjeev Sharma
3 min read

Advertisement

Advanced Graphs — Master Recap

Algorithm Index

00Complete guide (7 patterns, complexity table)
01Kruskal MST      → sort edges + Union-Find O(E log E)
02Prim MST         → min-heap expansion O(E log V)
03Tarjan SCC       → disc/low/stack O(V+E)
04Bridges & AP     → disc/low single DFS O(V+E)
05Floyd-Warshall   → all-pairs DP O(V³)
06Bellman-FordSSSP with negatives O(VE)
07A* Search        → heuristic Dijkstra O(E log V)
08Topological SortKahn's BFS / DFS 3-color O(V+E)
09Dijkstra States  → state-space (node, stops) O(E log V)
10Bipartite CheckBFS 2-coloring O(V+E)
11Min Cost PointsPrim on complete graph O()
12Min-Max Path     → binary search + BFS O(E log W)
13Count PathsDijkstra + ways[] array
14Critical ConnsTarjan bridges O(V+E)
15Reconstruct ItinHierholzer's Eulerian O(E log E)
16Swim Rising Water→ Dijkstra on grid O(n² log n)
17Longest Path DAG → topo-sort DP O(V+E)
18Network FlowEdmonds-Karp O(VE²)
19Master Recapthis file

Algorithm Selection Guide

Connect all vertices, min cost?
MST (Kruskal if sparse E log E, Prim if dense E log V)

SSSP, non-negative weights?
Dijkstra O((V+E) log V)

SSSP, negative weights, detect cycle?
Bellman-Ford O(VE)

All-pairs shortest path, V500?
Floyd-Warshall O(V³)

Strongly connected components?
Tarjan O(V+E) or Kosaraju O(V+E)

Bridge / cut vertex detection?
Tarjan bridge/AP algorithm O(V+E)

Ordering with dependencies?
Topological sort (Kahn's or DFS) O(V+E)

Is graph 2-colorable?
BFS bipartite check O(V+E)

Eulerian path (use all edges)?
Hierholzer's algorithm O(E log E)

Maximum flow?
Edmonds-Karp O(VE²) or Dinic O(V²E)

Path minimising maximum edge?
Binary search + BFS or Kruskal-like

Complexity Quick Reference

AlgorithmTimeSpaceNotes
KruskalO(E log E)O(V)Sort + UF
PrimO(E log V)O(V)Heap-based
DijkstraO(E log V)O(V)No negatives
Bellman-FordO(VE)O(V)Handles negatives
Floyd-WarshallO(V³)O(V²)All-pairs
Tarjan SCCO(V+E)O(V)Single DFS
Topo SortO(V+E)O(V)Kahn's or DFS
A*O(E log V)O(V)Admissible h
Edmonds-KarpO(VE²)O(V²)Max flow

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro