AI Unit 2
AI Unit 2
AI Unit 2
SEARCH
ALGORITHMS
1.Search Algorithms,
2. Random search,
5. Heuristic search,
7. A* algorithm
1. Search Algorithms
A search method is defined by picking the order of node expansion. Strategies are evaluated
along the following dimensions:
completeness: does it always find a solution if one exists?
time complexity: number of nodes generated
space complexity: maximum number of nodes in memory
optimality: does it always find the shortest path solution?
Uninformed Search is a class of general purpose algorithms that operates in a brute force way. The
search space is explored without leveraging on any information on the problem. Also called
blind search, or naïve search
Since the methods are generic they are intrinsically inefficient. Uninformed search strategies that do
not take into account the location of the goal. Intuitively, these algorithms ignore where they are
going until they find a goal and report success.
E.g. Random Search
2. Random Search
This method selects randomly a new state from the current one. If the goal state is reached, the
search terminates. Otherwise the methods randomly select an other operator to move to the next
state. Goal achievement is not insured in finite time.
A key issue in search is limiting the portion of the state space that must be explored to find a
solution.
The portion of the search space explored can be affected by the order in which states (and thus
solutions) are examined.
The search strategy determines this order by determining which node (partial solution) will be
expanded next.
Repeated states
Failure to detect repeated states can turn a linear problem into an exponential.
A random search, while never practical , Is nevertheless interesting to consider as a worst case
search. To program this search, modify step 4.a. so that a random state is chosen. Given enough
time, the goal state should be found, But a random search will probably take more time (more
states are checked) than any other method
BFS Algorithm checks whether the goal node exist or not. Path from start to goal node can be
obtained by maintaining CLOSED list with pointers back to its parent in the search tree.
Algorithm (BFS)
Solution :
State space for this problem can be described as the set of ordered pairs of integers(X,Y) such
that X represents the number of gallons of water in 5-g jug and Y for 3-g jug.
Start state is (0,0)
Goal state is(4,N) for any value of N<=3
Applying RULE 2 (Empty 5-g jug) to (5,3) will give(0,3) already explored state. Put a cross
mark.
Applying RULE 4 (Empty 3-g jug) to (5,3) will give(5,0) already explored state. Put a cross
mark.
Applying RULE 1 (Fill 5-g jug) to (2,3) will give (5,3) already explored state. Put a cross mark.
Applying RULE 2 (Empty 5-g jug) to (2,3) will give (0,3) already explored state. Put a cross
mark.
Applying RULE 4 (Empty 3-g jug) to (2,3) will give (2,0) state.
Applying RULE 1 (Fill 5-g jug) to (3,0) will give (5,0) already explored state. Put a cross mark.
Applying RULE 2 (Empty 5-g jug) to (3,0) will give (0,0) already explored state. Put a cross
mark.
Applying RULE 3 (Fill 3-g jug) to (3,0) will give (3,3) state.
Applying RULE 8 (pour water from 5-g jug to 3-g jug until 3-g jug is full) to (3,0) will give (0,3)
already explored state. Put a cross mark.
Applying RULE 1 (Fill 5-g jug) to (2,0) will give (5,0) already explored state. Put a cross mark.
Applying RULE 2 (Empty 5-g jug) to (2,0) will give (0,0) start state. Put a cross mark.
Applying RULE 3 (Fill 3-g jug) to (2,0) will give (2,3) already explored state. Put a cross mark.
Applying RULE 6 (Empty 5-g jug into 3-g jug) to (2,0) will give (0,2) state.
Applying RULE 1 (Fill 5-g jug) to (3,3) will give (5,3) already explored state. Put a cross mark.
Applying RULE 2 (Empty 5-g jug) to (3,3) will give (0,3) already explored state. Put a cross
mark.
Applying RULE 4 (Empty 3-g jug) to (3,3) will give (3,0) already explored state. Put a cross
mark.
Applying RULE 7 (Pour water from 3-g jug into 5-g jug until 5-g jug is full) to (3,3) will give
(5,1) state.
• Applying RULE 1 (Fill 5-g jug) to (0,2) will give (5,2) state.
• Applying RULE 3 (Fill 3-g jug) to (0,2) will give (0,3) already explored state. Put a cross mark.
• Applying RULE 4 (Empty 3-g jug) to (0,2) will give (0,0) start state. Put a cross mark.
• Applying RULE 5 (Empty 3-g jug into 5-g jug) to (0,2) will give (2,0) already explored state. Put
a cross mark.
• Applying RULE 2 (Empty 5-g jug) to (5,1) will give (0,1) state.
• Applying RULE 3 (Fill 3-g jug) to (5,1) will give (5,3 ) already explored state. Put a cross mark.
• Applying RULE 4 (Empty 3-g jug) to (5,1) will give (5,0) already explored state. Put a cross
mark.
• Applying RULE 8 (pour water from 5-g jug to 3-g jug until 3-g jug is full) to (5,1) will give (3,3)
already explored state. Put a cross mark.
• Applying RULE 2 (Empty 5-g jug) to (5,2) will give (0,2) already explored state. Put a cross
mark.
• Applying RULE 3 ( Fill 3-g jug) to (5,2) will give (5,3) already explored state. Put a cross
mark.
• Applying RULE 4 (Empty 3-g jug) to (5,2) will give (5,0) already explored state. Put a cross
mark.
• Applying RULE 8 (pour water from 5-g jug to 3-g jug until 3-g jug is full) to (5,2) will give (4,3)
.
Depth-First Search
In depth-first search we go as far down as possible into the search tree / graph before backing up
and trying alternatives.
It works by always generating a descendent of the most recently expanded node until
some depth cut off is reached.
then backtracks to next most recently expanded node and generates one of its
descendants.
So only path of nodes from the initial node to the current node is stored in order to execute the
algorithm.
For implementation, two lists called OPEN and CLOSED with the same conventions explained
earlier are maintained.
Here OPEN and CLOSED list are maintained as a stack.
If we discover that first element of OPEN is the Goal state, then search terminates
successfully else move it to closed list and stack its successor in open list.
We can get track of the path through state space as we traversed, but in those
situations where many nodes after expansion are in the closed list, we fail to keep
track of our path.
This information can be obtained by modifying CLOSED list by putting pointer back to its
parent in the search tree.
Comparisons
DFS
DFS is effective when there are few sub trees in the search tree that have only one connection
point to the rest of the states.
DFS can be dangerous when the path closer to the START and farther from the GOAL has been
chosen.
DFS is best when the GOAL exists in the lower left portion of the search tree.
DFS is memory efficient as the path from start to current node is stored, each node should
contain state and its parent.
DFS may not give optimal solution.
BFS
BFS is effective when the search tree has a low branching factor.
It can work even in trees that are infinitely deep.
It requires a lot of memory as number of nodes in level of the tree increases exponentially.
BFS is superior when the GOAL exists in the upper right portion of a search tree.
BFS gives optimal solution.
If match is found ,then path can be traced from start state to the matched state and from matched
state to the goal state.
It should be noted that each node has link to its successors as well as to its parents. These links
will help generating complete path from start to goal state.
‘b’ is the branching factor and ‘d’ to be depth of the tree in the worst case.
The reason for this approach is that each searches has time complexity o(b d/2) and o(b d/2 + b d/2 )
is much less than the running time of one search from the beginning to the goal, which would be
o(b d).
This search can be made in already existing graph/tree or search graph/tree can be generated as a
part of search.
STEP 1
STEP 2
STEP 3
The strategy can be improved little bit using the following techniques.
Start generating complete path, keeping track of the shortest path found so far.
Stop exploring any path as soon as its partial length becomes greater than the shortest path length
found so far.
This method is efficient than the first one but still requires exponential time that is directly
proportional to some number raised to ‘n’.
Table shows some possible paths generated using modified approach up to some level.
Some of the partial paths are pruned if the distance computed is less than minimum computed
distance so far between any pair of cities.
Initially ,first complete path is taken to be minimum and √ is put along with the distance, and if
the distance (full or partial ) is greater than previously calculated minimum, then x is put to show
pruning of that path.
Continue till all paths have been explored. In this case, there will be 4!=24 possible paths.
5.1 Heuristic Search techniques
Heuristics Techniques is a criterion for determining which among several alternatives will be the
most effective to achieve some goal. This techniques improves the efficiency of a search process
possibly by sacrificing claims of systematic and completeness. It no longer guarantees to find the
best solution but almost finds a very good solution. Heuristic are to find solution to hard problem
in less than exponential time.
7. A* Algorithm
A* Algorithm was proposed by Hart in 1972 is a combination of branch and bound and best
search methods combined with the dynamic programming.
It uses heuristic or evaluation function usually denoted by f(X) to determine the order in which
the search visits the node in the tree.
The heuristic function for a node N is defined as follows
f(N)=g(N)+H(N)
The function g is a measure of the cost of getting from the start node to the current node N. i.e
Sum of cost of the rules that were applied along the best path to the current node. The function h
is an estimate of additional cost of getting from the current node N to the goal state.
This is the place where knowledge about the problem domain is exploited.
A* is called OR graph/tree search algorithm.
A* incrementally searches all the routes starting form the start node until it finds the shortest
path to a goal.
Starting with the given node, the algorithm expands the node with lowest f(X) values.
It maintains a set of partial solution.
Unexpanded leaf node of expanded nodes are stored in a queue with the corresponding values.
This queue can be maintained as priority queue.
Algorithm A*
Input: Two states in the state space, START and GOAL
Local Variables: OPEN, CLOSED, Best_Node,SUCCs,OLD,FOUND
Output: Yes or No
Method:
Overestimation
We expand B to E,E to F and F to G for a solution path of length 4.
But assume that there is a direct path from D to a solution giving a path of length 2 as h value of
D is also overestimated.
We will never find it because of overestimating h(D). We may find some other worse solution
without ever expanding D.
So by overestimation h, we cannot be guaranteed to find the shortest path.
In this case, heuristic is locally admissible i.e. consistently finds the minimal path to each state
they encounter in the search.
The Monotone property is that the search space which is every where locally consistent with
heuristic function employed i.e. reaching each state along the shortest path from its ancestors.
With monotonic heuristic, if a state is rediscovered , it is not necessary to check whether the new
path is shorted.
Each Monotonic heuristic function is admissible.
7.5 Iterative-Deepening A*
Iterative deepening A*(IDA*) is a combination of the depth-first iterative deepening and A*
Algorithm.
Here the successive iterations are corresponding to increase values of the total cost of a path
rather than increasing depth of the search.
Algorithm works as follows
For each operation ,perform a DFS pruning off a branch when its total cost(g+h) exceeds
a given threshold.
The initial threshold starts at the estimate cost of the start state and increases for each
iteration of the algorithm.
The threshold used for the next iteration is the minimum cost of all values exceeded the
current threshold.
These steps are repeated till we find a goal state.