All Unit 2 Int 426
All Unit 2 Int 426
All Unit 2 Int 426
•Time Complexity
–How long (worst or average case) does it take to find a solution? Usually
measured in terms of the number of nodes expanded
•Space Complexity
–How much space is used by the algorithm? Usually measured in terms of the
maximum size that the “OPEN" list becomes during the search
•Optimality
–If a solution is found, is it guaranteed to be an optimal one? For example, is it the
one with minimum cost?
Search Techniques
• Best-first search
• Hill climbing search
• A* search
• Generate and test
• Constraint satisfaction
Algorithm : Breadth-First Search
1. Create a variable called NODE-LIST and set it to the initial state.
2. Until a goal state is found or NODE-LIST is empty:
(a) Remove the first element from NODE-LIST and call it E. If NODE-LIST
was empty, quit.
(b) For each way that each rule can match the state described in E do:
25
Two Levels of a Breadth-First
Search Tree
26
Breadth-First
s
– A complete search tree of 1
depth d where each non-leaf
1 b
node has b children, has a
total of 1 + b + b^2 + ... + b^d = 2 b^2
(b^(d+1) - 1)/(b-1) nodes
– Time complexity (# of nodes
generated): O(b^d)
– Space complexity (maximum
length of OPEN): O(b^d)
d b^d
27
A Depth-First Search Tree
28
• The drawback of depth-first search is that it can get stuck going down the
wrong path.
• Many problems have very deep or even infinite search trees, so depth-
first search will never be able to recover from an unlucky choice at one of
the nodes near the top of the tree. That means depth-first search is
neither complete nor optimal.
• depth-first search should be avoided for search trees with large or infinite
maximum depths.
Constraint Satisfaction Problems
Constraint Satisfaction Problems
INT 404
Introduction:
• Heuristic search is also called guided search because in this, the search is guided
in a specific direction.
• In heuristic search, besides normal production rules, additional information or
knowledge about the problems is given in the form of clue.
• The additional information or clue is called heuristics.
• This additional information or clue restricts the expansion of only promising
nodes in search tree and guides the search in a specific direction towards the
goal. Thus, it reduces the search space and the solution of the problem is obtained
efficiently.
• The additional information is given for search in terms of heuristic function.
Heuristic Function:
• The purpose of heuristic function is to guide the search process in the most
profitable search direction by suggesting which path to follow first, when more
than one search paths are available.
Designing Heuristic Function:
• The designing of heuristic function is a critical task. In the problems where start
and goal states are known, the heuristic function somehow relates current search
state with the desired goal state.
• The designing of heuristic function is done in such a way that it provides best
mathematical value on a move.
3 2 1 1 2 3
8 5 6 8 0 4
0 7 4 7 6 5
• The branching factor of 8-puzzle problem is 3. The middle tile is 4, for the corner
tiles it is 2 , otherwise it is 3.
• h= 2+0+2+1+2+2+1+0 = 10
• There exist multiple heuristic function for the same problem. in such situations , function
which brings the solution in minimum number of steps is considered best.
Generate-and-test
Best-first search
Hill climbing
Problem reduction
Constraint satisfaction
Means-ends analysis
44
Algorithm : Generate-and-Test
Do the following until satisfactory solution is obtained or no more
solutions can be generated.
1. Generate a possible solution.( for some problems, this means
generating a particular point in the problem space and for
others, it means generating a path from a start state).
2. Test to see if this is actually a solution by comparing the
chosen point or the endpoint of the chosen path to the set of
acceptable goal states.
3. If a solution has been found, quit. Otherwise, return to step 1.
45
GENERATE-AND-TEST
Acceptable for simple problems.
8
Generate-and-Test
There can be two methods to complete generate-and-test process.
1. Generate a random solution and test it. If found wrong create another solution
and test it again. This method might gives you a solution on the first trial.
However there is no guarantee that the solution will ever be found.
11
BEST-FIRST SEARCH
At each step of the BFS search process, we select the most
promising of the nodes we have generated so far.
12
BEST-FIRST SEARCH
A A A
B C D B C D
3 5 1 3 5
E F
4 6
A A
B C D B C D
5 5
G H E F G H E F
6 5 4 6 6 5 6
I J
13
2 1
BEST-FIRST SEARCH
OPEN: nodes that have been generated, but have not examined.
This is organized as a priority queue.
14
Algorithm : Best-First Search
1. Start with OPEN containing just the initial state.
(i) If it has not been generated before, evaluate it, add it to OPEN, and
record its parent.
(ii) If it has been generated before, change the parent if this new path is
better than the previous one. In that case, update the cost of getting
to this node and to any successors that this node may already,
have.
54
16
Hill Climbing
Hill Climbing is a heuristic search used for mathematical optimization problems in the
field of Artificial Intelligence.
Given a large set of inputs and a good heuristic function, the algorithm tries to find the best
possible solution to the problem in the most reasonable time period.
This solution may not be the absolute best (global optimal maximum) but it is sufficiently
good considering the time allotted. Therefore, it falls in the category of Local search
algorithm.
Example of this is the Travelling Salesman Problem, where we need to minimize the
distance travelled by the salesman.
Features of Hill Climbing
A heuristic function is one that ranks all the potential alternatives in a search
algorithm based on the information available. It helps the algorithm to select the
best route to its solution.
Hill climbing takes the feedback from the test procedure and the generator uses it in
deciding the next move in the search space. Hence, we call it as a variant of the
generate-and-test algorithm.
At any point in state space, the search moves in that direction only which optimises
the cost of function with the hope of finding the most optimum solution at the end.
Simple Hill-Climbing
Simple hill climbing is the simplest way to implement a hill-climbing algorithm.
It only evaluates the neighbour node state at a time and selects the first one which optimizes
current cost and set it as a current state. Less time consuming, Less optimal solution, The
solution is not guaranteed
1. Evaluate the initial state. If it is also a goal state, then return it and quit. Otherwise,
continue with the initial state as the current state.
2. Loop until a solution is found or until there are no new operators left to be applied in the
current state:
(a) Select an operator that has not yet been applied to the current state and apply it
to produce a new state.
(b) Evaluate the new state.
(i) If it is a goal state, then return it and quit.
(ii) If it is not a goal state but it is better than the current state, then make it the
current state.
(iii) If it is not better than the current state, then continue in the loop.
46
Problems in Hill climbing
Hill climbing cannot reach the best possible state if it enters any of the following
regions :
To overcome plateaus: Make a big jump. Randomly select a state far away from
the current state. Chances are that we will land at a non-plateau region
3. Ridge: is a special kind of local maximum. It is a area of search space that
is higher than surroundings areas and that itself has a slope.
But the orientation of the high region , compared to the set of available
moves and the directions in which they move, make it impossible to traverse
a ridge by single moves.
To overcome Ridge: You could use two or more rules before testing. It
implies moving in several directions at once.
Steepest-Ascent hill climbing
•Step 1: Evaluate the initial state, if it is goal state then return success and stop,
else make the current state as your initial state.
•Step 2: Loop until a solution is found or the current state does not change.
1. Let S be a state such that any successor of the current state will be better
than it.
2. For each operator that applies to the current state;
1. Apply the new operator and generate a new state.
2. Evaluate the new state.
3. If it is goal state, then return it and quit, else compare it to the S.
4. If it is better than S, then set new state as S.
5. If the S is better than the current state, then set the current state to S.
•Step 5: Exit.
A* algorithm
• The A * algorithm is a specialization of best-first search.
• It provides general guidelines about how to estimate goal distances for general
search graphs.
• At each node along the path to the goal node, A * algorithm generate all
successor nodes and computes an estimate of distance(cost) from the start node
to a goal node through each of the successors.
• It then chooses the successor with the shortest estimated distance for
expansion.
• It calculates the heuristic function based on distance of current node from start
state and distance of current node to goal state.
f(n) = g(n) + h(n)
Where,
✓ g(n) = the cost of moving from the initial cell to the current cell. Basically, it is the
sum of all the cells that have been visited since leaving the first cell.
✓ h(n) = also known as the heuristic value, it is the estimated cost of moving from
the current cell to the final cell. The actual cost cannot be calculated until the
final cell is reached. Hence, h is the estimated cost. We must make sure that
there is never an over estimation of the cost.
1. Start with OPEN containing only initial node. Set that node’s g
value to 0, its h’ value to whatever it is, and its f’ value to h’+0 or
h’. Set CLOSED to empty list.