of 68 /68
Depth-First Search  Depth-first search is another strategy for exploring a graph i.e. is a systematic way to find all the vertices reachable from a source vertex. Explore “deeper” in the graph whenever possible  Edges are explored out of the most recently discovered vertex v that still has unexplored edges When all of v’s edges have been explored, backtrack to the vertex from which v was discovered

# DFS algorithm

Embed Size (px)

### Text of DFS algorithm

8/4/2019 DFS algorithm

Depth-First Search

●  Depth-first search is another strategy for

exploring a graph i.e. is a systematic way to

find all the vertices reachable from a source

vertex.

■ Explore “deeper” in the graph whenever possible

■ Edges are explored out of the most recently

discovered vertex v that still has unexplored edges■ When all of v’s edges have been explored,

backtrack to the vertex from which v was

discovered

8/4/2019 DFS algorithm

Depth-First Search

● Vertices initially colored white

● Then colored gray when discovered

●Then black when finished

8/4/2019 DFS algorithm

DFS Example

sourcevertex

8/4/2019 DFS algorithm

DFS Example

1 | | |

|||

| |

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 | | |

|||

2 | |

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 | | |

||3 |

2 | |

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 | | |

||3 | 4

2 | |

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 | | |

|5 |3 | 4

2 | |

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 | | |

|5 | 63 | 4

2 | |

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 | 8 | |

|5 | 63 | 4

2 | 7 |

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 | 8 | |

|5 | 63 | 4

2 | 7 9 |

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 | 8 | |

|5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 | 8 |11 |

|5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 |12 8 |11 |

|5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 |12 8 |11 13|

|5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 |12 8 |11 13|

14|5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 |12 8 |11 13|

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

8/4/2019 DFS algorithm

DFS Example

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Tree edges

8/4/2019 DFS algorithm

DFS: Kinds of edges

● DFS introduces an important distinction

among edges in the original graph:

■ Tree edge: encounter new (white) vertex

■ Back edge: from descendent to ancestor

○ Encounter a grey vertex (grey to grey)

8/4/2019 DFS algorithm

DFS Example

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Tree edges  Back edges

8/4/2019 DFS algorithm

DFS: Kinds of edges

● DFS introduces an important distinction

among edges in the original graph:

■ Tree edge: encounter new (white) vertex

■ Back edge: from descendent to ancestor

■ Forward edge: from ancestor to descendent

○ Not a tree edge, though

○ From grey node to black node

8/4/2019 DFS algorithm

DFS Example

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Tree edges  Back edges  Forward edges

8/4/2019 DFS algorithm

DFS: Kinds of edges

● DFS introduces an important distinction

among edges in the original graph:

■ Tree edge: encounter new (white) vertex

■ Back edge: from descendent to ancestor

■ Forward edge: from ancestor to descendent

■ Cross edge: between a tree or subtrees

○ From a grey node to a black node

8/4/2019 DFS algorithm

DFS Example

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Tree edges  Back edges  Forward edges Cross edges

8/4/2019 DFS algorithm

DFS Applications

David Luebke 26 4/13/2012

8/4/2019 DFS algorithm

● Topological Sorting

● Strong connectedness

David Luebke 27 4/13/2012

8/4/2019 DFS algorithm

Topological Sort

● There are many problems involving a set of tasks in which some of the tasks must be donebefore others.

● For example, consider the problem of taking acourse only after taking its prerequisites.

● Is there any systematic way of linearly

arranging the courses in the order that theyshould be taken?

David Luebke 28 4/13/2012

8/4/2019 DFS algorithm

● Topological sort is a method of arranging the

vertices in a directed acyclic graph (DAG), as

a sequence, such that no vertex appear in the

sequence before its predecessor.

David Luebke 29 4/13/2012

8/4/2019 DFS algorithm

30

Topological Sort: DFS

G A B

D E

8/4/2019 DFS algorithm

31

Topological Sort: DFS

G   A  B

D E

dfs(A)

8/4/2019 DFS algorithm

32

Topological Sort: DFS

dfs(A)

dfs(D)

G   A  B

D  E

8/4/2019 DFS algorithm

33

Topological Sort: DFS

dfs(A)

dfs(D)

dfs(E)

G   A  B

D E

8/4/2019 DFS algorithm

34

Topological Sort: DFS

dfs(A)

dfs(D)

dfs(E)

dfs(F)

G   A  B

D E

8/4/2019 DFS algorithm

35

Topological Sort: DFS

dfs(A)

dfs(D)

dfs(E)

dfs(F)

dfs(H)

G   A  B

D E

8/4/2019 DFS algorithm

36

Topological Sort: DFS

dfs(A)

dfs(D)

dfs(E)

dfs(F)

G   A  B

D E

8/4/2019 DFS algorithm

37

Topological Sort: DFS

dfs(A)

dfs(D)

dfs(E)

A  B

D E

8/4/2019 DFS algorithm

38

Topological Sort: DFS

dfs(A)

dfs(D)

G   A  B

D  E

8/4/2019 DFS algorithm

39

Topological Sort: DFS

dfs(A)

dfs(D)

G   A  B

D  E

8/4/2019 DFS algorithm

40

Topological Sort: DFS

dfs(A)

G   A  B

D E

5 4

8/4/2019 DFS algorithm

41

Topological Sort: DFS

dfs(A)

G   A  B

D E

4 5

8/4/2019 DFS algorithm

42

Topological Sort: DFS

G   A  B

D E

4 5

8/4/2019 DFS algorithm

43

Topological Sort: DFS

G   A  B

D E

4 5

dfs(B)

8/4/2019 DFS algorithm

44

Topological Sort: DFS

G   A  B

D E

4 5

dfs(B)

8/4/2019 DFS algorithm

45

Topological Sort: DFS

G   A B

D E

4 5

3  2

8/4/2019 DFS algorithm

46

Topological Sort: DFS

G   A B

D E

4 5

dfs(C)

8/4/2019 DFS algorithm

47

Topological Sort: DFS

G   A B

D E

4 5

dfs(C)

8/4/2019 DFS algorithm

48

Topological Sort: DFS

G   A B

D E

4 5

dfs(C)

8/4/2019 DFS algorithm

49

Topological Sort: DFS

G   A B

D E

4 5

dfs(C)

8/4/2019 DFS algorithm

50

Topological Sort: DFS

G   A B

D E

4 5

dfs(C)

dfs(G)

8/4/2019 DFS algorithm

51

Topological Sort: DFS

G A B

D E

4 5

3  2

dfs(C)

1

8/4/2019 DFS algorithm

52

Topological Sort: DFS

G A B

D E

4 5

3  2 1

8/4/2019 DFS algorithm

53

Topological Sort: DFS

G A B

D E

4 5

3  2 1

Topological order:  C G B A D E F H

8/4/2019 DFS algorithm

Strongly Connected Components

● Definition A strongly connected component of

a directed graph G is a maximal set of vertices

C V such that for every pair of vertices u and

v, there is a directed path from u to v and adirected path from v to u.

● Strongly-Connected-Components(G)

David Luebke 54 4/13/2012

8/4/2019 DFS algorithm

● The disadvantage of Depth-First Search is that

there is a possibility that it may go down the

left-most path forever. Even a finite graph can

generate an infinite tree. One solution to thisproblem is to impose a cutoff depth on the

search.

David Luebke 55 4/13/2012

8/4/2019 DFS algorithm

● Depth-First Search is not guaranteed to find

the solution.

● And there is no guarantee to find a minimal

solution, if more than one solution exists.

David Luebke 56 4/13/2012

BFS in Directed graph

8/4/2019 DFS algorithm

BFS in Directed graph

● Pick a source vertex S to start.

● Find (or discover) the vertices that are

● Pick each child of S in turn and discover their

● Done when all children have been discovered

and examined.

● This results in a tree that is rooted at the source

vertex S.

David Luebke 57 4/13/2012

8/4/2019 DFS algorithm

David Luebke 58 4/13/2012

Example

8/4/2019 DFS algorithm

Initially, d[a] is set to 0 and the rest to ∞.Q   [a].Remove head: Q  [] children of a are c,b d[c]= ∞, d[b]= ∞ so d[c]  d[a]+1=1, d[b]  d[a]+1=1Q  [c b]

[b] children of c are e,f d[e]= ∞, d[f]= ∞ so d[e]  d[c]+1=2, d[f]  d[c]+1=2

Q  [b e f]

8/4/2019 DFS algorithm

Remove head: Q  [e f] children of b is f d[f] <> ∞, nothing done with it Remove head: Q  [f]

children of e is d, i, h d[d]= ∞, d[i]= ∞, d[h]= ∞ so d[d] = d[i] = d[h]  d[e]+1=3 Q  [f d i h]

Remove head: Q  [d i h] children of f is g,h d[g]= ∞, so d[g]  d[f]+1 = 3 Q  [d i h g]

8/4/2019 DFS algorithm

Each of these has children that are

already has a value less than ∞, so these will not set any further values and we are done

with the BFS.

8/4/2019 DFS algorithm

We can create tree out of order we visit the nodes

8/4/2019 DFS algorithm

BFS always computes the

shortest path distance in d[I] between S and vertex I.

8/4/2019 DFS algorithm

Pseudocode: Uses FIFO Queue Q

BFS(s) ; s is our source vertex for each u  V - {s} ; Initialize unvisited vertices to

do d[u]   ∞

d[s]  0 ; distance to source vertex is 0 Q  {s} ; Queue of vertices to visit while Q<>0 do

remove u from Q for each v  Adj[u] do ; Get adjacent vertices if d[v]= ∞ then d[v]  d[u]+1 ; Increment depth

put v onto Q ; Add to nodes to explore

8/4/2019 DFS algorithm

Application

s

8/4/2019 DFS algorithm

Testing bipartiteness

Finding all nodes within one connected

8/4/2019 DFS algorithm

Finding all nodes within one connectedcomponent

The set of nodes reached by a BFS(breadth-first search) form the connected

component containing the starting node.

8/4/2019 DFS algorithm

•Finding the shortest path between two nodes u and v

•Applications to image processing problems

Documents
Documents
Documents
Engineering
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Technology
Documents
Documents
Documents
Documents
Documents