Graphs
Vertices and edges: traversal, shortest paths, spanning trees, connectivity and network flow.
Basics
Graph vocabulary, ways to store a graph, and graphs hidden inside grids and puzzles.
- Graphs: vertices, edges, directed, weighted, degree, paths A graph is a set of vertices joined by edges, which can be directed or weighted. Learn degree, paths and cycles by hovering over a graph.
- Adjacency matrix, adjacency list, edge list Store a graph as an adjacency matrix, an adjacency list or an edge list. Click an edge to see it in all three and compare their costs.
- Implicit graphs: grids, states and puzzles Many problems are graphs in disguise: grid cells, game states and puzzle positions. Each cell or state is a vertex and each move is an edge.
Traversal
Visit every vertex with BFS and DFS, then find components, cycles and orderings.
- Breadth-first search Breadth-first search explores a graph in rings of increasing distance using a queue. Watch the frontier expand level by level.
- Depth-first search Depth-first search follows one path as deep as it can, then backtracks. Watch the path dive and retreat alongside the call stack.
- Connected components and flood fill A connected component is a group of vertices that can reach each other. Flood fill colours each component, as in counting islands on a grid.
- Cycle detection in undirected and directed graphs Detect cycles in undirected and directed graphs. In a directed graph, DFS colours nodes white, gray and black, and a gray-to-gray edge…
- Bipartite check by 2-coloring A graph is bipartite when its vertices split into two groups with every edge crossing between them. Two-colour it with BFS and watch for a…
- Topological sort: Kahn's algorithm and DFS finishing order A topological order lists tasks so every dependency comes first. Kahn’s algorithm ticks in-degree counters down; DFS uses finishing order.
Shortest paths
Fewest edges or least total weight: BFS, Dijkstra, Bellman-Ford, Floyd-Warshall and A*.
- Shortest paths in unweighted graphs; path reconstruction In an unweighted graph BFS finds shortest paths. Distance labels spread outward and parent arrows trace each path back to the source.
- Dijkstra's algorithm Dijkstra’s algorithm finds shortest paths with non-negative weights. It settles the closest unsettled vertex and relaxes its edges using a…
- Bellman-Ford and negative edges Bellman-Ford relaxes every edge n − 1 times, so it handles negative weights. One more round that still improves a distance reveals a…
- 0-1 BFS and multi-source BFS When edge weights are only 0 or 1, a deque replaces the priority queue. Multi-source BFS starts from many sources at once, as in rotting…
- Floyd-Warshall: all pairs Floyd-Warshall finds shortest paths between every pair of vertices. The distance matrix updates once for each vertex allowed as a midpoint.
- A* search with heuristics A* adds a heuristic estimate of the remaining distance to Dijkstra’s cost. A good heuristic aims the search at the goal and expands fewer…
Spanning trees and connectivity
Connect everything at minimum cost, and find the parts a graph cannot lose.
- Kruskal's algorithm Kruskal’s algorithm builds a minimum spanning tree by taking edges from cheapest to most expensive, skipping any edge that would form a…
- Prim's algorithm Prim’s algorithm grows a minimum spanning tree from one vertex, always adding the cheapest edge that reaches a new vertex.
- Strongly connected components: Kosaraju and Tarjan In a directed graph a strongly connected component is a set where every vertex reaches every other. Find them with Kosaraju’s or Tarjan’s…
Flow and matching
Push as much as possible through a network, and pair items up with matchings.
- Maximum flow: Ford-Fulkerson and Edmonds-Karp Maximum flow pushes as much as possible from source to sink without exceeding edge capacities. Augmenting paths push flow and residual…