DSA Master › Topic 13

Graphs

Model the problem as nodes and edges first, then pick an algorithm. Use BFS for shortest paths when edges are unweighted, DFS or Union-Find for connectivity, topological sort for dependencies, Dijkstra for weighted shortest paths, and Prim's or Kruskal's for the minimum cost to connect everything.

Concepts

How to recognise it

Common pitfalls

Problems

  1. Number of Islands (Medium)
  2. Max Area of Island (Medium)
  3. Clone Graph (Medium)
  4. Rotting Oranges (Medium)
  5. Course Schedule (Medium)
  6. Course Schedule II (Medium)
  7. Pacific Atlantic Water Flow (Medium)
  8. Graph Valid Tree (Medium, Premium)
  9. Number of Connected Components in an Undirected Graph (Medium, Premium)
  10. Redundant Connection (Medium)
  11. Network Delay Time (Medium)
  12. Cheapest Flights Within K Stops (Medium)
  13. Min Cost to Connect All Points (Medium)