lecture-34 graphs searching
Post on 06-Apr-2018
224 Views
Preview:
TRANSCRIPT
-
8/3/2019 Lecture-34 Graphs Searching
1/19
Graphs Searching
-
8/3/2019 Lecture-34 Graphs Searching
2/19
Graph Searching Given: a graph G = (V, E), directed or
undirected
Goal: methodically explore every vertex
and every edge
Ultimately: build a tree on the graph
Pick a vertex as the root
Choose certain edges to produce a tree
Note: might also build a forestif graph is not
connected
-
8/3/2019 Lecture-34 Graphs Searching
3/19
BreadthBreadth--First SearchFirst Search
Given a G=(V,E) and distinguishedsource
vertex s,BFS systematically explores the edges of G to
discover every vertex reachable from s.
Creates a BFS tree rooted at s that contains all such
vertices.
Expands the frontier between discovered and
undiscovered vertices uniformly across the breadth of
the frontier.
The algorithm discovers all vertices at distance k from
s before discovering any vertices at distance k+1
-
8/3/2019 Lecture-34 Graphs Searching
4/19
will associate vertex colors to guide the
algorithm
White vertices have not been discovered
All vertices start out white
Grey vertices are discovered but not fully explored
They may be adjacent to white vertices and represent the
frontierbetween the discovered and the undiscovered.
Black vertices are discovered and fully explored
They are adjacent only to black and gray vertices
Explore vertices by scanning adjacency list of
grey vertices
BreadthBreadth--First SearchFirst Search
-
8/3/2019 Lecture-34 Graphs Searching
5/19
BFS(G, s) {initialize vertices;
Q = {s}; // Q is a queue initialize to s
while (Q not empty) {
u = Dequeue(Q);
for each v u->adj {if (v->color == WHITE){
v->color = GREY;
v->d = u->d + 1;
v->p = u;
Enqueue(Q, v);}
}
u->color = BLACK;
}
}
What does v->p represent?
What does v->drepresent?
BreadthBreadth--First SearchFirst Search
-
8/3/2019 Lecture-34 Graphs Searching
6/19
g
g
g
g
g
g
g
g
r s t u
v w x y
BreadthBreadth--First Search: ExampleFirst Search: Example
-
8/3/2019 Lecture-34 Graphs Searching
7/19
Breadth-First Search: Example
g
g
0
g
g
g
g
g
r s t u
v w x y
sQ:
-
8/3/2019 Lecture-34 Graphs Searching
8/19
Breadth-First Search: Example
1
g
0
1
g
g
g
g
r s t u
v w x y
wQ: r
-
8/3/2019 Lecture-34 Graphs Searching
9/19
Breadth-First Search: Example
1
g
0
1
2
2
g
g
r s t u
v w x y
rQ: t x
-
8/3/2019 Lecture-34 Graphs Searching
10/19
Breadth-First Search: Example
1
2
0
1
2
2
g
g
r s t u
v w x y
Q: t x v
-
8/3/2019 Lecture-34 Graphs Searching
11/19
Breadth-First Search: Example
1
2
0
1
2
2
3
g
r s t u
v w x y
Q: x v u
-
8/3/2019 Lecture-34 Graphs Searching
12/19
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: v u y
-
8/3/2019 Lecture-34 Graphs Searching
13/19
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: u y
-
8/3/2019 Lecture-34 Graphs Searching
14/19
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: y
-
8/3/2019 Lecture-34 Graphs Searching
15/19
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q:
-
8/3/2019 Lecture-34 Graphs Searching
16/19
Breadth-First Search: Properties
BFS calculates the shortest-path distance
to the source node
Shortest-path distance H(s,v) = minimum
number of edges from s to v, org if v not
reachable from s
BFS builds breadth-first tree, in whichpaths to root represent shortest paths in G
Thus can use BFS to calculate shortest path
from one vertex to another in O(V+E) time
-
8/3/2019 Lecture-34 Graphs Searching
17/19
Depth First SearchDepth-first search: Strategy
Go as deep as can visiting un-visited nodes
Choose any un-visited vertex when you have a
choiceWhen stuck at a dead-end, backtrack as little as
possible
Back up to where you could go to another
unvisited vertex
Then continue to go on from that point
Eventually youll return to where you started
-
8/3/2019 Lecture-34 Graphs Searching
18/19
DFS AlgorithmDFS(G)for each vertex u V[G] {
color[u]=white
parent[u]=NULL
}
time=0
for each vertex u V[G] {
if color[u]=white then
DFS-VISIT(u)
}
-
8/3/2019 Lecture-34 Graphs Searching
19/19
DFS-VISIT(u)
color[u]=GRAYtime=time+1
d[u]=time
for each vertex v adj[u] {
if color[v]=white Then
parent[v]=u
DFS-VISIT(v)
}
color[u]=black
f[u]=time=time+1
top related