0% found this document useful (0 votes)
8 views

AI Unit 2 Module 1

Uploaded by

agnikdey520
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

AI Unit 2 Module 1

Uploaded by

agnikdey520
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Types of Search Algorithms

Based on the search problems, we can classify


the search algorithm as:

• 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

• Informed search, or heuristic search


– a cleverer strategy that searches toward the goal,
– based on the information from the current state so far
– Quick solution
– Less complexity
– Ex. A*, Best first etc.
Uninformed Search Strategies
Uninformed Search Algorithms
The uninformed search algorithm does not have any
domain knowledge such as closeness, location of the goal
state, etc. it behaves in a brute-force way. It only knows the
information about how to traverse the given tree and how
to find the goal state. This algorithm is also known as the
Blind search algorithm or Brute Force algorithm. The
uninformed search strategies are of six types.

• 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.

• Machine Learning: In machine learning, BFS aids in building decision trees,


especially for classification problems. The algorithm explores all possible next steps
in the decision process, ensuring a thorough analysis of each attribute's impact.

• Broadcast Networks: In networking, BFS is employed to manage the distribution of


messages or data across a network, ensuring all nodes receive the broadcast without
redundancies.
Advantages
• Find solution if it exists
• Tries to find minimal solution in least number
of steps

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

• Uninformed search algorithm

• Similar to DFS with pre determined limit (Used to explore search

space up to a certain depth).

• DLS can solve the drawback of infinite path problem in DFS

• Depth-first with depth cutoff k (maximal depth below which nodes

are not expanded), this approach is known as depth limited search


Depth Limited Search
• It is depth first search
– with a predefined maximum depth
– However, it is usually not easy to define the suitable
maximum depth
– too small  no solution can be found
– too large  the same problems are suffered from
• Anyway the search is
– complete
– but still not optimal
Depth Limited Search
S depth = 3
A D 3
6
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
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.

• If the answer is no: then we do nothing.

• If the answer is yes: then we return.


Example
• If F is goal node
• Depth limit is 2
• ABDECF

• In case of DFS,
• ABDGHEICF

• Eliminating disadvantage of DFS


Advantages:
• As we know the depth so it is memory efficient.

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?

Stop Condition: When graphs intersect


Bidirectional Strategy

2 fringe queues: FRINGE1 and FRINGE2

Time and space complexity = O(bd/2) << O(bd)


Bidirectional Search
S
Forward
A D Backwards

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:

• Bidirectional search is fast (It drastically reduces the time taken by


the search by having simultaneous searches.)
• Bidirectional search requires less memory.
• It also saves resources for users as it requires less memory capacity
to store all the searches.

Disadvantages:

• Implementation of the bidirectional search tree is difficult.


• In bidirectional search, one should know the goal state in advance.

You might also like