BFS and DFS 29
BFS and DFS 29
Edge
•An edge is a connecting link between two
vertices. Edge is also known as Arc. An edge is
represented as (startingVertex, endingVertex).
Terminologies…
Undirected Graph
•A graph with only undirected edges is said to be
undirected graph.
Directed Graph
•A graph with only directed edges is said to be directed
graph.
Adjacent
•If there is an edge between vertices A and B then both A
and B are said to be adjacent. In other words, Two vertices
A and B are said to be adjacent if there is an edge whose
end vertices are A and B.
Terminologies…
Degree
•Total number of edges connected to a vertex is
said to be degree of that vertex.
Indegree
•Total number of incoming edges connected to a
vertex is said to be indegree of that vertex.
Outdegree
•Total number of outgoing edges connected to a
vertex is said to be outdegree of that vertex.
Terminologies…
Self-loop
•An edge (undirected or directed) is a self-loop if its two
endpoints coincide.
Simple Graph
•A graph is said to be simple if there are no parallel and
self-loop edges.
Path
•A path is a sequence of alternating vertices and edges that
starts at a vertex and ends at a vertex such that each edge is
incident to its predecessor and successor vertex.
Graph Representations
• Graph data structure is represented using
following representations...
– Adjacency Matrix
– Adjacency List
Adjacency Matrix
• graph can be represented using a matrix of
size total number of vertices by total
number of vertices.
• This matrix is filled with either 1 or 0. Here,
1 represents there is an edge from row
vertex to column vertex and 0 represents
there is no edge from row vertex to column
vertex.
Adjacency List
• In this representation, every vertex of graph
contains list of its adjacent vertices.
Exercise
• Represent the graph using:
• 1. Adjacency Matrix A->B->C
B->D
• 2. Adjacency List
C
•A
D->C
•A
•B •C
•B •C
•D
•D
Exercise – Adjacency Matrix &
List
What is a Graph?
• Visiting a node
• Exploring a node
Nodes visited: D
Overview
• Breadth-first search starts
F C with given node
A
• Then visits nodes adjacent
B D in some specified order
H (e.g., alphabetical)
0
G E • Like ripples in a pond
1
Nodes visited: D, C
Overview
• Breadth-first search starts
F C with given node
A
• Then visits nodes adjacent
B D in some specified order
H (e.g., alphabetical)
0
G E • Like ripples in a pond
1
Nodes visited: D, C, E
Overview
• Breadth-first search starts
F C with given node
A
• Then visits nodes adjacent
B D in some specified order
H (e.g., alphabetical)
0
G E • Like ripples in a pond
1
Nodes visited: D, C, E, F
Overview
• When all nodes in ripple
F C are visited, visit nodes in
A next ripples
B D
H
0
G E
2 1
Nodes visited: D, C, E, F, G
Overview
• When all nodes in ripple
F C are visited, visit nodes in
A next ripples
B D
H
0
3 G E
2 1
Nodes visited: D, C, E, F, G, H
Overview
4 • When all nodes in ripple
F C are visited, visit nodes in
A next ripples
B D
H
0
3
G E
2 1
Nodes visited: D, C, E, F, G, H, A
Overview
4 • When all nodes in ripple
F C are visited, visit nodes in
A next ripples
B D
H
0
3
G E
2 1
Nodes visited: D, C, E, F, G, H, A, B
Walk-Through
Enqueued Array
F C A
A B Q
B C
D
H D
E
G E F
G
H
• Visiting a node
• Exploring a node
Depth First Search
DFS stands for Depth First Search is a edge based technique.
It uses the Stack Data Structure, performs two stages, first
visited vertices are pushed into stack and second if there is no
vertices then visited vertices are popped.
A,B,D,C,E,F
DFS
Walk-Through
Visited Array
F C A
A B
B C
D
H D
E
G E F
G
H
G √ E
H D
The order nodes are visited:
Visit G
D, C, E, G
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E F
G
G √ E
H D
The order nodes are visited:
Nodes D and H are adjacent to
D, C, E, G G. D has already been
visited. Decide to visit H.
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
H
E √
G E F
G
G √ E
H √ D
The order nodes are visited:
Visit H
D, C, E, G, H
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
H
E √
G E F
G
G √ E
H √ D
The order nodes are visited:
Nodes A and B are adjacent to H.
D, C, E, G, H Decide to visit A next.
Walk-Through
Visited Array
F C A √
A B
B C √
D A
H D √
H
E √
G E F
G
G √ E
H √ D
The order nodes are visited:
Visit A
D, C, E, G, H, A
Walk-Through
Visited Array
F C A √
A B
B C √
D A
H D √
H
E √
G E F
G
G √ E
H √ D
The order nodes are visited:
Only Node B is adjacent to A.
D, C, E, G, H, A Decide to visit B next.
Walk-Through
Visited Array
F C A √
A B √ B
B C √
D A
H D √
H
E √
G E F
G
G √ E
H √ D
The order nodes are visited:
Visit B
D, C, E, G, H, A, B
Walk-Through
Visited Array
F C A √
A B √
B C √
D A
H D √
H
E √
G E F
G
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B B. Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
H
E √
G E F
G
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B A. Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B H. Backtrack (pop the
stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B G. Backtrack (pop the
stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B E. Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √
H √ D
The order nodes are visited:
F is unvisited and is adjacent to
D, C, E, G, H, A, B D. Decide to visit F next.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √ F
H √ D
The order nodes are visited:
Visit F
D, C, E, G, H, A, B, F
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B, F F. Backtrack.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B, F D. Backtrack.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √
The order nodes are visited:
Stack is empty. Depth-first
D, C, E, G, H, A, B, F traversal is done.
DFS (Depth First Search)
• DFS traversal of a graph, produces a spanning tree as final result. Spanning Tree
is a graph without any loops. We use Stack data structure with maximum size of
total number of vertices in the graph to implement DFS traversal of a graph.
Step 1: Define a Stack of size total number of vertices in the graph.
Step 2: Select any vertex as starting point for traversal. Visit that vertex & push it
on to the Stack.
Step 3: Visit any one of the adjacent vertex of the verex which is at top of the stack
which is not visited and push it on to the stack.
Step 4: Repeat step 3 until there are no new vertex to be visit from the vertex on
top of the stack.
Step 5: When there is no new vertex to be visit then use back tracking and pop one
vertex from the stack.
Step 6: Repeat steps 3, 4 and 5 until stack becomes Empty.
Step 7: When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph
Back tracking is coming back to the vertex from which we came to current vertex.
ALGORITHM
DFS(G)
//Implements a depth-first search traversal of a given graph
//Input: Graph G=(V,E)
//Output: Graph G with its vertices marked with consecutive
integers
// in the order they are first encountered by the DFS traversal
mark each vertex in V with 0 as a mark of being “unvisited”
count ←0
for each vertex v in V do
if v is marked with 0
dfs(v)
dfs(v)
//visits recursively all the unvisited vertices connected to
vertex v by a path and numbers them in the order they are
encountered via global variable count
count ←count +1; mark v with count
for each vertex w in V adjacent to v do
if w is marked with 0
dfs(w)
Find the BFS and DFS and construct
BFS AND DFS SPANNING TREE
Cross Edge
S.NO BFS DFS
BFS(Breadth First Search) DFS(Depth First Search) uses
uses Queue data structure Stack data structure.
1 for finding the shortest
path.