AI unit2
AI unit2
Consider a 3x3 matrix with movable tiles numbered from 1 to 8 with a blank
space.
The tile adjacent to the blank space can slide into that space.
The objective is to reach a specified goal state similar to the goal state, as
shown in the below figure
The task is to convert the current state into goal state by sliding digits into
the blank space
2
o The path cost is the number of steps in the path where the cost of each step
is 1.
Note: The 8-puzzle problem is a type of sliding-block problem which is used
for testing new search algorithms in artificial intelligence.
4
1.3 Search Algorithm
o A search algorithm takes a search problem as input and returns a solution.
A problem graph, containing the start node S and the goal node G.
A strategy, describing the manner in which the graph will be traversed
to get to G.
A fringe, which is a data structure used to store all the possible states
(nodes) from the current states.
A tree, that results while traversing to the goal node.
A solution plan, which the sequence of nodes from S to G.
5
DEPTH FIRST SEARCH:
• Depth-first search (DFS) is an algorithm for traversing or searching tree or
graph data structures.
• The algorithm starts at the root node and explores as far as possible along
each branch before backtracking.
• It uses last in-first-out strategy and hence it is implemented using a stack.
d= the depth of the search tree = the number of levels of the search
tree.
6
Advantage of Depth-first Search:
DFS uses extremely little memory.
It takes less time to reach the goal node than the BFS method.
Algorithm
7
DEPTH-LIMITED SEARCH ALGORITHM
A depth-limited search algorithm is similar to depth-first search with a
predetermined limit.
Depth-limited search can solve the drawback of the infinite path in
the Depth-first search.
In this algorithm, the node at the depth limit will treat as it has no
successor nodes further.
Depth-limited search can be terminated with two Conditions of failure:
o Standard failure value: It indicates that problem does not have any
solution.
Cutoff failure value: It defines no solution for the problem within a given depth limit.
8
Example
9
Breadth First Search Algorithm:
Example:
Which solution would BFS find to move from node S to node G if run on the graph below
Solution
10
Path: S -> D -> G
Further, change the depth-limit =[0-3], it will again expand the nodes from level 0 till level
3 and the search terminate with A->B->D->F->E->H sequence where H is the desired goal
node.
11
The performance measure of Iterative deepening search
Completeness: Iterative deepening search may or may not reach the
goal state.
Optimality: It does not give an optimal solution always.
Space Complexity: It has the same space complexity as BFS,
i.e., O(bd).
Time Complexity: It has O(d) time complexity.
Example:
Which solution would UCS find to move from node S to node G if run on the graph
below?
12
Solution.
Cost: 5
BIDIRECTIONAL SEARCH
The strategy behind the bidirectional search is to run two searches simultaneously--one forward
search from the initial state and other from the backside of the goal--hoping that both searches
will meet in the middle.
As soon as the two searches intersect one another, the bidirectional search terminates with the goal
node.
This search is implemented by replacing the goal test to check if the two searches intersect.
Because if they do so, it means a solution is found.
13
INFORMED (HEURISTIC) SEARCH STRATEGIES:
Here, the algorithms have information on the goal state, which
helps in more efficient searching.
This information is obtained by something called a heuristic.
Search Heuristics:
In an informed search, a heuristic is a function h(n) estimates how close a state
is to the goal state.
h(n) = estimated cost of the cheapest path from the state at node n to a goal state.
Greedy Search
A* Tree Search
A* Graph Search
GREEDY SEARCH:
In greedy search, we expand the node closest to the goal node. The “closeness” is
estimated by a heuristic h(x).
14
Advantage: Works well with informed search problems,
with fewer steps to reach a goal.
Disadvantage: Can turn into unguided DFS in the worst case.
Example:
Question. Find the path from S to G using greedy search. The heuristic values h of each
node below the name of the node.
Solution
15
A* TREE SEARCH:
A* Tree Search, or simply known as A* Search, combines the strengths of uniform-cost
search and greedy search.
In this search, the heuristic is the summation of the cost in UCS, denoted by g(x), and the
cost in the greedy search, denoted by h(x). The summed cost is denoted by f(x).
Heuristic:
The following points should be noted with heuristics in A* search.
f(x) = g(x) + h(x)
h(x) is called the forward cost and is an estimate of the distance of the
current node from the goal node.
g(x) is called the backward cost and is the cumulative cost of a node
from the root node.
A* search is optimal only when for all nodes, the forward cost for a node
h(x) underestimates the actual cost h*(x) to reach the goal.
This property of A* heuristic is called admissibility. Admissibility: 0 <=
h(x) <= h*(x)
16
Solution.
Starting from S, the algorithm computes g(x) + h(x) for all nodes in the fringe at
each step, choosing the node with the lowest sum.
A* algorithm
A* Graph Search:
17
two successive nodes A and B, given by h(A) – h (B), is less than or equal
to the backward cost between those two nodes g(A -> B). This property
of the graph search heuristic is called consistency.
Consistency: h(A) – h(B) <= g ( A->B )
Example:
Question. Use graph searches to find paths from S to G in the following graph.
Solution
Cost: 7
18