Graphs (1)
Formally, a graph is defined as G = (V, E), where V is a set of vertices and E is a set of edges. In a directed graph, each edge is an ordered pair ‹s, d› from source vertex s to destination vertex d, meaning 0 ≤ |E| ≤ |V|2 (or |V|(|V| − 1) if self-loops are excluded). The "size" of G depends on both |V| and |E|, though sometime f(|V|, |E|) is simplified to f(V, E).
Under this definition, graphs generalize trees and linear data structures, and can themselves be extended into multigraphs, hypergraphs, and knowledge graphs.
A graph may be either directed or undirected, depending on whether the underlying relation is asymmetric or symmetric. In undirected graphs, edges are unordered pairs {s, d}.
A path is a sequence of consecutive edges, and its length (number of edges) can be zero or any positive integer. A cycle (or loop) is a non-empty path that starts and ends at the same vertex. In a directed graph, a cycle can consist of a single self-loop edge, whereas in an undirected graph, a simple cycle is usually required to contain at least three distinct edges.
An undirected graph is connected if there is a path between every pair of vertices. A directed graph is strongly connected if there is a directed path from every vertex to every other vertex; it is weakly connected if the underlying undirected graph (obtained by ignoring edge directions) is connected.
The two standard data structures for representing a graph are the adjacency list and the adjacency matrix. An adjacency list uses Θ(V + E) space and is preferable for sparse graphs (such as when E = O(V)), whereas an adjacency matrix uses Θ(V2) space and is better suited for dense graphs (such as when E = Θ(V2)). Additionally, an adjacency matrix provides O(1) edge-existence queries, and allows edges to be traversed in both directions (outgoing and incoming) at the same cost, whereas a standard adjacency list only stores outgoing edges.
In the algorithms below, both representations are abstracted using G.adj[u] to denote the adjacency list or set {v ∈ V | ‹u, v› ∈ E}.
A graph is weighted if every edge has an associated numerical weight representing a cost, distance, or capacity, which can be stored directly within the adjacency list nodes or matrix entries.
Graph search (traversal) algorithms systematically explore the vertices of a graph by following its edges.
Breadth-First Search (BFS): Starting from a source vertex s, BFS explores vertices layer by layer in order of increasing distance from s. In the algorithm below, each vertex is assigned a color to track its discovery state: undiscovered (WHITE), discovered and currently in the frontier (GRAY), or fully explored (BLACK). For each vertex u, the algorithm records its predecessor (u.π) and shortest-path distance (u.d) from s. A first-in, first-out queue Q manages the frontier of gray vertices.
Example:
When all vertices are reachable from the source s, the running time of BFS is Θ(V + E).
As a by-product, BFS constructs a search tree rooted at s that contains a shortest path (in terms of edge count) from s to every reachable vertex in G. In line 13, encountering an adjacent vertex v whose color is not WHITE indicates a cycle—with the caveat that in an undirected graph, we must ignore the immediate parent (v ≠ u.π), as traversing an undirected edge back to a vertex's predecessor does not constitute a cycle.
Depth-First Search (DFS): Rather than exploring level by level, DFS explores as deeply as possible along each branch before backtracking.
If the queue in the iterative BFS template above is replaced with a stack, the traversal behaves like DFS; if it is replaced with a priority queue, it becomes Best-First Search. In all of these single-source variants, only vertices reachable from the starting vertex are visited.
The following recursive DFS algorithm iterates over all vertices without requiring a single designated source, thereby generating a depth-first forest of one or more trees if the graph is disconnected or not strongly connected from the first vertex. (Iterative BFS can similarly be wrapped in an outer loop over all vertices to cover the entire graph.)
This version of DFS records two timestamps for each vertex u: discovery time u.d (when u turns GRAY) and finishing time u.f (when u turns BLACK). These timestamps reveal structural properties of the graph used by subsequent algorithms.
Example:
Edges explored during traversal form the depth-first forest (tree edges). All other edges in a directed graph can be classified into three types based on ancestor-descendant relationships in the forest: back edges (B, pointing to an ancestor), forward edges (F, pointing to a non-child descendant), and cross edges (C, connecting vertices with no ancestor-descendant relationship).
The running time of DFS is Θ(V + E).
A topological sort of a directed acyclic graph (DAG) G = (V, E) is a linear ordering of all its vertices such that for every directed edge ‹u, v› ∈ E, vertex u appears before vertex v in the ordering. If the graph contains a cycle, no valid linear ordering exists.
Topological sorting can be performed iteratively (Kahn's algorithm) by repeatedly selecting and removing a vertex v with in-degree zero along with its outgoing edges, until either all vertices are ordered or no vertex with in-degree zero remains (which proves the graph is cyclic).
Alternatively, topological sorting can be implemented directly using DFS by inserting each vertex onto the front of a linked list as soon as it finishes:
Its running time is Θ(V + E).
Example:
DFS can also be used to decompose a directed graph into its strongly connected components (SCCs). The algorithm uses the transpose of G, denoted GT, which is obtained by reversing the direction of every edge in G. Because G and GT share the exact same strongly connected components, running DFS first on G to compute vertex finishing times, and then on GT in decreasing order of those finishing times, isolates each SCC as a separate tree in the second depth-first forest.
Example:
The correctness of this algorithm is justified using the properties of DFS and is explained in the textbook. Its total running time is Θ(V + E), matching that of DFS.