Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

INTRODUCTION TO

ARTIFICIAL
INTELLIGENCE
Unit 4
05/06/2024 Spring' 2024 2

Acknowledgment
• Some of the slides of this lecture have been taken from:
• The lecture slides of CS188 – “Introduction to Artificial Intelligence”
UC Berkeley
05/06/2024 Spring' 2024 3

Outline
• Uninformed Search Strategies
• Breadth First Search
• Depth First Search
• Depth Limited Search
• Iterative Deepening Search
05/06/2024 Spring' 2024 4

BFS – Explained
• In BFS, graph is searched from the root node in order of
the distance from the root

• BFS checks each node nearest the root before


descending to the next level

• All the nodes are expanded at a given depth in the search


tree before any nodes at the next level are expanded
05/06/2024 Spring' 2024 5

BFS - Explained
• BFS implementation uses a FIFO (first-in first-out) queue

• New nodes go back of the queue, old ones expanded first

• Goal test is applied to each node when it is generated


rather than when it is selected for expansion

• The algorithm discards any new path to a state already in


the frontier or explored set
• It is easy to see that any such path must be at least as deep as the
one already found. Thus, breadth-first search always has the
shallowest path to every node on the frontier.
05/06/2024 Spring' 2024 6

Breadth-First Search
Strategy: expand a G
a shallowest b c
node first e
d f
Implementation: S h
Fringe is a FIFO p r
queue q

d e p
Search
b c e h r q
Tiers
a a h r p q f

p q f q c G

q c G a

a
05/06/2024 Spring' 2024 7

BFS - Comments
d is the
• Complete?
depth
• Yes
• Optimal?
• Yes, in a non-weighted graph
• Time Complexity
• O(bd)
• Space Complexity
• O(bd), d is depth of the solution, not the depth of the tree
05/06/2024 Spring' 2024 8

BFS – Comments
• Useful when
• Space is not a problem
• You want to find the solution containing the fewest arcs (Does not
consider cost of the solution)
• Few solutions may exist, and at least one has a short path length
• Infinite paths may exist – explores all of the search space, even
with infinite paths
• Poor method when
• All solutions have a long path length
• There is some heuristic knowledge available
05/06/2024 Spring' 2024 9

DFS – Explained
• Searching begins at the root node

• Exhaustively searches each branch to its greatest depth


before backtracking to previously unexplored branches

• The implementation of DFS uses a LIFO (last-in first-out)


queue / Stack
05/06/2024 Spring' 2024 10

Depth-First Search
Strategy: expand a a G
deepest node first b c

Implementation: Fringe e
d f
is a LIFO stack S h
p q r

d e p

b c e h r q

a a h r p q f

p q f q c G

q c G a

a
05/06/2024 Spring' 2024 11

DFS – Comments
• Complete?
• No, in case of loops the algorithm cycles indefinitely
• Optimal?
• No
• Space Complexity
• O(bd)
• Time Complexity
• O(bd)
d is the
depth
05/06/2024 Spring' 2024 12

DFS – Comments
• Appropriate when
• Space is restricted
• Many solutions exist, perhaps with long path lengths, particularly
for the case where nearly all paths lead to a solution
• The order of the neighbors of a node are added to the stack can be
tuned so that solutions are found on the first try (A smart or Lucky
Tie breaker)
• Not suitable when
• It is possible to get caught in infinite paths – when the graph is
infinite or when there are cycles in the graph
• Solutions exist at shallow depth – the search may look at many
long paths before finding the short solutions
05/06/2024 Spring' 2024 13

BFS and DFS Algorithms


BFS DFS
• Let L be a list containing the • Let L be a list containing the
initial state initial state
• Loop • Loop
• If L is empty return failure – If L is empty return failure
• Node  remove-first(L) – Node  remove-first(L)
• If Node is a goal – If Node is a goal
• Return the path from initial state of • Return the path from initial state
Node of Node
• Else – Else
• Generate all successors of Node • Generate all successors of Node
• Add generated nodes to the back • Add generated nodes to the front
of L of L
• End Loop • End Loop
05/06/2024 Spring' 2024 14

Activity
• Start Node – A
• Goal – G
• Path taken for DFS,
BFS and nodes
explored
• Status of queue/stack
on every move
05/06/2024 Spring' 2024 15

Depth First vs. Breadth First


05/06/2024 Spring' 2024 16

Variants of DFS
• Depth – Limited Search (DLS)
• Iterative Deepening Search (IDS)
05/06/2024 Spring' 2024 17

Depth – Limited Search


• Modification of DFS

• Depth is provided below which the algorithm would not go

• Nodes below the depth are omitted from the search

• Keeps algorithm from indefinite cycling

• Reduces search scope


05/06/2024 Spring' 2024 18

Depth – Limited Search


• Complete?
• No, if goal node is below the depth
• Yes, depth of search is same as the depth of the tree (which is
finite)
• Optimal?
• No, first path to the goal is found instead of the shortest
• Space Complexity
• Same as DFS, O(bd)
• Time Complexity
• Same as DFS, O(bd)
• d is the imposed depth
05/06/2024 Spring' 2024 19

Iterative Deepening Search (IDS)


• Derivate of DLS that combines features of depth-first
search with breadth-first search

• Performs DLS search with increased depth till the goal is


found

• Depth begins at one, and keep increasing till either goal is


found or no further nodes can be expanded

• Not susceptible to cycles (feature from DLS)

• Finds goal nearest to the root node (feature of BFS)


05/06/2024 Spring' 2024 20

Iterative Deepening Search


• Complete?
• Yes
• Optimal?
• Yes
• Space Complexity
• O(bd)
• Time Complexity
• O(bd)
05/06/2024 Spring' 2024 21

Exercise
• Find path from S to G
• For each DFS, DLS
(d=3), IDS, BFS
• Search tree constructed
during the search
• List of nodes in the order
in which they were
expanded
• State of the Queue when
the node was expanded
05/06/2024 Spring' 2024 22

Solution to DFS
05/06/2024 Spring' 2024 23

Depth – Limited Search


05/06/2024 Spring' 2024 24

Homework
• Solutions for BFS and IDS are left as an exercise.

You might also like