week 11 of 15
Graphs I: traversal
Get comfortable with these coding mechanics first: building an adjacency list from an edge list, a visited set, a queue, a directions list [(1,0),(-1,0),(0,1),(0,-1)], bounds checks before indexing. Template to type from memory by the end of the week: grid or graph search: visited set, queue or recursion, loop over neighbours.
- 91Number of Islands (opens LeetCode in a new tab)Grid DFS or BFSMedium
- Common mistake
- Checking bounds after indexing instead of before.
- 92Clone Graph (opens LeetCode in a new tab)DFS with a mapMedium
- Common mistake
- Storing the copy in the map after recursing.
- 93Rotting Oranges (opens LeetCode in a new tab)Multi-source BFSMedium
- Common mistake
- Counting a minute for a level that did no work.
- 94Is Graph Bipartite? (opens LeetCode in a new tab)Colouring with BFS or DFSMedium
- Common mistake
- Disconnected graphs: start a search from every uncoloured node.
- 95Pacific Atlantic Water Flow (opens LeetCode in a new tab)Reverse DFS from the bordersMedium
- Common mistake
- Comparing in the wrong direction. Water flows uphill from the ocean, so use >=.
- 96Course Schedule (opens LeetCode in a new tab)Topological sort (Kahn's)Medium
- Common mistake
- Edge direction: [a, b] means b comes before a.
- 97Course Schedule II (opens LeetCode in a new tab)Topological orderMedium
- Common mistake
- Returning a partial order when there is a cycle.