outline and reading depth-first search subgraph ...flocchin/csi2114-04/slides/dfs6pp.pdf · a...

6
Depth-First Search 9/14/2004 2:44 PM 1 9/14/2004 2:44 PM Depth-First Search 1 Depth-First Search D B A C E 9/14/2004 2:44 PM Depth-First Search 2 Outline and Reading Definitions (§6.1) Subgraph Connectivity Spanning trees and forests Depth-first search (§6.3.1) Algorithm Example Properties Analysis Applications of DFS (§6.5) Path finding Cycle finding 9/14/2004 2:44 PM Depth-First Search 3 Subgraphs A subgraph S of a graph G is a graph such that The edges of S are a subset of the edges of G The edges of S are a subset of the edges of G A spanning subgraph of G is a subgraph that contains all the vertices of G Subgraph Spanning subgraph 9/14/2004 2:44 PM Depth-First Search 4 Connectivity A graph is connected if there is a path between every pair of vertices A connected component of a graph G is a maximal connected subgraph of G Connected graph Non connected graph with two connected components 9/14/2004 2:44 PM Depth-First Search 5 Trees and Forests A (free) tree is an undirected graph T such that T is connected T has no cycles This definition of tree is different from the one of a rooted tree A forest is an undirected graph without cycles The connected components of a forest are trees Tree Forest 9/14/2004 2:44 PM Depth-First Search 6 Spanning Trees and Forests A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree

Upload: others

Post on 11-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Outline and Reading Depth-First Search Subgraph ...flocchin/CSI2114-04/SLIDES/DFS6pp.pdf · A forest is an undirected graph without cycles The connected components of a forest are

Depth-First Search 9/14/2004 2:44 PM

1

9/14/2004 2:44 PM Depth-First Search 1

Depth-First Search

DB

A

C

E

9/14/2004 2:44 PM Depth-First Search 2

Outline and Reading

Definitions (§6.1)

� Subgraph

� Connectivity

� Spanning trees and forests

Depth-first search (§6.3.1)

� Algorithm

� Example

� Properties

� Analysis

Applications of DFS (§6.5)

� Path finding

� Cycle finding

9/14/2004 2:44 PM Depth-First Search 3

Subgraphs

A subgraph S of a graph G is a graph such that

� The edges of S are a subset of the edges of G

� The edges of S are a subset of the edges of G

A spanning subgraph of G is a subgraph that contains all the vertices of G

Subgraph

Spanning subgraph

9/14/2004 2:44 PM Depth-First Search 4

Connectivity

A graph is connected if there is a path between every pair of vertices

A connected component of a graph G is a maximal connected subgraph of G

Connected graph

Non connected graph with two connected components

9/14/2004 2:44 PM Depth-First Search 5

Trees and Forests

A (free) tree is an undirected graph T such that� T is connected

� T has no cycles

This definition of tree is different from the one of a rooted tree

A forest is an undirected graph without cycles

The connected components of a forest are trees

Tree

Forest

9/14/2004 2:44 PM Depth-First Search 6

Spanning Trees and Forests

A spanning tree of a connected graph is a spanning subgraph that is a tree

A spanning tree is not unique unless the graph is a tree

Spanning trees have applications to the design of communication networks

A spanning forest of a graph is a spanning subgraph that is a forest

Graph

Spanning tree

Page 2: Outline and Reading Depth-First Search Subgraph ...flocchin/CSI2114-04/SLIDES/DFS6pp.pdf · A forest is an undirected graph without cycles The connected components of a forest are

Depth-First Search 9/14/2004 2:44 PM

2

9/14/2004 2:44 PM Depth-First Search 7

Depth-First Search

Depth-first search (DFS) is a general technique for traversing a graph

A DFS traversal of a graph G � Visits all the vertices and

edges of G

� Determines whether G is connected

� Computes the connected components of G

� Computes a spanning forest of G

DFS on a graph with nvertices and m edges takes O(n + m ) time

DFS can be further extended to solve other graph problems� Find and report a path

between two given vertices

� Find a cycle in the graph

Depth-first search is to graphs what Euler tour is to binary trees

9/14/2004 2:44 PM Depth-First Search 8

9/14/2004 2:44 PM Depth-First Search 9

DFS(v)Mark v visitedfor each incident edge (v,w)

if w not visitedDFS(w)

DFS Algorithm –visit of the nodes only

9/14/2004 2:44 PM Depth-First Search 10

With a Stack

2

8

1011

9

7

6

53 4

12

1

Visited: {1} to visit

POP � (1,2)

Visited: {1,2} to visit

T={(1,2)}

POP � (2,3)

Visited: {1,2,3} to visit

T={(1,2), (2,3)}

(1,10)

(1,11)

(1,2)

(2,3)

(1,10)

(1,11)

(2,10)

(3,12)

(3,4)

(1,10)

(1,11)

(2,10)

9/14/2004 2:44 PM Depth-First Search 11

POP � (3,12)

Visited: {1,2,3,12} to visit

T={(1,2), (2,3), (3,12)}

(3,4)

(1,10)

(1,11)

(2,10)

2

8

1011

9

7

6

53 4

12

1

9/14/2004 2:44 PM Depth-First Search 12

2

8

1011

9

7

6

53 4

12

1

POP � (3,4) to visit

Visited: {1,2,3,12,4}

T={(1,2), (2,3), (3,12), (3,4)}

POP � (4,10)

Visited: {1,2,3,12,4,10}

T={(1,2), (2,3), (3,12), (3,4), (4,10)}

(4,10)

(4,5)

(1,10)

(1,11)

(2,10)

(10,8)

(10,6)

(4,5)

(1,10)

(1,11)

(2,10)

to visit

Page 3: Outline and Reading Depth-First Search Subgraph ...flocchin/CSI2114-04/SLIDES/DFS6pp.pdf · A forest is an undirected graph without cycles The connected components of a forest are

Depth-First Search 9/14/2004 2:44 PM

3

9/14/2004 2:44 PM Depth-First Search 13

2

8

1011

9

7

6

53 4

12

1

T={(1,2), (2,3), (3,12), (3,4), (4,10)}

9/14/2004 2:44 PM Depth-First Search 14

Complexity with the Stack

Number of PUSH:

Number of POP:

∑∈

=Vv

mvd 2)(

∑∈

=Vv

mvd 2)(

Tot: O(m)

d(v) = degree of v

9/14/2004 2:44 PM Depth-First Search 15

Depth-First Search + edge labelingAlgorithm DFS(v); Input: A vertex v in a graph

Output: A labeling of the edges as “discovery” edges and “backedges”

for each edge e incident on v do

if edge e is unexplored then

let w be the other endpoint of e

if vertex w is unexplored then

label e as a discovery edge

recursively call DFS(w)

else label e as a backedge

9/14/2004 2:44 PM Depth-First Search 16

Example

DB

A

C

E

DB

A

C

E

DB

A

C

E

discovery edge

back edge

A visited vertex

A unexplored vertex

unexplored edge

9/14/2004 2:44 PM Depth-First Search 17

Example (cont.)

DB

A

C

E

DB

A

C

E

DB

A

C

E

DB

A

C

E

9/14/2004 2:44 PM Depth-First Search 18

Properties of DFS

Property 1DFS(G, v) visits all the vertices and edges in the connected component of v

Property 2The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v

DB

A

C

E

Page 4: Outline and Reading Depth-First Search Subgraph ...flocchin/CSI2114-04/SLIDES/DFS6pp.pdf · A forest is an undirected graph without cycles The connected components of a forest are

Depth-First Search 9/14/2004 2:44 PM

4

9/14/2004 2:44 PM Depth-First Search 19

Analysis of DFS + labeling

Setting/getting a vertex/edge label takes O(1) time

Each vertex is labeled twice

� once as UNEXPLORED

� once as VISITED

Each edge is labeled twice

� once as UNEXPLORED

� once as DISCOVERY or BACK

Method incidentEdges is called once for each vertex

DFS runs in O(n + m) time provided the graph is represented by the adjacency list structure

O(n + m) = O(m)9/14/2004 2:44 PM Depth-First Search 20

Conclusion

If we represent the graph with an adjacency list

Complexity of DFS is O(m)

WORST CASE: m = O(n2), when …

9/14/2004 2:44 PM Depth-First Search 21

Another Example

9/14/2004 2:44 PM Depth-First Search 22

B C

D E

G

A

F

A

C

B

D

F

G

Step 1:

B C

D E

G

A

F

A

Step 2:

F B E C G

A

A

F E

E D A

A E

F B E C G

B A

C A

D F E

F E D A

G A E

E G D FA

E G D FA

9/14/2004 2:44 PM Depth-First Search 23

B C

D

G

A

F

A

Step 3:

F B E C G

B A

C A

D F E

E G D F

F E D A

G A E

E

B C

D

A

F

A

Step 4:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

A

E G D FA

Back Edge

9/14/2004 2:44 PM Depth-First Search 24

B C

D

A

F

A

Step 5:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA

B C

D

A

F

A

Step 6:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA

Page 5: Outline and Reading Depth-First Search Subgraph ...flocchin/CSI2114-04/SLIDES/DFS6pp.pdf · A forest is an undirected graph without cycles The connected components of a forest are

Depth-First Search 9/14/2004 2:44 PM

5

9/14/2004 2:44 PM Depth-First Search 25

B C

D

A

F

A

Step 7:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA

B C

A

F

A

Step 8:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA D

9/14/2004 2:44 PM Depth-First Search 26

B C

A

F

A

Step 10:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA D

B C

A

F

A

Step 9:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA D

9/14/2004 2:44 PM Depth-First Search 27

B C

A

F

A

Step 11:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA D

B C

A

F

A

Step 12:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA D

9/14/2004 2:44 PM Depth-First Search 28

BC

A

F

A

Step 13:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA D

C

A

F

A

Step 14:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA D

B

9/14/2004 2:44 PM Depth-First Search 29

C

A

F

A

Step 15:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA D

B

C

A

F

A

Step 16:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA D

B

9/14/2004 2:44 PM Depth-First Search 30

A

F

A

Step 17:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA D

BC

A

F

A

Step 18:

F B E C G

B A

C A

D F E

F E D A

G A E

E

G

E G D FA D

BC

G

G

Page 6: Outline and Reading Depth-First Search Subgraph ...flocchin/CSI2114-04/SLIDES/DFS6pp.pdf · A forest is an undirected graph without cycles The connected components of a forest are

Depth-First Search 9/14/2004 2:44 PM

6

9/14/2004 2:44 PM Depth-First Search 31

A

Step 19:

F B E C G

B A

C A

D F E

F E D A

G A E

G

E G D FA

A

F

ED

BC

And we’re done!

9/14/2004 2:44 PM Depth-First Search 32

Path Finding

We can specialize the DFS algorithm to find a path between two given vertices u and z using the template method pattern

We call DFS(G, u) with u as the start vertex

We use a stack S to keep track of the path between the start vertex and the current vertex

As soon as destination vertex z is encountered, we return the path as the contents of the stack

9/14/2004 2:44 PM Depth-First Search 33

1

2

34

5

2 -- 6

2 1 5 4 3

6

6

9/14/2004 2:44 PM Depth-First Search 34

Algorithm pathDFS(G, v, z)

setLabel(v, VISITED)

S.push(v)

if v = zreturn S.elements()

for all e ∈ G.incidentEdges(v)

if getLabel(e) = UNEXPLOREDw ← opposite(v,e)

if getLabel(w) = UNEXPLOREDsetLabel(e, DISCOVERY)

S.push(e)

pathDFS(G, w, z)

S.pop(e)

else

setLabel(e, BACK)

S.pop(v)

9/14/2004 2:44 PM Depth-First Search 35

Cycle Finding

We can specialize the DFS algorithm to find a simple cycle using the template method pattern

We use a stack S to keep track of the path between the start vertex and the current vertex

As soon as a back edge (v, w) is encountered, we return the cycle as the portion of the stack

from the top to vertex w

9/14/2004 2:44 PM Depth-First Search 36

Algorithm cycleDFS(G, v, z)

setLabel(v, VISITED)

S.push(v)

for all e ∈ G.incidentEdges(v)

if getLabel(e) = UNEXPLOREDw ← opposite(v,e)

S.push(e)

if getLabel(w) = UNEXPLOREDsetLabel(e, DISCOVERY)

pathDFS(G, w, z)

S.pop(e)

else

T ← new empty stack

repeat

o ← S.pop()

T.push(o)

until o = wreturn T.elements()

S.pop(v)