Graphs model relationships: networks, dependencies, routes, permissions, and many real-world system structures. Graph algorithms help you traverse, rank, and optimize those relationships efficiently. Example: shortest-path algorithms answer “what’s the cheapest route” while BFS/DFS answer “what’s reachable”.

Diagram

flowchart TD
  A[Graph problem] --> B{Need reachability or levels}
  B -->|Yes| C[DFS BFS]
  B -->|No| D{Need minimum path cost}
  D -->|Yes with non negative weights| E{Have a heuristic to the target}
  E -->|Yes| E1[A Star Search]
  E -->|No| E2[Dijkstra]
  D -->|Can have negative edges| F[Bellman Ford]
  D -->|Need all pairs shortest paths| G[Floyd Warshall]
  A --> H{Need structure not distance}
  H -->|Cheapest way to connect every node| I[Minimum Spanning Tree]
  H -->|Mutually reachable groups in a digraph| J[Strongly Connected Components]
  H -->|Single points of failure| K[Articulation Points and Bridges]
  H -->|Clusters in an undirected graph| M[Connected Components]
  H -->|Throughput through a capacitated network| L[Maximum Flow]

Algorithm Selection

Shortest path

AlgorithmSolvesTimeConstraint
DFS BFSReachability, shortest path by edge countO(V + E)Unweighted graphs
DFS BFSCycle detection, topological sort, componentsO(V + E)Any graph
DijkstraSingle-source shortest pathO((V + E) log V)Non-negative weights
A* SearchPoint-to-point shortest pathO((V + E) log V), far fewer nodes expandedNon-negative weights and an admissible heuristic
Greedy Best-First SearchFast point-to-point path, not optimalO((V + E) log V)Heuristic only; sacrifices optimality for speed
Bidirectional SearchPoint-to-point shortest pathO(b^(d/2)) vs O(b^d)Target known; graph must be reversible
Bellman-FordSingle-source shortest pathO(V·E)Handles negative edges; detects negative cycles
Floyd-WarshallAll-pairs shortest pathO(V³) time, Θ(V²) spaceSmall/dense graphs; detects negative cycles

Structure and connectivity

AlgorithmSolvesTimeConstraint
Minimum Spanning TreeCheapest edge set connecting all verticesO(E log V)Connected, undirected, weighted
Topological SortLinear order respecting dependenciesO(V + E)Directed acyclic graph
Strongly Connected ComponentsMaximal mutually-reachable vertex setsO(V + E)Directed graphs
Connected ComponentsMaximal connected vertex setsO(V + E)Undirected graphs
Articulation Points and BridgesCut vertices and cut edgesO(V + E)Undirected graphs
Maximum FlowMax s–t throughput; min cutO(V·E²) (Edmonds–Karp)Capacitated network

NOTE

Not every graph problem admits a polynomial algorithm. Hamiltonian Cycle — visit every vertex exactly once and return to the start — is NP-complete, so it sits outside the selection tables above: there is no known efficient algorithm to choose, only exponential search and heuristics.

Questions

References

16 items under this folder.