2. B. Tech Unit II Problem Solving
2. B. Tech Unit II Problem Solving
Problem Solving
By
Dr. Mrudul Dixit
Problem Solving and Constraint Satisfaction Problems
1. Stuart Russell, Peter Norvig, 'Artificial Intelligence”, A Modern Approach ', Pearson Education/Prentice Hall of India,
(3rd Edition), (2010)
2. Elaine Rich, Kevin Knight and Shivshankar Nair, 'Artificial Intelligence', Tata McGraw Hill, (3rd
Edition), (2009)
Problem-solving agent
• Formulate goal:
• be in Bucharest
• Formulate problem:
– states: various cities
– actions: drive between cities
• Find solution:
– sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest
Well-defined problems and solutions
Initial State the initial state for our agent in Romania might be
described as In(Arad)
from the state In(Arad), the successor function for the Romania
Actions problem would return { (G o(Sibzu),I n(Sibiu)), (Go(T imisoara), In(
Tzmisoara)), (Go(Zerznd),I n(Zerind)))
State Space is the map of Romania with path as roads
Sudoku
Problem Solving
Approaches
• A toy problem is intended to illustrate or exercise various problem-solving methods.
• It can be given a concise, exact description.
• This means that it can be used easily by different researchers to compare the
performance of algorithms.
• Example Vaccum agent:
• This can be formulated as a problem as follows:
• States: The agent is in one of two locations, each of which might or might not
contain dirt. Thus there are 2 x 2^2 = 8 possible world states.
• Initial state: Any state can be designated as the initial state.
• Successor function: This generates the legal states that result from trying the three
actions (Left, Right, and Suck).
• Goal test: This checks whether all the squares are clean.
• Path cost: Each step costs 1, so the path cost is the number of steps in the path.
The state space for the vacuum world. Arcs denote actions: L = Left, R = Right, S =
Suck.
● State Space Search : Number of states in which the problem can go till it reaches
the goal state
● s : {S,A, Actions(s), Result(s,a), Cost(s,a)}
● S: various states namely start, goal and intermediate
● A: Set of all possible actions : up, down, left, right movement in puzzle problem
● Precise representation of 8 puzzle problem: 3 * 3 board has 9 spaces with 8
numbers on tiles and 1 is empty space
● As the intermediate state are created they will be compared with goal state and if
they match search is stopped
● Action(s) : is selected action out of available up down left or right
● Result(s,a): Resultant state after action is completed
● Cost(s,a): cost of moving to the next state
8 Puzzle problem
The 8-puzzle, consists of a 3 x 3 board with eight numbered tiles and a blank space.
A tile adjacent to the blank space can slide into the space. The object is to reach a
specified goal state, such as the one shown on the right of the figure.
The standard formulation is as follows:
• States: A state description specifies the location of each of the eight tiles and the
blank in one of the nine squares.
• Initial state: Any state can be designated as the initial state. Any given goal
can be reached from exactly half of the possible initial states
• Successor function: This generates the legal states that result from trying the
four actions (blank moves Left, Right, Up, or Down).
• Goal test: This checks whether the state matches the goal configuration
• Path cost: Each step costs 1, so the path cost is the number of steps in the path.
States: locations of tiles
Actions/operators: move blank left, right,
up, down
goal test: goal state (given)
path cost: 1 per move
• The 8-puzzle belongs to the family of sliding-block puzzles, which are often used
as test problems for new search algorithms in AI.
• This general class is known to be NP-complete ie. Nondeterministic Polynomial
time (is a complexity class used to classify decision problems.)
• The State Space of the (n^2 -1) problem has only half of these states are
reachable from any given state
• The 8-puzzle has 9!/2 = 181,440 reachable states ( total States are 9!)and is
easily solved.
• The 15-puzzle (on a 4 x 4 board) has around 1.3 trillion states, and random
instances can be solved optimally in a few milliseconds by the best search
algorithms.
• The 24-puzzle (on a 5 x 5 board) has around 10^25 states, and random instances
are still quite difficult to solve optimally with current machines and algorithms.
Initial State Final State
1 2 3 1 2 3
4 8 - 4 5 6
7 6 5 7 8 -
1 2 3 1 2 3 1 2 3 1 2 3
4 8 5 4 8 5 4 - 5 4 5 -
7 6 - 7 - 6 7 8 6 7 8 6
1 2 3 1 2 3
4 - 8 4 5 6
7 6 5 7 8 -
1 2 3 1 2 3 1 2 3 1 2 3
4 8 - 4 8 5 4 8 5 4 - 5
7 6 5 7 6 - 7 - 6 7 8 6
4 5 6 4 5 -
7 8 - 7 8 6
Initial State Final State
1 2 3 1 2 3
4 - 8 4 5 6
7 6 5 7 8 -
1 2 3 1 2 3 1 2 3 1 2 3
4 6 8 4 6 8 4 6 - 4 - 6
7 - 5 7 5 - 7 5 8 7 5 8
4 5 6 4 5 6
7 8 - 7 - 8
8 Queens Problem
• The goal of the 8-queens problem is to place
eight queens on a chessboard (8 by 8) such
that no queen attacks any other.
• Almost a solution of the 8-queens problem
but the queen in the first column is on the
same diagonal as the Queen in the last column.
Q1
Q1
Q2
Q2
Q4 Q3
Q4
Q3
Place the queens such that there is no queen in that row or column or in
diagonal
Q1
Q2
Q3
Q5
Q4
Q6
Q7
Q8
Q1
Q3
Q2
Q4
Q5
Q6
Q7
Q8
12 Unique Solutions
8-Queens Problem
8 Queens Problem
8 (X,Y | X+Y <=3^ (0,X+Y) {Pour all water from 4-gallon jug
X>0) into 3-gallon jug}
http://aimaterials.blogspot.com/p/blog-page_18.html
Initialization: Iteration 1: Iteration 4:
Start State: (0,0) Current State: (X,3) Current State : (4,2)
Apply Rule 2: Apply Rule 7: Apply Rule 3:
(X,Y | Y<3) -> (X,Y | X+Y<=4 ^Y>0) (X,Y | X>0)
(X,3) (X+Y,0) (0,Y)
{Fill 3-gallon jug} {Pour all water from 3-gallon jug {Empty 4-gallon jug}
Now the state is (X,3) into 4-gallon jug} Now state is (0,2)
Now the state is (3,0)
Iteration 5:
Current State : (0,2)
Iteration 2: Apply Rule 9:
Current State : (3,0) (0,2)
Apply Rule 2: (2,0)
(X,Y | Y<3) -> {Pour 2 gallon water from 3 gallon
(3,3) jug into 4 gallon jug}
{Fill 3-gallon jug} Now the state is (2,0)
Now the state is (3,3)
Goal Achieved.
Iteration 3:
Current State:(3,3)
Apply Rule 5:
(X,Y | X+Y>=4 ^ Y>0)
(4,Y-(4-X))
{Pour water from 3-gallon jug into
4-gallon jug until 4-gallon jug is full}
Now the state is (4,2)
State Space Tree:
https://www.eecis.udel.edu/~mccoy/courses/cisc4-681.10f/lec-materials/handouts/search-water-jug-handout.pdf
Real World Problems: Route Finding Problem
• Route-finding algorithms are used in a variety of applications, such as routing in
computer networks, military operations planning, and airline travel planning
systems.
• These problems are typically complex to specify.
• Example of an airline travel problem :
• States: Each is represented by a location (e.g., an airport) and the current time.
• Initial state: This is specified by the problem.
• Successor function: This returns the states resulting from taking any scheduled
flight (perhaps further specified by seat class and location), leaving later than the
current time plus the within-airport transit time, from the current airport to another.
• Goal test: Are we at the destination by some prespecified time
• Path cost: This depends on monetary cost, waiting time, flight time, customs and
immigration procedures, seat quality, time of day, type of airplane, frequent-flyer
mileage awards, and so on.
Real World Problems: Route Finding Problem
• Commercial travel advice systems use a problem formulation of this kind, with
many additional complications to handle the fare structures that airlines impose.
• Any seasoned traveller knows, however, that not all air travel goes according to
plan.
• A really good system should include contingency plans-such as backup
reservations on alternate flights to the extent that these are justified by the cost and
likelihood of failure of the original plan.
Real World Problems: Travelling Salesperson Problem
• Search techniques used are an explicit search tree that is generated by the initial
state and the successor function that together define the state space.
• A search graph will be used rather than a search tree, when the same state can be
reached from multiple paths
• The root of the search tree is a search node corresponding to the initial state
•
Search Solutions
• Example : finding a route from Arad to Bucharest.
• Initial State :In Arad
• The first step is to test whether this is a goal state.
• Its not so not a goal state, we need to consider some other states.
• This is done by expanding the current state i.e. applying the successor
function to the current state thereby generating a new set of states.
• In this case, we get three new states: In(Sibiu), In(Timisoara), and In(Zerind)
• Every time we have to check whether its a goal state or not, if not ,expand the
current state
• The choice of which state to expand is determined by the search strategy by
using general tree-search algorithm
Search Solutions
• It is important to distinguish between the state space and the search tree.
• For the route finding problem, there are only 20 states in the state space, one for
each city.
• But there are an infinite number of paths in this state space, so the search tree
has an infinite number of nodes.
• For example, the three paths Arad-Sibiu, Arad-Sibiu-Arad, Arad-Sibiu-Arad-Sibiu
are the first three of an infinite sequence of paths.
• A good search algorithm avoids such repeated paths
• A node is a data structure with five components:
• STATE: the state in the state space to which the node corresponds;
• PARENT-NODE: the node in the search tree that generated this node;
• ACTION: the action that was applied to the parent to generate the node;
• PATH-COST: the cost, traditionally denoted by g ( n ) , of the path from the initial
state to the node, as indicated by the parent pointers; and
• DEPTH: the number of steps along the path from the initial state.
• A node is a bookkeeping 'data structure used to represent the search tree.
• A state corresponds to a configuration of the world
Search Solutions
• A node is a bookkeeping 'data structure used to represent the search tree.
• A state corresponds to a configuration of the world
• Nodes are on particular paths, as defined by PARENT-NODE pointers, whereas
states are not.
• Two different nodes can contain the same world state, if that state is generated via
two different search paths.
• The collection of nodes that have been generated but not yet expanded-this
collection is called the fringe.
• Each element of the fringe is a leaf node i.e. a node with no successors in the
tree.
• The collection of nodes is implemented as a queue
Search Solutions
• The operations on a queue are as follows:
• Make Queue (Elements ) Creates a queue with the given element(s).
• Empty (queue) returns true only if there are no more elements in the queue.
• First(queue) returns the first element of the queue.
• Remove First (queue): returns first (queue) and removes it from the queue.
• Insert (Element, queue) inserts an element into the queue and returns the resulting
• queue. (
• Insert ALL (Elements, Queue) inserts a set of elements into the queue and returns
the resulting queue
•
Performance measure for Problem Solving
• Linear Search :
• https://www.geeksforgeeks.org/linear-search/
• https://www.hackerearth.com/practice/algorithms/searching/linear-search/tut
orial/#:~:text=Linear%20search%20is%20used%20on,all%20the%20element
s%20with%20value%20.
• Binary Search :
• https://www.geeksforgeeks.org/binary-search/
• https://www.tutorialspoint.com/data_structures_algorithms/binary_search_al
gorithm.htm#:~:text=Binary%20search%20looks%20for%20a,left%20of%20
the%20middle%20item.
Uninformed Search or Blind Search Strategies
• Breadth-first search:
• It is a simple strategy in which the root node is expanded first, then all the
successors of the root node are expanded next, then their successors, and so
on.
• All the nodes are expanded at a given depth in the search tree before any nodes at
the next level are expanded.
• Breadth-first search is implemented by calling TREE-SEARCH with an empty
fringe that is a first-in-first-out (FIFO) queue, assuring that the nodes that are
visited first will be expanded first.
• The FIFO queue puts all newly generated successors at the end of the queue,
which means that shallow nodes are expanded before deeper nodes.
•
Uninformed Search or Blind Search Strategies
3
2
4 5 6 7
https://www.google.com/search?q=breadth+first+search+algorithm&source=lnms&tbm=vid&sa=X&ved=2ahUKEw
jVssmggLf7AhXTcGwGHZpeCcoQ_AUoAnoECAEQBA&biw=1443&bih=742&dpr=0.9#fpstate=ive&vld=cid:04112
a3f,vid:QRq6p9s8NVg
http://artificialintelligence-notes.blogspot.com/2010/07/8-puzzle-problem.htm
https://www.youtube.com/watch?v=_CrEYrcImv0
https://www.tutorialspoint.com/data_structures_algorithms/breadth_first_traversal.htm#:~:text=Breadth%20First%2
0Search%20(BFS)%20algorithm,and%20G%20lastly%20to%20D.
https://www.youtube.com/watch?v=_CrEYrcImv0
http://artificialintelligence-notes.blogspot.com/2010/07/8-puzzle-problem.htm
Represent using BFS
Initial State Final State
1 2 3 1 2 3
4 6
4 5 6
7 5 8
7 8 -
1 2 3 2 3 1 2 3
4 6 1 6 7 4 6
7 5 8 7 5 8 5 8
Applications Of BFS
• Depth-first search :
• Depth-first search always expands the deepest node in the current fringe of
the search tree
• The search proceeds immediately to the deepest level of the search tree, where the
nodes have no successors.
• As those nodes are expanded, they are dropped from the fringe, so then the
search "backs up" to the next shallowest node that still has unexplored successors.
• It can be implemented by
– TREE-SEARCH with a last-in-first-out (LIFO) queue, also known as a stack.
– depth-first search with a recursive function that calls itself on each of its children
in turn.
• Depth-first search has very modest memory requirements
• It needs to store only a single path from the root to a leaf node, along with the
remaining unexpanded sibling nodes for each node on the path
• Once a node has been expanded, it can be removed from memory as soon as all
its descendants have been fully explored
Depth-first search
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
Depth-first search
• Depth-first search :
B G H
C F I L
E J K
D
Properties of Depth-first search
DFS explores as far as possible along each branch before backtracking. It uses a stack (or
recursion) to manage traversal.
Algorithm:
1. Input:
○ Graph G(V,E) : V = set of vertices, E = set of edges.
○ Starting node sss.
2. Initialize:
○ Create a stack S and push the starting node s : S=[s]
○ Create a list or set to track visited nodes: Visited={s}
3. Traversal:
○ While S is not empty:
1. Pop a node uuu from S .
2. Process uuu (e.g., print or store it in the traversal result).
3. For each unvisited neighbor vvv of uuu (in reverse order to maintain order):
■ Mark v as visited: Visited.add(v)
■ Push v onto S S.append(v)
4. End:
○ The traversal ends when S becomes empty.
Depth-first search Algorithm
A Starting Node: A
/ | \
B C D Steps (using a stack):
/ \ \
E F G 1. Initialize a stack with the starting node: Stack = [A]
2. Mark A as visited: Visited = [A]
3. Pop A, explore its neighbors (B, C, D) and push them in reverse
Adjacency List order:
A: [B, C, D] ○ Stack = [D, C, B]
B: [ E, F] ○ Visited = [A]
C: [] 4. Pop B, explore its neighbors (E, F), and push them in reverse order:
D: [ G] ○ Stack = [D, C, F, E]
E: [] ○ Visited = [A, B]
F: [] 5. Continue the process with E, F, C, D, and G:
G: [] ○ Stack = []
○ Visited = [A, B, E, F, C, D, G]
http://artificialintelligence-notes.blogspot.com/2010/07/8-puzzle-problem.htm
depth-first search
• A variant of depth-first search called backtracking search uses still less memory than DFS
• In backtracking, only one successor is generated at a time rather than all successors;
each partially expanded node remembers which successor to generate next.
• In this way, only O(m) memory is needed rather than O(bm)…. b branching factor , Max.
depth m
• Backtracking search facilitates memory saving (and time-saving) trick: the idea of
generating a successor by modifying the current state description directly rather than
copying it first.
• This reduces the memory requirements to just one state description and O(m) actions.
• For this to work, we must be able to undo each modification when we go back to generate
the next successor.
• The drawback of depth-first search is that it can make a wrong choice and get stuck
going down a very long (or even infinite) path when a different choice would lead to a
solution near the root of the search tree.
Depth Limited search
• Depth first search has some desirable properties — space complexity.
• But if wrong branch expanded (with no solution on it), then it may not terminate.
• So a depth limit is introduced on the branches to be expanded.
• Indicating not to expand the branch below this depth.
• This is useful if the maximum depth of the solution is known
• Depth-limited search can terminate with two kinds of failure:
– the standard failure value indicates no solution;
– the cut off value indicates no solution within the depth limit.
Depth Limited search
• Depth-limited Search :The problem of unbounded trees can be solved by supplying
depth-first search with a predetermined depth limit l.
• The nodes at depth l are treated as if they have no successors.
• This approach is called depth-limited search.
• But it also introduces incompleteness if we choose l < d/m, i.e. the shallowest goal
is beyond the depth limit.
• Depth-limited search will also be non-optimal if we choose l > d.
• Its time complexity is O(b ^ l ) and its space complexity is O(bm) … b/m
branching factor and m maximum depth
• Depth-first search can be viewed as a special case of depth-limited search with l=
infinity.
•
Depth Limited search
• The unbounded tree problem appeared in Depth First Search can be fixed by
imposing a limit on the depth that DFS can reach, this limit is depth limit
• Depth Limited Search solves the infinite path problem
• Performance Measure:
– Completeness: DLS is not complete when depth limit(l) is less than depth
(d) Then DLS will never reach a goal,
– Optimality: DLS is not optimal even if l > d.
– Time Complexity: O(b^ l )
– Space Complexity: O(bm )
• DLS can be used when the there is a prior knowledge to the problem and that
the depth of shallowest goal is known
•
Iterative deepening depth-first search
Data structure BFS uses Queue to find the DFS uses Stack to find the shortest path.
1
shortest path.
Source BFS is better when target is DFS is better when target is far from source.
2
closer to Source.
Suitability for As BFS considers all neighbour DFS is more suitable for decision tree. As
decision tree so it is not suitable for decision with one decision, we need to traverse further
3
tree used in puzzle games. to augment the decision. If we reach the
conclusion, we won.
1. Topological Sorting:
○ DFS is used to perform topological sorting of a Directed Acyclic Graph (DAG).
○ Example: Determining the order of task execution in a build system (e.g., dependency resolution).
2. Cycle Detection:
○ DFS is used to detect cycles in directed and undirected graphs.
○ Example: Verifying deadlock conditions in an operating system or resource allocation.
3. Pathfinding in Mazes or Games:
○ DFS can explore all paths in a maze or game.
○ Example: Exploring all possible moves in chess or solving a maze puzzle.
4. Connected Components:
○ DFS is used to find connected components in a graph.
○ Example: Identifying clusters of related nodes in social networks.
5. Artificial Intelligence and Game Trees:
○ DFS is the backbone of algorithms like Minimax, which evaluate game states.
○ Example: AI decision-making in games like Tic-Tac-Toe or Chess.
6. Network Analysis:
○ DFS is used to analyze strongly connected components in directed graphs.
○ Example: Analyzing communication patterns in a network.
7. Solving Recurrence Relations:
○ DFS is used in divide-and-conquer algorithms to solve recursive problems.
○ Example: Solving problems like finding connected regions in an image or matrix (e.g., flood fill in image
processing).
8. Backtracking Algorithms:
○ DFS powers backtracking techniques for solving constraint satisfaction problems.
○ Example: Solving Sudoku, N-Queens problem, or generating permutations.
Bidirectional search
• Bidirectional search runs two simultaneous searches
– one forward from the initial state and
– the other backward from the goal, stopping when the two searches meet
• Informed search strategy: Uses problem-specific knowledge beyond the definition of the
problem itself and find solutions more efficiently than an uninformed strategy.
• Informed search algorithms use additional information (heuristics) about the problem domain to
guide the search process more effectively than uninformed algorithms.
• These algorithms rely on domain-specific knowledge to estimate the cost or distance to the goal,
improving efficiency in finding solutions
• It is represented by h(n), and it calculates the cost of an optimal path between the
pair of states.
• The value of the heuristic function is always positive.
•
Search and exploration
• Best first search is an instance of graph search algorithm in which a node is
selected for expansion based on evaluation function f (n).
• The node which is the lowest evaluation is selected for the explanation
because the evaluation function measures distance to the goal.
• Best first search can be implemented using a priority queue, a data structure
that will maintain the fringe in ascending order of f values.
• This search algorithm serves as combination of depth first and breadth first
search algorithm.
• Best first search algorithm is often referred greedy algorithm this is because
they quickly attack the most desirable path as soon as its heuristic weight
becomes the most desirable.
•
Best-First Search
Minimum Cost is heuristic function so D is selected, and successors are generated,
search goes to E and F but they have higher cost than B and C so The search
shifts to B
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
2 1
Expands the most promising node according to a specified rule, often based on a heuristic function
h(n) which estimates the cost to reach the goal from node n
Search and exploration
• There are many variations of Best first search algorithms with different evaluation
functions called as heuristic function h(n)
• h(n) is estimated cost of the cheapest path from node n to a goal node.
• Greedy Best first search, A*, etc. are variations in Best Search algorithms
• Example : In a route finding problem that 'straight line' distance from the node to the goal can
be used to evaluate each node.
● For example, in Romania, one might estimate the cost of the cheapest path from Arad to
Bucharest via the straight-line distance from Arad to Bucharest.
• Performance Measures
● Completeness: Depends on the heuristic; may fail in infinite spaces.
● Optimality: Not guaranteed unless combined with cost information
● Time Complexity: O(b^m) , where b is the branching factor and m is the maximum depth.
● Space Complexity: O(b^m) (requires keeping all nodes in memory).
•
Search and exploration
• Best First Search is implemented using priority queue which is implemented
using a heaps
• The priority queue is implemented using Min(or Max) Heap, and insert and
remove operations take O(log n) time.
• Performance of the algorithm depends on how well the cost or evaluation
function is designed.
• A priority queue is an abstract data type similar to a regular queue or stack data
structure in which each element additionally has a "priority" associated with it. In a
priority queue, an element with high priority is served before an element with low
priority.
• While priority queues are implemented with heaps
• A priority queue is a concept like "a list" or "a map"; just as a list can be
implemented with a linked list or an array, a priority queue can be implemented
with a heap
•
100 10 5
A B D F
● There are 2 lists made open list and
20 60 10 close list.
50 20 ● When a Node is expanded and node
connecting to shortest heuristic
S C E G distance it goes into closed list
50 100 10
So the Best Path from source to goal is in close list and is A,C,D,F,G and the path cost is 105
Search and exploration
• Greedy best-first search :
• It tries to expand the node that is closest to the goal, on the grounds that
this is likely to lead to a solution quickly.
• It evaluates nodes by using just the heuristic function: f (n) = h(n).
• The algorithm is called greedy as at each step it tries to get as close to the
goal as it can.
• Greedy best-first search resembles depth-first search
• It follow a single path to the goal, but will back up when it hits a dead end.
• It is not optimal like the depth first search
• Completeness : It is incomplete as it can move on to infinite path and never
try the other possibilities
• The worst-case time and space complexity is O(b m), where m is the
maximum depth of the search space.
• But with a good heuristic function the complexity can be reduced
Romania with step costs in km
Greedy best-first search example
Greedy best-first search example
Greedy best-first search example
Greedy best-first search example
Straight line distance : hSLD
Search and exploration
• Greedy Best First Search Algorithm Properties:
• Time Complexity : O(bm), but a good heuristic can give dramatic improvement
• Optimal : No
• https://www.youtube.com/watch?v=dv1m3L6QXWs
•
Search and exploration
• A* search (A Star):
• * (star) in the A* algorithm means that the algorithm is admissible, i.e. it is guaranteed
that it finds the shortest path in the graph if this path exists
• A * algorithm is a searching algorithm that searches for the shortest path between the
initial and the final state. It is used in various applications, such as maps.
• In maps the A* algorithm is used to calculate the shortest distance between the source
(initial state) and the destination (final state).
• It combines the benefits of Dijkstra's algorithm (optimal pathfinding) and Greedy
Best-First Search (speed) to find the least-cost path to a goal.
Search and exploration
• A* search (A Star): Minimizing the total estimated solution cost
• It is most widely know best-first search, it avoids expanding paths that are already
expensive
• It evaluates nodes by combining the cost to reach the node g(n) and the cost to get
from the node to the goal h(n)
• f(n) = g(n)+h(n) where
– g(n) gives the path cost from the start node to node n
– h(n) is the estimated cost of the cheapest path from n to the goal
– f (n) is estimated cost of the cheapest solution through n
• To find the cheapest solution the node with the lowest value of g(n) + h(n) is
selected
• It is good strategy if the heuristic function h(n) satisfies certain conditions
• A* search is both complete and optimal.
• A* is optimal if h(n) is an admissible heuristic i.e h(n) never overestimates the cost to
reach the goal i.e. optimistic
• Admissible heuristics are by nature optimistic, because they think the cost of solving
the problem is less than it actually is.
• Since g(n) is the exact cost to reach n so f (n) never overestimates the true cost of a
solution through n.
*
A search example
Stages in an A* search for Bucharest. Nodes are labelled with f = g + h. G is distance from one
node to another H is straight line distance
Sibiu = 140+253 140 = Arad to Sibiu straight distance from Sibiu to Bucharest =253
*
A search example
417+0
• Time Complexity: Exponential in the depth of the solution O(b^d) , where b is the
branching factor and d is the depth of the optimal solution.
• Space Complexity: O(b^d) , as it stores all open and closed nodes.
• Properties:
● Complete : Yes
● Optimal : Yes
• https://www.google.com/search?q=A*+algorithm+video&oq=A*+algorithm+video&aq
s=chrome..69i57j0i22i30.5370j0j7&sourceid=chrome&ie=UTF-8#fpstate=ive&vld=ci
d:7ab1a285,vid:-L-WgKMFuhE
D
Cost from Node to HEURISTIC h(n)
Node g(n)
A B S 7
S-A 1
A 6
S-B 4
B 2
A-B 2
S C C 1
A-C 5
D 0
A-D 0
B-C 2
C-D 3
1 S 4 HEURISTIC h(n)
2 S 7
A B
A 6
2
12 5 B 2
C 1
D C
3 D 0
● S->A = 7
● S->B = 6 … SELECT THIS
● EXPAND B
● S->B->C = 7 COST OF S->A AND S->B->C = 7 GO ANOTHER ROUTE
● S->A : S->A->B=5 S->A->C=7 S->A->D=13 PATH 4
● S->A->B=5 WHICH IS LESS THAN S->B->C = 7 SO EXPAND B NODE
● S->A->B->C =6 EXPAND C
● S->A->B->C->D =8 PATH 1 PATH COST GREATER S->B->C = 7 SO EXPAND C
● S->B->C->D = 9 PATH 2 THIS GREATER THAN S->A->C=7 EXPAND C
● S->A->C->D = 9 PATH 3
● sHORTEST PATH IS S->A->B->C->D =8
1 S 4
Cost from Node to HEURISTIC h(n)
2 Node g(n)
A B S 7
S-A 1
2 A 6
12 5 S-B 4
B 2
A-B 2
D C C 1
3 A-C 5
D 0
A-D 0
Start Node : S Goal Node : D
B-C 2
f(n) = g(n) + h(n)
C-D 3
● WE HAVE 4 PATHS :
● S=0+7 =7 … START NODE HAS HEURISTIC SO WILL HV ESTIMATED COST
● S -> A = 1 +6 = 7 ….. heuristic of the child node will be taken
● S -> B = 4 + 2 =6 Select shortest path so select S -> B and expand and go to child nodes of B
● S -> B -> C = (4 +2 )+ 1 =7 ….. S-> A = 7 and S -> B -> C = 7 so we will check the other route
● S -> B -> C->D = (4 +2+3 )+ 0 =9
● S -> A->B = (1+2)+2 =5
● S -> A->C = (1+5)+1 =7
● S -> A->D = (1+12)+0 =13 ….. S -> A->B IS SHORTEST DISTANCE SO EXPAND THIS
● S -> A->B->C = (1+2+2) + 1 =6 … ONLY ONE PATH SO EXPAND TO D
● S -> A->B->C->D = (1+2+2+3)+0 = 8
● S->A->C->D = (1+5+3)+0 = 9
1 S 4
Cost from Node to HEURISTIC h(n)
2 Node g(n)
A B S 7
S-A 1
2 A 6
12 5 S-B 4
B 2
A-B 2
D C C 1
3 A-C 5
D 0
A-D 0
Start Node : S Goal Node : D
B-C 2
f(n) = g(n) + h(n)
C-D 3
• At each iteration, the cutoff value is the smallest f-cost of any node that
exceeded the cutoff on the previous iteration.
• IDA* is practical for many problems with unit step costs and avoids the
substantial overhead associated with keeping a sorted queue of nodes.
•
Search and exploration
• Memory-bounded algorithms: RBFS
• Memory-bounded algorithms are search techniques designed to handle problems where
memory is a limiting factor.
• These algorithms trade off time for space by managing the information they store during
search.
• Recursive Best-First Search (RBFS) algorithm extends Best-First Search to work within a
limited memory framework.
• RBFS is a search algorithm designed for solving optimization problems, particularly when
memory constraints prevent the use of algorithms like A* that require storing all generated
nodes in memory.
• It keeps the track of f-value of the best alternative path available from ancestor of current
node
• If the current node exceeds the limit, the recursion unwinds back to the alternative path.
• As the recursion unwinds, RBFS replaces the f -value of each node along the path with the best f
-value of its children.
• In this way, RBFS remembers the f -value of the best leaf in the forgotten subtree and can
therefore decide whether it's worth re-expanding the sub tree at some later time
• RBFS is somewhat more efficient than IDA*, but still suffers from excessive node regeneration.
Search and exploration
● Recursive Best-First Search Algorithm:
○ Start with the initial node and expand it based on the heuristic.
○ Maintain a priority ordering of paths based on the cost function f(n)=g(n)+h(n), where:
○ If the path becomes unpromising, backtrack to explore the next best path.
● Advantages of RBFS:
○ Uses constant memory (linear in the depth of the search tree).
○ Handles large search spaces better than A* when memory is a constraint.
● Disadvantages of RBFS:
○ Can re-expand nodes multiple times, leading to higher time complexity compared to A*.
○ Performance depends heavily on the accuracy of the heuristic.
Search and exploration
● Applications of RBFS : RBFS is useful where memory is a limiting factor, such as:
● Pathfinding in Robotics:
○ Planning paths for autonomous robots in large environments with limited onboard
memory.
● Game Playing:
○ Used in AI for games where memory needs to be conserved while exploring game trees
(e.g., Chess, Go).
● Search in Large State Spaces:
○ Solving puzzles (e.g., 8-puzzle, 15-puzzle) where the state space is too large to store in
memory.
● Planning in AI:
○ Helps in hierarchical task planning and partial-order planning when state spaces grow
exponentially.
● Real-Time Systems:
○ Applied in systems requiring real-time decision-making without the luxury of storing
large amounts of data
Search and exploration
• Local Search Algorithms And Optimization Problems
• The previous search algorithms are designed to explore search spaces
systematically
• It keeps one or more paths in memory and record which alternatives have been
explored at each point along the path and which have not.
• When a goal is found, the path to that goal also constitutes a solution to the
problem.
• But sometimes the path to goal is not relevant what matter is the result such as
in
– 8 queens or puzzle problem,
– integrated-circuit design,
– factory-floor layout,
– job-shop scheduling
– automatic programming,
– telecommunications network optimization,
– vehicle routing,
– portfolio management
•
Search and exploration
• Local Search Algorithms And Optimization Problems
• Algorithm searches locally not globally, its greedy i.e. it moves ahead till it
gets best move else it stops and there is no backtracking
• Local search algorithms operate using a single current state (rather than
multiple paths) and move only to neighbours of that state.
• The paths followed by the search are not retained and are not systematic
•
Initial State Goal State
1 2 4 1 4 7
5 Current 5 - 7 2 5 8
heuristic
3 6 8 3 6 -
4 5 6 Heuristic values for the successor
1 2 4 1 2 4 1 2 4
- 5 7 5 7 - 5 6 7
3 6 8 3 6 8 3 - 8
5 5
- 2 4 1 2 4
1 5 7 3 5 7
3 6 8 - 6 8 Not saved in memory
When the heuristic 5 is present at level 2 which is greater than 4 the algo will
stop and it is local maxima
https://www.youtube.com/watch?v=rA3a8QDtYLs
Search and exploration
• Local search in continuous space:
• Local search methods work on complete state formulation
• They keep only small number of nodes in the memory
• Local search algorithm is useful for solving optimization problems where
– it's often easy to find the solution
– But hard to find the best solution
• The goal of the algorithm is to find optimal configuration in limited time
• In path finding its easy to find multiple paths but specific best path is difficult
task
• In many optimization problems path is irrelevant and the goal state itself is
solution so can go for iterative method
• Local search is ideal when paths are known
• Ex.N queens problem once we have final configuration, we can get solution
how we get solution is not important
Search and exploration
• Constraint Satisfaction Problems (CSP) :
• The states and goal test conform to a standard, structured, and very simple
representation
• Search algorithms can be defined that take advantage of the structure of
states and use general-purpose rather than problem-specific heuristics to
enable the solution of large problems
• The standard representation of the goal test reveals the structure of the
problem itself .
• This leads to methods for problem decomposition and to an understanding of
the connection between the structure of a problem and the difficulty of
solving it.
• CSP is defined by a set of variables, XI, X2,. . . , Xn, and a set of
constraints, C1, C2,. . . , Cn
• Each variable Xi has a nonempty domain Di of possible values
• Each constraint Ci involves some subset of the variables and specifies the
allowable combinations of values for that subset.
• Examples of simple problems that can be modeled as a constraint satisfaction
problem are Eight queens puzzle, Map coloring problem, Sudoku,
Crosswords, etc.
Search and exploration
• Constraint Satisfaction Problems (CSP) :
• An assignment that does not violate any constraints is called a consistent or legal
assignment.
• A complete assignment is one in which every variable is mentioned, and a solution
to a CSP is a complete assignment that satisfies all the constraints.
• Some CSPs also require a solution that maximizes an objective function.
Color Map
Cryptarithmetic
• Crypt arithmetic is a type of mathematical game consisting of a
mathematical equation among unknown numbers, whose digits are represented by letters.
• Each letter should represent a different digit, and (as in ordinary arithmetic notation) the
leading digit of a multi-digit number must not be zero
• 19 is the max value with a carryover for two one-digit number in the same column.
• Carry over can only be 1 or 0 ( 0 or 1 as per as per vinod chandra book AI and ML
and only 1 as per Russel Norvig Book) we can go with any keeping constraints
satisfied
Algorithm for Solving Cryptarithmetic Puzzles
○ Begin assigning possible digits to the letters, starting with the constraints and parts of the
equation with the fewest possibilities.
○ Use a systematic approach, making sure to keep track of your assignments.
7. Check for Consistency:
○ After making an assignment, check if it maintains consistency within the equation and
adheres to the constraints.
○ If a digit becomes inconsistent with other assignments, backtrack and try a different digit.
8. Iterate and Refine:
○ Continue the process of assigning digits, checking for consistency, and refining your
assignments.
○ Use logical deductions and process of elimination to narrow down possibilities.
Example : Cryptarithmetic
Cryptarithmetic
T = W = O =4
T W O 7 3 {0,1,2,3,4,5,6,7,8,9}
+ T W O T = W = O =4
+ 7 3
----------------------
1 4 6 8
F O U R
7 6 5
7 6 5
+1 +1
1 5 3 0
Other Solutions :
938+938=1876 , 928+928=1856 , 867+867=1734, 846+846=1692, 836+836=1672 ,
765+765=1530 , 734+734=1468
Crypt arithmetic
•
{0,1,2,3,4,5,6,7,8,9} ->
1. F+F Should generate carry 5 +5 =10 {0,1,2,3,4,5,6,7,8,9} ->
FOUR {0,1,2,3,4,5,6,7,8,9} ->
+ FOUR 2. E =1, I = 0
3. O+O =G 2+2 =4 G=4 {0,1,2,3,4,5,6,7,8,9} ->
-----------------
EIG H T 4. U+U =H 3+3 =6 H=6
5. R+R =T 7+7 =14 4 is assigned to G so
cannot use {0,1,2,3,4,5,6,7,8,9} ->
6. 8+8 =16 then R=8 T =6 H=7 OR
Solution 1 {0,1,2,3,4,5,6,7,8,9}
7. 9 + 9 =18 R = 9 T =8 H =7
Solution 2
5 2 3 4
+ 5 2 3 4
E= 1 0 4 6 8
Cryptarithmetic
C4 C3 C2 C1 C0
FORTY 29786
+ T EN + 850 1 2 1 1
+ 850
+ TEN
----- F=2 O =9 R =7 T =8 Y=6
---------------------- 31486
SIXTY T =8 E=5 N=0
T =8 E =5 N=0
Column 1: N is either 0 or 5. If N=5, then the carry to
Col2 is 1 and there is no possible value for E, so N=0. S=3 I=1 X=4 T Y=6
Column 2: E is either 0 or 5, but N=0, so E=5, and
the carry to Col3 is 1.
Column 4: I is either 0 or 1, but N=0, so I=1, O=9,
carry from Col3 is 2, and the carry to Col5 is 1.
Column 3: For 1+R+T+T >= 20, R and T must be in
{6,7,8}.
Column 5: F+1=S, so {F,S} is either {2,3} or {3,4}, so
X is not 3.
Column 3: 1+R+T+T = 22 or 24, so R is odd and
must be 7, 2T=14 or 16, T is 7 or 8, but R=7, so T=8,
X=4.
Column 5: F=2, S=3.
Column 1: Y=6.
B=7 L= 9 A= 2 C=0 K=8
https://tamura70.gitlab.io/web-puzzle/cryptarithm/c/out/colors.out
Crypt arithmetic
• SEND + MORE = MONEY
1 1
9 5 6 7
+ 1 0 8 5
1 0 6 5 2
+1 +1
S E N D 9 5 6 7
1 0 8 5
+ M O R E
M O N E Y 1 0 6 5 2
Crypt arithmetic
• It is cryptography ie encryption and decryption of data being transmitted
• Suppose we have expression
– SEND + MORE = MONEY
• Assign a unique value from 0 to 9 to each character
• S +M value can be max 9+8= 17 so
M value will be 1 so put value 1 at every 1 1
https://atozmath.com/Crypto.aspx
https://www.youtube.com/watch?v=HC6
Y49iTg1k
POTATO+TOMATO=PUMPKIN
(168486+863486=1031972)
1. Solve for D, E, F in the cryptarithmetic puzzle: DEF - FED = 303.
2. XYZ+XYZ=484 Solve
3. TO + GO = OUT. Find the value of T + G + O + U?
4. HERE = COMES – SHE, (assume S = 8). Find values of R + H + O
5. POINT + ZERO = ENERGY THEN E+N+E+R+G+Y ?
6. If AA + BB + CC = ABC, then what is the value of A+B+C= ?
7. If NO + NO + GUN = HUNT, what is the value of HUNT?
8. IF EAT + THAT = APPLE, What is A + P + P + L + E?
9. USSR + USA = PEACE, what is the value of P + E + A + C + E?
10. Find the value of A + B + C + D + E + F + G, given equation: F R E E + B I R D = C A G E
Real World CSPs
● Assignment Problems : Who teaches what class
● Timetabling Problems : Which Class is offered when and where
● Hardware Configuration
● Spreadsheets
● Transportation scheduling
● Factory Scheduling
● FloorPlanning
Search and exploration
• Backtracking search for CSP:
• Backtracking is a general search algorithm used to solve CSPs by incrementally building a solution and
backtracking when constraints are violated.
• It explores all possible variable assignments recursively.
• Steps of Backtracking
○ Select an unassigned variable.
○ Assign a value to the variable from its domain.
○ Check if the current assignment is consistent with the problem constraints.
○ If consistent, continue; ELSE backtrack by undoing the last assignment and trying a different value.
• Example
• Consider solving a simple CSP like a Sudoku puzzle.
• The algorithm assigns numbers to cells while ensuring row, column, and block constraints are respected.
• If no valid number fits in a given cell, backtracking occurs.
• The algorithm is modeled on the recursive depth-first search
• Backtracking search is used for a depth-first search that chooses values for one variable at a time and
backtracks when a variable has no legal values left to assign.
• Backtracking is an uninformed algorithm
Search and exploration
• Propagating information through constraints:
○ Forward checking AND Constraint propagation
• Forward Checking in CSP:
• Forward checking is a refinement of backtracking that helps prevent unnecessary searches by
keeping track of legal values for unassigned variables.
• How Forward Checking Works
• When a variable is assigned a value, forward checking updates the domains of neighboring
variables.
• If any domain becomes empty, backtracking occurs immediately.
• Advantages:
• Reduces the search space early.
• Avoids wasting time exploring inconsistent branches.
• Example
• In a graph-coloring problem, if node A is assigned "red," forward checking removes "red" from
the domains of adjacent nodes, simplifying future choices.
Search and exploration
• Propagating information through constraints: Forward checking :
• One way to make better use of constraints during search is called forward checking.
• Whenever a variable X is assigned, the forward checking process looks at each unassigned
variable Y that is connected to X by a constraint and deletes from Y's domain any value that is
inconsistent with the value chosen for X.
• WA = red is assigned first; then forward checking deletes red from the domains of the neighboring
variables NT and SA. After Q = green, green is deleted from the domains of NT, SA, and NSW. After V
= blue, blue is deleted from the domains of NSW and SA, leaving SA with no legal values.
Search and exploration
• Propagating information through constraints:
• Constraint propagation in CSP : It is process of using constraints to reduce the search space by
updating variable domains and ensuring local consistency.
• Types of Consistency
• Node Consistency: Each variable satisfies its unary constraints.
• Arc Consistency (AC3): For every pair of connected variables, ensure that for each value of one
variable, there is a compatible value in the other.
• Path Consistency: Involves triples of variables to maintain consistency.
• Arc Consistency (AC3 Algorithm) Example:
○ Consider two variables X and Y with domains {1, 2, 3}.
○ Constraint: X<Y
○ If X=3 cannot satisfy X<Y it is removed from the domain.
• Advantages :
○ Speeds up problem-solving by filtering out invalid solutions.
○ Often used in combination with backtracking and forward checking for efficiency.
Search and exploration
• Propagating information through constraints: Constraint Propagation:
• The forward checking detects many inconsistencies, it does not detect all of them.
• Diagram shows that when WA is red and Q is green, both NT and SA are forced to be blue.
• But they are ,adjacent and so cannot have the same value.
• Forward checking does not detect this as an inconsistency, because it does not look far enough
ahead.
• Constraint propagation is the general term for propagating the implications of a constraint on
one variable onto other variables;
Search and exploration
• Games: Optimal decisions in games:
• In a multiagent environments, any given agent will need to consider the actions of other agents
and how they affect its own welfare.
• The unpredictability of these other agents can introduce many possible contingencies into the
agent's problem solving process
• In Competitive multiagent environments the agents' goals are in conflict, give rise to
adversarial search problems known as games.
• In AI games are specialized kind-what game theorists call deterministic, turn-taking, two-player,
zero-sum games of perfect information.
• This means deterministic, fully observable environment in which there are two agents whose
actions must alternate and in which the utility values at the end of the game are always equal and
opposite
Search and exploration
• For example, if one player wins a game of chess (+1), the other player necessarily loses (-1).
• It is this opposition between the agents' utility functions that makes the situation adversarial (
opposition/conflict)
• Games have engaged the intellectual faculties of humans to an alarming degree-for as long as
civilization has existed.
• For AI researchers, the abstract nature of games makes them an appealing subject for study.
• The state of a game is easy to represent, and agents are usually restricted to a small number
of actions whose outcomes are defined by precise rules
Search and exploration
• Optimal Decision Games:
• A game can be formally defined as a kind of search problem with the following components:
• The initial state, which includes the board position and identifies the player to move.
• A successor function, which returns a list of (move, state) pairs, each indicating a legal move
and the resulting state.
• A terminal test, which determines when the game is over, States where the game has
ended are called terminal states.
• A utility function (also called an objective function or payoff function), which gives a
numeric value for the terminal states.
• In chess, the outcome is a win, loss, or draw, with values +1, -1, or 0.
• Some games have a wider ,variety of possible outcomes; the payoffs in backgammon range from
+192 to -192.
• GAME TREE : The initial state and the legal moves for each side define the game tree for the
game.
Search and exploration
• Trees are used in game playing, in particular for the problems of searching game trees.
– Rubik’s Cube
– Sliding puzzle.
• Two-player games.
– Chess
– Checkers
– Othello
– Eight Queens
– Sudoku
•
Search and exploration
• Each game consists of a problem space, an initial state, and a single (or a set of) goal states.
• Mini-Max Algorithm:
• Zero Sum games: Games with two players in which one person's gains are the result of another
person's losses are called zero-sum games
• The minimax algorithm is a specialized search algorithm which returns the optimal sequence of
moves for a player in an zero-sum game.
• In the game tree that results from the algorithm, each level represents a move by either of two
players, say A- and B-player.
•
Search and exploration
• Minimax is a kind of backtracking algorithm that is used in decision making and game theory to
find the optimal move for a player, assuming that your opponent also plays optimally.
• It is widely used in two player turn-based games such as Tic-Tac-Toe, Backgammon, Chess, etc.
• The maximizer tries to get the highest score possible while the minimizer tries to do the opposite
and get the lowest score possible.
• In a given state if the maximizer has upper hand then, the score of the board will tend to be some
positive value.
• If the minimizer has the upper hand in that board state then it will tend to be some negative value.
• The values of the board are calculated by some heuristics which are unique for every type of game.
Search and exploration
• Consider a game which has 4 final states and paths to reach final state are
from root to 4 leaves of a perfect binary tree
• Assume you are the maximizing player and you get the first chance to move,
i.e., you are at the root and your opponent at next level.
•Maximizer goes LEFT: It is now the minimizers turn. The minimizer now has a choice
between 3 and 5. Being the minimizer it will definitely choose the least among both,
that is 3
•Maximizer goes RIGHT: It is now the minimizers turn. The minimizer now has a
choice between 2 and 9. He will choose 2 as it is the least among the two values.
Search and exploration
• Being the maximizer you would choose the larger value that is 3.
• Hence the optimal move for the maximizer is to go LEFT and the optimal value
is 3.
• The minimax algorithm explores the entire game tree using a depth-first
search.
• At each node in the tree where A-player has to move, A-player would like to
play the move that maximizes the payoff.
• Thus, A-player will assign the maximum score amongst the children to the
node where Max makes a move.
• Similarly, B-player will minimize the payoff to A-player.
• The maximum and minimum scores are taken at alternating levels of the
tree, since A and B alternate turns.
The minimax algorithm computes the minimax decision for the leaves of the
game tree and than backs up through the tree to give the final value to the
current state.
Example tic-tac-toe game : From the initial state, MAX has nine possible moves.
Play alternates between MAX'S placing an x and MIN'S placing an o until we reach leaf nodes
corresponding to terminal states such that one player has three in a row or all the squares are filled.
The number on each leaf node indicates the utility value of the terminal state from the point of view of
MAX;
high values are assumed to be good for MAX and bad for MIN (which is how the players get their
names).
It is MAX'S job to use the search tree (particularly the utility of terminal states) to determine the best
move.
Search and exploration
• The mini-max search procedure is depth first , depth limited search procedure
• “backup” values for each non leaf nodes until computed for root node
• At Min node the backed up value is minimum of the values associated with its children.
• At max node the backed up value is maximum of the values associated with its children.
Mini Max algorithm
• Properties of Minimax algorithm:
• Complete- Min-Max algorithm is Complete. It will definitely find a solution (if exist), in the finite
search tree.
• Optimal- Min-Max algorithm is optimal if both opponents are playing optimally.
• Time complexity- As it performs DFS for the game-tree, so the time complexity of Min-Max
algorithm is O(bm), where b is branching factor of the game-tree, and m is the maximum depth of
the tree.
• Space Complexity- Space complexity of Minimax algorithm is also similar to DFS which
is O(bm).
• Limitation of the minimax Algorithm:
• The main drawback of the minimax algorithm is that it gets really slow for complex games such as
Chess, etc. This type of games has a huge branching factor, and the player has lots of choices to
decide.
Search and exploration
• Alpha Beta Pruning algorithm :
• The problem with minimax search is that the number of game states it has to examine
is exponential in the number of moves.
• It is possible to compute the correct minimax decision without looking at every node in the
game tree.
• It helps to arrive at correct mini-max algo decision without looking at every node of the tree.
• While using mini-max situations may arise when search of a particular branch can safely
terminate
• So while doing search figure out the nodes that do not require to be expanded
• Alpha > = Beta that brach is pruned ie no need of traversing
• alpha -> maximise and initially -> - infinity : maximiser playing alpha value can be changed
• beta -> Minimiser and initially -> + infinity : minimiser playing beta value can change
Search and exploration
• The procedure:
– Max player cuts off search when he knows Min player can force a probably bad outcome
– Min Player cuts off the search when he knows Max player can force probably good outcome
– Apply alpha cut off means we stop search of a particular branch because we see that we
already have better opportunity elsewhere
– Apply beta cut off means we stop search of a particular branch because we see that the
opponent already has a better opportunity elsewhere
•
Search and exploration
15
16
Search and exploration
MAX
Back up Min
No Point in exploring
this branch
20
Back up Max 25
Alpha Beta Pruning
B B
Stages in the calculation of the optimal decision for the game tree
At each point, the range of possible values for each node is shown.
(a) The first leaf below B has the value 3. Hence, B, which is a MIN node,
has a value of at most 3.
(b) The second leaf below B has a value of 12; MIN would avoid this
move, so the value of B is still at most 3.
A A
B B
C
(c) The third leaf below B has a value of 8; we have seen all B's successors, so the
value of B is exactly 3. Now, we can infer that the value of the root is at least 3,
because MAX has a choice worth 3 at the root.
(d) The first leaf below C has the value 2. Hence, C, which is a MIN node, has a
value of at most 2. But we know that B is worth 3, so MAX would never choose C.
Therefore, there is no point in looking at the other successors of C. So we prune
those 2 branches of C
This is an example of alpha-beta pruning.
A A
B B
C C
D D
(e) The first leaf below D has the value 14, so D is worth at most 14. This is still
higher than MAX'S best alternative (i.e., 3), so we need to keep exploring
D's successors. Notice also that we now have bounds on all of the successors of
the root, so the root's value is also at most 14.
(f) The second successor of D is worth 5, so again we need to keep exploring.
The third successor is worth 2, so now D is worth exactly 2. MAX'S decision at
the root is to move to B, giving a value of 3.
LEVEL 1
Max =α β =+∞
α = -∞
β =+∞ LEVEL 2
β =+∞
Min Min α = -∞
α = -∞
=β
β =+∞ β =+∞ β =+∞
β =+∞
α = -∞ α = -∞ α = -∞
α = -∞
β =+∞
α = -∞
10 5 7 11 12 8 9 8 5 12 11 12 9 8 7 10
β =+∞
β =+∞->7 LEVEL 2
Min Min α = -∞
α = -∞
=β
β =+∞->7 β =+∞ β =+∞
β =+∞ α = -∞ α = -∞
α = -∞
α = -∞->5->7
10 5 7 11 12 8 9 8 5 12 11 12 9 8 7 10
β =+∞ LEVEL 2
β =+∞->7
Min Min α = -∞
α = -∞
=β
β =+∞->7 β =+∞ β =+∞
β =+∞ α = -∞ α = -∞
α = -∞
α = -∞->5->7
10 5 7 11 12 8 9 8 5 12 11 12 9 8 7 10
β =+∞ LEVEL 2
β =+∞->7
Min Min α = -∞
α = -∞
=β
β =7 β =+∞ β =+∞
β =+∞ α = -∞ -> 7 α = -∞
α = -∞
α = -∞->5->7
β =7
β =+∞ -> β =+∞ ->7
α = -∞
10->5 α =5
α = -∞
Min Min Min Min Min Min Min Min LEVEL 4
10 5 7 11 12 8 9 8 5 12 11 12 9 8 7 10
β =+∞ LEVEL 2
β =+∞->7
Min Min α =7
α = -∞
=β
β =+∞ β =+∞
β =+∞ β =7 α = -∞
α = -∞ -> 7 α =7
α = -∞->5->7
β =7 β =+∞->5
β =+∞ -> β =+∞ ->7
α = -∞ α =7
10->5 α =5
α = -∞
Min Min Min Min Min Min Min Min LEVEL 4
10 5 7 11 12 8 9 8 5 12 11 12 9 8 7 10
Level 1: Max: α = -∞, β =+∞ will be compared α = -∞, β =7 at level2 and the value of
α =7 and will propagate to the next level 2,3 and 4
Level 4: Min: α = 7, β = +∞ compared 5 and β = 5 alpha >=Beta so prune the branch 12
Level 3 : β =+∞ α = 7 no change
LEVEL 1
Max =α β =+∞
α = -∞->7
β =+∞ β =11
β =+∞ β =7 α =7
α = -∞ -> 7 α = 7->11
α = -∞->5->7 LEVEL 3
β =7 β =+∞->5
β =+∞ -> β =+∞ ->7 β =+∞->11
α = -∞ α =7
10->5 α =5 α =7
α = -∞
Min Min Min Min Min Min Min Min LEVEL 4
10 5 7 11 12 8 9 8 5 12 11 12 9 8 7 10
β =7 β =+∞ β =11
β =+∞
α = -∞ -> 7 α = 7->11 α = 7->8 LEVEL 3
α = -∞->5->7
β =7 β =+∞->5 β =11->7
β =+∞ -> β =+∞ ->7 β =11 ->9->8 α =8
α = -∞ α =7 β =+∞->11
10->5 α =5 α =7
α =7
α = -∞
Min Min Min Min Min Min Min Min LEVEL 4
10 5 7 11 12 8 9 8 5 12 11 12 9 8 7 10
β =7 β =+∞ β =11
β =+∞
α = -∞ -> 7 α = 7->11 α = 7->8 LEVEL 3
α = -∞->5->7
β =7 β =+∞->5 β =11->7
β =+∞ -> β =+∞ ->7 β =11 ->9->8 α =8
α = -∞ α =7 β =+∞->11
10->5 α =5 α =7
α =7
α = -∞
Min Min Min Min Min Min Min Min LEVEL 4
10 5 7 11 12 8 9 8 5 12 11 12 9 8 7 10
β =7 β =+∞ β =11
β =+∞
α = -∞ -> 7 α = 7->11 α = 7->8 LEVEL 3
α = -∞->5->7
β =7 β =+∞->5 β =11->7
β =+∞ -> β =+∞ ->7 β =11 ->9->8 α =8
α = -∞ α =7 β =+∞->11
10->5 α =5 α =7
α =7
α = -∞
Min Min Min Min Min Min Min Min LEVEL 4
10 5 7 11 12 8 9 8 5 12 11 12 9 8 7 10
Nodes in memory using Mini-max : 15 min max nodes with 16 terminal nodes
Nodes in memory using Alpha Beta Pruning : 14 min max nodes and terminal
nodes 12 Saving of space Number of Alpha cut off = 01, Beta cutoff =02
http://people.cs.pitt.edu/~litman/cou
rses/cs2710/lectures/pruningRevie
w.pdf