Module_3_L1_Problem_Solving_Methods_BFS_DFS
Module_3_L1_Problem_Solving_Methods_BFS_DFS
• Objective: fastest?,
safest? Most energy
efficient?
F <- : ₹1
Search Problem : Definitions
• S (start) : Starting State
• Actions (A) : Possible Actions
FCW || G
• Cost (S, A) : action cost
FC-> : ₹1 FW-> : ₹1
• Succ (S,A) : Successor
• IsEnd (S) : reached end state?
W || FCG C || FWG
FG <- : ₹1 F <- : ₹1 FG <- : ₹1
F <- : ₹1
FW || CG FGW || C FC || WG FCG || W
FW -> : ₹1 FC -> : ₹1
G ||FWC G ||FWC
F <- : ₹1 F <- : ₹1
FG -> : ₹1 FG -> : ₹1
||FGWC FG ||WC ||FGWC
FG ||WC
Transportation Problem
• Street with blocks numbered from 1 to n
• Walking from s to s+1 takes 1 minute
• Taking a magic tram from s to 2s takes 2 minutes
• How to travel from 1 to n in least time?
1 2 3 4 ..... N
Water Jug Problem
• https://www.transum.org/Software/Investigations/jugs.asp
Water Jug Problem
5,0
2,0 5,3
0,2
5,2
4,3
Search Algorithms
Breadth First Search [BFS] Algorithm
Depth First Search [DFS] Algorithm
Two terms to know before we dive in:
Exploring
Vertex / Node
Two more terms to know before we dive in:
BREADTH
DEPTH
BFS
[]
[A]
[B,C]
A
*Remember Queue is FIFO
BFS
[C,D,E,F]
[D,E,F]
[D,E,F,G]
[E,F,G]
[F,G,H]
[G,H,I]
[H, I]
[I]
[]
• While Exploring a vertex can I chose any node of my choice to visit? YES
• Is it mandatory that we have to start from Node A? NO, If mentioned in
question then YES
https://visualgo.net/en/dfsbfs?slide=1
Perform BFS for the following graph 6
1 5
4 2 7
3 8
10 9
FCWG ||
Start State / FW-> : ₹1
FC-> : ₹1
Initial State FG-> : ₹1
CG || FW
WG || FC WC || FG
F <- : ₹1
Yesterday’s Statement:
“You can start from any Node”
FCW || G
FC-> : ₹1 FW-> : ₹1
W || FCG C || FWG
FG <- : ₹1 F <- : ₹1 FG <- : ₹1
F <- : ₹1
FW || CG FGW || C FC || WG FCG || W
FW -> : ₹1 FC -> : ₹1
G ||FWC G ||FWC
F <- : ₹1 F <- : ₹1
FG -> : ₹1 FG -> : ₹1
||FGWC FG ||WC ||FGWC
FG ||WC
Which node will you start FCW || G
from?
WC || FG
FCWG ||
CG || FW FCW || G ||FGWC
FCW || G
WG || FC
A
*Stack, Stack is LIFO [Last In First Out]
DFS
A
*Stack, Stack is LIFO [Last In First Out]
DFS
C
B
*Stack, Stack is LIFO [Last In First Out]
DFS
F
E
D
C
*Stack, Stack is LIFO [Last In First Out]
DFS
FH
E
D
C
*Stack, Stack is LIFO [Last In First Out]
DFS
H
E
D
C
*Stack, Stack is LIFO [Last In First Out]
DFS
E
D
C
*Stack, Stack is LIFO [Last In First Out]
DFS
D
C
*Stack, Stack is LIFO [Last In First Out]
DFS
G
C
*Stack, Stack is LIFO [Last In First Out]
DFS
G
*Stack, Stack is LIFO [Last In First Out]
DFS
I
*Stack, Stack is LIFO [Last In First Out]
Perform DFS for the following graph
Perform DFS for the following graph
Perform DFS for the following graph
Broader View of Depth
First Search (DFS)
Algorithm
Advantages of BFS Disadvantages of BFS
• Optimal Solution – BFS is guaranteed to • Space complexity – BFS can be memory-
find the shortest path between two intensive, especially for large graphs or
nodes in an unweighted graph. trees. It needs to store all the visited
• Completeness – BFS is complete, nodes in a queue data structure, which
meaning it will find the solution if it can take up a lot of memory.
exists. • Time complexity – BFS can be slow for
• Uniform cost search – BFS can be graphs with a large number of nodes or
modified to search for the shortest path edges.
in a weighted graph by replacing the
queue data structure with a priority
queue.
• Level-wise traversal – BFS visits all the
nodes at a given level before moving to
the next level. This approach makes it
useful in certain scenarios such as
finding the shortest path or exploring a
game state.
Advantages of DFS Disadvantages of DFS