graphs searching. graph searching given: a graph g = (v, e), directed or undirected goal:...
TRANSCRIPT
![Page 1: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/1.jpg)
Graphs Searching
![Page 2: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/2.jpg)
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 rootChoose certain edges to produce a treeNote: might also build a forest if graph is not
connected
![Page 3: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/3.jpg)
Breadth-First SearchBreadth-First Search Given a G=(V,E) and distinguished source 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
![Page 4: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/4.jpg)
will associate vertex “colors” to guide the algorithmWhite 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 frontier between 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
Breadth-First SearchBreadth-First Search
![Page 5: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/5.jpg)
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->d represent?
Breadth-First SearchBreadth-First Search
![Page 6: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/6.jpg)
r s t u
v w x y
Breadth-First Search: ExampleBreadth-First Search: Example
![Page 7: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/7.jpg)
Breadth-First Search: Example
0
r s t u
v w x y
sQ:
![Page 8: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/8.jpg)
Breadth-First Search: Example
1
0
1
r s t u
v w x y
wQ: r
![Page 9: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/9.jpg)
Breadth-First Search: Example
1
0
1
2
2
r s t u
v w x y
rQ: t x
![Page 10: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/10.jpg)
Breadth-First Search: Example
1
2
0
1
2
2
r s t u
v w x y
Q: t x v
![Page 11: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/11.jpg)
Breadth-First Search: Example
1
2
0
1
2
2
3
r s t u
v w x y
Q: x v u
![Page 12: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/12.jpg)
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: v u y
![Page 13: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/13.jpg)
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: u y
![Page 14: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/14.jpg)
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: y
![Page 15: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/15.jpg)
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: Ø
![Page 16: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/16.jpg)
Breadth-First Search: Properties
BFS calculates the shortest-path distance to the source nodeShortest-path distance (s,v) = minimum number
of edges from s to v, or if v not reachable from s BFS builds breadth-first tree, in which paths
to root represent shortest paths in GThus can use BFS to calculate shortest path from
one vertex to another in O(V+E) time
![Page 17: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/17.jpg)
Depth First SearchDepth-first search: Strategy
Go as deep as can visiting un-visited nodes
Choose any un-visited vertex when you have a choice
When 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 you’ll return to where you started
![Page 18: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/18.jpg)
DFS AlgorithmDFS(G)
for each vertex u V[G] {color[u]=whiteparent[u]=NULL
}time=0 for each vertex u V[G] {
if color[u]=white thenDFS-VISIT(u)
}
![Page 19: Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:](https://reader035.vdocuments.us/reader035/viewer/2022081821/56649ccf5503460f9499b515/html5/thumbnails/19.jpg)
DFS-VISIT(u)color[u]=GRAYtime=time+1d[u]=time for each vertex v adj[u] {
if color[v]=white Thenparent[v]=uDFS-VISIT(v)
}color[u]=blackf[u]=time=time+1