0% found this document useful (0 votes)
19 views19 pages

Lec-35Graph - Copy (5)

Uploaded by

akyadav123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views19 pages

Lec-35Graph - Copy (5)

Uploaded by

akyadav123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

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

1
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
2
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)
}

3
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
4
Depth-First Search: Example

u v w

x y z

5
Depth-First Search: Example

u v w

1/

x y z

6
Depth-First Search: Example

u v w

1/ 2/

x y z

7
Depth-First Search: Example

u v w

1/ 2/

3/

x y z

8
Depth-First Search: Example

u v w

1/ 2/

4/ 3/

x y z

9
Depth-First Search: Example

u v w

1/ 2/

4/ 3/

x y z

10
Depth-First Search: Example

u v w

1/ 2/

4/5 3/

x y z

11
Depth-First Search: Example

u v w

1/ 2/

4/5 3/6

x y z

12
Depth-First Search: Example

u v w

1/ 2/7

4/5 3/6

x y z

13
Depth-First Search: Example

u v w

1/ 2/7

F B

4/5 3/6

x y z

14
Depth-First Search: Example

u v w

1/8 2/7

F B

4/5 3/6

x y z

15
Depth-First Search: Example

u v w

1/8 2/7 9/

B C
F

4/5 3/6

x y z

16
Depth-First Search: Example

u v w

1/8 2/7 9/

B C
F

4/5 3/6 10/


B
x y z

17
Depth-First Search: Example

u v w

1/8 2/7 9/

B C
F

4/5 3/6 10/11


B

x y z

18
Depth-First Search: Example

u v w

1/8 2/7 9/12

B C
F

4/5 3/6 10/11


B
x y z

19

You might also like