2
2
2. Scrutinize and analyse the problem carefully, for some features may have a central effect
on the chosen method of solution.
3. Segregate and represent the background knowledge needed in the solution of the
problem.
4. Choose the best solving techniques for the problem and apply to solve a solution.
Search and Space
Search
● Search is fundamental to the problem-solving process.
● Search is a general mechanism that can be used when a more direct method is not
known.
● Search provides the framework into which more direct methods for solving subparts
of a problem can be embedded
● Very large number of AI problems are formulated as search problems
Space
● A problem space is represented by a directed graph,
○ Where nodes represent search state
○ Paths represent the operators applied to change the state
● Problem space is logically and programmatically represent as a tree.
DEFINING PROBLEM AS A STATE SPACE SEARCH
The representation of games leads to a state space representation and it is common for
well-organized games with some structure
This representation allows for the formal definition of a problem that needs the movement
from a set of initial positions to one of a set of target positions.
3 6 8 4
8 2 7 7 6 5
{(0,0), (4,0), (0,3), (4,3), (3,0), (1,3), (0,2), (2,0), (3,3), (3,0)...}
Water Jar Problem
A = all possible actions
1. (four, three) if four < 4 (4, three) fill four from tap
2. (four, three) if three< 3 (four, 3) fill three from tap
3. (four, three) If four > 0 (0, three) empty four into drain
4. (four, three) if three > 0 (four, 0) empty three into drain
5. (four, three) if four + three<4 (four + three, 0) empty three into four
6. (four, three) if four + three<3 (0, four + three) empty four into three
7. (0, three) If three > 0 (three, 0) empty three into four
8. (four, 0) if four > 0 (0, four) empty four into three
9. (0, 2) (2, 0) empty three into four
10. (2, 0) (0, 2) empty four into three
11. (four, three) if four < 4 (4, three-diff) pour diff, 4-four, into four from three
12. (three, four) if three < 3 (four-diff, 3) pour diff, 3-three, into three from four
Solution: Water Jar Problem
Solution: Water Jar Problem
4 0
1 3
1 0
0 1
4 1
2 3
Missionaries and Cannibals
The Missionaries and Cannibals problem illustrates the use of state space search for
planning under constraints:
● Three missionaries and three cannibals wish to cross a river using a two person boat.
● If at any time the cannibals outnumber the missionaries on either side of the river,
they will eat the missionaries.
● How can a sequence of boat trips be performed that will get everyone to the other
side of the river without any missionaries being eaten?
Missionaries and Cannibals
State representation:
M = {S, A, A(s), Result(S,a), Cost(S,a)}
Start state: (3,3) end state: (0,0) on LHS
A: All set of Actions = {MM,MC,CC,M,C}
MM: two Missionaries cross the river
MM
MC
CC
C
Possible Actions
MM
MC
CC
C
Possible
Actions
MM
MC
CC
C
Problem and State Space Search
● Search proceeds with different types of ‘search control strategies’
Examples of Depth First Search (DFS), Breadth First Search (BFS), Greedy Search, A* Search, AO* Search, Hill Climbing
Algorithms Branch and Bound Algorithm, Best first search, Heuristic DFS
Syllabus
Search Techniques:
● Problem solving agents
● Uninformed search strategies:
○ Breadth first search
○ Depth first search
○ Uniform cost search
● Informed search strategies:
○ Greedy best-first search,
○ A* search,
○ Hill climbing search
● Adversarial Search
○ Introduction to the Domain of a game
○ Optimal decisions in games
○ Minimax algorithm
○ Alpha-beta pruning.
Madhura Vyawahare
Search Algorithm Terminologies
● Problem solving agents are goal-based agents. They decide what to do by finding sequences of
actions that lead to desirable states.
● Search: Searching is a step by step procedure to solve a search problem in a given search space.
● A search problem can have three main factors:
○ Search Space: Search space represents a set of possible solutions, which a system may have
○ Start State: It is a state from where agent begins the search
○ Goal test: It is a function which observe the current state and returns whether the goal state is achieved
or not.
● Search tree: A tree representation of search problem is called Search tree. The root of the search
tree initial state.
● State space and search tree
● Graph traversal: is visiting and exploring a node of the graph for processing
Madhura Vyawahare
Search Algorithm Terminologies
● Actions: It gives the description of all the available actions to the agent.
● Transition model: A description of what each action do, can be represented as a transition
model.
● Solution: It is an action sequence which leads from the start node to the goal node.
● Optimal Solution: If a solution has the lowest cost among all solutions.
Madhura Vyawahare
Properties of Search Algorithms
Following are the four essential properties of search algorithms to compare the efficiency
of these algorithms:
● BFS algorithm starts searching from the root node of the tree and expands all
successor node at the current level before moving to nodes of next level.
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Breadth-First Search
Advantages:
● BFS will provide a solution if any solution exists.
● If there are more than one solutions for a given problem, then BFS will provide the minimal solution
which requires the least number of steps.
Disadvantages:
● It requires lots of memory since each level of the tree must be saved into memory to expand the next
level.
● BFS needs lots of time if the solution is far away from the root node.
Applications of BFS:
1. Crawlers in Search Engines
2. GPS Navigation systems
3. Find the Shortest Path & Minimum Spanning Tree for an unweighted graph
4. Broadcasting
5. Peer to Peer Networking
Madhura Vyawahare
Breadth-First Search
Time Complexity:
● Time Complexity of BFS algorithm can be obtained by the number of nodes traversed
in BFS until the shallowest Node.
● Where the d = depth of shallowest solution and b is a nodes at every state.
● T (b) = 1+b2+b3+.......+ bd = O (bd)
Space Complexity:
● Space complexity of BFS algorithm is given by the Memory size of frontier which is
O(bd).
Completeness: BFS is complete, which means if the shallowest goal node is at some finite
depth, then BFS will find a solution.
Optimality: BFS is optimal if path cost is a non-decreasing function of the depth of the
node.
Madhura Vyawahare
Breadth-First Search
Madhura Vyawahare
Breadth-First Search
Madhura Vyawahare
Depth-First Search
● Depth-first search is a recursive algorithm for traversing a tree or graph data structure.
● It is called the depth-first search because it starts from the root node and follows each
path to its greatest depth node before moving to the next path.
Madhura Vyawahare
Depth-First Search
Advantage:
● DFS requires very less memory as it only needs to store a stack of the nodes on the
path from root node to the current node.
● It takes less time to reach to the goal node than BFS algorithm (if it traverses in the
right path).
Disadvantage:
● There is the possibility that many states keep re-occurring, and there is no guarantee
of finding the solution.
● DFS algorithm goes for deep down searching and sometime it may go to the infinite
loop.
Madhura Vyawahare
Depth-First Search
● Completeness: DFS search algorithm is complete within finite state space as it will
expand every node within a limited search tree.
● Time Complexity: Time complexity of DFS will be equivalent to the node traversed
by the algorithm.
T(n)= 1+ n2+ n3 +.........+ nm=O(dm) Where, m= maximum depth of any node and this
can be much larger than d (Shallowest solution depth)
● Space Complexity: DFS algorithm needs to store only single path from the root
node, hence space complexity of DFS is equivalent to the size of the fringe set, which
is O(dm).
● Optimal: DFS search algorithm is non-optimal, as it may generate a large number of
steps or high cost to reach to the goal node.
Madhura Vyawahare
DFS Algorithm
● Push root node in the stack
● Till stack is not empty
○ Visit node
■ If node is goal : stop
■ Else Push all children of node in stack
○ If all successors are explored then pop node and backtrack
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Depth-Limited Search Algorithm
● A depth-limited search algorithm is similar to depth-first search with a
predetermined limit.
● Solve the drawback of the infinite path in the Depth-first search.
● 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:
○ 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.
Madhura Vyawahare
Madhura Vyawahare
Depth-Limited Search Algorithm
Advantages:
Disadvantages:
Madhura Vyawahare
Depth-Limited Search Algorithm
● Completeness: DLS search algorithm is complete if the solution is above the
depth-limit.
● Time Complexity: Time complexity of DLS algorithm is O(bl).
● Space Complexity: Space complexity of DLS algorithm is O(b*l) .
● Optimal: Depth-limited search can be viewed as a special case of DFS, and
it is also not optimal even if l>d.
Madhura Vyawahare
Uniform Cost Search
● Used for traversing a weighted tree or graph
● The primary goal of the uniform-cost search is to find a path to the
goal node which has the lowest cumulative cost
● Uniform-cost search expands nodes according to their path costs form
the root node.
● Implemented by the priority queue
● It gives maximum priority to the lowest cumulative cost.
● Uniform cost search is equivalent to BFS algorithm if the path cost of
all edges is the same.
Madhura Vyawahare
Madhura Vyawahare
Uniform Cost Search
Advantages:
○ Uniform cost search is complete and optimal because at every state the path with the
least cost is chosen.
Disadvantages:
○ It does not care about the number of steps involved in searching and only concerned
about path cost. Due to which this algorithm may be stuck in an infinite loop.
Madhura Vyawahare
Madhura Vyawahare
Uninformed vs. Informed Search
Uninformed Informed
Madhura Vyawahare
8 Puzzle Problem Without Heuristic
1 2 3 1 2 3
4 6 4 5 6
7 5 8 7 8
Madhura Vyawahare
Heuristic in AI
● Heuristic value: Information which helps in choosing option to get optimal
solution
● Blind search results in NP due to large complexity (exponential)
● To have a polynomial solution heuristic values are used
● Blind: optimal solution but large complexity
● Heuristic: good solution but optimality is not guaranteed.
● Methods:
○ Euclidean distance
○ Manhattan distance
Madhura Vyawahare
8 Puzzle Problem with Heuristic
1 2 3 1 2 3
4 6 4 5 6
7 5 8 7 8
Madhura Vyawahare
Greedy Best-First Search
● Greedy best-first search algorithm always selects the path which appears best at
that moment.
● It is the combination of depth-first search and breadth-first search algorithms.
● It uses the heuristic function and search.
● Best-first search allows us to take the advantages of both algorithms.
● With the help of best-first search, at each step, we can choose the most promising
node.
● It is implemented by the priority queue.
● We expand the node which is closest to the goal node and the closest cost is
estimated by heuristic function:
f(n) = h(n).
Were, h(n)= estimated cost from node n to the goal.
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Greedy Best-First Search Algorithm
Best first search algorithm:
1. Place the starting node into the OPEN list.
2. If the OPEN list is empty, Stop and return failure.
3. Remove the node n, from the OPEN list which has the lowest value of h(n), and
places it in the CLOSED list.
4. Expand the node n, and generate the successors of node n.
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.
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.
7. Return to Step 2.
Madhura Vyawahare
Greedy Best-First Search Algorithm
Advantages:
o Best first search can switch between BFS and DFS by gaining the advantages of both the
algorithms.
Disadvantages:
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.
Madhura Vyawahare
A* Search Algorithm
● A* search is the most commonly known form of best-first search.
● It uses heuristic function h(n), and cost to reach the node n from the start state g(n).
● It has combined features of UCS and greedy best-first search, by which it solve the
problem efficiently.
● A* search algorithm finds the shortest path through the search space using the
heuristic function.
● This search algorithm expands less search tree and provides optimal result faster.
f(n) = g(n)+h(n)
● In A* search algorithm, we use search heuristic as well as the cost to reach the node.
Madhura Vyawahare
A* Search Algorithm
Madhura Vyawahare
Madhura Vyawahare
Example
Madhura Vyawahare
A* Search Algorithm
Advantages:
● A* search algorithm is the best algorithm than other search algorithms.
● A* search algorithm is optimal and complete.
● This algorithm can solve very complex problems.
Disadvantages:
● It does not always produce the shortest path as it mostly based on heuristics and
approximation. (underestimation and overestimation of heuristic)
● A* search algorithm has some complexity issues.
● Memory requirement is high as it keeps all generated nodes in the memory
● Not practical for various large-scale problems.
Madhura Vyawahare
A* Search Algorithm
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: A* is admissible in underestimation. An admissible heuristic is optimistic in nature.
Consistency: 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)
Madhura Vyawahare
Admissibility of A*
Madhura Vyawahare
A* Search Algorithm
Step1: Place the starting node in the OPEN list.
Step 2: Check if the OPEN list is empty or not, if the list is empty then return failure and stops.
Step 3: Select the node from the OPEN list which has the smallest value of evaluation function
(g+h), if node n is goal node then return success and stop, otherwise
Step 4: Expand node n and generate all of its successors, and put n into the closed list. For
each successor n', check whether n' is already in the OPEN or CLOSED list, if not then
compute evaluation function for n' and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it should be attached to the back
pointer which reflects the lowest g(n') value.
Step 6: Return to Step 2.
Madhura Vyawahare
Local Search Algorithm and Optimization Problems
Madhura Vyawahare
Local Search Algorithm and Optimization Problems
● Not systematic
Madhura Vyawahare
Local Search Algorithm and Optimization Problems
● Beam Search
● Hill Climbing
● Simulated Annealing
● Genetic Algorithms
Madhura Vyawahare
Hill Climbing Search
● Local search algorithm
● Follows Greedy approach
○ Continuously moves in the direction of increasing elevation/value to find the
peak of the mountain or best solution to the problem.
● Looks to its good immediate neighbor state and not beyond that
● No Backtracking is possible
● It terminates when it reaches a peak value where no neighbor has a higher value
● Used when a good heuristic and large set of inputs are available
● No need to maintain and handle the search tree or graph as it only keeps a single
current state
● Efficient in terms of space complexity
Madhura Vyawahare
Limitations of Hill Climbing
● Local Maximum: Local maximum is a state which is better than its neighbor states,
but there is also another state which is higher than it.
○ Example C, C++ vs Java
● Global Maximum: Global maximum is the best possible state of state space
landscape. It has the highest value of objective function
● Flat local maximum: It is a flat space in the landscape where all the neighbor states of
current states have the same value.
● Shoulder: It is a plateau region which has an uphill edge
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Algorithm for Simple Hill Climbing
Step 1: Evaluate the initial state, if it is goal state then return success and Stop.
Step 2: Loop Until a solution is found or there is no new operator left to apply.
Step 3: Select and apply an operator to the current state.
Step 4: Check new state:
a. If it is goal state, then return success and quit.
b. Else if it is better than the current state then assign new state as a current state.
c. Else if not better than the current state, then return to step 2.
Step 5: Exit.
Madhura Vyawahare
Adversarial Search
● Agents are surrounded by competitive environment
● Adversarial search is a search, where we examine the problem which arises when
we try to plan ahead of the world and other agents are planning against us.
● In previous topics, we have studied the search strategies which are only associated
with a single agent that aims to find the solution which often expressed in the form of
a sequence of actions.
● But, there might be some situations where more than one agent is searching for the
solution in the same search space, and this situation usually occurs in game playing.
● The environment with more than one agent is termed as multi-agent environment, in
which each agent is an opponent of other agent and playing against each other.
● Each agent needs to consider the action of other agent and effect of that action on
their performance.
Madhura Vyawahare
Introduction to Domain of Games
● Perfect information: A game with the perfect information is that in which agents can look into
the complete board. Agents have all the information about the game, and they can see each
other moves also. Examples are Chess, Checkers, Go, etc.
● Imperfect information: If in a game agents do not have all information about the game and
not aware with what's going on, such type of games are called the game with imperfect
information, such as Cards, Battleship, blind, Bridge, etc.
● Deterministic games: Deterministic games are those games which follow a strict pattern and set
of rules for the games, and there is no randomness associated with them. Examples are bridge,
chess, Checkers, Go, tic-tac-toe, etc.
● Non-deterministic games: Non-deterministic are those games which have various
unpredictable events and has a factor of chance or luck. This factor of chance or luck is
introduced by either dice or cards. These are random, and each action response is not fixed. Such
games are also called as stochastic games.
Example: Ludo, Backgammon, Monopoly, Poker, etc.
Madhura Vyawahare
Formalization of the Problem
A game can be defined as a type of search in AI which can be formalized of the
following elements:
● Initial state: It specifies how the game is set up at the start.
● Terminal-Test(s): Terminal test is true if the game is over, else it is false at any case.
The state where the game ends is called terminal states.
● Action(s): It returns the set of legal moves in state space.
● Result(s, a): It is the transition model, which specifies the result of moves in the state
space.
● Player(s): It specifies which player has moved in the state space.
● Utility(s, p): A utility function gives the final numeric value for a game that ends in
terminal states s for player p. It is also called payoff function. Outcomes are a win,
loss, or draw and its payoff values or utility values are +1, -1, and 0.
Madhura Vyawahare
Game Tree
Madhura Vyawahare
Min-Max Algorithm
● Specialized search algorithm that returns optimal sequence of moves for a player in zero sum
game
● Mini-max algorithm is a recursive and backtracking algorithm which is used in
decision-making and game theory.
● It provides an optimal move for the player assuming that opponent is also playing optimally.
● Mini-Max algorithm uses recursion to search through the game-tree.
● Min-Max algorithm is mostly used for game playing in AI. Such as Chess, Checkers, tic-tac-toe,
go, and various two-players game.
● This Algorithm computes the minimax decision for the current state.
● In this algorithm two players play the game, one is called MAX and other is called MIN.
● Both the players fight it as the opponent player gets the minimum benefit while they get the
maximum benefit.
Madhura Vyawahare
Min-Max Algorithm Assumptions
● Simple DFS is used for exploration of complete Game Tree
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Min-Max Algorithm
Advantages:
Limitations:
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Alpha beta pruning
Madhura Vyawahare
Alpha-beta Pruning
● It is a modified version of the minimax algorithm.
● It is an optimization technique for the minimax algorithm.
● Min-max search algorithm: the number of game states it has to examine are
exponential in depth of the tree.
● This is a technique by which without checking each node of the game tree we can
compute the correct minimax decision, and this technique is called pruning.
● This involves two threshold parameter Alpha and beta for future expansion, so it is
called alpha-beta pruning.
● It is also called as Alpha-Beta Algorithm.
Madhura Vyawahare
Alpha-beta Pruning
Madhura Vyawahare
Madhura Vyawahare
Step 2: At Node D, the value of α will be calculated as its
turn for Max.
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
A
B C
D E F G
H I J K L M N O
4 48 15 25 35 23 19 -5 -25 11 -46 7 45 -9 48 10
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Madhura Vyawahare
Max= 3
𝛃= 2
MIN = 3
Max= 3
𝞪= 7 𝞪= 2
MIN = 7
MIN = 3 𝛃= 2 MIN = 2 𝛃= 1
Madhura Vyawahare
Max= 3
MIN = 3 MIN = 1
Max= 3
Max= 8 Max= 1 𝞪= 4
MIN = -1 MIN = 1
MIN = 3 MIN = -5 𝛃= -4 MIN = 4
MIN = 8
Madhura Vyawahare
Madhura Vyawahare
Max= 10
MIN = 2
MIN = 10
MIN = 4
𝞪= 15
Max= 15
Max= 7 Max= 2 Max= 7
Max= 4 Max= 10 Max= 11
𝞪= 9
Madhura Vyawahare
A
B C
D E F G
H I J K L M N O
2 8 3 1 6 9 8 9 3 10 2 14 16 8 15 18
Madhura Vyawahare
Madhura Vyawahare