0% found this document useful (0 votes)
30 views64 pages

w10l2 Bfs & Dfs

The document discusses two common graph traversal algorithms: Breadth-First Search (BFS) and Depth-First Search (DFS). BFS is used to find the shortest paths in unweighted graphs by exploring vertices layer by layer, while DFS explores as far as possible along each branch before backtracking. The document also includes code examples and applications for both algorithms.

Uploaded by

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

w10l2 Bfs & Dfs

The document discusses two common graph traversal algorithms: Breadth-First Search (BFS) and Depth-First Search (DFS). BFS is used to find the shortest paths in unweighted graphs by exploring vertices layer by layer, while DFS explores as far as possible along each branch before backtracking. The document also includes code examples and applications for both algorithms.

Uploaded by

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

Breadth First Search

Depth First Search


Week-10, Lesson-02

CSE@DIU 1
Graph Traversal
• Application example
• Given a graph representation and a vertex s in the graph
• Find paths from s to other vertices
• Two common graph traversal algorithms
• Breadth-First Search (BFS)
• Find the shortest paths in an unweighted graph
• Depth-First Search (DFS)
• Topological sort
• Find strongly connected components

2
CSE@DIU
BFS and Shortest Path Problem
• Given any source vertex s, BFS visits the other vertices at increasing distances
away from s. In doing so, BFS discovers paths from s to other vertices
• What do we mean by “distance”? The number of edges on a path from s

Example
0
Consider s=vertex 1
8
2
Nodes at distance 1?
1 2 s 9 1 2, 3, 7, 9
1
1
Nodes at distance 2?
3 7 8, 6, 5, 4
1
6 2
4 Nodes at distance 3?
5
2 2 3
CSE@DIU 0
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

4
CSE@DIU
Breadth-First Search
• “Explore” a graph, turning it into a tree
• One vertex at a time
• Expand frontier of explored vertices across the breadth of the frontier
• Builds a tree over the graph
• Pick a source vertex to be the root
• Find (“discover”) its children, then their children, etc.

5
CSE@DIU
Breadth-First Search
• Every vertex of a graph contains a color at every moment:
• White vertices have not been discovered
• All vertices start with white initially
• Grey vertices are discovered but not fully explored
• They may be adjacent to white vertices
• 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

6
CSE@DIU
Breadth-First Search: The Code
Data: color[V], prev[V],d[V]
While(Q not empty)
{
BFS(G) // starts from here
u = DEQUEUE(Q);
{
for each v ∈ adj[u]{
for each vertex u ∈
V-{s} if (color[v] == WHITE){
color[v] = GREY;
{
d[v] = d[u] + 1;
color[u]=WHITE;
prev[v] = u;
prev[u]=NIL; Enqueue(Q, v);
d[u]=inf; }
} }
color[s]=GRAY; color[u] = BLACK;
d[s]=0; prev[s]=NIL; }
Q=empty; }

ENQUEUE(Q,s);
7
CSE@DIU
Breadth-First Search: Example
z s t u

∞ ∞ ∞ ∞

∞ ∞ ∞ ∞
v w x y

Vertex z s t u v w x y
color W W W W W W W W
d ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
8
CSE@DIU
prev nil nil nil nil nil nil nil nil
Breadth-First Search: Example
z s t u

∞ 0 ∞ ∞

∞ ∞ ∞ ∞
v w x y
Q: s
vertex z s t u v w x y
Color W G W W W W W W
d ∞ 0 ∞ ∞ ∞ ∞ ∞ ∞
9
CSE@DIU
prev nil nil nil nil nil nil nil nil
Breadth-First Search: Example
z s t u

1 0 ∞ ∞

∞ 1 ∞ ∞
v w x y
Q: s w z
vertex z s t u v w x y
Color G B W W W G W W
d 1 0 ∞ ∞ ∞ 1 ∞ ∞
10
CSE@DIU
prev s nil nil nil nil s nil nil
Breadth-First Search: Example
z s t u

1 0 2 ∞

∞ 1 2 ∞
v w x y
Q: s w z t x
vertex z s t u V w X y
Color G B G W W B G W
d 1 0 2 ∞ ∞ 1 2 ∞
11
CSE@DIU
prev s nil w nil nil s w nil
Breadth-First Search: Example
z s t u

1 0 2 ∞

2 1 2 ∞
v w x y
Q: s w z t x v
vertex z s t u v w x y
Color B B G W G B G W
d 1 0 2 ∞ 2 1 2 ∞
12
CSE@DIU
prev s nil w nil r s w nil
Breadth-First Search: Example
z s t u

1 0 2 3

2 1 2 ∞
v w x y
Q: s w z t x v u
vertex z s t u v w x y
Color B B B G G B G W
d 1 0 2 3 2 1 2 ∞
13
CSE@DIU
prev s nil w t r s w nil
Breadth-First Search: Example
z s t u

1 0 2 3

2 1 2 3
v w x y
Q: s w z t x v u y
vertex z s t u v w x y
Color B B B G G B B G
d 1 0 2 3 2 1 2 3
14
CSE@DIU
prev s nil w t r s w x
Breadth-First Search: Example
z s t u

1 0 2 3

2 1 2 3
v w x y
Q: s w z t x v u y
vertex z s t u v w x y
Color B B B G B B B G
d 1 0 2 3 2 1 2 3
15
CSE@DIU
prev s nil w t r s w x
Breadth-First Search: Example
z s t u

1 0 2 3

2 1 2 3
v w x y
Q: s w z t x v u y
vertex z s t u v w x y
Color B B B B B B B G
d 1 0 2 3 2 1 2 3
16
CSE@DIU
prev s nil w t r s w x
Breadth-First Search: Example
z s t u

1 0 2 3

2 1 2 3
v w x y
Q: s w z t x v u y
vertex z s t u v w x y
Color B B B G B B B B
d 1 0 2 3 2 1 2 3
17
CSE@DIU
prev s nil w t r s w x
BFS: The Code (again)
Data: color[V], prev[V],d[V]
While(Q not empty)
{
BFS(G) // starts from here
u = DEQUEUE(Q);
{
for each v ∈ adj[u]{
for each vertex u ∈
V-{s} if (color[v] == WHITE){
color[v] = GREY;
{
d[v] = d[u] + 1;
color[u]=WHITE;
prev[v] = u;
prev[u]=NIL; Enqueue(Q, v);
d[u]=inf; }
} }
color[s]=GRAY; color[u] = BLACK;
d[s]=0; prev[s]=NIL; }
Q=empty; }

ENQUEUE(Q,s);
18
CSE@DIU
Breadth-First Search: Print Path
Data: color[V], prev[V],d[V]

Print-Path(G, s, v)
{
if(v==s)
print(s)
else if(prev[v]==NIL)
print(No path);
else{
Print-Path(G,s,prev[v]);
print(v);
}
}
19
CSE@DIU
Amortized Analysis
• Stack with 3 operations:
• Push, Pop, Multi-pop
• What will be the complexity if “n” operations are performed?

20
CSE@DIU
BFS: Complexity
Data: color[V], prev[V],d[V]
While(Q notu empty)
= every vertex, but only once
{ (Why?)
BFS(G) // starts from here
u = DEQUEUE(Q);
{
for each v ∈ adj[u]{
for each vertex u ∈
V-{s} if(color[v] == WHITE){
color[v] = GREY;
O(V)
{
O(V) d[v] = d[u] + 1;
color[u]=WHITE;
prev[v] = u;
prev[u]=NIL; Enqueue(Q, v);
d[u]=inf; }
} }
color[s]=GRAY; color[u] = BLACK;
d[s]=0; prev[s]=NIL; }
Q=empty; }

ENQUEUE(Q,s);
What will be the running time?
21
CSE@DIU Total running time: O(V+E)
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
• Proof given in the book (p. 472-5)
• 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

22
CSE@DIU
Application of BFS
• Find the shortest path in an undirected/directed unweighted graph.
• Find the bipartiteness of a graph.
• Find cycle in a graph.
• Find the connectedness of a graph.

23
CSE@DIU
Depth-First Search

CSE@DIU 24
Depth-First Search
• Input: 1 2
• G = (V, E) (No source vertex given!) 3
• Goal: 5 4
• Explore the edges of G to “discover” every vertex in V starting at the most
current visited node
• Search may be repeated from multiple sources

• Output:
• 2 timestamps on each vertex:
• d[v] = discovery time
• f[v] = finishing time (done with examining v’s adjacency list)
• Depth-first forest
CSE@DIU 25
Depth-First Search
• Search “deeper” in the graph whenever possible 1 2

• Edges are explored out of the most recently 3


discovered vertex v that still has unexplored edges 5 4

• After all edges of v have been explored, the search


“backtracks” from the parent of v
• The process continues until all vertices reachable from the
original source have been discovered
• If undiscovered vertices remain, choose one of them as a
new source and repeat the search from that vertex
• DFS creates a “depth-first forest”
CSE@DIU 26
DFS Additional Data Structures
• Global variable: time-stamp
• Incremented when nodes are discovered or finished
• color[u] – similar to BFS
• White before discovery, gray while processing and black
when finished processing
• prev[u] – predecessor of u
• d[u], f[u] – discovery and finish times

CSE@DIU 27
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
DFS(G) // where prog starts {
{ color[u] = GREY;
Initialize time = time+1;
for each vertex u ∈ V
d[u] = time;
{ for each v ∈ Adj[u]
color[u] = WHITE; {
prev[u]=NIL; if(color[v] == WHITE){
f[u]=inf; d[u]=inf; prev[v]=u;
DFS_Visit(v);}
}
}
time = 0; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }

}
CSE@DIU 28
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
DFS(G) // where prog starts {
{ color[u] = GREY;
time = time+1;
for each vertex u ∈ V
d[u] = time;
{ for each v ∈ Adj[u]
color[u] = WHITE; {
prev[u]=NIL; if(color[v] == WHITE){
f[u]=inf; d[u]=inf; prev[v]=u;
DFS_Visit(v);}
}
}
time = 0; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }

}
CSE@DIU 29
What does u[d] represent?
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
DFS(G) // where prog starts {
{ color[u] = GREY;
time = time+1;
for each vertex u ∈ V
d[u] = time;
{ for each v ∈ Adj[u]
color[u] = WHITE; {
prev[u]=NIL; if(color[v] == WHITE){
f[u]=inf; d[u]=inf; prev[v]=u;
DFS_Visit(v);}
}
}
time = 0; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }

}
CSE@DIU 30
What does f[d] represent?
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
DFS(G) // where prog starts {
{ color[u] = GREY;
time = time+1;
for each vertex u ∈ V
d[u] = time;
{ for each v ∈ Adj[u]
color[u] = WHITE; {
prev[u]=NIL; if(color[v] == WHITE){
f[u]=inf; d[u]=inf; prev[v]=u;
DFS_Visit(v);
}
}}
time = 0; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }

} Will all vertices eventually be colored black?


CSE@DIU 31
DFS Example
source
vertex
S D F

B C G

CSE@DIU 32
DFS Example
source
vertex d
S D F
f
1 | | |

A
| |
E

| | |
B C G

CSE@DIU 33
DFS Example
source
vertex d
S D F
f
1 | | |

A
2 | |
E

| | |
B C G

CSE@DIU 34
DFS Example
source
vertex d
S D F
f
1 | | |

A
2 | |
E

3 | | |
B C G

CSE@DIU 35
DFS Example
source
vertex d
S D F
f
1 | | |

A
2 | |
E
3 |
| |
4
B C G

CSE@DIU 36
DFS Example
source
vertex d
S D F
f
1 | | |

A
2 | |
E
3 |
5 | |
4
B C G

CSE@DIU 37
DFS Example
source
vertex d
S D F
f
1 | | |

A
2 | |
E
3 | 5 |
|
4 6
B C G

CSE@DIU 38
DFS Example
source
vertex d
S D F
f
1 | | |

A
2 |
|
7
E
3 | 5 |
|
4 6
B C G

CSE@DIU 39
DFS Example
source
vertex d
S D F
f
1 | 8 | |

A
2 |
|
7
E
3 | 5 |
|
4 6
B C G

CSE@DIU 40
DFS Example
source
vertex d
S D F
f
1 | 8 | |

A
2 |
9 |
7
E
3 | 5 |
|
4 6
B C G
What is the structure of the grey vertices?
What do they represent?
CSE@DIU 41
DFS Example
source
vertex d
S D F
f
1 | 8 | |

A
2 | 9
7 |10
E
3 | 5 |
|
4 6
B C G

CSE@DIU 42
DFS Example
source
vertex d
S D F
f
8
1 | |
|11
A
2 | 9
7 |10
E
3 | 5 |
|
4 6
B C G

CSE@DIU 43
DFS Example
source
vertex d
S D F
f
1 8
|
|12 |11
A
2 | 9
7 |10
E
3 | 5 |
|
4 6
B C G

CSE@DIU 44
DFS Example
source
vertex d
S D F
f
1 8
13|
|12 |11
A
2 | 9
7 |10
E
3 | 5 |
|
4 6
B C G

CSE@DIU 45
DFS Example
source
vertex d
S D F
f
1 8
13|
|12 |11
A
2 | 9
7 |10
E
3 | 5 |
14|
4 6
B C G

CSE@DIU 46
DFS Example
source
vertex d
S D F
f
1 8
13|
|12 |11
A
2 | 9
7 |10
E
3 | 5 | 14|
4 6 15
B C G

CSE@DIU 47
DFS Example
source
vertex d
S D F
f
1 8 13|
|12 |11 16
A
2 | 9
7 |10
E
3 | 5 | 14|
4 6 15
B C G

CSE@DIU 48
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
DFS(G) // where prog starts {
{ color[u] = GREY;
time = time+1;
for each vertex u ∈ V
d[u] = time;
{ for each v ∈ Adj[u]
color[u] = WHITE; {
prev[u]=NIL; if (color[v] == WHITE)
f[u]=inf; d[u]=inf; prev[v]=u;
DFS_Visit(v);
}
}
time = 0; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }

} What will be the running time?


CSE@DIU 49
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
DFS(G) // where prog starts {
{ color[u] = GREY;
time = time+1;
for each vertex u ∈ V
d[u] = time;
{ for each v ∈ Adj[u] O(V)
color[u] = WHITE; {
prev[u]=NIL; O(V) if (color[v] == WHITE)
f[u]=inf; d[u]=inf; prev[v]=u;
DFS_Visit(v);
}
}
time = 0; color[u] = BLACK;
for each vertex u ∈ VO(V) time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }

} Running time: O(V2) because call DFS_Visit on each vertex,


CSE@DIU 50
and the loop over Adj[] can run as many as |V| times
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
DFS(G) // where prog starts {
{ color[u] = GREY;
time = time+1;
for each vertex u ∈ V
d[u] = time;
{ for each v ∈ Adj[u]
color[u] = WHITE; {
prev[u]=NIL; if (color[v] == WHITE)
f[u]=inf; d[u]=inf; prev[v]=u;
DFS_Visit(v);
}
}
time = 0; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }

}
BUT, there is actually a tighter bound.
CSE@DIU How many times will DFS_Visit() actually be called? 51
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
DFS(G) // where prog starts {
{ color[u] = GREY;
time = time+1;
for each vertex u ∈ V
d[u] = time;
{ for each v ∈ Adj[u]
color[u] = WHITE; {
prev[u]=NIL; if (color[v] == WHITE)
f[u]=inf; d[u]=inf; prev[v]=u;
DFS_Visit(v);
}
}
time = 0; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }

} So, running time of DFS = O(V+E)


CSE@DIU 52
DFS: Kinds of edges
• DFS introduces an important distinction among edges in the original
graph:
• Tree edge: encounter new (white) vertex
• The tree edges form a spanning forest
• Can tree edges form cycles? Why or why not?
• No

CSE@DIU 53
DFS Example
source
vertex
d f
1 8 13|1
|12 |11 6

2 | 9
7 |10

3 | 5 | 14|1
4 6 5

Tree edges

54
DFS: Kinds of edges

● DFS introduces an important distinction among edges in


the original graph:
■ Tree edge: encounter new (white) vertex
■ Back edge: from descendent to ancestor
○ Encounter a grey vertex (grey to grey)
○ Self loops are considered as to be back edge.

55
DFS Example
source
vertex
d f
1 8 13|1
|12 |11 6

2 | 9
7 |10

3 | 5 | 14|1
4 6 5

Tree edges Back edges

56
DFS: Kinds of edges

● DFS introduces an important distinction among edges in


the original graph:
■ Tree edge: encounter new (white) vertex
■ Back edge: from descendent to ancestor
■ Forward edge: from ancestor to descendent
○ Not a tree edge, though
○ From grey node to black node

57
DFS Example
source
vertex
d f
1 8 13|1
|12 |11 6

2 | 9
7 |10

3 | 5 | 14|1
4 6 5

Tree edges Back edges Forward edges

58
DFS: Kinds of edges

● DFS introduces an important distinction among edges in


the original graph:
■ Tree edge: encounter new (white) vertex
■ Back edge: from descendent to ancestor
■ Forward edge: from ancestor to descendent
■ Cross edge: between a tree or subtrees
○ From a grey node to a black node

59
DFS Example
source
vertex
d f
1 8 13|1
|12 |11 6

2 | 9
7 |10

3 | 5 | 14|1
4 6 5

Tree edges Back edges Forward edges Cross edges

60
DFS: Kinds of edges

● DFS introduces an important distinction among edges in


the original graph:
■ Tree edge: encounter new (white) vertex
■ Back edge: from descendent to ancestor
■ Forward edge: from ancestor to descendent
■ Cross edge: between a tree or subtrees
● Note: tree & back edges are important; most algorithms
don’t distinguish forward & cross

61
More about the edges

● Let (u,v) is an edge.


■ If (color[v] = WHITE) then (u,v) is a tree edge
■ If (color[v] = GRAY) then (u,v) is a back edge
■ If (color[v] = BLACK) then (u,v) is a forward/cross edge
○ Forward Edge: d[u]<d[v]
○ Cross Edge: d[u]>d[v]

62
Textbooks & Web References
• Text Book (Chapter 22)
• www.geeksforgeeks.org

CSE@DIU 63
Thank you &
Any question?

CSE@DIU 64

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy