Академический Документы
Профессиональный Документы
Культура Документы
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
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 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 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
5 | 6
14|15
DFS Applications
David Luebke
26
4/13/2012
David Luebke
27
4/13/2012
Topological Sort
There are many problems involving a set of
arranging the courses in the order that they should be taken? The Answer is Topological Sort
David Luebke 28 4/13/2012
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
F H
30
dfs(A)
F H
31
dfs(A) dfs(D)
F H
32
F H
33
F H
34
F H
35
F H 7
36
H 7
37
dfs(A) dfs(D)
H 7
38
dfs(A) dfs(D)
H 7
39
dfs(A)
D 4
H 7
40
dfs(A)
D 4
H 7
41
D 4
H 7
42
dfs(B)
D 4
H 7
43
dfs(B)
D 4
H 7
44
D 4
H 7
45
dfs(C)
D 4
H 7
46
dfs(C)
D 4
H 7
47
dfs(C)
D 4
H 7
48
dfs(C)
D 4
H 7
49
dfs(C) dfs(G)
D 4
H 7
50
dfs(C)
D 4
H 7
51
D 4
H 7
52
D 4
H 7
Topological order: C G B A D E F H
53
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
the solution. And there is no guarantee to find a minimal solution, if more than one solution exists.
David Luebke
56
4/13/2012
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.
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
Application s
Testing bipartiteness
The set of nodes reached by a BFS (breadth-first search) form the connected component containing the starting node.
nodes u and v
Applications to image processing problems