19
Graph Traversals t 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 verte and each edge.

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?

Embed Size (px)

Citation preview

Page 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?

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.

Page 2: 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?

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.

Page 3: 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?

A BFS Example

s

d

b

g

f

e

c

a

L = s 0

Page 4: 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?

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, c1 frontier of exploration

Page 5: 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?

s

d

b

g

f

e

c

a

L = s 0

L = a, c1

L = d, e, f 2

Dashed edges were explored but had previously been discovered.

Page 6: 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?

s

d

b

g

f

e

c

a

L = s 0

L = a, c1

L = d, e, f 2

L = b, g 3

Page 7: 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?

s

d

b

g

f

e

c

a

L = s 0

L = a, c1

L = d, e, f 2

L = b, g 3

The Finish

Page 8: 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?

s

d

b

g

f

e

c

a

Breadth-First Tree

0

1

1

2

2

2

3

3

d(b): shortest distance from s to b

The number of visited vertices is one more than the number of treeedges and hence at most one more than the number of explored edges.

Page 9: 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?

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

i+1

Page 10: 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?

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 … u v

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.

Page 11: 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?

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

ve

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 thanthe number of scanned edges; hence ≤ |E| + 1.

Page 12: 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?

Depth-First Search

Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck.

v

G G G1 2

3

G is completely traversedbefore exploring G and G .

12 3

From Computer Algorithms by S. Baase and A. van Gelder

Page 13: 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?

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)

DFS-visit(v) time time + 1 disc(v) time for each u Adj(v) do if disc(u) = unseen then DFS-visit(u)

Page 14: 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?

A DFS Example

a

lg

f

b

c ed

j

k

ih

time = 1

Page 15: 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?

a

lg

f

b

c ed

j

k

ih

1

2

Page 16: 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?

a

lg

f

b

c ed

j

k

ih

1

2 3

4

5

Page 17: 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?

a

lg

f

b

c ed

j

k

ih

1

2 3

4

5

6 7

Page 18: 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?

a

lg

f

b

c ed

j

k

ih

1

2 3

4

5

6 7

8 9

Page 19: 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?

a

lg

f

b

c ed

j

k

ih

1

2 3

4

5

6 7

8 9

10

11 12