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)

Citation preview

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 1/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

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 2/68

Depth-First Search

● Vertices initially colored white

● Then colored gray when discovered

●Then black when finished

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 3/68

DFS Example

 sourcevertex

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 4/68

DFS Example

1 | | |

|||

| |

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 5/68

DFS Example

1 | | |

|||

2 | |

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 6/68

DFS Example

1 | | |

||3 |

2 | |

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 7/68

DFS Example

1 | | |

||3 | 4

2 | |

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 8/68

DFS Example

1 | | |

|5 |3 | 4

2 | |

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 9/68

DFS Example

1 | | |

|5 | 63 | 4

2 | |

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 10/68

DFS Example

1 | 8 | |

|5 | 63 | 4

2 | 7 |

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 11/68

DFS Example

1 | 8 | |

|5 | 63 | 4

2 | 7 9 |

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 12/68

DFS Example

1 | 8 | |

|5 | 63 | 4

2 | 7 9 |10

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 13/68

DFS Example

1 | 8 |11 |

|5 | 63 | 4

2 | 7 9 |10

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 14/68

DFS Example

1 |12 8 |11 |

|5 | 63 | 4

2 | 7 9 |10

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 15/68

DFS Example

1 |12 8 |11 13|

|5 | 63 | 4

2 | 7 9 |10

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 16/68

DFS Example

1 |12 8 |11 13|

14|5 | 63 | 4

2 | 7 9 |10

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 17/68

DFS Example

1 |12 8 |11 13|

14|155 | 63 | 4

2 | 7 9 |10

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 18/68

DFS Example

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

 sourcevertex

 d f 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 19/68

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

http://slidepdf.com/reader/full/dfs-algorithm 20/68

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

http://slidepdf.com/reader/full/dfs-algorithm 21/68

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

http://slidepdf.com/reader/full/dfs-algorithm 22/68

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

http://slidepdf.com/reader/full/dfs-algorithm 23/68

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

http://slidepdf.com/reader/full/dfs-algorithm 24/68

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

http://slidepdf.com/reader/full/dfs-algorithm 25/68

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

http://slidepdf.com/reader/full/dfs-algorithm 26/68

DFS Applications

David Luebke 26 4/13/2012

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 27/68

 

● Topological Sorting

● Strong connectedness

David Luebke 27 4/13/2012

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 28/68

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?

The Answer is Topological Sort

David Luebke 28 4/13/2012

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 29/68

● 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

http://slidepdf.com/reader/full/dfs-algorithm 30/68

30

Topological Sort: DFS

G A B 

D E 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 31/68

31

Topological Sort: DFS

G   A  B 

D E 

dfs(A)

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 32/68

32

Topological Sort: DFS

dfs(A)

dfs(D)

G   A  B 

D  E 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 33/68

33

Topological Sort: DFS

dfs(A)

dfs(D)

dfs(E)

G   A  B 

D E 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 34/68

34

Topological Sort: DFS

dfs(A)

dfs(D)

dfs(E)

dfs(F)

G   A  B 

D E 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 35/68

35

Topological Sort: DFS

dfs(A)

dfs(D)

dfs(E)

dfs(F)

dfs(H)

G   A  B 

D E 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 36/68

36

Topological Sort: DFS

dfs(A)

dfs(D)

dfs(E)

dfs(F)

G   A  B 

D E 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 37/68

37

Topological Sort: DFS

dfs(A)

dfs(D)

dfs(E)

 A  B 

D E 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 38/68

38

Topological Sort: DFS

dfs(A)

dfs(D)

G   A  B 

D  E 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 39/68

39

Topological Sort: DFS

dfs(A)

dfs(D)

G   A  B 

D  E 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 40/68

40

Topological Sort: DFS

dfs(A)

G   A  B 

D E 

5 4

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 41/68

41

Topological Sort: DFS

dfs(A)

G   A  B 

D E 

4 5 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 42/68

42

Topological Sort: DFS

G   A  B 

D E 

4 5 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 43/68

43

Topological Sort: DFS

G   A  B 

D E 

4 5 

dfs(B)

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 44/68

44

Topological Sort: DFS

G   A  B 

D E 

4 5 

dfs(B)

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 45/68

45

Topological Sort: DFS

G   A B 

D E 

4 5 

3  2 

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 46/68

46

Topological Sort: DFS

G   A B 

D E 

4 5 

dfs(C)

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 47/68

47

Topological Sort: DFS

G   A B 

D E 

4 5 

dfs(C)

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 48/68

48

Topological Sort: DFS

G   A B 

D E 

4 5 

dfs(C)

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 49/68

49

Topological Sort: DFS

G   A B 

D E 

4 5 

dfs(C)

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 50/68

50

Topological Sort: DFS

G   A B 

D E 

4 5 

dfs(C)

dfs(G)

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 51/68

51

Topological Sort: DFS

G A B 

D E 

4 5 

3  2 

dfs(C)

1

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 52/68

52

Topological Sort: DFS

G A B 

D E 

4 5 

3  2 1

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 53/68

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

http://slidepdf.com/reader/full/dfs-algorithm 54/68

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

http://slidepdf.com/reader/full/dfs-algorithm 55/68

Disadvantages of DFS

● 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

http://slidepdf.com/reader/full/dfs-algorithm 56/68

● 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

http://slidepdf.com/reader/full/dfs-algorithm 57/68

BFS in Directed graph 

● Pick a source vertex S to start.

● Find (or discover) the vertices that are

adjacent to S.

● Pick each child of S in turn and discover their

vertices adjacent to that child.

● 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

http://slidepdf.com/reader/full/dfs-algorithm 58/68

David Luebke 58 4/13/2012

Example

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 59/68

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] 

Remove head: Q  

[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

http://slidepdf.com/reader/full/dfs-algorithm 60/68

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

http://slidepdf.com/reader/full/dfs-algorithm 61/68

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

http://slidepdf.com/reader/full/dfs-algorithm 62/68

We can create tree out of order we visit the nodes

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 63/68

BFS always computes the 

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

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 64/68

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 

Breadth – first search

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 65/68

Breadth first search

Application

s

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 66/68

  Testing bipartiteness 

Finding all nodes within one connected

8/4/2019 DFS algorithm

http://slidepdf.com/reader/full/dfs-algorithm 67/68

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

http://slidepdf.com/reader/full/dfs-algorithm 68/68

•Finding the shortest path between two nodes u and v 

•Applications to image processing problems