0% found this document useful (0 votes)
8 views63 pages

Module_3_L1_Problem_Solving_Methods_BFS_DFS

The document discusses problem-solving methods and techniques in AI, focusing on search problems and algorithms such as Breadth First Search (BFS) and Depth First Search (DFS). It includes practical applications like route finding, robot motion planning, and puzzle solving, alongside examples like the Cabbage, Wolf, and Goat puzzle. The document also outlines the advantages and disadvantages of BFS and DFS, emphasizing their respective efficiencies and completeness in finding solutions.
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)
8 views63 pages

Module_3_L1_Problem_Solving_Methods_BFS_DFS

The document discusses problem-solving methods and techniques in AI, focusing on search problems and algorithms such as Breadth First Search (BFS) and Depth First Search (DFS). It includes practical applications like route finding, robot motion planning, and puzzle solving, alongside examples like the Cabbage, Wolf, and Goat puzzle. The document also outlines the advantages and disadvantages of BFS and DFS, emphasizing their respective efficiencies and completeness in finding solutions.
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/ 63

Important!

• Quiz 2 – Timing has been revised to 20 mins


• Rest of the rules remain same
Problem Solving Methods and
Techniques in AI
"Strategies and Innovations for Intelligent Decision-Making"
Search Problems
Application : Route Finding

Objective: shortest? Fastest? Most scenic?

Actions: Go straight, turn left, turn right


Application : Robot motion planning

• Objective: fastest?,
safest? Most energy
efficient?

• Actions: Translate and


rotate joints
Application : Solving Puzzles

Objective: Reach a certain configuration


Actions: move pieces (e.g. Move12Down)
"Navigating the River: A Cabbage, Wolf, and Goat Puzzle”
• A farmer wants to get his Cabbage, Wolf
and Goat across a river. He has a boat
that only holds two of them. He cannot
leave the cabbage and goat alone or the
goat and wolf alone. How many river
crossing does he need?
• Remember: For each crossing Farmer
needs to pay ₹1.
• Goal : To get Cabbage, wolf and goat to
the other side of the river, with the
lowest cost possible, keeping in mind all
the constraints. How much money
should the farmer pay to achieve the
goal?
What were you thinking when you were
trying to solve the problem?
Farmer Goat Cabbage Wolf

Actions: F-> F<- All Possible Actions


FC-> FC<-
Approach: Build a search
FG-> FG<- tree (“What if?”)
FW-> FW<-
FCWG ||
FW-> : ₹1
FC-> : ₹1
FG-> : ₹1
CG || FW
WG || FC WC || FG

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,3 5,3 0,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:

Visiting the vertex / Node

Exploring
Vertex / Node
Two more terms to know before we dive in:
BREADTH

DEPTH
BFS

[]

*Remember Queue is FIFO


BFS

[A]

*Remember Queue is FIFO


BFS

[B,C]

A
*Remember Queue is FIFO
BFS

[C,D,E,F]

*Remember Queue is FIFO


AB
BFS

[D,E,F]

*Remember Queue is FIFO


ABC
BFS

[D,E,F,G]

*Remember Queue is FIFO


ABC
BFS

[E,F,G]

*Remember Queue is FIFO


ABCD
BFS

[F,G,H]

*Remember Queue is FIFO


ABCDE
BFS

[G,H,I]

*Remember Queue is FIFO


ABCDEF
BFS

[H, I]

*Remember Queue is FIFO


ABCDEFG
BFS

[I]

*Remember Queue is FIFO


ABCDEFGH
BFS

[]

*Remember Queue is FIFO


ABCDEFGHI
Breadth First Search
• Algorithm for searching a graph

When you think of BFS two things to remember:


• Breadth = Broad / Wide
• Algorithm proceeds horizontally before it proceeds vertically
• Queue [First In First Out (FIFO)]
Series of Questions
• Is it mandatory that we have to start from Node A? NO, If mentioned in question
then YES

• 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

• Let’s Try to begin from Node B

• Let’s Re-draw the entire graph


Perform BFS for the following graph
BFS Simulator

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

If Initial State is given, then start from that node OR


Specifically if Root node is mentioned in question
then start from Root node itself
*Assumed there is no Root node
*There is no Initial State as such
THE MAIN FOCUS WAS NO
MATTER HOW THE GRAPH LOOKS
LIKE YOU CAN PERFORM BFS
Perform BFS for the following graph
Broader View of Breadth First Search
Depth First Search
DFS

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

• Memory-efficient – DFS uses less • Completeness – DFS is not


memory than BFS. Only the path guaranteed to find the solution if it
from the root node to the current exists. If the graph has cycles, it
node is needed. may loop forever.
• Time Efficient – For graphs with • Non-optimal solution – DFS may
many nodes or edges, DFS may be not find the shortest path between
faster than BFS. Its time complexity two nodes in an unweighted graph.
is O(b^m), where b is the branching It may find a longer path before
factor and m is the maximum graph finding the shortest path.
depth.
• Depth-first search – DFS explores
the deepest path first. This
approach makes it useful in certain
scenarios, such as finding the
maximum depth of a tree or graph.
Perform BFS and DFS for the following
DIRECTED graph
Perform BFS and DFS for the following
DIRECTED graph
Perform BFS and DFS for the following
DIRECTED graph

You might also like