0% found this document useful (0 votes)
24 views

AI Unit2 ProblemSolving

Uploaded by

btechproject404
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

AI Unit2 ProblemSolving

Uploaded by

btechproject404
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 191

Unit II

Problem Solving

By
Dr. Mrudul Dixit
Problem-solving agent

Problem Solving agent • A simple problem-solving agent-


• It first formulates a goal and a
problem,
Goal formation • searches for a sequence of actions
that would solve the problem,
• then executes the actions one at a
Problem Formulation time.
• When this is complete, it formulates
another goal and starts over.
Search Algorithm • when it is executing the sequence it
ignores its percepts: it assumes that
the solution it has found will always
Execution Phase work.
Problem-solving Agent.
• Problem-solving agents decide what to do by finding sequences of actions that
lead to desirable states.
• Goal formulation, based on the current situation and the agent's performance
measure, is the first step in problem solving.
– Goals help organize behaviour by limiting the objectives that the agent is
trying to achieve.
• Problem formulation is the process of deciding what actions and states to
consider, given a goal
• An agent with several immediate options of unknown value can decide what to
do by just examining different possible sequences of actions that lead to states of
known value, and then choosing the best sequence
• This process of looking for such a sequence is called search.
• Search algorithm takes a problem as input and returns a solution in the form of
an action sequence.
• Execution Phase: Once a solution is found, the actions it recommends can be
carried out.
Example: Romania

• On holiday in Romania; currently in Arad.


• Flight leaves tomorrow from Bucharest

• 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

• A problem can be defined formally by four components:


1. The initial state that the agent starts in
2. Description of the possible actions available to the agent. The most common
formulation uses a successor function.
– Given a particular state x, SUCCESSOR-FN(x)
– returns a set of (action, successor) ordered pairs,
– where each action is one of the legal actions in state x
– and each successor is a state that can be reached from x by applying the action.
– Together, the initial state and successor function implicitly define the state
space of the problem-the set of all states reachable from the initial state.
– The state space forms a graph in which the nodes are states and the arcs
between nodes are actions.
3. The goal test, which determines whether a given state is a goal state.
– Sometimes there is an explicit set of possible goal states, and the test simply
checks whether the given state is one of them.
Well-defined problems and solutions

• A problem can be defined formally by four components:


4. A path cost function that assigns a numeric cost to each path. The
problem-solving agent chooses a cost function that reflects its own performance
measure
– The step cost of taking action A to go from state X to state Y is denoted by C (
X,A,Y).
• The preceding 4 elements define a problem and can be gathered together into as
single data structure that is given as input to a problem-solving algorithm.
• A solution to a problem is a path from the initial state to a goal state.
• Solution quality is measured by the path cost function, and an optimal solution
has the lowest path cost among all solutions.
Well-defined problems and solutions

• A problem can be defined formally by four components:

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

Goal The agent's goal in Romania is the singleton set {In(Bucharest)}

For the agent trying to get to Bucharest, time is of the essence, so


Path Cost
the cost of a path might be its length in kilometers.
Problem Solving Approaches
• The problem-solving approach has been applied to a vast array of task
environments.
1) 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.
– Ex. Vacuum world problem , 8 puzzle problem , 8 queens problem
2) A real-world problem is one whose solutions people actually care about.
• They tend not to have a single agreed-upon description, but we will attempt to
give the general flavor of their formulations.
• Ex.
– Route finding problem, Touring problem, Travelling Salesperson problem
– VLSI Layout problem, Robot Navigation
– Automatic assembly sequencing, Protein design problem
– Internet searching problem
Rubik's Cube

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.

States : integer dirt and robot location


Actions: Left, Right, Suck
goal test: no dirt at all locations
path cost: 1 per action
8 Puzzle problem

● 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

Path Cost is 5 i.e. 5 moves required to achieve final goal


state 1 2 3
Initial State { (1 2 3) (4 8 -) (7 6 5) }
Move 1 : { (1 2 3) (4 8 5) (7 6 -) } 4 5 6
Move 2 : { (1 2 3) (4 8 5) (7 - 6) }
Move 3 : { (1 2 3) (4 - 5) (7 8 6) } 7 8 -
Move 4 : { (1 2 3) (4 5 -) (7 8 6) }
Move 5 : : { (1 2 3) (4 5 6) (7 8 -) }
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 - 4 8 5 4 8 5 4 - 5

7 6 5 7 6 - 7 - 6 7 8 6

Path Cost is 6 i.e. 6 moves required


to achieve final goal state 1 2 3 1 2 3

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

Path Cost is 6 i.e. 6 moves required


to achieve final goal state 1 2 3 1 2 3

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.

• This problem can be solved by searching for a solution.


• The initial state is given by the empty chess board.
• Placing a queen on the board represents an action in the search problem.
• A goal state is a configuration where none of the queens attacks any of the others.
Note that every goal state is reached after exactly 8 actions.
8 Queens Problem

• There is an efficient special-purpose algorithms to solve this n-queens problems


• Its an interesting test problem for search algorithms.
• There are two main kinds of formulation.
– An incremental formulation involves operators that augment the state
description, starting with an empty state; for the 8-queens problem, this means
that each action adds a queen to the state
– A complete-state formulation starts with all 8 queens on the board and moves
them around.
• In either case, the path cost is of no interest because only the final state counts
• Incremental formulation :
• States: Any arrangement of 0 to 8 queens on the board is a state.
• Initial state: No queens on the board.
• Successor function: Add a queen to any empty square.
• Goal test: 8 queens are on the board, none attacked.
4 Queens Problem Attacking Mode

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

• In this formulation, we have 64 . 63 . . .57 3 1.8 x 1014 possible sequences to


investigate.
• A better formulation would prohibit placing a queen in any square that is already
attacked:
• States: Arrangements of n queens (0 < n < 8), one per column in the leftmost n
columns, with no queen attacking another are states.
• Successor function: Add a queen to any square in the leftmost empty column such
that it is not attacked by any other queen.
• This formulation reduces the 8-queens state space from 3 x 1014 to just 2,057, and
solutions are easy to find.
Water Jug Problem
• Given two jugs, a 4-gallon one and a 3-gallon one. Neither has any measuring mark
on it.
• There is a pump that can be used to fill the jugs with water. How can you get exactly
2 gallons of water into the 4-gallon jug.
• The state space for this problem can be described as the set of ordered pairs of
integers (x,y)
• Where, X represents the quantity of water in the 4-gallon jug X= 0,1,2,3,4
• Y represents the quantity of water in 3-gallon jug Y=0,1,2,3
• Start State: (0,0)
• Goal State: (2,0)

Water Jug Problem Production Rules
● Generate
Rule State Process
production
rules for the 1 (X,Y | X<4) (4,Y) {Fill 4-gallon jug}
water jug 2 (X,Y |Y<3) (X,3) {Fill 3-gallon jug}
problem
3 (X,Y |X>0) (0,Y) {Empty 4-gallon jug}

4 (X,Y | Y>0) (X,0) {Empty 3-gallon jug}

5 (X,Y | X+Y>=4 ^ (4,Y-(4-X)) {Pour water from 3-gallon jug


Y>0) into 4-gallon jug until 4-gallon jug is full}

6 (X,Y | X+Y>=3 (X-(3-Y),3) {Pour water from 4-gallon jug


^X>0) into 3-gallon jug until 3-gallon jug is full}

7 (X,Y | X+Y<=4 (X+Y,0) {Pour all water from 3-gallon jug


^Y>0) into 4-gallon jug}

8 (X,Y | X+Y <=3^ (0,X+Y) {Pour all water from 4-gallon jug
X>0) into 3-gallon jug}

9 (0,2) (2,0){Pour 2 gallon water from 3 gallon jug


into 4 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

• Touring problems are closely related to route-finding problems with a small


difference.
• In Route finding, the actions correspond to trips between adjacent cities
• The Traveling Salesperson Problem (TSP) is a touring problem in which each
city must be visited exactly once.
• The aim is to find the shortest tour.
• These algorithms are used for
● planning trips for traveling salespersons,
● planning movements of automatic circuit-board drills
● Stocking machines on shop floors.
Search Solutions
• The toy and the real world problems can be solved by a search through the state
space (successive configurations or states of an instance are considered, with the
goal of finding a goal state with a desired property).

• 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

• Measuring problem-solving performance:


• The output of a problem-solving algorithm is either failure or a solution.
• The algorithm's performance can be evaluated in 4 ways:
– Completeness: Is the algorithm guaranteed to find a solution when there is one?
– Optimality: Does the strategy find the optimal solution,
– Time complexity: How long does it take to find a solution?
– Space complexity: How much memory is needed to perform the search?

Performance measure for Problem Solving

• Measuring problem-solving performance:


• Time and space complexity are always considered with respect to some
measure of the problem difficulty
• The measure is the size of the state space graph, because the graph is viewed as an
explicit data structure that is input to the search program.
• In AI, where the graph is represented implicitly by the initial state and successor
function and is frequently infinite, complexity is expressed in terms of three
quantities:
– the branching factor or maximum number of successors of any node;
– d, the depth of the shallowest goal node;
– m, the maximum length of any path in the state space.
• Search Cost: To assess the effectiveness of a search algorithm it is used and
depends on the time complexity n memory usage
– Time in terms of the number of nodes generated during the search,
– Space in terms of the maximum number of nodes stored in memory.
• Total cost,: it combines the search cost and the path cost of the solution found.
Search Algorithms

Uninformed Search Algorithms Informed Search Algorithms


or Blind search

1. Breadth first 1. Best First Search

2. Depth-first search 2. A* Search


Backtracking search
Depth-limited search
Iterative deepening depth-first
search 3. Memory Bounded Heuristic Search
Iterative deepening A* (IDA)
3. Bidirectional search Recursive best-first search

Uninformed search is a searching technique which have no additional


information about the distance from current state to the goal.
Informed Search is a another technique which have additional information
about the estimate distance from the current state to the goal.
Chat GPT by Dr. Yogesh HArbhau Kulkarni
https://www.youtube.com/watch?v=ZX-vtEGoP7M
Search Algorithms

Uninformed Search Algorithms Informed Search Algorithms


or Blind search

1. Breadth first 1. Best First Search

2. Depth-first search 2. A* Search


Backtracking search
Depth-limited search
Iterative deepening depth-first
search 3. Memory Bounded Heuristic Search
Iterative deepening A* (IDA)
3. Bidirectional search Recursive best-first search

Uninformed search is a searching technique which have no additional


information about the distance from current state to the goal.
Informed Search is a another technique which have additional information
about the estimate distance from the current state to the goal.
Uninformed Search or Blind Search Strategies

• 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

• Uninformed Search or Blind Search :


– They have no additional information about states beyond that provided in the
problem definition.
– They generate successors and distinguish a goal state from a non-goal state.
• Informed search or heuristic search: Strategies that know whether one non- goal
state is "more promising" than another , based on the heuristic function
• Search Strategies for uninformed search:
1. Breadth-first search
2. Depth-first search
● Backtracking search
● Depth-limited search
● Iterative deepening depth-first search
3. Bidirectional search
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

• Evaluation of Breadth-first search Algo :


• It is complete-if the shallowest goal node is at some finite depth d,
breadth-first search will eventually find it after expanding all shallower nodes
• The shallowest goal node is not necessarily the optimal one
• Breadth-first search is optimal if the path cost is a nondecreasing function of the
depth of the node i.e. when all actions have the same cost.

Depth 0 : root
Depth is edges between the targeting node and the root
Edge – connection between one node to another.

Order in which nodes are expanded


Uninformed Search or Blind Search Strategies

• Time and memory taken to complete a breadth first search:


– every state has b successors
– The root of the search tree generates b nodes at the first level, each of which
generates b more nodes, for a total of b2 at the second level
– Each of these generates b more nodes, yielding b3 nodes at the third level…
– suppose that the solution is at depth d.
– If all levels are expanded excluding the the last node at level d (since the goal
itself is not expanded), generating bd-1 - b nodes at level d +1.
– Then the total number of nodes generated is

– Every node generated must remain in memory, because it is either part of
the fringe or is an ancestor of a fringe node.
• The space complexity is the same as the time complexity (plus one node for the
root).
Properties of breadth-first search
• Complete: Yes (if b is finite) … b no. of nodes

• Time: 1+b+b2+b3+… +bd + b(bd-1) = O(bd+1) ….. d depth of the tree

• Space: O(bd+1) (keeps every node in memory)

• Optimal: Yes (if cost = 1 per step)

• Space is the bigger problem (more than time)

1 • number of levels 2 nodes at each level 2, 4

2
3 • Time and space complexity = O(b d+1)

4 5 6 7
• b =2 d=2 Time complexity = 2 2+1 = 2 3 =8

• Drawbacks of Breadth-first search:


– the memory requirements are a bigger problem than is the execution time
Uninformed Search or Blind Search Strategies

• Drawbacks of Breadth-first search:


– the memory requirements are a bigger problem than is the execution time

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
Uninformed Search or Blind Search Strategies

• 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

Expand deepest unexpanded node


• Implementation:
• fringe = LIFO queue, i.e., put successors at front
Depth-first search

Expand deepest unexpanded node


• Implementation:
– fringe = LIFO queue, i.e., put successors at front
Depth-first search

• Expand deepest unexpanded node


• Implementation:
– fringe = LIFO queue, i.e., put successors at front
Depth-first search

Expand deepest unexpanded node

• Implementation:
• fringe = LIFO queue, i.e., put successors at front
Depth-first search

• Expand deepest unexpanded node


• Implementation:
– fringe = LIFO queue, i.e., put successors at front
Depth-first search

• Expand deepest unexpanded node


• Implementation:
– fringe = LIFO queue, i.e., put successors at front
Depth-first search

• Expand deepest unexpanded node


• Implementation:
– fringe = LIFO queue, i.e., put successors at front
Depth-first search

• Expand deepest unexpanded node


• Implementation:
– fringe = LIFO queue, i.e., put successors at front
Depth-first search

• Expand deepest unexpanded node


• Implementation:
– fringe = LIFO queue, i.e., put successors at front
Depth-first search

Expand deepest unexpanded node


• Implementation:
– fringe = LIFO queue, i.e., put successors at front
Depth-first search

Expand deepest unexpanded node


• Implementation:
– fringe = LIFO queue, i.e., put successors at front
Depth-first search

Expand deepest unexpanded node


• Implementation:
– fringe = LIFO queue, i.e., put successors at front
Uninformed Search or Blind Search Strategies

• Properties of depth-first search:


• For a state space with branching factor b and maximum depth m, depth-first
search requires storage of only bm + 1 nodes, d is the shallowest solution
• Complete: No: fails in infinite-depth spaces, spaces with loops
• Modify to avoid repeated states along path
• Its complete in finite spaces only
• Time: O(bm): terrible if m is much larger than d
– but if solutions are dense, may be much faster than breadth-firs
• Space: O(bm), i.e., linear space!
• Optimal: No
• The nodes are visited in following manner:

B G H

C F I L

E J K
D
Uninformed Search or Blind Search Strategies

• Depth-first search :

Nodes that have been


expanded and have
no descendants in the
fringe can be removed
from memory; these
are shown in black.
Nodes at depth 3 are
assumed to have no
successors and M is
the only goal node.
DFS for 8 puzzle problem

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 t < d, 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).
• Depth-first search can be viewed as a special case of depth-limited search with
l= infinity.

Depth Limited search
• Example:
• On the map of Romania there are 20 cities.

• Therefore, we know that if there is a solution, it must be of length 19 at the

longest, so l = 19 is a possible choice.

• But fact is that any city can be reached from any other city at most in 9

steps.

• This number is the diameter of the state space and gives the depth limit,

• This leads to more efficient depth-limited search.

• For most problems, good depth limit is not known until problem is solved.
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

• Iterative deepening search (or iterative deepening depth-first search) is


used in combination with depth-first search, that finds the best depth limit.
• It does this by gradually increasing the limit-first 0, then 1, then 2, and so
on-until a goal is found.
• This will occur when the depth limit reaches d, the depth of the shallowest goal
node
• Iterative deepening combines the benefits of depth-first and breadth-first
search.
• Like depth-first search, its memory requirements are very modest O(bd) to be
precise.
• Like breadth-first search, it is complete when the branching factor is
finite and optimal when the path cost is a nondecreasing function of the depth
of the node.
• Do Depth Limited Search (D L S) for depth n = 0; if solution found, return
it;
• otherwise do DLS for depth n = n + 1; if solution found, return it, etc;
• So repeat DLS for all depths until solution found.
• Iterative deepening search is analogous to breadth-first search in that it
explores a complete layer of new nodes at each iteration before going on to
the next layer.
• Iterative deepening is the preferred uninformed search method when
there is a large search space and the depth of the solution is not known
Iterative deepening search l =0
Iterative deepening search l =1
Iterative deepening search l =2
Iterative deepening search l =3

Four iterations of iterative deepening search on a binary tree.


Comparison between Breadth Search Algorithm and Depth First Search
Algorithm:

Sr. No. Key BFS DFS

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.

Speed BFS is slower than DFS. DFS is faster than BFS.


4

Time Time Complexity of BFS = Time Complexity of DFS O(bm) m> d m is


Complexity O(bd+1). maximum depth
5

Space Space required is more Space required is less


Complexity
6
Bidirectional search
• Bidirectional search run s two simultaneous searches
– one forward from the initial state and
– the other backward from the goal, stopping when the two searches meet

• Bidirectional search is implemented by having one or both of the searches


check each node before it is expanded to see if it is in the fringe of the other
search tree; if so, a solution has been found
• Time and space complexity of bidirectional search is O(b d/2 )
Draw the graph /diagram BFS and DFS for Tic Tac Toe Game
Search and exploration
• Blind

Heuristic search Blind Search


Search and exploration
• Uninformed search strategies can find solutions to problems by
systematically generating new states and testing them against the goal
• Informed search strategy: Uses problem-specific knowledge beyond the
definition of the problem itself and find solutions more efficiently than an
uninformed strategy.
• The various search algos under informed search are :
1. Best-first search
1. Greedy best-first search
2. A* search: Minimizing the total estimated solution cost
3. Memory Bounded Heuristic Search :
● Iterative deepening A* (IDA)
● Recursive best-first search
Search and exploration
• Best First Search:
• It is informed heuristic search algo
• Heuristic is a function finds the most promising path.
• It takes the current state of the agent as its input and produces the estimation
of how close agent is from the goal.
• The heuristic method might not always give the best solution, but it
guaranteed to find a good solution in reasonable time.
• Heuristic function estimates how close a state is to the goal.

• 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
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.

Search and exploration
• Best-first search
• The worst case time complexity for Best First Search is O(n * Log n)
where n is number of nodes. In worst case, we may have to visit all nodes
before we reach goal.
• 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

● Star Node is A, Goal node is G


1. Open List {A, B,C,S} …… A is root node it goes in open list, it is expanded and B, C and S being successor
nodes go in the open list
Open List{B,C,S}
Close List : (A} ….. A is expanded so its moved to close list
cost AB = 100, AS=50 AC =20 , AC being smallest cost C is expanded and moved to close list
Close List : {A, C}
2. Open List (B,S, D, E} …….CD = 60, CE=100, CD being smallest D is expanded and moved to close list
Close List : {A, C, D}
2. Open List : { B,S,E, F} ….. DF=5, DE=10, DF being smallest F is expanded and moved to close list
Close List : {A, C, D, F}
2. Open List : { B,S,E, F,G} …. G is the Goal node so its moved to close list
Close List : {A, C, D, F, G}

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:

• Completeness: No – can get stuck in loops.

• Time Complexity : O(bm), but a good heuristic can give dramatic improvement

• Space Complexity: O(bm) - keeps all nodes in memory

• 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).
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

Arad to Bucharest straight distance 366

Idea: avoid expanding paths that are already expensive


g(n) = cost so far to reach n
h(n) = estimated cost from n to goal
f(n) = estimated total cost of path through n to goal
f(n)= g(n) +h(n)

Stages in an A* search for Bucharest. Nodes are labelled with f = g + h. h is straight


line distance
*
A search example
75
140
118

Sibiu = 140+253 140 = Arad to Sibiu


straight distance from Sibiu to Bucharest =253

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

Stages in an A* search for Bucharest. Nodes are labelled with f = g + h. H is straight


line distance
*
A search example

Stages in an A* search for Bucharest. Nodes are labelled with f = g + h. H is straight


line distance
*
A search example

Stages in an A* search for Bucharest. Nodes are labelled with f = g + h. H is straight


line distance
*
A search example

417+0

f -cost (450) is higher than that of Pitesti (417). Another way to


say this is that there might be a solution through Pitesti whose cost is as low as 417, so
the algorithm will not settle for a solution that costs 450.

Stages in an A* search for Bucharest. Nodes are labelled with f = g + h. H is straight


line distance
Search and exploration
• Computation time is not, however, A*'s main drawback because it keeps all
generated nodes in memory
• But memory requirements are large
• A* is not practical for many large-scale problems.
• A* is commonly used for the common pathfinding problem in applications
such as games
• Properties of A*:
● Complete : Yes

● Time Complexity : High, exponential

● Space Complexity: High as all nodes kept in memory

● Optimal : Yes

• https://www.google.com/search?q=A*+algorithm+video&oq=A*+algorithm+vid
eo&aqs=chrome..69i57j0i22i30.5370j0j7&sourceid=chrome&ie=UTF-8#fpstat
e=ive&vld=cid:7ab1a285,vid:-L-WgKMFuhE

• https://www.javatpoint.com/ai-informed-search-algorithms
• https://www.google.com/search?q=depth++first+search+algorithm+with+example+puzzle&biw=1443&bih=742&tbm=vid&ei=eh
l3Y9-xMpiOseMPxfSpyAM&ved=0ahUKEwjf1qmEgbf7AhUYR2wGHUV6CjkQ4dUDCA0&uact=5&oq=depth++first+search+
algorithm+with+example+puzzle&gs_lcp=Cg1nd3Mtd2l6LXZpZGVvEAMyBQghEKABMgUIIRCgAToECAAQQzoFCAAQgA
Q6CAgAEBYQHhAPOgYIABAWEB46BQgAEIYDOggIIRAWEB4QHToHCCEQoAEQCjoECCEQFVC_B1jMRWDSUGgAc
AB4AIAB0wGIAZEVkgEGMC4yMC4xmAEAoAEBwAEB&sclient=gws-wiz-video#fpstate=ive&vld=cid:1a89ceb2,vid:dvWk0
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

Start Node : S Goal Node : D

f(n) = g(n) + h(n)

● 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

● WE HAVE 4 PATHS WITH THE ESTIMATED FUNCTION COST F(N)


● S -> B -> C->D = (4 +2+3 )+ 0 =9
● S -> A->B->C->D = (1+2+2+3)+0 = 8
● S -> A->D = (1+12)+0 =13
● S->A->C->D = (1+5+3)+0 = 9
● THE SHORTEST COST IS S -> A->B->C->D = (1+2+2+3)+0 = 8
Search and exploration
• Memory-bounded heuristic search:

• The simplest way to reduce memory requirements for A* is to use


iterative deepening to the heuristic search context, resulting in the
Iterative-Deepening A* (IDA*) algorithm.
• The main difference between IDA* and standard iterative deepening is that
the cutoff used is the f -cost (g + h) rather than the depth;

• 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
• Recursive best-first search (RBFS) is a simple recursive algorithm works
like standard best-first search using only linear space
• 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
• Memory-bounded algorithms: RBFS and MA*
• RBFS changes the path (mind) depending on the F-cost function
• These mind changes occur because every time the current best path is
extended, there is a good chance that its f -value will increase, h is usually less
optimistic for nodes closer to the goal.
• When this happens, particularly in large search spaces, the second-best path
might become the best path, so the search has to backtrack to follow it.
• Each mind change corresponds to an iteration of IDA*, and could
require many re-expansions of forgotten nodes to recreate the best path
and extend it one more node.
• RBFS is an optimal algorithm if the heuristic function h(n) is admissible.
• Its space complexity is linear in the depth of the deepest optimal solution
• The time complexity is difficult to characterize: it depends both on the
accuracy of the heuristic function and on how often the best path changes as
nodes are expanded.
Search and exploration
• Memory-bounded algorithms, called RBFS and MA*
• Both IDA* and RBFS are subject to the exponential increase in complexity
associated with searching on as they cannot check for repeated states other
than those on the current path.
• So they may explore the same state many times.
• IDA* and RBFS use too little memory.
• Between iterations, IDA* retains only a single number ie. the current f -cost limit
and RBFS retains more information in memory, but it uses only linear space:
even if more memory were available, RBFS has no way to make use of it.
• It's always better to use all available memory.
• Two algorithms that do this are MA* (memory-bounded A*) and SMA*
(simplified MA*).
Search and exploration
• Heuristic Functions:
• Heuristic is thumb rule
• Definition by Judea Pearl : Heuristics are criteria, methods or principles for
deciding which among several alternative courses of action promises to be the
most effective in order to achieve some goal
• Heuristic function is used to identify the promising search path
• Heuristic function h(n) at node n is an estimate of optimum cost from the
current node to goal
• h(n)= estimated cost of the cheapest path from node n to goal node
• Example : Path from Kolkata to Guwahati
• Heuristic for Guwahati is straight line distance between Kolkata and Guwahati
• H(Kolkata) = Euclidean distance (Kolkata, Guwahati)
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

• The 2 advantages of local search are

– they use very little memory-usually a constant amount;

– they find reasonable solutions in large or infinite (continuous) state spaces


for which systematic algorithms are unsuitable
• Local search algorithms are useful for solving pure optimization problems, in
which find the best state according to an objective function
Search and exploration
• Local Search Algorithms And Optimization Problems
• State space landscape:
• A landscape has both "location" (defined by the state) and "elevation“
(defined by the value of the heuristic cost function or objective function).
• If elevation corresponds to cost, then the aim is to find the lowest valley-a
global minimum;
• If elevation corresponds to an objective function, then the aim is to find the
highest peak-a global maximum,.
• Local search algorithms explore this landscape.
• A complete, local search algorithm always finds a goal if one exists;
• An optimal algorithm always finds a, global minimum/maximum.
Value

A one-dimensional state space landscape in which elevation corresponds to


the objective function.
The aim is to find the global maximum.
Hill-climbing search modifies the current state to try to improve it, as shown
by the arrow.
Search and exploration
• Hill Climbing Search
• It is a loop that continually moves in the direction of increasing value i.e.
uphill.
• It terminates when it reaches a "peak" where no neighbor has a higher value.
• The algorithm does not maintain a search tree
• So the current node data structure need only record the state and its
objective function value.
• Hill-climbing does not look ahead beyond the immediate neighbors of the
current state.
• At each step the current node is replaced by the best neighbor i.e. with
highest value
• With heuristic cost estimate h is used, the neighbor with the lowest h can be
found.
• Local-search algorithms use a complete-state formulation
• Hill climbing is called greedy local search because it grabs a good
neighbor state without thinking ahead about where to go next.
• But greedy algorithms often perform quite well.
● 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.
● Global Maximum: Global maximum is the best possible state of state
space landscape. It has the highest value of objective function.
● Current state: It is a state in a landscape diagram where an agent is
currently present.
● 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.
• Hill climbing often gets stuck for the
following reasons:
– Local maxima: a local maximum is a
peak that is higher than each of its
neighboring states, but lower than the
global maximum.
– Hill-climbing algorithms that reach the
vicinity of a local maximum will be drawn
upwards towards the peak, but will then be
stuck with nowhere else to go.
– Ridges result in a sequence of local
maxima that is very difficult for greedy
algorithms to navigate.
– Plateau: a plateau is an area of the state
space landscape where the evaluation
function is flat. It can be a flat local
maximum, from which no uphill exit
exists, or a shoulder, from which it is
possible to make progress
– A hill-climbing search might be unable to
find its way off the plateau.
Search and exploration
• A hill climbing algorithm only evaluates the neighbor node state at a time and
selects the first one which optimizes current cost and set it as a current state.
• It only checks it's one successor state, and if it finds better than the current
state, then move else be in the same state.
• This algorithm is less time consuming, gives less optimal solution and at
times the solution is not guaranteed

• The success of hill climbing depends on the shape of the state-space


landscape
• If there are few local maxima and plateaux, random-restart hill climbing will
find a good solution very quickly.
• Generally many real problems have a landscape that looks more like a family
of porcupines on a flat floor, with miniature porcupines living on the tip of
each porcupine needle, ad infinitum.
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
• Variants of hill-climbing:
• Stochastic hill climbing: it chooses at random from among the uphill
moves;
– the probability of selection can vary with the steepness of the uphill
move.
– This usually converges more slowly but at times can give better solution
• First-choice hill climbing: It implements stochastic hill climbing by
generating successors randomly until one is generated that is better than the
current state.
– This is a good strategy when a state has many (e.g., thousands) of
successors.
• The Stochastic and First Choice of hill-climbing algorithms are incomplete
and they often fail to find a goal when one exists because they can get stuck
on local maxima.
• Random-restart hill climbing : It adopts the well known statement, "If at
first you don't succeed, try, try again."
– It conducts a series of hill-climbing searches from randomly
generated initial state and stopping when a goal is found.
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
Color Map
Constraint Graph
Flip Classroom
Constraint Satisfaction Problem : Crypt Arithmetic
Cryptarithmetic
• Crypt arithmetic is a type of mathematical game consisting of a
mathematical equation among unknown numbers, whose digits are represented by letters.

• The goal is to identify the value of each letter

• The equation is typically a basic operation of arithmetic, such as addition, multiplication,


or division

• 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

• Alphabets can take up only one distinct value.

• Alphabets can only take up values between 0-9.

• Decoded numbers can’t begin with 0, for example, 0813.

• Problems are uni-solutional.

• 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
Basic Approach to solve Cryptarithmetic Questions:

Following mentioned points are meant to be kept in mind while solving cryptarithmetic questions:
1. Understand the Problem:
○ Carefully read the cryptarithmetic puzzle to understand the given equations and constraints.
○ Identify the letters that represent unique digits in the equation.
2. List the Letters:
○ List all the distinct letters involved in the puzzle.
○ Assign a placeholder for each letter (e.g., A, B, C, … Z) to represent digits.
3. Formulate the Equation:
○ Convert the given words into a mathematical equation.
○ Ensure that the equation follows standard mathematical rules (addition, subtraction,
multiplication, or division).
4. Identify Constraints:
○ Identify any constraints or relationships given in the puzzle. For example, if a letter cannot
be zero (0), note it down.
5. Start with the Easiest Part:
○ Look for parts of the equation that are straightforward to solve. This may involve finding
single-digit additions or subtractions.
Basic Approach to solve Cryptarithmetic Questions:

6. Trial and Error:


a. Begin assigning possible digits to the letters, starting with the constraints and parts of the
equation with the fewest possibilities.
b. Use a systematic approach, making sure to keep track of your assignments.

7. Check for Consistency:


c. After making an assignment, check if it maintains consistency within the equation and adheres
to the constraints.
d. If a digit becomes inconsistent with other assignments, backtrack and try a different digit.

8. Iterate and Refine:

e. Continue the process of assigning digits, checking for consistency, and refining your
assignments.
f. Use logical deductions and process of elimination to narrow down possibilities.
g.
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
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.
Crypt arithmetic


S = 9 E=5 N =6 D =7 {0,1,2,3,4,5,6,7,8,9} ->
1. Now E+O=N O=0 E and N cannot have
same value so a carry should be generated {0,1,2,3,4,5,6,7,8,9} ->
+ M =1 O =0 R =8 E =5 from N+R {0,1,2,3,4,5,6,7,8,9} ->
+1 +1 2. E+O+1 = N i.e E+0 +1 =N {0,1,2,3,4,5,6,7,8,9}
3. E=5 N+R =5 , E+0+1 =N=6 R=9 So conflict
M =1 O=0 N =6 E =5 Y =2 as S=9
4. We can generate carry from D+E so that R=8
5. D+E = 7+5=12

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
1 1
• Assign a unique value from 0 to 9 to each character
S E N D 9 5 6 7
• S +M value can be max 9+8= 17 so
M value will be 1 so put value 1 at every + M O R E + 1 0 8 5
Location where there is 1 M O N E Y 1 0 6 5 2
• S should have such a value that adding 1 The number assignment is not
will generate borrow so it S should be 9 and unique n for every addition
borrows is required to be
O will get value zero(0), M =1 considered
• N + R should generate carry
• E + 1 = N (Eq 1) , N+R(+1) carry from =E+10 (Eq2)
• Substitute Eq 1 in Eq 2 eliminate N so E+1+R(+1) = E+10
• (+1) May or may not be considered so in eq. E will get cancelled R=9
• But 9 is already used. So consider +1 so R = 8 and there is carry from N+R
• D+E should also generate carry and 9 and 8 are now used 2 combinations 7+6 or 7+5 are
possible as we shud not get sum as 10, D=7 E=5 E+1 =N =6
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

F=5 O=2 U=3 R=8

+ F=5 O=2 U=3 R=8


+1
E= 1 I=0 G =4 H=7 T=6

5 2 3 4

+ 5 2 3 4
E= 1 0 4 6 8
B=7 L= 9 A= 2 C=0 K=8

B LAC K + G= 5 R= 3 E=4 E=4 N=6


+ +1 +1
GREEN
--------------------
ORANGE O=1 R= 3 A= 2 N =6 G= 5 E=4

https://tamura70.gitlab.io/web-puzzle/cryptarithm/c/out/colors.out
Cryptarithmetic

Cryptarithmetic Puzzle Solver


http://bach.istc.kobe-u.ac.jp/llp/crypt.html

POTATO+TOMATO=PUMPKIN
(168486+863486=1031972)

https://www.youtube.com/watch?v=HC6Y49iT
g1k
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:
• 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

• Propagating information through constraints:

1. Forward checking

2. Constraint propagation
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.
• Following is the The progress of a map-coloring search with forward checking

• 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:
• 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.
• Mathematical game theory, a branch of economics, views any multiagent
environment as a game provided that the impact of each agent on the others is
"significant," regardless of whether the agents are cooperative or competitive
• 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.
• The games are classified into three groups:
• Single-player pathfinding problems.
– Rubik’s Cube
– Sliding puzzle.
– Travelling Salesman Problem.
• Two-player games.
– Chess
– Checkers
– Othello
• Constraint satisfaction problems.
– 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.
• A problem space is a mathematical abstraction in a form of a tree:
– the root represents current state
– nodes represent states of the game
– edges represent moves
– leaves represent final states (win, loss or draw)
• 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.
• In Minimax the two players are called maximizer and minimizer.
• The maximizer tries to get the highest score possible while the minimizer tries to do
the opposite and get the lowest score possible.
• Every board state has a value associated with it.
• 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.

• Which move you would make as a maximizing player considering that


your opponent also plays optimally? .

•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 Game tree is as follows:

• A function implementation is required that calculates the value of the board


depending on the placement of pieces on the board. This function is often
known as Evaluation Function.
• It is sometimes also called Heuristic Function.
• The evaluation function is unique for every type of game.
• The basic idea behind the evaluation function is to give a high value for a
board if maximizer‘s turn or a low value for the board if minimizer‘s turn.
• https://www.geeksforgeeks.org/minimax-algorithm-in-game-theory-set-1-introd
uction/
Step-1: In the first step, the algorithm
generates the entire game-tree and apply
the utility function to get the utility values
for the terminal states. In the tree diagram,
let's take A is the initial state of the tree.
Suppose maximizer takes first turn which
has worst-case initial value =- infinity, and
minimizer will take next turn which has
worst-case initial value = +infinity
Step 2: Now, first we find the utilities value
for the Maximizer, its initial value is -∞, so
we will compare each value in terminal state
with initial value of Maximizer and
determines the higher nodes values. It will
find the maximum among the all.
•For node D max(-1,- -∞) =>
max(-1,4)= 4
•For Node E max(2, -∞) => max(2,
6)= 6
•For Node F max(-3, -∞) =>
max(-3,-5) = -3
•For node G max(0, -∞) = max(0, 7) =
7
Step 3: In the next step, it's a turn for
minimizer, so it will compare all nodes
value with +∞, and will find the 3rd layer
node values.
•For node B= min(4,6) = 4
•For node C= min (-3, 7) = -3

Step 3: Now it's a turn for Maximizer, and it will


again choose the maximum of all nodes value and
find the maximum value for the root node. In this
game tree, there are only 4 layers, hence we reach
immediately to the root node, but in real games,
there will be more than 4 layers.

For node A max(4, -3)= 4


Game tree for Tic Tac Toe game

Value =-1 if all 0’s are in line


Value =1 if all 1’s are in line
Value is 0 if game is locked

• 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 minimax algorithm :


• The minimax algorithm computes the minimax decision from the current
state.
• It uses a simple recursive computation of the minimax values of each
successor state, directly implementing the defining equations.
• The recursion proceeds all the way down to the leaves of the tree, and then
the minimax values are backed up through the tree as the recursion
unwinds.
• The minimax algorithm performs a complete depth-first exploration of the
game tree.
• If the maximum depth of the tree is m, and there are b legal moves at each
point, then the time complexity of the minimax algorithm is O(b m).
• The space complexity is O(bm) for an algorithm that generates all
successors at once, or O(m) for an algorithm that generates successors one
at a time
• This algorithm serves as the basis for the mathematical analysis of games
Mini Max algorithm

• The mini-max search procedure is depth first , depth limited search


procedure
• 1. Searching the game tree using min-max algo
• 2. Steps used to pick up next move:
• A)Start in Max node with current board config
• B) expand nodes down to some depth of look ahead in game
• Apply evaluate function at each of the left node
• “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 Mini-Max 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 Mini-max 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.
• The exponent cannot be eliminated but can be effectively cut to half.
• 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

• Alpha Beta Pruning algorithm :


• 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
– Apply both forms in alpha beta pruning

Search and exploration

• Alpha Beta Pruning algorithm :


• Alpha Cutoff:
• It may be found that in the current branch, an opponent can achieve a
state with a lower value for us than our achievable state in another
branch.
• So the current branch is the one that we certainly not move the game to search
of this branch can be safely terminated

Choose max No Point in exploring


this branch

15

16
Search and exploration

• Alpha Beta Pruning algorithm :


• Beta Cutt off:
• It is reverse process of alpha cut
• It may be found that in the current branch, we would be able to achieve a
state which has a higher value for us than one of the opponent can hold as
to in other branch
• The current branch can be identified as one that the opponent will certainly not
move the game to search in this branch can be safely terminated

MAX

Back up Min

No Point in exploring
this branch
20
Back up Max 25
Alpha Beta Pruning

• Alpha Beta Pruning algorithm :


• 1. Alpha-beta procedes in depth first manner
• 2. An alpha value is an initial or temporary value associated with MAX
node
– Because MAX nodes are given maximum value among the children
– An alpha value can never decrease it can only go up
• 3. A beta value is an initial or temporary value associated with MIN node
– Min nodes are given Minimum value among their children
– A beta value can never increase, it can only go down
• 4. Applying alpha cut off means We Stop search of a particular branch because
we see that we already have better opportunity elsewhere
• 5. Applying beta cut off means we stop search of a particular branch because
we see that opponent already has better opportunity elsewhere.
A two-ply game tree. The A nodes are "MAX nodes," in which it is MAX'S
turn to move, and the D nodes are "MIN nodes."
The terminal nodes show the utility values for MAX; the other nodes are
labeled with their minimax values.
MAX'S best move at the root is a1, because it leads to the successor with
the highest minimax value, and MIN'S best reply is b1, because it leads to
the successor with the lowest minimax value.
A A

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 α = -∞
α = -∞

β =+∞ β =+∞ β =+∞
β =+∞
α = -∞ α = -∞ α = -∞
α = -∞

Max Max Max Max LEVEL 3

β =+∞
α = -∞

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

alpha >= Beta


Pruning is cutting branch and not searching
Alternately have max and min players
Initialize alpha =-infinity and Beta = +infinity and left side traced first
LEVEL 1
Max =α β =+∞
α = -∞

β =+∞
β =+∞->7 LEVEL 2
Min Min α = -∞
α = -∞

β =+∞->7 β =+∞ β =+∞
β =+∞ α = -∞ α = -∞
α = -∞
α = -∞->5->7

Max Max Max Max LEVEL 3

β =+∞ -> β =+∞ ->7


10->5 α =5
α = -∞ LEVEL 4
Min Min Min Min Min Min Min Min

10 5 7 11 12 8 9 8 5 12 11 12 9 8 7 10

Level 4: Min: β =+∞ -> 10->5


Level 3: Max : compare α = -∞ with α = -∞ and β = 5 at level 4
Assign α = 5 (max value)
Level 4: Min: α = 5 β =+∞ compare β =+∞ with 7 n 11 n β =7
LEVEL 1
Max =α β =+∞
α = -∞

β =+∞ LEVEL 2
β =+∞->7
Min Min α = -∞
α = -∞

β =+∞->7 β =+∞ β =+∞
β =+∞ α = -∞ α = -∞
α = -∞
α = -∞->5->7

Max Max Max Max LEVEL 3

β =+∞ -> β =+∞ ->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 3: Max :β =+∞ α = 5, will be compared with α = 5 and β =7 so


at Level 3 β =+∞ α = 7
Level 2: Min: α = -∞ β =+∞ compared with β =+∞ and α = 7 β =7
LEVEL 1
Max =α β =+∞
α = -∞

β =+∞ LEVEL 2
β =+∞->7
Min Min α = -∞
α = -∞

β =7 β =+∞ β =+∞
β =+∞ α = -∞ -> 7 α = -∞
α = -∞
α = -∞->5->7

Max Max Max Max LEVEL 3

β =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 4: Min : α = -∞, β =7 will be compared with 12 and 8 so no change in value of β


Level 3: Max: β =7, α = -∞ compared α = -∞ β =7at level 4 so α = 7
at level 3 alpha >= Beta alpha = Beta = 7 so prune the branch on right side
Level 2 : β =7, α = -∞
LEVEL 1
Max =α β =+∞
α = -∞->7

β =+∞ LEVEL 2
β =+∞->7
Min Min α =7
α = -∞

β =+∞ β =+∞
β =+∞ β =7 α = -∞
α = -∞ -> 7 α =7
α = -∞->5->7

Max Max Max Max LEVEL 3

β =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 LEVEL 2


β =+∞->7
Min Min α =7
α = -∞

β =+∞ β =11
β =+∞ β =7 α =7
α = -∞ -> 7 α = 7->11
α = -∞->5->7 LEVEL 3

Max Max Max Max

β =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

Level 3 : β =+∞ α = 7 no change


Level 4: Min: α = 7, β = +∞ compared 11 and 12 , β = 11
Level 3 : β =+∞ α = 7 compare α = 7 with β =11 and α = 7 the α = 11
Level 2 : α = 7 and β =+∞ ->11 which will propagate to level 3 and 4
LEVEL 1
Max =α β =+∞
α = -∞->7

β =+∞ ->11 LEVEL 2


β =+∞->7
Min Min α =7
α = -∞

β =7 β =+∞ β =11
β =+∞
α = -∞ -> 7 α = 7->11 α = 7->8 LEVEL 3
α = -∞->5->7

Max Max Max Max

β =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

Level 3 : α = 7 and β =11


Level 4: α = 7 and β =11, β =11 will be compared with 9 and 8 and β =8
Level 3 : β =11 α = 7, α = 7 will be compared with β =8 and α = 7 so α =8
Level 4 : β =11 α = 8 , β =11 is compared with 7 so β =7
LEVEL 1
Max =α β =+∞
α = -∞->7 ->11

β =+∞ ->11 LEVEL 2


β =+∞->7
Min Min α =7
α = -∞

β =7 β =+∞ β =11
β =+∞
α = -∞ -> 7 α = 7->11 α = 7->8 LEVEL 3
α = -∞->5->7

Max Max Max Max

β =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

Level 4 : β =11 α = 8 , β =11 is compared with 7 and 10 so β =7


alpha >= Beta i.e 8 >=7 so prune the branch
Propogating back Level 3 : β =11 α = 7->8 Level 2: β =+∞ ->11 α = 7
Level 1: β =+∞ α = -∞->7 so α will change to 11 after comparing with 7 and 11 at level 2
Max player will have value 11
LEVEL 1
Max =α β =+∞
α = -∞->7

β =+∞ ->11 LEVEL 2


β =+∞->7
Min Min α =7
α = -∞

β =7 β =+∞ β =11
β =+∞
α = -∞ -> 7 α = 7->11 α = 7->8 LEVEL 3
α = -∞->5->7

Max Max Max Max

β =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

You might also like