Presentation is loading. Please wait.

Presentation is loading. Please wait.

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?

Similar presentations


Presentation on theme: "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?"— Presentation transcript:

1 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.

2 Breadth-First Search Find the shortest path from a source node s to all other nodes. Outputs 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, …

3 A BFS Example s d b g f e c a L =  s  0

4 s d b g f e a Visualized as many simultaneous explorations starting from s and spreading out independently. L =  s  0 L =  a, c  1 frontier of exploration c

5 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 resulted in the discovery of no new vertices.

6 s d b g f e c a L =  s  0 L =  a, c  1 L =  d, e, f  2 L =  b, g  3

7 The Finish s d b g f e c a L =  s  0 L =  a, c  1 L =  d, e, f  2 L =  b, g  3

8 Breadth-First Tree s d b g f e c a 0 1 1 2 2 2 3 3 d(b): shortest distance from s to b # visited vertices = 1 + # tree edges  1 + # explored edges.

9 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 + 1 0 i i+1 i

10 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. k Corollary At the finish of BFS, d(v) is the length of the shortest path from s to v. k k+1 By hypothesis u  L. Thus v  L. Every node v at distance k+1 from s must be adjacent to a node u at distance k.

11 Running Time O(|E|) if G is represented using adjacency lists. # edge scans ≤ |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 # inserted vertices  1 + # scanned edges Courtesy of Dr. Fernandez-Baca  |E| + 1.


Download ppt "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?"

Similar presentations


Ads by Google