Вы находитесь на странице: 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 vs edges have been explored, backtrack to the vertex from which v was discovered

Depth-First Search
Vertices initially colored white

Then colored gray when discovered


Then black when finished

DFS Example
source vertex

DFS Example
source vertex

d
1 |

f
| |

DFS Example
source vertex

d
1 |

f
| |

2 |

DFS Example
source vertex

d
1 |

f
| |

2 |

3 |

DFS Example
source vertex

d
1 |

f
| |

2 |

3 | 4

DFS Example
source vertex

d
1 |

f
| |

2 |

3 | 4

5 |

DFS Example
source vertex

d
1 |

f
| |

2 |

3 | 4

5 | 6

DFS Example
source vertex

d
1 |

f
8 | |

2 | 7

3 | 4

5 | 6

DFS Example
source vertex

d
1 |

f
8 | |

2 | 7

9 |

3 | 4

5 | 6

DFS Example
source vertex

d
1 |

f
8 | |

2 | 7

9 |10

3 | 4

5 | 6

DFS Example
source vertex

d
1 |

f
8 |11 |

2 | 7

9 |10

3 | 4

5 | 6

DFS Example
source vertex

f
8 |11 |

1 |12

2 | 7

9 |10

3 | 4

5 | 6

DFS Example
source vertex

f
8 |11 13|

1 |12

2 | 7

9 |10

3 | 4

5 | 6

DFS Example
source vertex

f
8 |11 13|

1 |12

2 | 7

9 |10

3 | 4

5 | 6

14|

DFS Example
source vertex

f
8 |11 13|

1 |12

2 | 7

9 |10

3 | 4

5 | 6

14|15

DFS Example
source vertex

f
8 |11 13|16

1 |12

2 | 7

9 |10

3 | 4

5 | 6

14|15

DFS Example
source vertex

f
8 |11 13|16

1 |12

2 | 7

9 |10

3 | 4
Tree edges

5 | 6

14|15

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)

DFS Example
source vertex

f
8 |11 13|16

1 |12

2 | 7

9 |10

3 | 4
Tree edges Back edges

5 | 6

14|15

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

DFS Example
source vertex

f
8 |11 13|16

1 |12

2 | 7

9 |10

3 | 4

5 | 6

14|15

Tree edges Back edges Forward edges

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

DFS Example
source vertex

f
8 |11 13|16

1 |12

2 | 7

9 |10

3 | 4

5 | 6

14|15

Tree edges Back edges Forward edges Cross edges

DFS Applications

David Luebke

26

4/13/2012

Topological Sorting Strong connectedness

David Luebke

27

4/13/2012

Topological Sort
There are many problems involving a set of

tasks in which some of the tasks must be done before others.


For example, consider the problem of taking a

course only after taking its prerequisites.


Is there any systematic way of linearly

arranging the courses in the order that they should be taken? The Answer is Topological Sort
David Luebke 28 4/13/2012

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

Topological Sort: DFS


C

F H

30

Topological Sort: DFS


C

dfs(A)

F H

31

Topological Sort: DFS


C

dfs(A) dfs(D)

F H

32

Topological Sort: DFS


C

dfs(A) dfs(D) dfs(E)


G A B

F H

33

Topological Sort: DFS


C

dfs(A) dfs(D) dfs(E) dfs(F)


G A B

F H

34

Topological Sort: DFS


C

dfs(A) dfs(D) dfs(E) dfs(F) dfs(H)

F H

35

Topological Sort: DFS


C

dfs(A) dfs(D) dfs(E) dfs(F)


G A B

F H 7

36

Topological Sort: DFS


C

dfs(A) dfs(D) dfs(E)


G A B

H 7

37

Topological Sort: DFS


C

dfs(A) dfs(D)

H 7

38

Topological Sort: DFS


C

dfs(A) dfs(D)

H 7

39

Topological Sort: DFS


C

dfs(A)

D 4

H 7

40

Topological Sort: DFS


C

dfs(A)

D 4

H 7

41

Topological Sort: DFS


C

D 4

H 7

42

Topological Sort: DFS


C

dfs(B)

D 4

H 7

43

Topological Sort: DFS


C

dfs(B)

D 4

H 7

44

Topological Sort: DFS


C

D 4

H 7

45

Topological Sort: DFS


C

dfs(C)

D 4

H 7

46

Topological Sort: DFS


C

dfs(C)

D 4

H 7

47

Topological Sort: DFS


C

dfs(C)

D 4

H 7

48

Topological Sort: DFS


C

dfs(C)

D 4

H 7

49

Topological Sort: DFS


C

dfs(C) dfs(G)

D 4

H 7

50

Topological Sort: DFS


C

dfs(C)

D 4

H 7

51

Topological Sort: DFS


0 C

D 4

H 7

52

Topological Sort: DFS


0 C

D 4

H 7

Topological order: C G B A D E F H
53

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 a directed path from v to u. Strongly-Connected-Components(G)

David Luebke

54

4/13/2012

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 this problem is to impose a cutoff depth on the search.

David Luebke

55

4/13/2012

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


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

Example

David Luebke

58

4/13/2012

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=1 Q [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]

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]

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.

We can create tree out of order we visit the nodes

BFS always computes the shortest path distance in d[I] between S and vertex I.

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

Application s

Testing bipartiteness

Finding all nodes within one connected component

The set of nodes reached by a BFS (breadth-first search) form the connected component containing the starting node.

Finding the shortest path between two

nodes u and v
Applications to image processing problems

Вам также может понравиться