Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle? Will removing a single edge disconnect G? If G is directed, what are the strongly connected components? If G is the WWW, follow links to locate information. Most graph algorithms need to examine or process each vertex and each edge.
Breadth-First Search Find the shortest path from a source node s to all other nodes. It returns the distance of each vertex from s. a tree of shortest paths called the breadth-first tree. Idea: Find nodes at distance 0, then at distance 1, then at distance 2, etc.
A BFS Example s d b g f e c a L = s 0
s d b g f e c a Visualized as many simultaneous explorations starting from s and spreading out independently. L = s 0 L = a, c 1 frontier of exploration
s d b g f e c a L = s 0 L = a, c 1 L = d, e, f 2 Dashed edges were explored but had previously been discovered.
s d b g f e c a L = s 0 L = a, c 1 L = d, e, f 2 L = b, g 3
s d b g f e c a L = s 0 L = a, c 1 L = d, e, f 2 L = b, g 3 The Finish
s d b g f e c a Breadth-First Tree d(b): shortest distance from s to b The number of visited vertices is one more than the number of tree edges and hence at most one more than the number of explored edges.
The BFS Algorithm BFS(G, s) for each v V(G) – s do d(v) // shortest distance from s d(s) 0 // initialization L s T i 0 while L ≠ do // all nodes at distance i from s L for each u L do for each v Adj(u) do if d(v) = then // not yet visited d(v) d(u) + 1 insert v into L T T {(u, v)} i i i i+1 i
Correctness Lemma For each i, the set L includes all nodes at distance i from s. i Proof By induction on the distance k from s. s … uv k Basis: L = s includes the only node at distance 0 from s. 0 Inductive step: Suppose L consists of all nodes at distance k ≥ 0 from s. Every node v at distance k+1 from s must be adjacent to a node u at distance k. By hypothesis u L. Thus v L. k k k+1 Corollary At the finish of BFS, d(v) is the length of the shortest path from s to v.
Running Time O(|E|) if G is represented using adjacency lists. The total number of edge scans is ≤ |E| for a directed graph ≤ 2 |E| for an undirected graph u v e u v e O(|V| ) if represented as an adjacency matrix. 2 Every vertex is inserted at most once into some list L. i The number of inserted vertices is at most one more than the number of scanned edges; hence ≤ |E| + 1.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed before exploring G and G From Computer Algorithms by S. Baase and A. van Gelder
The DFS Algorithm DFS(G) time 0 // global variable for each v V(G) do disc(v) unseen for each v V(G) do if disc(v) = unseen then DFS-visit(v) time time + 1 disc(v) time for each u Adj(v) do if disc(u) = unseen then DFS-visit(u)
A DFS Example a l g f b ced j k ih time = 1
a l g f b ced j k ih 1 2
a l g f b c e d j k ih
a l g f b ced j k ih
a l g f b ced j k ih
a l g f b ced j k ih