AI Unit 2 Module 1
AI Unit 2 Module 1
• Uninformed search
• Informed search
Search strategies
• Uninformed search
– no information about the number of steps
– or the path cost from the current state to the goal
– search the state space blindly
– Time consuming
– More complexity
– Ex. DFS, BFS
• Breadth-first search
• Depth-first search
• Depth-limited search
• Bidirectional search
• Uniform cost search
• Iterative deepening depth-first search
Breadth First Search
• It expands all the node at given depth
before proceeding to the next node/ level.
• Use queue for implementation
• The root node is expanded first (FIFO)
• All the nodes generated by the root node
are then expanded
• And then their successors and so on
Breadth First Strategy
2 3
4 5 6 7
Breadth First Strategy
2 3
4 5 6 7
Breadth First Strategy
2 3
4 5 6 7
Breadth First Strategy
2 3
4 5 6 7
Breadth First Search
S
A D
B D A E
C E E B B F
11
D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17
G 25
Breadth First Search (Analysis)
• Breadth First Search
– Complete – find the solution eventually
– FIFO
– Optimal, if step cost is 1
The disadvantage
– if the branching factor of a node is large,
– for even small instances (e.g., chess)
• the space complexity and the time complexity are
enormous
Breadth First Search algorithm
i) Enter starting node on Queue
ii) If the queue is empty, then return fail and
stop
iii) If first element on queue is goal node then
return success and stop
ELSE
iv) Remove and explore first element from
queue and place children at end of queue
v) Go to step (ii)
EXAMPLE
A A
B C D
C D E F
E F G H I
F G H I J
G H I J K
H I J K L
Path: A B C D E F G H I J K L
Applications of BFS Algorithm
• Shortest Path in Unweighted Graphs: BFS is ideal for finding the shortest path in
unweighted graphs, such as in routing algorithms where each hop has the same cost.
This application is crucial for network designs where the shortest route from one node
to another needs to be identified quickly and efficiently.
• Puzzle Games: In gaming, BFS helps in solving puzzles where the goal is to find the
minimum number of moves to reach a target state. It's extensively used in games like
mazes or sliding puzzles, where each move is uniform and leads to a new state.
• Social Networks: BFS plays a critical role in social networking platforms. It's used to
calculate the degrees of separation between users, often seen in features like
"friends of friends". This helps in understanding and visualizing the network's
structure by exploring user connections layer by layer.
• Web Crawling: Search engines use BFS for web crawling to index web pages.
Starting from a source page, the crawler visits all linked pages at the current depth
before moving to pages linked in the next layer, ensuring comprehensive indexing.
Disadvantages
• Requires more memory
• Requires lot of time if the solution is far from
root node
Do it
Path : A B D E F G H J L
Do it
Depth First Search
• It starts from root node and follows each path to its
greatest depth node before moving to the next path
• Stack is used for DFS
• Always expands one of the nodes at the deepest level
of the tree
• Only when the search hits a dead end
– goes back and expands nodes at shallower levels
– Dead end leaf nodes but not the goal
• Backtracking search
– only one successor is generated on expansion
– rather than all successors
– fewer memory
– Less time
Depth First Search
• Expand deepest unexpanded node
• Implementation:
LIFO queue, i.e., put successors at front
Depth First Search
• Expand deepest unexpanded node
• Implementation:
LIFO queue, i.e., put successors at front
Depth First Search
• Expand deepest unexpanded node
• Implementation:
LIFO queue, i.e., put successors at front
Depth First Search
• Expand deepest unexpanded node
• Implementation:
LIFO queue, i.e., put successors at front
Depth First Search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
Depth First Search
• Expand deepest unexpanded node
• Implementation:
LIFO queue, i.e., put successors at front
Depth First Search
• Expand deepest unexpanded node
• Implementation:
LIFO queue, i.e., put successors at front
Depth First Search
• Expand deepest unexpanded node
• Implementation:
LIFO queue, i.e., put successors at front
Depth First Search
• Expand deepest unexpanded node
• Implementation:
LIFO queue, i.e., put successors at front
Depth First Search
• Expand deepest unexpanded node
• Implementation:
LIFO queue, i.e., put successors at front
Depth First Search
• Expand deepest unexpanded node
• Implementation:
LIFO queue, i.e., put successors at front
Depth First Search
• Expand deepest unexpanded node
• Implementation:
LIFO queue, i.e., put successors at front
Depth First Search
S
A D
B D A E
C E E B B F
11
D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17 G 25
Depth First Search algorithm
1. Initialize an empty stack and an empty set for visited vertices.
2. Push the start vertex onto the stack.
3. While the stack is not empty:
a) Pop the current vertex.
b) If it’s the goal vertex, return “Path found”.
c) Add the current vertex to the visited set.
d) Get the neighbors of the current vertex.
e) For each neighbor not visited, push it onto the stack.
4. If the loop completes without finding the goal, return “Path not
found”.
Depth First Search (Analysis)
• Not complete
– because a path may be infinite or looping
– then the path will never fail and go back try another
option
• Not optimal
– it doesn't guarantee the best solution
• It overcomes
– the time and space complexities
Example G
H
S
A H
H
I
B
C J
H
D K
E
C J
H
Path: S A B D E C G H I K J
Advantages
• Requires less memory
• Requires less time to reach goal node if
traversing the right path
Disadvantages
• No guarantee of finding a solution
• If number of nodes is very much high then
searching in other direction will be
problematic
• Can cause infinite loop
Do it
Path : A B E J F D G L
Depth Limited Strategy
C E E B B F
11
D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17
G 25
Algorithm
1. We start with finding and fixing a start node.
2. Then we search along with the depth using the DFS algorithm.
3. Then we keep checking if the current node is the goal node or not.
• In case of DFS,
• ABDGHEICF
Disadvantages:
• Depth limited search also has a disadvantage of
incompleteness.
• It may not be optimal if the problem has more than
one solution.
• The effectiveness of the Depth-Limited Search (DLS)
algorithm is largely dependent on the depth limit
specified. If the depth limit is set too low, the
algorithm may fail to find the solution altogether.
Bidirectional Search
• Run two simultaneous searches at the same time
– one forward from the initial state, another backward from
the goal
– stop when the two searches meet
• However, computing backward is difficult
– A huge amount of goal states
– at the goal state, which actions are used to compute it?
– can the actions be reversible to computer its predecessors?
B D A E
C E E B B F
11
D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17
G 25
Intersection
Forward Backward
A G
ABC GEF
ABCD GEFD
PATH: A B C D E F D
Solve it
Solution
Forward Backward
A G
AB GKJ
ABC GKJI
ABCEF GKJIY
ABCEFH GKJIYH
PATH: A B C E F H Y I J K G
Solve it
Solution
Forward Backward
A G
ABD GKY
ABDC GKYI
ABDCEF GKYIL J
ABDCEFH GKYIL JH
PATH: A B D C E F H J L I Y K G
Advantages:
Disadvantages: