Elementary Graph Algorithms
Algorithms · Advanced Placement
Elementary Graph Algorithms: the big picture
A graph consists of vertices and edges, directed or undirected, and the choice of representation shapes every algorithm built on it. An adjacency list stores for each vertex the list of its neighbours, taking space proportional to vertices plus edges and allowing a vertex's neighbours to be enumerated in time proportional to their number, which suits sparse graphs. An adjacency matrix uses a bit or weight per vertex pair, taking space proportional to the square of the vertex count but answering edge-existence queries in constant time, which suits dense graphs and algorithms that test specific edges. Breadth-first search explores vertices in order of distance from a source using a queue, computes shortest path distances in an unweighted graph and produces a breadth-first tree. Depth-first search explores as far as possible before backtracking, using a stack implicitly through recursion, and its discovery and finishing times classify edges as tree, back, forward or cross edges, which is the basis of several further algorithms. A back edge in a directed graph indicates a cycle, so depth-first search detects cycles. Topological sorting of a directed acyclic graph orders vertices so that every edge points forward, obtained by listing vertices in decreasing finish time. Strongly connected components are found by two depth-first passes.
Graph Representations, Breadth-First & Depth-First Search
Two representations, two traversals, and most graph algorithms follow. An adjacency list costs space proportional to vertices plus edges and suits sparse graphs; a matrix costs the square of the vertex count but answers edge queries instantly. Breadth-first search uses a queue and visits in order of distance, which makes it the shortest-path algorithm for unweighted graphs. Depth-first search uses a stack and goes deep before backtracking, and its discovery and finish times are what detect cycles, order a directed acyclic graph, and find strongly connected components.
{{illustration:algos-l14-f1}}
{{illustration:algos-l14-f2}}
Key relations
- Traversal costs and representations. T_BFS = T_DFS = O(V + E) ; Sₗᵢₛₜ = O(V + E) ; Sₘₐₜᵣᵢₓ = O(V²)
Topological Sort, Cycles & Strongly Connected Components
A directed acyclic graph can be linearised so every edge points forward, which is what a build system or package manager needs to order its work. Depth-first search gives that order for free: list the vertices in decreasing finish time. If a back edge appears instead, there is a cycle and no topological order exists, which is exactly the circular dependency error those tools report. Strongly connected components — maximal sets where every vertex reaches every other — are found with two depth-first passes, the second on the transposed graph.
{{illustration:algos-l14-f3}}
{{illustration:algos-l14-f4}}
Key relations
- Topological order and component condensation. u → v ⇒ f(u) > f(v) in a DAG ; G^SCC is always acyclic