Download presentation

Presentation is loading. Please wait.

Published byShana Phillips Modified over 2 years ago

1
Graphs Searching

2
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 forest if graph is not connected

3
Breadth-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

4
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 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 Search

5
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 Search

6
rstu vwxy Breadth-First Search: Example

7
0 rstu vwxy s Q:

8
Breadth-First Search: Example 1 0 1 rstu vwxy w Q: r

9
Breadth-First Search: Example 1 0 1 2 2 rstu vwxy r Q: tx

10
Breadth-First Search: Example 1 2 0 1 2 2 rstu vwxy Q: txv

11
Breadth-First Search: Example 1 2 0 1 2 2 3 rstu vwxy Q: xvu

12
Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: vuy

13
Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: uy

14
Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: y

15
Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: Ø

16
Breadth-First Search: Properties BFS calculates the shortest-path distance to the source node Shortest-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 G Thus can use BFS to calculate shortest path from one vertex to another in O(V+E) time

17
Depth First Search Depth-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

18
DFS Algorithm DFS(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) }

19
DFS-VISIT(u) color[u]=GRAY time=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

Similar presentations

OK

Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.

Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on suspension type insulators and conductors Addition for kids ppt on batteries File type ppt on cybercrime virus Ppt on political parties and electoral process in the united Animated ppt on magnetism for kids Ppt on management by objectives examples Free ppt on brain machine interface ethics Ppt on physical methods of gene transfer in plants Ppt on natural disaster flood Ppt on eid festival food