You are on page 1of 172

Unit 2

Search

• Searching is the universal technique of problem solving in


Artificial Intelligence
• A search problem consists of:
– A State Space. Set of all possible states where you can be.
– A Start State. The state from where the search begins.
– A Goal Test. A function that looks at the current state
returns whether or not it is the goal state.
– The Solution to a search problem is a sequence of actions,
called the plan that transforms the start state to the goal
state.

2
Search Problem Components
• Search tree: A tree representation of search problem is called
Search tree. The root of the search tree is the root node which is
corresponding to the initial state.
• Actions: It gives the description of all the available actions to the
agent.
• Transition model: A description of what each action do, can be
represented as a transition model.
• Path Cost: It is a function which assigns a numeric cost to each
path.
• Solution: It is an action sequence which leads from the start
node to the goal node.
• Optimal Solution: If a solution has the lowest cost among all
solutions.
3
General Search Algorithm

4
Examples of Search Algorithm

• TSP
• 8 Puzzle problem
• Tic Tac Toe
• N-queen problem
• Tower of Hanoi
• Water Jug Problem
• Blocks World
• Vacuum Cleaner

5
Vacuum Cleaner

6
8 Puzzle Problem

7
Parameters for Search Evaluation
• Completeness: Is the algorithm guaranteed to find a solution if
there exist one?
• Optimality: Does the algorithm find the optimal solution?
• Time complexity: How long does it take for the algorithm to
find a solution?
• Space complexity: How much memory is consumed in finding
the solution?

8
Uninformed and Heuristic Search Strategie
• Based on the information about the problem available for the
search strategies, the search algorithms are classified into
uninformed (Blind) and informed (or heuristic) search algorithms.
• For the uninformed search algorithms, the strategies have no
additional information about the states beyond that provided by
the problem definition. Generate successors and differentiate
between goal and non-goal states.
• Strategies that know whether one non-goal state
is more promising than the other is called informed search
strategies or heuristic search strategies.

9
Uninformed Search

• The uninformed search does not contain any domain knowledge


such as closeness, the location of the goal.
• It operates in a brute-force way as it only includes information
about how to traverse the tree and how to identify leaf and goal
nodes.
• Uninformed search applies a way in which search tree is searched
without any information about the search space like initial state
operators and test for the goal, so it is also called blind search.
• It examines each node of the tree until it achieves the goal node.

10
Uninformed Search methods

11
BFS
• Breadth-first search is the most common search strategy for
traversing a tree or graph. This algorithm searches
breadthwise in a tree or graph, so it is called breadth-first
search.
• BFS algorithm starts searching from the root node of the tree
and expands all successor node at the current level before
moving to nodes of next level.
• The breadth-first search algorithm is an example of a general-
graph search algorithm.
• Breadth-first search implemented using FIFO queue data
structure.

12
Breadth First Search(BFS)

• Breadth First Search (BFS) is an algorithm for traversing an unweighted Graph or


a Tree.
• BFS starts with the root node and explores each adjacent node before exploring
node(s) at the next level.
• BFS makes use of Queue for storing the visited nodes of the graph / tree.
Steps
• Choose any node in the graph, designate it as source node and mark it as visited.
• Using the adjacency matrix of the graph find all the unvisited adjacent nodes to
the search node and enqueue them into the queue Q
• Then the node is dequeued from the queue. Mark that node as visited and
designate it as the new search node.
• Repeat step 2 and 3 using the new search node.
• This process continues until the queue Q which keeps track of the adjacent
nodes is empty.
Breadth-First Search Algorithm:
BFS Algorithm

15
Child Node

16
Example-1
EXAMPLE-2
Breadth-First Search Algorithm:
Breadth-First Search Algorithm:
Breadth-First Search Algorithm:
Breadth-First Search Algorithm:
Breadth-First Search Algorithm:
Features of Breadth-First Search Algorithm:
• Space complexity
• In the breadth-first search algorithm, all the nodes at a particular level must
be saved until their child nodes in the next level have been generated.
• The space complexity is therefore proportional to the number of nodes at
the deepest level of the graph. Given a graph with branching factor b
(number of children at each node) and depth d, the asymptotic space
complexity is the number of nodes at the deepest level O(bd).
• Time complexity
• In the worst case, breadth-first search has to traverse through all paths to all
possible nodes, thus the time complexity of this algorithm asymptotically
approaches O(bd).
• However, the time complexity can also be expressed as O( | E | + | V | ),
since every vertex and every edge will be explored in the worst case.
Application of BFS
• GPS Navigation systems
• Computer Networks
• Face book
Find the solution:
Depth First Search (DFS)

• DFS is an algorithm for traversing a Graph or a Tree.


• DFS starts with the root node and explores all the nodes along the depth of the selected path
before backtracking to explore the next path.
• DFS makes use of Stack for storing the visited nodes of the graph / tree.
Steps
• Choose any node in the graph. Designate it as the search node and mark it as
visited.
• Using the adjacency matrix of the graph, find a node adjacent to the search node
that has not be visited yet. Designate this as the new search node and mark it as
visited.
• Repeat step 2 using the new search node. If no nodes satisfying (2) can be found
return to the previous search node and continue from there.
• When a return to the previous search node in (3) is impossible, the search from
the originally chosen search node is complete.
• If the graph still contains unvisited nodes, choose any node that has not been visited and repeat
step 1 through 4.
Depth-first Search Algorithm:
Example-1
EXAMPLE-2
Depth-first Search Algorithm:
Consider the graph and find the spanning tree,
Depth-first Search Algorithm:
Depth-first Search Algorithm:
Data structure used for storing graph or tree :
AdjacencyList
Data structure used for depth first search : 
Stack
Time complexity of depth first search :
O ( V + E ) for an adjacency list implementation of a
graph or a tree. ‘V’ is the number of vertices
and ‘E’ is the number of edges in a graph/tree.
Applications of DFS
• Detecting cycle in a graph 
•  Path Finding
• Solving puzzles with only one solution, such
as mazes
Find the solution
Uniform Cost search
• Uniform-cost search is a searching algorithm used for traversing a
weighted tree or graph.
• This algorithm comes into play when a different cost is available for each
edge.
• The primary goal of the uniform-cost search is to find a path to the goal
node which has the lowest cumulative cost. Uniform-cost search expands
nodes according to their path costs form the root node.
• It can be used to solve any graph/tree where the optimal cost is in
demand.
• A uniform-cost search algorithm is implemented by the priority queue.
• It gives maximum priority to the lowest cumulative cost. Uniform cost
search is equivalent to BFS algorithm if the path cost of all edges is the
same.
44
Uniform Cost search - Algorithm

45
Uniform Cost search - Example

46
Uniform Cost search- Working

47
Uniform Cost search - Implementatio

48
Uniform Cost search - Implementation

49
Uniform Cost search- Time and space

50
Depth Limited Search

• A depth-limited search algorithm is similar to depth-first search with


a predetermined limit.
• Depth-limited search can solve the drawback of the infinite path in
the Depth-first search.
• In this algorithm, the node at the depth limit will treat as it has no
successor nodes further.

51
DLS

52
DLS - Algorithm

53
DLS - Limitation
• Standard failure value: It indicates that problem does not have any
solution.
• Cutoff failure value: It defines no solution for the problem within a
given depth limit.
• Advantages:
– Depth-limited search is Memory efficient.
• Disadvantages:
– Depth-limited search also has a disadvantage of incompleteness.
– It may not be optimal if the problem has more than one solution

54
DLS - Working

55
DLS – Time and Space

• Completeness: DLS search algorithm is complete if the solution is


above the depth-limit.
• Time Complexity: Time complexity of DLS algorithm is O(bℓ).
• Space Complexity: Space complexity of DLS algorithm is O(b×ℓ).
• Optimal: Depth-limited search can be viewed as a special case of
DFS, and it is also not optimal even if ℓ>d.

56
Iterative Deepening Depth First Search(IDDFS)

• A search algorithm which suffers neither the drawbacks of


breadth-first nor depth-first search on trees is depth-first iterative-
deepening
• IDDFS combines depth-first search’s space-efficiency and breadth-
first search’s fast search (for nodes closer to root).
• Iterative deepening depth first search (IDDFS) is a hybrid of BFS
and DFS. In IDDFS, we perform DFS up to a certain “limited depth,”
and keep increasing this “limited depth” after every iteration
• The idea is to perform depth-limited DFS repeatedly, with an
increasing depth limit, until a solution is found

57
IDDFS-Example 1

58
IDDFS

59
IDDFS –Example-2
• When the graph has cycles. This is interesting
as there is no visited flag in IDDFS.
Solution
IDDFS – Time and Space

62
Bidirectional Search
• Bidirectional search is a graph search algorithm which find
smallest path from source to goal vertex. It runs two
simultaneous search – 
• Forward search from source/initial vertex toward goal vertex
• Backward search from goal/target vertex toward source vertex
• Bidirectional search replaces single search graph(which is
likely to grow exponentially) with two smaller sub graphs –
one starting from initial vertex and other starting from goal
vertex. 
• The search terminates when two graphs intersect.
Example

Suppose we want to find if there exists a path from vertex 0 to vertex


14. Here we can execute two searches, one from vertex 0 and other
from vertex 14. When both forward and backward search meet at
vertex 7, we know that we have found a path from node 0 to 14 and
search can be terminated now

Path exist between 0 and 14


Intersection at: 7
*****Path*****
0 4 6 7 8 10 14
When to use bidirectional approach?
We can consider bidirectional approach when- 
• Both initial and goal states are unique and completely defined.
• The branching factor is exactly the same in both directions.
Performance measures
• Completeness : Bidirectional search is complete if BFS is used in
both searches.
• Optimality : It is optimal if BFS is used for search and paths have
uniform cost.
• Time and Space Complexity : Time and space complexity
is O(bd/2).
Informed Search

• Informed Search algorithms have information on the goal


state which helps in more efficient searching.
• This information is obtained by a function that estimates how
close a state is to the goal state.
• Informed search algorithm uses the idea of heuristic, so it is
also called Heuristic search.

66
Heuristic Search

• Add domain-specific information to select what is the best path


to continue searching along
• Define a heuristic function, h(n), that estimates the "goodness"
of a node n. Specifically, h(n) = estimated cost (or distance) of
minimal cost path from n to a goal state.
• The term heuristic means "serving to aid discovery" and is an
estimate, based on domain-specific information that is
computable from the current state description, of how close
we are to a goal

67
Generate and Test
• Generate-and-test search algorithm is a very simple algorithm
that guarantees to find a solution if done systematically and
there exists a solution.
• The generate and Test algorithm is a depth first search
procedure because complete possible solutions are generated
before test.
• This can be implemented states are likely to appear often in a
tree
• It can be implemented on a search graph rather than a tree

68
Heuristic function
• Heuristic is a function which is used in Informed Search, and it 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, however, 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
• h(n) <= h*(n)  
• Here h(n) is heuristic cost, and h*(n) is the estimated cost. Hence
heuristic cost should be less than or equal to the estimated cost.
69
Generate and Test

70
Generate and Test - Example

• The delivery robot must carry out a number of delivery activities, A, B, C,


D, and E. Suppose that each activity happens at any of times 1, 2, 3, or 4
• {B≠3), (C≠2), (A≠B), ( B≠C), (C<D), (A=D),
(E<A), (E<B), (E<C), (E<D), ( B≠D)}
A– 14 4
B–124 2
C- 134 3
D- 4 4
E 1

71
TSP - Example

1–3–5–7 -6 – 4–2
72
Pure Heuristic Search:

• Pure heuristic search is the simplest form of heuristic search


algorithms. It expands nodes based on their heuristic value h(n). It
maintains two lists, OPEN and CLOSED list. In the CLOSED list, it
places those nodes which have already expanded and in the OPEN
list, it places nodes which have yet not been expanded.
• On each iteration, each node n with the lowest heuristic value is
expanded and generates all its successors and n is placed to the
closed list. The algorithm continues unit a goal state is found.
• In the informed search we will discuss two main algorithms which
are given below:
• Best First Search Algorithm(Greedy search)
• A* Search Algorithm
Best First Search

• Best first search is a traversal technique that decides which


node is to be visited next by checking which node is the most
promising one and then check it.
• Best first search is an instance of graph search algorithm in
which a node is selected for expansion based on evaluation
function f (n)
• Best first search can be implemented a priority queue, a data
structure that will maintain the fringe in ascending order of f
values.

74
Best-first Search Algorithm (Greedy Search):

• Greedy best-first search algorithm always selects the path which


appears best at that moment. It is the combination of depth-first
search and breadth-first search algorithms. It uses the heuristic
function and search. Best-first search allows us to take the
advantages of both algorithms. With the help of best-first search, at
each step, we can choose the most promising node. In the best first
search algorithm, we expand the node which is closest to the goal
node and the closest cost is estimated by heuristic function, i.e.
• f(n)= g(n).   
• Were, h(n)= estimated cost from node n to the goal.
• The greedy best first algorithm is implemented by the priority
queue.
Best First Search - Algorithm
Step 1: Place the starting node into the OPEN list.
Step 2: If the OPEN list is empty, Stop and return failure.
Step 3: Remove the node n, from the OPEN list which has the
lowest value of h(n), and places it in the CLOSED list.
Step 4: Expand the node n, and generate the successors of node n.
Step 5: Check each successor of node n, and find whether any
node is a goal node or not. If any successor node is goal node, then
return success and terminate the search, else proceed to Step 6.
Step 6: For each successor node, algorithm checks for evaluation
function f(n), and then check if the node has been in either OPEN
or CLOSED list. If the node has not been in both list, then add it to
the OPEN list.
Step 7: Return to Step 2.

76
Adv and Disadv

Advantages:
Best first search can switch between BFS and DFS by gaining
the advantages of both the algorithms.
This algorithm is more efficient than BFS and DFS algorithms.
Disadvantages:
It can behave as an unguided depth-first search in the worst
case scenario.
It can get stuck in a loop as DFS.
This algorithm is not optimal.
Applications of BFS
• Best-first search and its more advanced
variants have been used in such
applications as games and web crawlers.
• In a web crawler, each web page is
treated as a node, and all the hyperlinks
on the page are treated as unvisited
successor nodes.
BFS - Example

79
Solution
• Expand the nodes of S and put in the CLOSED list
• Initialization: Open [A, B], Closed [S]
• Iteration 1: Open [A], Closed [S, B]
• Iteration 2: Open [E, F, A], Closed [S, B]
                  : Open [E, A], Closed [S, B, F]
• Iteration 3: Open [I, G, E, A], Closed [S, B, F]
                  : Open [I, E, A], Closed [S, B, F, G]
• Hence the final solution path will be: 
S----> B----->F----> G
Time and Space complexity
Time Complexity: The worst case time complexity of Greedy
best first search is O(bm).
Space Complexity: The worst case space complexity of Greedy
best first search is O(bm). Where, m is the maximum depth of
the search space.
Complete: Greedy best-first search is also incomplete, even if
the given state space is finite.
Optimal: Greedy best first search algorithm is not optimal.
A* Search

• It is an informed search algorithm, as it uses information


about path cost and also uses heuristics to find the solution.
• To find the shortest path between nodes or graphs
• A * algorithm is a searching algorithm that searches for the
shortest path between the initial and the final state
• It is an advanced BFS that searches for shorter paths first
rather than the longer paths.
• A* is optimal as well as a complete algorithm

82
A* Search
• It calculates the cost, f(n) (n being the neighboring node), to travel to all of
the neighboring nodes, and then enters the node with the lowest value
of f(n).
• These values are calculated with the following formula:
f(n)=g(n)+h(n)
• g(n) being the value of the shortest path from the start node to node n,
and h(n) being a heuristic approximation of the node's value.

83
Heuristic value h(n)

• A given heuristic function h(n) is admissible if it never overestimates the


real distance between n and the goal node.
• Therefore, for every node n , h(n)≤ h*(n)
• h*(n) being the real distance between n and the goal node.
• A given heuristic function h(n) is consistent if the estimate is always less
than or equal to the estimated distance between the goal n and any given
neighbor, plus the estimated cost of reaching that neighbor: c(n,m)
+h(m)≥h(n)
• c(n,m) being the distance between nodes n and m. Additionally, if h(n) is
consistent, then we know the optimal path to any node that has been
already inspected. This means that this function is optimal.

84
A* Search Algorithm
Step1: Place the starting node in the OPEN list.
Step 2: Check if the OPEN list is empty or not, if the list is empty
then return failure and stops.
Step 3: Select the node from the OPEN list which has the smallest
value of evaluation function (g+h), if node n is goal node then
return success and stop, otherwise
Step 4: Expand node n and generate all of its successors, and put n
into the closed list. For each successor n', check whether n' is
already in the OPEN or CLOSED list, if not then compute evaluation
function for n' and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it
should be attached to the back pointer which reflects the lowest
g(n') value.
Step 6: Return to Step 2.
85
Adv and Disadv

Advantages:
A* search algorithm is the best algorithm than other search
algorithms.
A* search algorithm is optimal and complete.
This algorithm can solve very complex problems.
Disadvantages:
It does not always produce the shortest path as it mostly
based on heuristics and approximation.
A* search algorithm has some complexity issues.
The main drawback of A* is memory requirement as it
keeps all generated nodes in the memory, so it is not
practical for various large-scale problems.
Difference between BFS and A*
S Parameters     Best-First Search       A* Search     

The evaluation function for best- The evaluation function for A*


 1 Evaluation Function first search is f(n) = h(n). search is f(n) = h(n) + g(n).

This search algorithm does not This search algorithm involves


 2 Past Knowledge involve past knowledge. past knowledge.

 3 Completeness Best-first search is not complete. A* search is complete.

Best-first search is not optimal as


A* search is optimal as the path
 4 Optimal the path found may not be
found is always optimal.
optimal.

Its time complexity is O(bm) and


Its time complexity is O(bm) and
space complexity can be
Time and Space polynomial. space complexity is also O(bm).
 5 Complexity where b is the branching and m is where b is the branching and m is
the maximum depth of the search
the maximum depth of the search
tree 
tree 

 6 Memory  It requires less memory. It requires more memory.

It keeps all the fringe or border


It keeps all the nodes in the
 7 Type of nodes kept nodes in the memory while
memory while searching.
searching.
Applications of A*
• It is commonly used in web-based maps
and games to find the shortest path at the
highest possible efficiency.
• A* is used in many artificial intelligence
applications, such as search engines.
• It is used in other algorithms such as the
Bellman-Ford algorithm to solve the
shortest path problem.
Example
• In this example, we will traverse the given graph using the A* algorithm. The
heuristic value of all states is given in the below table so we will calculate the f(n)
of each state using the formula f(n)= g(n) + h(n), where g(n) is the cost to reach any
node from start state.
• Here we will use OPEN and CLOSED list.
Solution
• Initialization: {(S, 5)}
• Iteration1: {(S--> A, 4), (S-->G, 10)}
• Iteration2: {(S--> A-->C, 4), (S--> A-->B, 7), (S-->G, 10)}
• Iteration3: {(S--> A-->C--->G, 6), (S--> A-->C--->D, 11), (S--> A-->B, 7), (S-->G, 10)}
• Iteration 4 will give the final result, as
S--->A--->C--->G it provides the optimal path with cost 6.
Points to remember:
• A* algorithm returns the path which occurred first, and it does not search for all
remaining paths.
• The efficiency of A* algorithm depends on the quality of heuristic.
• A* algorithm expands all nodes which satisfy the condition f(n)<="" li="">
Complete: A* algorithm is complete as long as:
• Branching factor is finite.
• Cost at every action is fixed.
Optimal: A* search algorithm is optimal if it follows below two conditions:
Admissible: the first condition requires for optimality is that h(n) should be an
admissible heuristic for A* tree search. An admissible heuristic is optimistic in nature.
Consistency: Second required condition is consistency for only A* graph-search.
• If the heuristic function is admissible, then A* tree search will always find the least
cost path.
Time and Space complexity
• Time Complexity: The time complexity of A*
search algorithm depends on heuristic
function, and the number of nodes expanded
is exponential to the depth of solution d. So
the time complexity is O(b^d), where b is the
branching factor.
• Space Complexity: The space complexity of A*
search algorithm is O(b^d)
Example-2
• Given an initial state of a 8-puzzle problem
and final state to be reached-

Find the most cost-effective path to reach the final state from initial
state using A* Algorithm.
Consider g(n) = Depth of node and h(n) = Number of misplaced tiles.
Solution
• A* Algorithm maintains a tree of paths
originating at the initial state.
• It extends those paths one edge at a time.
• It continues until final state is reached.
AO* Algorithm

• AO* Algorithm basically based on  problem


decompositon (Breakdown problem into small
pieces)
• When a problem can be divided into a set of sub
problems, where each sub problem can be solved
separately and a combination of these will be a
solution, AND-OR graphs or AND - OR trees are used
for representing the solution. 
• The decomposition of the problem or problem
reduction generates AND arcs.
AND-OR Graph 

The figure shows an AND-OR graph 


To pass any exam, we have two options, either cheating or hard work.
In this graph we are given two choices, first do cheating or (The red line) work
hard and (The arc) pass. 
When we have more than one choice and we have to pick one, we apply OR
condition to choose one.(That's what we did here).
Basically the ARC here denote AND condition.
Here we have replicated the arc between the work hard and the pass because
by doing the hard work possibility of passing an exam is more than cheating.
A* Vs AO*

• Both are part of informed search technique and


use heuristic values to solve the problem.
• The solution is guaranteed in both algorithm.
• A*  always gives an optimal solution (shortest
path with low cost) But It is not guaranteed to
that AO*  always provide an optimal solutions.
• Reason: Because AO* does not explore all the
solution path once it got solution.
Example
• The algorithm always moves towards a lower cost value.
• Basically, We will calculate the cost function here (F(n)= G (n) +
H (n))
• H:  heuristic/ estimated  value of the nodes. and G: actual
cost or edge value (here unit value).
• Here we have taken the edges value 1 , meaning we have to
focus solely on the heuristic value.
• The Purple color values are edge values (here all are same that
is one).
• The Red color values are Heuristic values for nodes.
• The Green color values are New Heuristic values for nodes.
Procedure:
• In the above diagram we have two ways from A to D or A to B-C (because of and
condition). calculate cost to select a path
• F(A-D)= 1+10 = 11            and               F(A-BC) = 1 + 1 + 6 +12 = 20
• As we can see F(A-D) is less than F(A-BC) then the algorithm choose the
path F(A-D).
• Form D we have one choice that is F-E.
• F(A-D-FE) = 1+1+ 4 +4 =10
• Basically 10 is the cost of reaching FE from D. And Heuristic value of node D also
denote the cost of reaching FE from D. So, the new Heuristic value of D is 10. 
• And the Cost from A-D remain same that is 11.
• Suppose we have searched this path and we have got the Goal State, then we
will never explore the other path. (this is what AO* says  but here we are going
to explore other path as well to see what happen)
Let's Explore the other path:
• In the above diagram we have two ways from A to D or A to B-C (because of and condition). calculate cost to
select a path
• F(A-D)= 1+10 = 11            and               F(A-BC) = 1 + 1 + 6 +12 = 20
• As we know the cost is more of F(A-BC) but let's take a look 
• Now from B we have two path G and H , let's calculate the cost
• F(B-G)= 5+1 =6    and F(B-H)= 7 + 1 = 8
• So, cost from F(B-H) is more than F(B-G) we will take the path B-G.
• The Heuristic value from G to I is 1 but let's calculate the cost form G to I.
• F(G-I) = 1 +1 = 2. which is less than Heuristic value 5. So, the new Heuristic value form G to I is 2.
• If it is a new value, then the cost from G to B must also have changed. Let's see the new cost form (B to G)
• F(B-G)= 1+2 =3 . Mean the New Heuristic value of B is 3.
• But A is associated with both B and C .
• As we can see from the diagram C only have one choice or one node to explore that is J. The Heuristic value
of C is 12.
• Cost form C to J= F(C-J) = 1+1= 2 Which is less than Heuristic value 
• Now the New Heuristic value of C is 2. 
• And the New Cost from A- BC that is F(A-BC) = 1+1+2+3 = 7 which is less than F(A-D)=11. 
• In this case Choosing path A-BC is more cost effective and good than that of A-D.
• But this will only happen when the algorithm explores this path as well. But according to the algorithm,
algorithm will not accelerate this path (here we have just did  it to see how the other path can also be
correct).
• But it is not the case in all the cases that it will happen in some cases that the algorithm will get optimal
solution.
Local Search
• Local search is a heuristic method for solving computationally
hard optimization problems.
• Local search can be used on problems that can be formulated as finding a
solution maximizing a criterion among a number of candidate solutions.
• Local search algorithms move from solution to solution in the space of
candidate solutions (the search space) by applying local changes, until a
solution deemed optimal is found or a time bound is elapsed.
• Advantages: Very little memory — usually constant amount- Can often
find reasonable solutions in infinite (continuous) state spaces
• Examples
Hill Climbing
Local Beam Search
Genetic ALgorithm
Hill Climbing-Introduction
• Hill climbing algorithm is a local search algorithm which continuously
moves in the direction of increasing elevation/value to find the peak of the
mountain or best solution to the problem.

It terminates when it reaches a peak value where no neighbor has a


higher value.

• Hill climbing algorithm is a technique which is used for optimizing the


mathematical problems. One of the widely discussed examples of Hill
climbing algorithm is Traveling-salesman Problem in which we need to
minimize the distance traveled by the salesman.
Hill Climbing
• Given - a large set of inputs and a good heuristic
function,
the algorithm tries to find the best possible solution
to the problem in the most reasonable time period.
• This solution may not be the absolute best(global
optimal maximum) but it is sufficiently good
considering the time allotted.
• Hill-climbing solves the problems where we need
to maximize or minimize a given real function by
selecting values from the given inputs.
Features of Hill Climbing:
• Generate and Test variant: Hill Climbing is the
variant of Generate and Test method. The Generate
and Test method produce feedback which helps to
decide which direction to move in the search space.
• Greedy approach: Hill-climbing algorithm search
moves in the direction which optimizes the cost.
• No backtracking: It does not backtrack the search
space, as it does not remember the previous states.
Examples for Hill Climbing
• Travelling Salesperson
• 8 Puzzle problem
• N queens problem
Hill Climbing terminologies
• The state-space landscape is a graphical
representation of the hill-climbing algorithm
which is showing a graph between various
states of algorithm and Objective
function/Cost.
• On Y-axis we have taken the function which
can be an objective function or cost function,
and state-space on the x-axis.
• If the function on Y-axis is cost then, the
goal of search is to find the global minimum
and local minimum.
• If the function of Y-axis is Objective
function, then the goal of the search is to find
the global maximum and local maximum.
Different regions in the state space landscape

• 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.
Problems with Hill Climbing
• Local Maxima – closest to global maxima and
becomes an approximate solution
• Plateau - your value doesn't change much if you
move in any direction.
• Ridges - your value doesn't change much if you
move in one direction, but it falls a lot if you move
in the other directions.
Problems in Hill Climbing Algorithm:

1. Local Maximum: A local maximum is a peak state in the


landscape which is better than each of its neighboring states,
but there is another state also present which is higher than
the local maximum.
Solution: Backtracking technique can be a solution of the local
maximum in state space landscape. Create a list of the
promising path so that the algorithm can backtrack the search
space and explore other paths as well.
Problems in Hill Climbing Algorithm:

2. Plateau: A plateau is the flat area of the search space in


which all the neighbor states of the current state contains the
same value, because of this algorithm does not find any best
direction to move. A hill-climbing search might be lost in the
plateau area.
Solution: The solution for the plateau is to take big steps or
very little steps while searching, to solve the problem.
Randomly select a state which is far away from the current
state so it is possible that the algorithm could find non-plateau
region.
Problems in Hill Climbing Algorithm:

3. Ridges: A ridge is a special form of the local maximum. It


has an area which is higher than its surrounding areas, but
itself has a slope, and cannot be reached in a single move.
• Solution: With the use of bidirectional search, or by moving
in different directions, we can improve this problem.
Algorithm
The algorithm is as follows :
• Step1: Generate possible
solutions.
• Step2: Evaluate to see if this is
the expected solution.
• Step3: If the solution has been
found quit else go back to step 1.
Hill Climbing Algorithm
H=5
Solution to Hill climbing
• Random initialization of Starting Point
Local Search Algorithm-
Simulated Annealing
• A hill-climbing algorithm which never makes a move towards a lower
value guaranteed to be incomplete because it can get stuck on a local
maximum.
• If algorithm applies a random walk, by moving a successor, then it may
complete but not efficient. 
• Simulated Annealing is an algorithm which yields both efficiency and
completeness.
• In mechanical term Annealing is a process of hardening a metal or glass
to a high temperature then cooling gradually, so this allows the metal to
reach a low-energy crystalline state. 
• The same process is used in simulated annealing in which the algorithm
picks a random move, instead of picking the best move. 
•  If the random move improves the state, then it follows the same path.
Otherwise, the algorithm follows the path which has a probability of
less than 1 or it moves downhill and chooses another path.
Simulated Annealing
Algorithm
• First generate a random solution
• Calculate it’s cost using some cost function
• Generate a random neighbour solution and
calculate it’s cost
• Compare the cost of old and new random
solution
• If C old > C new then go for old solution
otherwise go for new solution
• Repeat steps 3 to 5 until you reach an
acceptable optimized solution of given problem
Local Beam Search
• A heuristic search algorithm that examines a graph
by extending the most promising node in a limited
set is known as beam search.
• Beam search is a heuristic search technique that
always expands the W number of the best nodes at
each level. It progresses level by level and moves
downwards only from the best W nodes at each level.
• Beam Search uses breadth-first search to build its
search tree. 
• Beam search uses breadth-first search to build its search tree.
• At each level of the tree, it generates all successors of the states at the
current level, sorting them in increasing order of heuristic cost. However, it
only stores a predetermined number (β), of best states at each level called
the beamwidth. Only those states are expanded next.
• The greater the beam width, the fewer states are pruned.
• No states are pruned with infinite beam width, and beam search is identical
to breadth-first search.
• The beamwidth bounds the memory required to perform the search. Since a
goal state could potentially be pruned, beam search sacrifices completeness
(the guarantee that an algorithm will terminate with a solution if one exists).
• Beam search is not optimal, which means there is no guarantee that it will
find the best solution. In general, beam search returns the first solution
found.
• Once reaching the configured maximum search depth (i.e., translation
length), the algorithm will evaluate the solutions found during a search at
various depths and return the best one that has the highest probability.
Example
Input: Start & Goal States. Select top W elements from OPEN list and
Local Variables: OPEN, NODE, SUCCS, put it in
W_OPEN, FOUND W_OPEN list and empty the OPEN list.
Output: Yes or No (yes if the search is for each NODE from W_OPEN list
successfully done) {
if NODE = Goal,
Start then FOUND = true
Take the inputs else
NODE = Root_Node & Found = False Find SUCCs of NODE. If any with its
If : Node is the Goal Node, estimated
Then Found = True, cost & Store it in OPEN list
Else : }
Find SUCCs of NODE if any, with its }
estimated cost& If FOUND = True,
store it in OPEN List then return Yes
While (Found == false & not able to proceed else
further), do return No
{ Stop
Sort OPEN List
Local Beam Search
Optimization
• Optimization is the process of making something better. In any
process, we have a set of inputs and a set of outputs.

• Optimization refers to finding the values of inputs in such a way


that we get the “best” output values. The definition of “best”
varies from problem to problem, but in mathematical terms, it
refers to maximizing or minimizing one or more objective
functions, by varying the input parameters.
• The set of all possible solutions or values which the inputs can
take make up the search space. In this search space, lies a point
or a set of points which gives the optimal solution. The aim of
optimization is to find that point or set of points in the search
space.
Genetic Algorithms
• Genetic Algorithm (GA) is a search-based optimization
technique based on the principles of Genetics and Natural
Selection. It is frequently used to find optimal or near-optimal
solutions to difficult problems which otherwise would take a
lifetime to solve. It is frequently used to solve optimization
problems, in research, and in machine learning.
• Genetic Algorithms are sufficiently randomized in nature, but
they perform much better than random local search.
• GA was developed by John Holland and his students and
colleagues at the University of Michigan, most notably David
E. Goldberg and has since been tried on various optimization
problems with a high degree of success.
Genetic Algorithms
• In GAs, we have a pool or a population of possible solutions to the given problem.
These solutions then undergo recombination and mutation (like in natural genetics),
producing new children, and the process is repeated over various generations.
• Each individual (or candidate solution) is assigned a fitness value (based on its objective
function value) and the fitter individuals are given a higher chance to mate and yield more
“fitter” individuals. This is in line with the Darwinian Theory of “Survival of the Fittest”.
• In this way we keep “evolving” better individuals or solutions over generations, till we reach a
stopping criterion.
• Advantages of GAs
•GAs have various advantages which have made them immensely popular. These include −
 Does not require any derivative information (which may not be available for many real-world problems).

 Is faster and more efficient as compared to the traditional methods.


 Has very good parallel capabilities.
 Optimizes both continuous and discrete functions and also multi-objective problems.
 Provides a list of “good” solutions and not just a single solution.
 Always gets an answer to the problem, which gets better over the time.
 Useful when the search space is very large and there are a large number of parameters
involved.
• Limitations of GAs
•Like any technique, GAs also suffer from a few limitations. These include −
 GAs are not suited for all problems, especially problems which are simple and for which
derivative information is available.

 Fitness value is calculated repeatedly which might be computationally expensive for some
problems.

 Being stochastic, there are no guarantees on the optimality or the quality of the solution.

 If not implemented properly, the GA may not converge to the optimal solution.

• GA – Motivation
– Genetic Algorithms have the ability to deliver a “good-enough” solution “fast-enough”. This
makes genetic algorithms attractive for use in solving optimization problems.
Basic Terminology
• It is essential to be familiar with some basic terminology:
 Population − It is a subset of all the possible (encoded) solutions to the
given problem. The population for a GA is analogous to the population for
human beings except that instead of human beings, we have Candidate
Solutions representing human beings.
 Chromosomes − A chromosome is one such solution to the given problem.
 Gene − A gene is one element position of a chromosome.
 Allele − It is the value a gene takes for a particular chromosome.
•The basic structure of a GA is as follows −
•We start with an initial population (which may be generated at random or seeded by
other heuristics), select parents from this population for mating. Apply crossover and
mutation operators on the parents to generate new off-springs. And finally, these off-
springs replace the existing individuals in the population and the process repeats. In
this way genetic algorithms actually try to mimic the human evolution to some
extent.
•Genetic algorithms simulate the process of natural selection which means
those species who can adapt to changes in their environment are able to survive
and reproduce and go to next generation. In simple words, they simulate “survival
of the fittest” among individual of consecutive generation for solving a
problem. Each generation consist of a population of individuals and each
individual represents a point in search space and possible solution. Each
individual is represented as a string of character/integer/float/bits. This string is
analogous to the Chromosome.
• Search space
• The population of individuals are maintained within search space. Each
individual represents a solution in search space for given problem. Each
individual is coded as a finite length vector (analogous to chromosome) of
components. These variable components are analogous to Genes. Thus a
chromosome (individual) is composed of several genes (variable components)
• Fitness Score
• A Fitness Score is given to each individual which shows the ability of an
individual to “compete”. The individual having optimal fitness score (or near
optimal) are sought. 
• The GAs maintains the population of n individuals (chromosome/solutions)
along with their fitness scores. The individuals having better fitness scores are
given more chance to reproduce than others. The individuals with better fitness
scores are selected who mate and produce better offspring by combining
chromosomes of parents. 
Operators of Genetic Algorithms
• Once the initial generation is created, the algorithm evolves the generation
using following operators – 
1) Selection Operator: The idea is to give preference to the individuals
with good fitness scores and allow them to pass their genes to successive
generations. 
2) Crossover Operator: This represents mating between individuals. Two
individuals are selected using selection operator and crossover sites are
chosen randomly. Then the genes at these crossover sites are exchanged
thus creating a completely new individual (offspring). For example – 

3) Mutation Operator: The key idea is to insert random genes in offspring


to maintain the diversity in the population to avoid premature convergence.
For example – 
pseudo-code for a GA
GA()
initialize population
find fitness of population

while (termination criteria is reached) do


parent selection
crossover with probability pc
mutation with probability pm
decode and fitness calculation
survivor selection
find best
return best
GA for traveling salesman problem (TSP)

• TSP Problem statement:


– “Given a list of cities and the distances between each
pair of cities, what is the shortest possible route that
visits each city and returns to the origin city?”
• Given this, there are two important rules to keep
in mind:
1.Each city needs to be visited exactly one time
2.We must return to the starting city, so our total
distance needs to be calculated accordingly
• Few definitions, rephrased in the context of the TSP:
• Gene: a city (represented as (x, y) coordinates)
• Individual (aka “chromosome”): a single route satisfying the
conditions above
• Population: a collection of possible routes (i.e., collection of
individuals)
• Parents: two routes that are combined to create a new route
• Mating pool: a collection of parents that are used to create our
next population (thus creating the next generation of routes)
• Fitness: a function that tells us how good each route is (in our
case, how short the distance is)
• Mutation: a way to introduce variation in our population by
randomly swapping two cities in a route
• Elitism: a way to carry the best individuals into the next
generation
Applications of Genetic Algorithm:
• Genetic Algorithm in Robotics:

Robotics is one of the most discussed fields in the computer industry today. It is used in various industries

in order to increase profitability efficiency and accuracy. As the environment in which robots work with the

time change, it becomes very tough for developers to figure out each possible behavior of the robot in order

to cope with the changes. This is the place where the Genetic Algorithm places a vital role. Hence a suitable

method is required, which will lead the robot to its objective and will make it adaptive to new situations as it

encounters them. Genetic Algorithms are adaptive search techniques that are used to learn high-

performance knowledge structures.

• Genetic Algorithm in Financial Planning:

Genetic algorithms are extremely efficient for financial modeling applications as they are driven by

adjustments that can be used to improve the efficiency of predictions and return over the benchmark set. In

addition, these methods are robust, permitting a greater range of extensions and constraints, which may not

be accommodated in traditional techniques.


Adversarial Search

• Adversarial search is a search, where we examine the problem which arises when we
try to plan ahead of the world and other agents are planning against us.
• In previous topics, we have studied the search strategies which are only associated with a
single agent that aims to find the solution which often expressed in the form of a
sequence of actions.
• But, there might be some situations where more than one agent is searching for the
solution in the same search space, and this situation usually occurs in game playing.
• The environment with more than one agent is termed as multi-agent environment, in
which each agent is an opponent of other agent and playing against each other. Each
agent needs to consider the action of other agent and effect of that action on their
performance.
• So, Searches in which two or more players with conflicting goals are trying to explore
the same search space for the solution, are called adversarial searches, often known as
Games.
• Games are modeled as a Search problem and heuristic evaluation function, and these are
the two main factors which help to model and solve games in AI.
Types of Games in AI:

• Perfect information: A game with the perfect information is that in which agents
can look into the complete board. Agents have all the information about the
game, and they can see each other moves also. Examples are Chess, Checkers, Go,
etc.
• Imperfect information: If in a game agents do not have all information about the
game and not aware with what's going on, such type of games are called the
game with imperfect information, such as tic-tac-toe
• Deterministic games: Deterministic games are those games which follow a strict
pattern and set of rules for the games, and there is no randomness associated
with them. Examples are chess, Checkers, Go, tic-tac-toe, etc.
• Non-deterministic games: Non-deterministic are those games which have various
unpredictable events and has a factor of chance or luck. This factor of chance or
luck is introduced by either dice or cards. These are random, and each action
response is not fixed. Such games are also called as stochastic games.
Example: Monopoly, Poker, etc.
Game playing-Important concepts
• Adversarial search: Search based on Game theory-
Agents- Competitive environment
• According to game theory, a game is played between
two players. To complete the game, one has to win the
game and the other looses automatically.’
• Such Conflicting goal- adversarial search
• Game playing technique- Those games- Human
Intelligence and Logic factor- Excluding other factors like
Luck factor
– Tic-Tac-Toe, Checkers, Chess – Only mind works, no luck works
Game playing-Important concepts
• Techniques required to get the
best optimal solution (Choose
Algorithms for best optimal
solution within limited time)
– Pruning: A technique which
allows ignoring the unwanted
portions of a search tree which
make no difference in its final
result.
– Heuristic Evaluation
Function: It allows to
approximate the cost value at
each level of the search tree,
before reaching the goal node.
Game playing and knowledge structure-
Elements of Game Playing search
• To play a game, we use a game tree to know all the • For example, in chess, tic-tac-toe, we have two
possible choices and to pick the best one out. or three possible outcomes. Either to win, to lose,
There are following elements of a game-playing: or to draw the match with values +1,-1 or 0.
• S0: It is the initial state from where a game begins. • Game Tree for Tic-Tac-Toe
• PLAYER (s): It defines which player is having the – Node: Game states, Edges: Moves taken by players
current turn to make a move in the state.
• ACTIONS (s): It defines the set of legal moves to be
used in a state.
• RESULT (s, a): It is a transition model which defines
the result of a move.
• TERMINAL-TEST (s): It defines that the game has
ended and returns true.
• UTILITY (s,p): It defines the final value with which
the game has ended. This function is also known
as Objective function or Payoff function. The price
which the winner will get i.e.
• (-1): If the PLAYER loses.
• (+1): If the PLAYER wins.
• (0): If there is a draw between the PLAYERS.
Game playing and knowledge structure-
Elements of Game Playing search
• INITIAL STATE (S0): The top node in the
game-tree represents the initial state in the
tree and shows all the possible choice to
pick out one.
• PLAYER (s): There are two players, MAX and
MIN. MAX begins the game by picking one
best move and place X in the empty square
box.  
• ACTIONS (s): Both the players can make
moves in the empty boxes chance by
chance.
• RESULT (s, a): The moves made
by MIN and MAX will decide the outcome of
the game.
• TERMINAL-TEST(s): When all the empty
boxes will be filled, it will be the terminating
state of the game.
• UTILITY: At the end, we will get to know
who wins: MAX or MIN, and accordingly,
the price will be given to them.
Game as a search problem
• Types of algorithms in Adversarial search
– In a normal search, we follow a sequence of actions to
reach the goal or to finish the game optimally. But in
an adversarial search, the result depends on the
players which will decide the result of the game. It is
also obvious that the solution for the goal state will be
an optimal solution because the player will try to win
the game with the shortest path and under limited
time.
• Minmax Algorithm
• Alpha-beta Pruning
Game as a search problem-Minimax
approach
• Mini-max algorithm is a recursive or backtracking algorithm which is used in decision-
making and game theory. It provides an optimal move for the player assuming that
opponent is also playing optimally.
• Mini-Max algorithm uses recursion to search through the game-tree.
• Min-Max algorithm is mostly used for game playing in AI. Such as Chess, Checkers, tic-tac-
toe, go, and various tow-players game. This Algorithm computes the minimax decision for
the current state.
• In this algorithm two players play the game, one is called MAX and other is called MIN.
• Both the players fight it as the opponent player gets the minimum benefit while they get
the maximum benefit.
• Both Players of the game are opponent of each other, where MAX will select the maximized
value and MIN will select the minimized value.
• The minimax algorithm performs a depth-first search algorithm for the exploration of the
complete game tree.
• The minimax algorithm proceeds all the way down to the terminal node of the tree, then
backtrack the tree as the recursion.
Game as a search problem-Minimax
approach
• Minimax/Minmax/MM/Saddle Point:
– Decision strategy-Game Theory
• Minimize loosing chances- Maximize winning chances
• Two-player game strategy 
• Players will be two namely:
• MIN: Decrease the chances of MAX to win the game.
• MAX: Increases his chances of winning the game.
• Result of the game/Utility value
– Heuristic function propagating from initial node to root node
– Backtracking technique-Best choice
Minimax Algorithm
• Follows DFS
– Follows same path cannot change in middle- i.e., Move
once made cannot be altered- That is this is DFS and not
BFS
• Algorithm
– Keep on generating the game tree/ search tree till a limit d.
– Compute the move using a heuristic function.
– Propagate the values from the leaf node till the current
position following the minimax strategy.
– Make the best move from the choices.
Working of Min-Max Algorithm:

• The working of the minimax algorithm can be easily described using an


example. Below we have taken an example of game-tree which is
representing the two-player game.
• In this example, there are two players one is called Maximizer and other
is called Minimizer.
• Maximizer will try to get the Maximum possible score, and Minimizer
will try to get the minimum possible score.
• This algorithm applies DFS, so in this game-tree, we have to go all the
way through the leaves to reach the terminal nodes.
• At the terminal node, the terminal values are given so we will compare
those value and backtrack the tree until the initial state occurs.
Following are the main steps involved in solving the two-player game
tree:
Minimax Algorithm
• For example, in the figure, the two
players MAX and MIN are there. MAX starts the
game by choosing one path and propagating all
the nodes of that path. Now, MAX will backtrack
to the initial node and choose the best path
where his utility value will be the maximum. After
this, its MIN chance. MIN will also propagate
through a path and again will backtrack,
but MIN will choose the path which could
minimize MAX winning chances or the utility
value.
• So, if the level is minimizing, the node will accept
the minimum value from the successor nodes. If
the level is maximizing, the node will accept the
maximum value from the successor.
• Note: The time complexity of MINIMAX algorithm
is O(bd) where b is the branching factor and d is
the depth of the search tree.
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, go, etc. This type of games has a huge
branching factor, and the player has lots of choices
to decide. This limitation of the minimax algorithm
can be improved from alpha-beta pruning
Alpha beta pruning
• Advanced version of MINIMAX algorithm
• Any optimization algorithm- performance
measure is the first consideration
• Drawback of Minimax:
– Explores each node in the tree deeply to provide the
best path among all the paths
– Increases time complexity
• Alpha beta pruning: Overcomes drawback by
less exploring nodes of search tree
Alpha beta pruning
• Cutoff the search by exploring less number of nodes
• It makes same moves as Minimax algorithm does- but prunes
unwanted branches using the pruning techniques
• Alpha beta pruning works on 2 threshold values α and β
– α: It is the best highest value, a MAX player can have. It is the lower
bound, which represents negative infinity value.
– β: It is the best lowest value, a MIN player can have. It is the upper
bound which represents positive infinity.
• So, each MAX node has α-value, which never decreases, and each
MIN node has β-value, which never increases.
• Note: Alpha-beta pruning technique can be applied to trees of
any depth, and it is possible to prune the entire subtrees easily.
Alpha beta pruning
• Consider the below example of a game tree
where P and Q are two players.
• The game will be played alternatively, i.e., chance by chance.
• Let, P be the player who will try to win the game by
maximizing its winning chances.  Q is the player who will try
to minimize P’s winning chances.
• Here, α will represent the maximum value of the nodes,
which will be the value for P as well. 
• β will represent the minimum value of the nodes, which will
be the value of Q.
Alpha beta pruning
• Any one player will start the game. Following the DFS order, the player will choose one path
and will reach to its depth, i.e., where he will find the TERMINAL value.
• If the game is started by player P, he will choose the maximum value in order to increase its
winning chances with maximum utility value.
• If the game is started by player Q, he will choose the minimum value in order to decrease the
winning chances of A with the best possible minimum utility value.
• Both will play the game alternatively.
• The game will be started from the last level of the game tree, and the value will be chosen
accordingly.
• Like in the below figure, the game is started by player Q. He will pick the leftmost value of the
TERMINAL and fix it for beta (β). Now, the next TERMINAL value will be compared with the β-
value. If the value will be smaller than or equal to the β-value, replace it with the current β-
value otherwise no need to replace the value.
• After completing one part, move the achieved β-value to its upper node and fix it for the
other threshold value, i.e., α.
• Now, its P turn, he will pick the best maximum value. P will move to explore the next part only
after comparing the values with the current α-value. If the value is equal or greater than the
current α-value, then only it will be replaced otherwise we will prune the values.
• The steps will be repeated unless the result is not obtained.
• So, number of pruned nodes in the above example are four and MAX wins the game with the
maximum UTILITY value, i.e.,3
• The rule which will be followed is: “Explore nodes if necessary otherwise prune the
unnecessary nodes.”
• Note: It is obvious that the result will have the same UTILITY value that we may get from the
MINIMAX strategy.
Working of Alpha-Beta Pruning:
• Step 1: At the first step the, Max player will start first move from node A where α= -∞ and β=
+∞, these value of alpha and beta passed down to node B where again α= -∞ and β= +∞, and
Node B passes the same value to its child D.
Working of Alpha-Beta Pruning:
• Step 2: At Node D, the value of α will be calculated as its turn for Max. The
value of α is compared with firstly 2 and then 3, and the max (2, 3) = 3 will
be the value of α at node D and node value will also 3.
• Step 3: Now algorithm backtrack to node B, where the value of β will
change as this is a turn of Min, Now β= +∞, will compare with the
available subsequent nodes value, i.e. min (∞, 3) = 3, hence at node B now
α= -∞, and β= 3.
Working of Alpha-Beta Pruning:
• In the next step, algorithm traverse the next successor of Node B which is
node E, and the values of α= -∞, and β= 3 will also be passed.
• Step 4: At node E, Max will take its turn, and the value of alpha will
change. The current value of alpha will be compared with 5, so max (-∞,
5) = 5, hence at node E α= 5 and β= 3, where α>=β, so the right successor
of E will be pruned, and algorithm will not traverse it, and the value at
node E will be 5.
Working of Alpha-Beta Pruning:
Step 5: At next step, algorithm again backtrack the tree, from node B to node
A. At node A, the value of alpha will be changed the maximum available value
is 3 as max (-∞, 3)= 3, and β= +∞, these two values now passes to right
successor of A which is Node C.
At node C, α=3 and β= +∞, and the same values will be passed on to node F.
Step 6: At node F, again the value of α will be compared with left child which
is 0, and max(3,0)= 3, and then compared with right child which is 1, and
max(3,1)= 3 still α remains 3, but the node value of F will become 1.
Working of Alpha-Beta Pruning:
Step 7: Node F returns the node value 1 to node C, at C α= 3 and β= +∞, here
the value of beta will be changed, it will compare with 1 so min (∞, 1) = 1.
Now at C, α=3 and β= 1, and again it satisfies the condition α>=β, so the next
child of C which is G will be pruned, and the algorithm will not compute the
entire sub-tree G.
Working of Alpha-Beta Pruning:
Step 8: C now returns the value of 1 to A here the best value for A is max (3,
1) = 3. Following is the final game tree which is the showing the nodes which
are computed and nodes which has never computed. Hence the optimal
value for the maximizer is 3 for this example.
Move Ordering in Alpha-Beta pruning:
• The effectiveness of alpha-beta pruning is highly dependent on the order in
which each node is examined. Move order is an important aspect of alpha-
beta pruning.
• It can be of two types:
• Worst ordering: In some cases, alpha-beta pruning algorithm does not prune
any of the leaves of the tree, and works exactly as minimax algorithm. In this
case, it also consumes more time because of alpha-beta factors, such a move
of pruning is called worst ordering. In this case, the best move occurs on the
right side of the tree. The time complexity for such an order is O(b m).
• Ideal ordering: The ideal ordering for alpha-beta pruning occurs when lots of
pruning happens in the tree, and best moves occur at the left side of the tree.
We apply DFS hence it first search left of the tree and go deep twice as
minimax algorithm in the same amount of time. Complexity in ideal ordering
is O(bm/2).
Rules to find good ordering:

• Following are some rules to find good ordering in alpha-


beta pruning:
• Occur the best move from the shallowest node.
• Order the nodes in the tree such that the best nodes
are checked first.
• Use domain knowledge while finding the best move. Ex:
for Chess, try order: captures first, then threats, then
forward moves, backward moves.
• We can bookkeep the states, as there is a possibility
that states may repeat.
Game theory problems
• Game theory is basically a branch of mathematics that is
used to typical strategic interaction between different
players (agents), all of which are equally rational, in a
context with predefined rules (of playing or maneuvering)
and outcomes. 
• GAME can be defined as a set of players, actions,
strategies, and a final playoff for which all the players are
competing.
• Game Theory has now become a describing factor for both
Machine Learning algorithms and many daily life
situations.
https://www.geeksforgeeks.org/game-theory-in-ai/
Game theory problems
• Types of Games
– Zero-Sum and Non-Zero Sum Games: In non-zero-sum games, there are multiple players and
all of them have the option to gain a benefit due to any move by another player. In zero-sum
games, however, if one player earns something, the other players are bound to lose a key
playoff.
– Simultaneous and Sequential Games: Sequential games are the more popular games where
every player is aware of the movement of another player. Simultaneous games are more
difficult as in them, the players are involved in a concurrent game. BOARD GAMES are the
perfect example of sequential games and are also referred to as turn-based or extensive-form
games.
– Imperfect Information and Perfect Information Games: In a perfect information game, every
player is aware of the movement of the other player and is also aware of the various strategies
that the other player might be applying to win the ultimate playoff. In imperfect information
games, however, no player is aware of what the other is up to. CARDS are an amazing example
of Imperfect information games while CHESS is the perfect example of a Perfect Information
game.
– Asymmetric and Symmetric Games: Asymmetric games are those win in which each player has
a different and usually conflicting final goal. Symmetric games are those in which all players
have the same ultimate goal but the strategy being used by each is completely different.
– Co-operative and Non-Co-operative Games: In non-co-operative games, every player plays for
himself while in co-operative games, players form alliances in order to achieve the final goal.
https://www.geeksforgeeks.org/game-theory-in-ai/
Game theory problems
• Nash equilibrium:
Nash equilibrium can be considered the essence of Game
Theory. It is basically a state, a point of equilibrium of
collaboration of multiple players in a game. Nash Equilibrium
guarantees maximum profit to each player.
Let us try to understand this with the help of Generative
Adversarial Networks (GANs).
• What is GAN?
– It is a combination of two neural networks: the Discriminator and
the Generator. The Generator Neural Network is fed input images
which it analyzes and then produces new sample images, which are
made to represent the actual input images as close as possible
GAN
• Once the images have been produced, they are sent to the
Discriminator Neural Network(DNN). This neural network judges the
images sent to it and classifies them as generated images and actual
input images. If the image is classified as the original image, the DNN
changes its parameters of judging.
• If the image is classified as a generated image, the image is rejected
and returned to the Generative Neural Network(GNN). The GNN then
alters its parameters in order to improve the quality of the image
produced.
• This is a competitive process which goes on until both neural networks
do not require to make any changes in their parameters and there can
be no further improvement in both neural networks. This state of no
further improvement is known as NASH EQUILIBRIUM.
• In other words, GAN is a 2-player competitive game where both players
are continuously optimizing themselves to find a Nash Equilibrium.
GAN
Where is GAME THEORY now?
• Game Theory is increasingly becoming a
part of the real-world in its various
applications in areas like public health
services, public safety, and wildlife.
• Currently, game theory is being used in
adversary training in GANs, multi-agent
systems, and imitation and reinforcement
learning. In the case of perfect
information and symmetric games, many
Machine Learning and Deep Learning
techniques are applicable.
• The real challenge lies in the development
of techniques to handle incomplete
information games, such as Poker.
• The complexity of the game lies in the fact
that there are too many combinations of
cards and the uncertainty of the cards
being held by the various players.
Game theory problems
• Prisoner’s Dilemma.
• Closed-bag exchange Game,
• The Friend or Foe Game, and
• The iterated Snowdrift Game.

https://www.geeksforgeeks.org/game-theory-in-ai/
References
• 1. Parag Kulkarni, Prachi Joshi, “Artificial Intelligence –Building Intelligent
Systems ”PHI learning private Ltd, 2015
• 2.Kevin Night and Elaine Rich, Nair B., “Artificial Intelligence (SIE)”, Mc Graw
Hill- 2008.
• 3.Stuart Russel and Peter Norvig “AI – A Modern Approach”, 2nd Edition,
Pearson Education 2007
• 4. www.javatpoint.com
• 5. www.geeksforgeeks.org
• 6.www.mygreatlearning.com
• 7. www.tutorialspoint.com

172

You might also like