Overview

Foundational definitions and the generic search procedure underlying every graph traversal algorithm in this vault. Depth First Search (DFS), Breadth First Search (BFS), and Explore are each a specific choice of data structure for the frontier in the Graph Search algorithm below.


Foundational Concepts

Graphs

A graph is specified by nodes and edges:

Directed edge: — an edge from to .

Graph Representations

Adjacency MatrixAdjacency List

Adjacency Matrix

A matrix :

Symmetric if is undirected.

  • PRO: check for an edge in time
  • CON: uses up space

Adjacency List

For each node, there is a list of outgoing edges.

  • PRO: just space
  • PRO: easily iterate through a node’s neighbors
  • CON: check for an edge in time

Graph Search

Graph Search is a core foundational outline of most graph algorithms, serving as a template for algorithms such as DFS, BFS, Dijkstra’s, etc.

  • Instance: a graph and a starting vertex
  • Output: a list of all vertices reachable from by a directed path in

At each point in a graph search algorithm, the vertices are partitioned into:

  • : explored
  • : frontier
  • : unreached

Pseudocode

Algorithm 41 Graph Search

procedure GraphSearch(G,sG, s)

XX = empty, FF = {s}\{ s \}, U=VFU = V - F

while FF is not empty do

Pick ww in FF

for all (w,y)E(w, y) \in E do

if y∉Xy \not\in X or FF then

move yy from UU to FF

move ww from FF to XX

return X

Implementing , , is a set: Array of Booleans indexed by vertex

  • Test Membership:
  • Insert:

is a set: Stack or Queue + Array of Booleans

  • Find and Delete (Pop, Dequeue, Flip Boolean):
  • Test Membership:
  • Insert (Push, Enqueue, Flip Boolean):

is a set: Array of Booleans

  • Test Membership:
  • Delete:

Choosing = stack gives Depth First Search (DFS) / Explore; choosing = queue gives Breadth First Search (BFS).

Runtime Analysis

Since each is added to at most once, each is also deleted from at most once:

Correctness

If , then there is a path from to :

  • Loop Invariant: after the iteration of the while loop, every element of or is reachable from in .
  • Base Case: before the loop, is empty and .
  • Inductive Hypothesis: suppose the loop invariant is true after iterations.
  • Inductive Step:
    1. Pick a vertex in .
    2. Move all neighbors of into if they’re in — if there’s a path from to and an edge , then there’s a path from to .
    3. Move from to — by the IH, there is a path from to .
  • Thus, it remains true that all elements of and are reachable from .

If by the end of the algorithm, then there is not a path from to :

  • Suppose by contradiction that there is a vertex reachable from that is not in . Then there is a path from to .
  • Let be the last vertex in the path that is in , and be the next vertex after in the path.
  • Then must have been in at some point. When was picked from , must have been moved from to . And down the line, must have been moved from to — contradicting that .

Conclusion: since both directions hold — there is a path from to — the algorithm correctly computes exactly the set of vertices reachable from .


Notes in This Section

NoteDescription
Graph Algorithms SummaryA quick, notesheet-style overview of Graph Algorithms
Depth First Search (DFS)Graph Search with = stack (via recursion), run over all vertices to cover disconnected components
ExploreThe low-level, single-component recursive implementation of Graph Search with = stack
Breadth First Search (BFS)Graph Search with = queue; guarantees shortest paths on unweighted graphs
Dijkstra’s AlgorithmGraph Search with = priority queue keyed by cumulative distance; handles weighted graphs (non-negative weights)
Prim’s AlgorithmSame loop shape as Dijkstra’s, but is keyed by single-edge cost to build a minimum spanning tree instead
Kruskal’s AlgorithmA different greedy MST approach — sorts all edges globally and uses Disjoint Sets & Up-Trees instead of a frontier-based search
Strongly Connected Components (SCC)Two-pass DFS (with a reversed graph) that decomposes a directed graph into its strongly connected components
A-Star SearchGraph Search with = priority queue keyed by cumulative distance distance away from goal; handles weighted graphs (non-negative) and more efficient compared to Dijkstra’s

Related Categories