Graphs
3 programs — patterns, complexity, and code
| # | Program Name | Pattern | Time | Space | Key Idea | Level |
|---|---|---|---|---|---|---|
| 1 | numberOfIslands | BFS / DFS Grid | O(m·n) | O(m·n) | Flood-fill each unvisited land cell with DFS, counting how many floods started. | Middle |
| 2 | courseSchedule | Topological Sort (Kahn) | O(V+E) | O(V+E) | BFS from nodes with in-degree 0; cycle exists if not all nodes are processed. | Middle |
| 3 | cloneGraph | BFS + HashMap | O(V+E) | O(V) | Map original node → clone; BFS to visit neighbors and link clones. | Middle |