Graph Traversal

69
Graph Traversal Chapter 9

description

Graph Traversal. Chapter 9. Problem. Given a directed or undirected graph G=(V,E), find a way to traverse all of its vertices. Solution. Depth First Search (DFS) Breadth First Search (BFS). DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. - PowerPoint PPT Presentation

Transcript of Graph Traversal

Page 1: Graph Traversal

Graph TraversalGraph Traversal

Chapter 9

Page 2: Graph Traversal

ProblemProblem

Given a directed or undirected graph G=(V,E), find a way to traverse all of its vertices.

Page 3: Graph Traversal

SolutionSolution

Depth First Search (DFS) Breadth First Search (BFS)

Page 4: Graph Traversal

DFSDFS

Page 5: Graph Traversal

DFSDFS

Page 6: Graph Traversal

DFSDFS

Page 7: Graph Traversal

DFSDFS

Page 8: Graph Traversal

DFSDFS

Page 9: Graph Traversal

DFSDFS

Page 10: Graph Traversal

DFSDFS

Page 11: Graph Traversal

DFSDFS

Page 12: Graph Traversal

DFSDFS

Page 13: Graph Traversal

DFSDFS

Page 14: Graph Traversal

DFSDFS

Page 15: Graph Traversal

DFSDFS

Page 16: Graph Traversal

DFSDFS

Page 17: Graph Traversal

DFSDFS

Page 18: Graph Traversal

DFSDFS

Page 19: Graph Traversal

DFSDFS

Page 20: Graph Traversal

DFSDFS

Page 21: Graph Traversal

DFSDFS

And so on ...

Page 22: Graph Traversal

DFSDFS

We need also to record

– the birth time of any node: the time of the first visit to the node.

– the death time: the time after all of its children are visited and ..also dead

Page 23: Graph Traversal

Algorithm: DFSAlgorithm: DFSInput: graph G=(V,E) directed or undirected

Output: preordering of the vertices in DFS spanning tree (or forest)

1. predfn 0; postdfn 0; % global variables

2. For each vertex vV3. mark v unvisited 4. End for5. For each vV % for connected components

6. if v is unvisited then dfs(v)7. End for

Page 24: Graph Traversal

Procedure dfs(v)Procedure dfs(v)

1. Mark v visited

2. predfn predfn + 1 % birth time

3. For each edge (v,w) E

4. if w is marked unvisited then dfs(w)

5. End for

6. postdfn postdfn + 1 % death time

Page 25: Graph Traversal

DefinitionsDefinitions

In a directed graph, (u,v) is– a tree edge, if it is in the DFS tree, i.e., v is

marked unvisited – back edge, if v is marked visited &

ancestor of u– Forward edge, if v marked visited &

descendant of u– Cross edge, if it is otherwise.

Page 26: Graph Traversal

Tree edge

back edge

Forward edge

Cross edge

Page 27: Graph Traversal

DefinitionsDefinitions

In an undirected graph, (u,v) is– a tree edge if it is in the DFS tree – back edges, otherwise.

Page 28: Graph Traversal

ExampleExample

a

c

d

b f

e1

Page 29: Graph Traversal

ExampleExample

a

c

d

b f

e1

2

Page 30: Graph Traversal

ExampleExample

a

c

d

b f

e1

2

3

Page 31: Graph Traversal

ExampleExample

a

c

d

b f

e1

2

3

4

Page 32: Graph Traversal

ExampleExample

a

c

d

b f

e1

2

3

4

5

Page 33: Graph Traversal

ExampleExample

a

c

d

b f

e1

2

3

4

5Back edge

Page 34: Graph Traversal

ExampleExample

a

c

d

b f

e1

2

3

4

5, 1Back edge

Page 35: Graph Traversal

ExampleExample

a

c

d

b f

e1

2

3

4

5, 1Back edge

6

Page 36: Graph Traversal

ExampleExample

a

c

d

b f

e1

2

3

4

5, 1Back edge

6

Cross edge

Page 37: Graph Traversal

ExampleExample

a

c

d

b f

e1

2

3

4

5, 1Back edge

6

Cross edge

Back edge

Page 38: Graph Traversal

ExampleExample

a

c

d

b f

e1

2

3

4

5, 1Back edge

6, 2

Cross edge

Back edge

Page 39: Graph Traversal

ExampleExample

a

c

d

b f

e1

2

3

4, 3

5, 1Back edge

6, 2

Cross edge

Back edge

Page 40: Graph Traversal

ExampleExample

a

c

d

b f

e1

2

3, 4

4, 3

5, 1Back edge

6, 2

Cross edge

Back edge

Page 41: Graph Traversal

ExampleExample

a

c

d

b f

e1

2, 33, 4

4, 3

5, 1Back edge

6, 2

Cross edge

Back edge

Page 42: Graph Traversal

ExampleExample

a

c

d

b f

e1

2, 33, 4

4, 3

5, 1Back edge

6, 2

Cross edge

Back edge

Forward edge

Page 43: Graph Traversal

ExampleExample

a

c

d

b f

e1, 6

2, 53, 4

4, 3

5, 1Back edge

6, 2

Cross edge

Back edge

Forward edge

Page 44: Graph Traversal

Analysis of DFSAnalysis of DFS

Time = (m + n)

Applications: Testing acyclicity (is G a tree?) Strongly connected components

Page 45: Graph Traversal

BFSBFS

Page 46: Graph Traversal

BFSBFS

Page 47: Graph Traversal

BFSBFS

Page 48: Graph Traversal

BFSBFS

Page 49: Graph Traversal

BFSBFS

Page 50: Graph Traversal

BFSBFS

Page 51: Graph Traversal

BFSBFS

Page 52: Graph Traversal

BFSBFS

And so on ..

Page 53: Graph Traversal

Algorithm: BFSAlgorithm: BFSInput: graph G=(V,E) directed or undirected

Output: ordering of the vertices in BFS spanning tree (or forest)

1. bfn 0; % global variable

2. For each vertex vV3. mark v unvisited 4. End for5. For each vV % for connected components

6. if v is unvisited then bfs(v)7. End for

Page 54: Graph Traversal

Procedure bfs(v)Procedure bfs(v)

1. Q { v }

2. Mark v visited3. While Q { }4. v pop(Q)5. bfn bfn + 1 % birth time= time to give birth to all of its children

6. For each edge (v,w) E7. if w is marked unvisited then 8. push(w, Q)9. mark w visited 10. end if11. End for12. End while

Page 55: Graph Traversal

NoteNote

Order in which nodes are popped

= order in which they are pushed

= order in which are born

= order in which they give birth

Page 56: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

Page 57: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

2

Page 58: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

2

3

Page 59: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

2

3

4

Page 60: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

2

3

4

5

Page 61: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

2

3

4

5

6

Page 62: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

2

3

4

5

67

Page 63: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

2

3

4

5

67

Page 64: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

2

3

4

5

67 8

Page 65: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

2

3

4

5

67 8

9

Page 66: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

2

3

4

5

67 8

9

Page 67: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

2

3

4

5

67 8

9

Page 68: Graph Traversal

ExampleExample

a

bc

c

e

f

d

g h

1

2

3

4

5

67 8

9

Page 69: Graph Traversal

AnalysisAnalysis

Time = (m + n)

Applications: Computing the distances