You are on page 1of 64

Module 3: Problem Solving By

Searching
3.3 Uninformed Search
✓ BFS, DFS, DLS, DFID
3.4 Informed Search
✓ Greedy Best First Search
✓ A* Search
✓ Memory Bounding Heuristic Search
Local Search Algorithms and Optimization Problems
✓ Hill Climbing Search Simulates Annealing
✓ Genetic Algorithms
3.5 Adversarial Search
✓ Game Playing
✓ Min Max Search
✓ Alpha Beta Pruning
Uninformed Search Method
• Uninformed search is a class of general-purpose search algorithms
which operates in brute force-way.
• Do not have additional information about state or search space other
than how to traverse the tree.
Depth First Search
Breadth First Search

S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
Depth Limited Search
Depth First Iterative Deepening

1'st Iteration-----> A
2'nd Iteration----> A, B, C
3'rd Iteration------>A, B, D, E, C, F, G
4'th Iteration------>A, B, D, H, I, E, C, F, K, G
In the fourth iteration, the algorithm will find
the goal node.
Bidirectional Search
Uniform Cost Search
Informed Search Methods
• Informed search is very useful for large search space.
• This contains an array of knowledge such as how far we are from the goal, path
cost, how to reach to goal node, and etc.
• This knowledge helps the agents to explore less to search space and find goal
node more efficiently.
• This method used the idea of heuristic function.
• These techniques are very much efficient with respect to time and space as
compare to uninformed search methods.
• Information about goal node is present. Find optimal solution to reach the goal
node using heuristic function.
Heuristic Function
• Heuristic function finds the most promising path.
• It takes the current state of agent as input and tell us how close the agent is from
the goal node.
• It estimate how close a state is to the goal node.
• It is represented by h(n).
• It calculate the cost of an optimal path between pair of states.
• The value of this function is always positive.
• ℎ 𝑛 ≤ ℎ ∗ (𝑛)
• Where,
h(n) = heuristic cost.
h*(n) = actual cost.
Admissible Heuristic
• Heuristics don’t always lead to a lower cost.
• However, those that don’t overestimate the true or the lowest possible cost of a solution are called
admissible heuristics.
• This characteristic can guarantee the optimality of the solution.
• An admissible heuristic can be found by simplifying the original problem in terms of its constraints, reducing
it to a less constrained problem.
• As an example, let’s consider the eight puzzle problem:
Greedy Best First Search Algorithm
• It always select the path which appears best at the moments.
• It is a combination of DFS and BFS algorithm.
• It uses heuristic function and search.
• With the help of BFS, we choose the most promising node.
• In this we expand the node which is closest to the goal node and the closest node
is estimated by heuristic function.
• This algorithm is implemented by priority queue.
• This algorithm uses two list:
• OPEN: All those nodes that have been generated and have heuristic function
applied to them but not examined yet.
• CLOSE: Contains all the nodes that have been examined.
Advantages and Disadvantages
• Advantages:
• Best first search can switch between BFS and DFS by gaining the advantages of
both the algorithms.
• This algorithm is more efficient than BFS and DFS algorithms.
• Disadvantages:
• It can behave as an unguided depth-first search in the worst case scenario.
• It can get stuck in a loop as DFS.
• This algorithm is not optimal.
Algorithm of Greedy Best First Search
• Step 1: Place the starting node into the OPEN list.
• Step 2: If the OPEN list is empty, Stop and return failure.
• Step 3: Remove the node n, from the OPEN list which has the lowest value of h(n), and
places it in the CLOSED list.
• Step 4: Expand the node n, and generate the successors of node n.
• Step 5: Check each successor of node n, and find whether any node is a goal node or not.
If any successor node is goal node, then return success and terminate the search, else
proceed to Step 6.
• Step 6: For each successor node, algorithm checks for evaluation function f(n), and then
check if the node has been in either OPEN or CLOSED list. If the node has not been in
both list, then add it to the OPEN list.
• Step 7: Return to Step 2.
Example:
• In Greedy Best First Search we jump all around in the search to identify the
nodes with minimal evaluation function value.
• Give faster solution but no guarantee.
• Time Complexity: The worst case time complexity of Greedy best first
search is O(bm).
• Space Complexity: The worst case space complexity of Greedy best first
search is O(bm). Where, m is the maximum depth of the search space.
• Complete: Greedy best-first search is also incomplete, even if the given
state space is finite.
• Optimal: Greedy best first search algorithm is not optimal, as it goes on
selecting a single path and never checks for other possibilities.
A* Search Algorithm
• This algorithm is pronounced as “Ayster”.
• This algorithm is similar to Greedy Best First Search algorithm.
• The difference is GBFS takes h(n) as heuristic value.
• In GBFS h(n) = estimates cost of current node’s n from the goal node.
• A* takes the evaluation function as:
• F(n) = g(n) + h(n)
• Where,
g(n) = cost of the current node from the start state.
h(n) = estimated cost of current node from goal node.
Example:
Example:

Solution
Solution
• Points to remember:
• A* algorithm returns the path which occurred first, and it does not search for all
remaining paths.
• The efficiency of A* algorithm depends on the quality of heuristic.
• A* algorithm expands all nodes which satisfy the condition.
• Complete:
• A* algorithm is complete as long as:
• Branching factor is finite.
• Cost at every action is fixed.
• Optimal:
• A* search algorithm is optimal if it follows below two conditions:
❖Admissible: the first condition requires for optimality is that h(n) should be an
admissible heuristic for A* tree search. An admissible heuristic is optimistic in nature.
(if it never overestimates the actual cost from current state to goal state)
❖Consistency: Second required condition is consistency for only A* graph-search.
• If the heuristic function is admissible, then A* tree search will always find the least cost
path.
• Time Complexity:
• The time complexity of A* search algorithm depends on heuristic function, and the
number of nodes expanded is exponential to the depth of solution d. So the time
complexity is O(b^d), where b is the branching factor.
• Space Complexity:
• The space complexity of A* search algorithm is O(b^d).
Hill Climbing Search
• 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, it tries to find a sufficiently good
solution to the problem.
• This solution may not be the global optimal maximum.
• It used to solve computationally hard problems, which have multiple possible solution.
• Mathematical optimization problems imply that hill-climbing solves the problems where
we need to maximize or minimize a given real function by choosing values from the given
inputs.
• ‘Heuristic search’ means that this search algorithm may not find the optimal
solution to the problem. However, it will give a good solution in a reasonable
time.
Hill Climbing
Input : Problem
Local variable : Current and neighbor
Current initial node of problem
Loop
do, neighbour  highest valued successor of current node
If value [neighbour] <= value [current node]
Then
Return state [current node]
Else
Current neighbor
Hill Climbing Search
• For example, if you want to find D-Mart from your current location. There are n possible
paths with different directions to reach the destination. The heuristic function here will
give you the distance of each path which is reaching to D-Mart. So, that it becomes very
simple and time efficient for you to reach the destination.
• Hill Climbing is a variant of generating and test algorithm.
1. It will generate the possible solutions.
2. Test to see if this is expected solution.
3. If the solution is found, Quite else Go to Step 1.
• At any point in state space, the search moves in that direction only which optimizes the
cost of function with the hope of finding the optimal solution at the end.
• Types:
1. Simple Hill Climbing
2. Steepest-Accent Hill Climbing
Types of Hill Climbing
• Simple Hill Climbing:
• It examines the neighbouring nodes one by one.
• Selects the first neighbouring node which optimizes the current cost as the next
node.
• Steepest-Accent Hill Climbing:
• It first examines all the neighbouring nodes.
• Then selects the node closest to the solution state as of the next node.
Simple Hill Climbing Algorithm
• Step 1 : Evaluate the initial state. If it is a goal state then stop and return success.
Otherwise, make initial state as current state.
• Step 2 : Loop until the solution state is found or there are no new operators
present which can be applied to the current state.
✓a) Select a state that has not been yet applied to the current state and apply it to
produce a new state.
✓b) Perform these to evaluate new state
i. If the current state is a goal state, then stop and return success.
ii. If it is better than the current state, then make it current state and proceed
further.
iii. If it is not better than the current state, then continue in the loop until a
solution is found.
• Step 3 : Exit.
Steepest-Accent Hill Climbing Algorithm
• Step 1 : Evaluate the initial state. If it is a goal state then stop and return success.
Otherwise, make initial state as current state.
• Step 2 : Repeat these steps until a solution is found or current state does not change
➢a) Select a state that has not been yet applied to the current state.
➢b) Initialize a new ‘best state’ equal to current state and apply it to produce a new state.
➢c) Perform these to evaluate new state
i. If the current state is a goal state, then stop and return success.
ii. If it is better then best state, then make it best state else continue loop
with another new state.
➢d) Make best state as current state and go to Step 2: b) part.
• Step 3 : Exit
State Space Diagram for HC
State Space Diagram Explanation
• The state-space diagram is a graphical representation of the set of states our
search algorithm can reach verses the value of our objective function(the
function which we wish to maximize).
• X-axis: denotes the state space i.e. states or configuration our algorithm may
reach.
• Y-axis: denotes the values of objective function corresponding to a particular
state.
• The best solution will be that state space where the objective function has a
maximum value(global maximum).
Regions in State Space Diagram
1. Local maximum: It is a state which is better than its neighbouring state however
there exists a state which is better than it(global maximum). This state is better
because here the value of the objective function is higher than its neighbours.
2. Global maximum: It is the best possible state in the state space diagram. This is
because, at this stage, the objective function has the highest value.
3. Plateau/flat local maximum: It is a flat region of state space where neighbouring
states have the same value.
4. Ridge: It is a region that is higher than its neighbours but itself has a slope. It is a
special kind of local maximum.
5. Current state: The region of state space diagram where we are currently present
during the search.
6. Shoulder: It is a plateau that has an uphill edge.
Limitations of Hill Climbing
• Local Maximum:
At a local maximum all neighbouring states have a value that is worse than the
current state. Since hill-climbing uses a greedy approach, it will not move to the worse
state and terminate itself. The process will end even though a better solution may exist.
• Plateau:
On the plateau, all neighbours have the same value. Hence, it is not possible to
select the best direction.
• Ridge:
Any point on a ridge can look like a peak because movement in all possible
directions is downward. Hence the algorithm stops when it reaches this state.
To overcome from Problems
• To overcome the local maximum problem: Utilize the backtracking
technique. Maintain a list of visited states. If the search reaches an undesirable
state, it can backtrack to the previous configuration and explore a new path.
• To overcome plateaus: Make a big jump. Randomly select a state far away from
the current state. Chances are that we will land in a non-plateau region.
• To overcome Ridge: In this kind of obstacle, use two or more rules before
testing. It implies moving in several directions at once.
Adversarial Search
• It is a competitive activity which involves n players and played according to some
rules.
• It is a game environment where agents (players) are surrounded by a competitive
environment.
• A goal is given to agents. These agents compete with one another and try to
defeat one another to win the game.
• Game playing means where human intelligence and logic factor is used.
• Examples:
• Chess, Checker, Tic-Tac-Toe, etc.
• This search is based on game theory.
• 2 players, play, to complete one has to win.
Techniques to get best result
• Pruning:
• Which allows ignoring unwanted portions of search tree which does not
effect on result.
• Heuristic Evaluation Function:
• Approximate the cost value at each to level before we reach the goal node.
Elements of Game Playing
• S0 : Initial state where game starts.
• Player (s) : Defines which player having the current turn to make a
move in state.
• Actions (s) : Set of legal moves to be used in a state.
• Result (s, a) : Defines result of a move, transition model.
• Terminal Test (s) : Defines game has ended and returns TRUE.
• Utility (s, p) : Final value with which the game has ended.
• (-1) : If player loses.
• (+1) : If player wins.
• (0) : If there is a draw between players.
Game Tree with Tic-Tac-Toe
Elements in Tic-Tac-Toe

• INITIAL STATE (S0): The top node in the game-tree represents the initial state in the tree
and shows all the possible choice to pick out one.
• PLAYER (s): There are two players, MAX and MIN. MAX begins the game by picking one
best move and place X in the empty square box.
• ACTIONS (s): Both the players can make moves in the empty boxes chance by chance.
• RESULT (s, a): The moves made by MIN and MAX will decide the outcome of the game.
• TERMINAL-TEST(s): When all the empty boxes will be filled, it will be the terminating
state of the game.
• UTILITY: At the end, we will get to know who wins: MAX or MIN, and accordingly, the
price will be given to them.
Mini-Max Algorithm
• This algorithm is used to make decisions according to the status of the game.
• It delivers optimal move for the player.
• This algorithm widely used to play AI games: tic-tac-toe, chess, etc.
• Two players are in the game, one called ‘MAX’ and another one is ‘MIN’.
• Basic goal is to minimize enemy profit and maximize self profit.
• The Mini-Max algorithm conducts a depth-first search to explore the complete
game tree.
• Then proceeds down to the leaf node of the tree, then backtracks the tree using
recursive calls.
State Space representation of Tic-Tac-Toe
• 1. Initial State:
• An empty 3x3 matrix.
• 2. Intermediate State:
• Any arrangement of a 3x3 matrix obtained after applying valid rules on the
current state.
• 3. Final State:
• Same letters in a complete row or complete column, or complete diagonal
wins the game.
• 4. Rules:
• The players will get turns one after the other; they can mark their letters on
the empty cell.
Tic-Tac-Toe game to understand Mini-Max Algorithm
Tic-Tac-Toe Working
• During an intermediate state (A) in the game, Zoro has three choices
to pick from, i.e., state ‘B,’ ‘C,’ and ‘D.’
• Picking state ‘B’ will lead to the victory of Zoro, while
choosing ‘C’ and ‘D’ will result in further rounds of the game.
• In case Zoro attempts for the ‘C’ and ‘D’ state.
• Sanji will play accordingly, and so on.
• There will be scenarios when both win dis-jointly as Sanji wins in
state ‘H,’ and Zoro wins in state ‘J.’
• The other leaf nodes will result in a draw.
MIN MAX
MIN MAX
Properties of Mini-Max Algorithm
• It considered as complete if the game tree size is finite.
• It is consider optimal if it played against optimal number of opponents.
• Time complexity can be indicated as O(b^m).
• Space complexity can be indicated as O(b^m) using DFS exploration approach.
• Exact solution is infeasible in most of the game.
Alpha-Beta Pruning
• Alpha: Alpha is the best choice or the highest value that we have found at any
instance along the path of Maximizer. The initial value for alpha is – ∞.
• Beta: Beta is the best choice or the lowest value that we have found at any
instance along the path of Minimizer. The initial value for alpha is + ∞.
• The condition for Alpha-beta Pruning is that α >= β.
• Each node has to keep track of its alpha and beta values. Alpha can be updated
only when it’s MAX’s turn and, similarly, beta can be updated only when it’s MIN’s
chance.
• MAX will update only alpha values and MIN player will update only beta values.
• The node values will be passed to upper nodes instead of values of alpha and
beta during go into reverse of tree.
• Alpha and Beta values only be passed to child nodes.
Working of Alpha Beta
In the next step, algorithm traverse the next successor of Node B which is node E,
and the values of α= -∞, and β= 3 will also be passed.
• Step 8: C now returns the value of 1 to A here the best value for A is max (3, 1) =
3. Following is the final game tree which is the showing the nodes which are
computed and nodes which has never computed. Hence the optimal value for the
maximizer is 3 for this example.
Alpha Beta
THANK YOU!!!

You might also like