Chapter 13 - Graphs
Nodes, edges, and a visited set. Behind the variety of graph problems, a handful of algorithms come back again and again.
This page is the entry point of chapter 13 of my Rust solutions to the book Coding Interview Patterns. It sums up the pattern in a few lines, then lists the problems of the chapter. As explained on the series page, the detailed explanations of the algorithms are in the book.
The idea
A graph is stored as an adjacency list. Depth-first and breadth-first searches explore it with a visited set; breadth-first search also gives the shortest path when edges have no weight. A grid is a graph too, each cell linked to its neighbors. Beyond that, a few classics: topological sort for prerequisites, union-find for merging communities, Dijkstra for weighted shortest paths, and a minimum spanning tree to connect points at the lowest cost.
The chapter also has an introduction page with my notes on the key ideas and a checklist.
When to reach for it
- Islands, regions, infections spreading on a grid.
- Shortest path or minimum number of steps.
- Dependencies, ordering, cycles, connected components.
In Rust
Vec<Vec<usize>>is a simple adjacency list,Vec<bool>a visited set.- On a grid, compute neighbors with signed offsets and check the bounds before converting back to
usize. - Dijkstra uses
BinaryHeap<Reverse<(u32, usize)>>to always pop the closest node first.
Problems in this chapter
- p247 - Introduction: Introduction to the Graphs pattern for coding interviews, in Rust: the key idea, when to use it, and the problems of the chapter.
- p250 - Graph Deep Copy: Graph Deep Copy (Graphs) solved in Rust: from a reference to one node of an undirected graph, build an independent deep copy of the whole graph.
- p253 - Count Islands: Count Islands (Graphs), solved in Rust. Given a binary matrix (1=land, 0=water). Return the number of islands (connected land in the 4 directions).
- p258 - Matrix infection: Matrix Infection (Graphs) solved in Rust: each second, infected cells infect their 4 neighbors. How many seconds until the whole matrix is infected?
- p264 - Bipartite Graph Validation: Bipartite Graph Validation (Graphs), solved in Rust. Given an undirected graph. Determine if it is bipartite.
- p268 - Longest Increasing Path: Longest Increasing Path (Graphs) solved in Rust: find the longest strictly increasing path of adjacent cells in a matrix of u32.
- p272 - Shortest Transformation Sequence: Shortest Transformation Sequence (Graphs) solved in Rust: shortest chain of dictionary words from start to end, changing one letter at a time.
- p280 - Merging Communities: Merging Communities (Graphs) solved in Rust: people start in separate communities that merge when two members connect. A union-find exercise.
- p287 - Prerequisites: Prerequisites (Graphs) solved in Rust: given n courses and pairs of prerequisites, determine whether it is possible to enroll in all of them.
- bonus108 - Shortest Path: Shortest Path (Graphs), solved in Rust. n represents nodes (0 to n - 1). undirected graph. array of non-negative weighted edges. return an array where each…
- bonus116 - Connect the Dots: Connect the Dots (Graphs), solved in Rust. Given a set of points on a plane, determine the minimum cost to connect all these points. The cost of connecting…
Other chapters
All the chapters, with every problem, are listed on the series page.