0% found this document useful (0 votes)
40 views86 pages

Topic 03 Search

This document discusses different search algorithms used in artificial intelligence problem solving. It introduces graph theory and defines search problems using state spaces and graphs. It then covers uninformed searches like breadth-first search and depth-first search, as well as informed searches like A* search and adversarial searches using minimax. Examples of applying these searches to solve problems represented as maps are also provided.

Uploaded by

S Alsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views86 pages

Topic 03 Search

This document discusses different search algorithms used in artificial intelligence problem solving. It introduces graph theory and defines search problems using state spaces and graphs. It then covers uninformed searches like breadth-first search and depth-first search, as well as informed searches like A* search and adversarial searches using minimax. Examples of applying these searches to solve problems represented as maps are also provided.

Uploaded by

S Alsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 86

COE 292

Introduction to Artificial Intelligence

Problem Solving by Searching

12/23/2022 INTRODUCTION TO AI 1
Outline
Introduction to graph theory
Search Problems as a general problem solver
Types of Search Strategies
A. Uninformed Search:
1. Breadth-first search
2. Depth first Search
3. Uniform cost search
B. Informed (Heuristic) Search
1. A* Search
C. Adversarial Search using the Min-Max technique
Conclusion
INTRODUCTION TO AI 2
Graph Theory
Graph theory is a branch of mathematics a
highly utilized in computer science. b
c
However, graph theory has a myriad of
applications in many different domains. d e

It is a study of graphs, where graph is a f g


structure formed by vertices and edges
connecting the vertices. a
b
Maps, websites, communication networks, c
electrical circuits, program structures, …etc. d e
are practical examples of graphs. f g

INTRODUCTION TO AI 3
Undirected Graphs
Definition (Undirected Graph):
Graph is a pair of sets where is the set of
vertices and is the set of a
edges, . b
c
Example :
d e
f g

INTRODUCTION TO AI 4
Directed Graphs
Definition (Directed Graph):
Directed graph is a pair of sets where is the
set of vertices and is the set of edges, such as a
, where is an ordered pair, i.e. . b
c
d e
Each edge has a direction, i.e. is an edge f g
directed from node to node .

INTRODUCTION TO AI 5
Graph Terminology
Having directed graph , if there is an edge
from vertex to vertex , then is a (direct)
predecessor of and is a (direct) successor of .
 We denote Γ(u) as a set of successors of
vertex .
.
Connected acyclic graph is called a tree (can
be directed or undirected).
A tree with one special singled out vertex
labeled ”root” is called a rooted tree.
A vertex in a directed tree such that Γ(u) = { }
is called a leaf.
INTRODUCTION TO AI 6
FIFO Queue
A FIFO Queue is a linear data structure, a list, that
Queue
stores items in First In First Out (FIFO) manner.
 like a line of people waiting to be served
 The first element of the queue is called the head
Head Tail
 The last element of the queue is called the tail (Front) (Rear)
 An element is read from the head of the queue and is written at
the tail of the queue.
A FIFO queue in python can be implemented with lists
(ICS 104)

12/23/2022 INTRODUCTION TO AI 7
Stack
A Stack is a linear data structure, a list, that stores items in Last
In First Out (LIFO) manner.
 The first element of the LIFO queue (stack) is called the head
 An element is read from and is written at the head of the LIFO queue
(stack).
A stack in python can be implemented with lists (ICS 104)

12/23/2022 INTRODUCTION TO AI 8
Priority Queue
A priority queue is a collection of zero or
more items where each item is associated with Priority Queue
a priority.
Least
The highest priority can be either the Heighest
Priority Priority
minimum value of all the items, or the
maximum. Head Item Tail
(Front) Priority (Rear)
Inserting a new item in the priority queue
must ensure that the item is placed at the right
priority level.

12/23/2022 INTRODUCTION TO AI 9
Introduction to Search
Several problems can be formulated as finding a sequence of actions that
lead to a desirable goal
In these problems you often start in some situation (initial state) and want
to end at another (goal state)
To go from the initial state to the end state you have to take several actions
Each chosen action causes a change in the state
We want to find a subset of multiple actions to go from the initial state to
the goal state

Search algorithms are general solvers for such problems


INTRODUCTION TO AI 10
Search Problems Formulation
Problems are generally represented in terms of states (state space).
 A set of all possible states that the problem can be in.

 e.g. all the different permutations of the numbers 1,2,3,…, 8


that the 8-PUZZLE Problem can have.
State space
 4 states are shown below

Solution Initial state

Search
problem

The state space is modeled as a graph G = (V , E)


 state-space search is transformed to graph search Actions Goal state
 nodes are states in a problem-solving process

11
Search Problems Formulation
Example: Given a 3×3 board with 8 tiles (every tile has one number from 1 to 8) and one empty
space.
The objective is to place the numbers on tiles to match the final configuration using the empty space.
Start state: the initial configuration of the problem.
 e.g.

Goal state: the desired configuration of the problem


 e.g.

Actions : All of the agents allowed actions


 move the blank up ↑
 move the blank right →
 move the blank down ↓
 move the blank left ←

12
Search Problems Formulation
The process of searching can be
automated by applying the rules of
moves to cause transitions from one
state to next, until you reach to the goal
state.
Solution:
• A sequence of actions forms a path,
• a solution is a path from initial state
to a goal Path state The state space representation of 8-PUZZLE Problem

13
AI Search Algorithms
Search Algorithms

Uninformed Informed Adversarial Search


Search search

Depth First Breadth First Uniform Cost


Search (DFS) Search (BFS) Search A* Search Min-Max

INTRODUCTION TO AI 14
Search Examples
To illustrate how to solve problems by search, we will go over two
types of examples
Small Map: This problem is easy to solve by looking at it which will
give you an idea on how the search algorithm works
Large Map: This problem is a larger map on which you can witness
different search algorithm's behavior and the difference between
each search strategy

INTRODUCTION TO AI 15
Small Map Example
Given the map on the right,
starting at point 𝑆 (for Source)
find a path to point 𝐺 (for Goal).
The map shows direct path
connections between cities with
the distance.
During search, a tree will be
constructed starting with the
Initial City as the root of the tree.

INTRODUCTION TO AI 16
Search Solution Strategy
Additional Constraints
during search tree
construction:
No Cycles should be
formed
When branching,
order the nodes in
lexical order (D on
right of B but A on B's
left)

INTRODUCTION TO AI 17
Uninformed Search - Breadth-First Search
Breadth First Search (BFS) [Definition]:
Breadth-first search is a simple strategy in which the root
node is expanded first, then all the successors of the root
node are expanded next, then all their successors, and so
on until you find the node you are looking for.

INTRODUCTION TO AI 18
Breadth-First Search Conceptual Approach

Note the following:


1. Tree is generated level by level (shown in yellow nodes in each step)
2. Yellow node indicates that the node is expanded using an edge to its children
3. IN BFS, the Weights on the edges are ignored

INTRODUCTION TO AI 19
Breadth-First Search Overview
The BFS can be implemented using a queue, named here as Queue.
The front node of the queue is represented by Head.
The algorithm checks all the paths at a given length, before testing the paths of
longer length.
The expanded path is placed at the end of the Queue
The inputs to this algorithm are:
the graph,
start node , and
goal node 𝐺
If goal is reached it returns success.
If goal is not found, the algorithm returns fail and exits.
INTRODUCTION TO AI 20
Breadth-First Search Algorithm
Algorithm: BFS (Input: Graph, S, G)
1: Queue = [S]
2: repeat
3: if Queue.Head = G then
4: return success
5: end if
6: generate children set C of Queue.Head
7: append C to Queue
8: delete Queue.Head
9: until Queue = []
10: return fail

INTRODUCTION TO AI 21
Breadth-First Search Example
If the BFS is applied for this example, the Initial State is S
content of the queue is generated as follows : Goal State is G
1. [S]
2. [S,A][S,B]
3. [S,B][S,A,B][S,A,D]
4. [S,A,B][S,A,D][S,B,A][S,B,C]
5. [S,A,D][S,B,A][S,B,C][S,A,B,C]
6. [S,B,A][S,B,C][S,A,B,C][S,A,D,G]
7. [S,B,C][S,A,B,C][S,A,D,G][S,B,A,D]
8. [S,A,B,C][S,A,D,G] [S,B,A,D][S,B,C,E]
9. [S,A,D,G][S,B,A,D][S,B,C,E][S,A,B,C,E]
INTRODUCTION TO AI 22
Breadth-First Search Additional Example
For the graph shown, show the content of Initial State is S S
Goal State is G
the queue when using the BFS algorithm
The content of the queue step by step is: A B C
1. [S] D E F G H
2. [S,A][S,B][S,C]
3. [S,B][S,C][S,A,D][S,A,E]
4. [S,C][S,A,D][S,A,E] [S,B,F] [S,B,G]
S
5. [S,A,D][S,A,E][S,B,F][S,B,G][S,C,G][S,C,H]
6. [S,A,E][S,B,F][S,B,G][S,C,G][S,C,H]
A B C
7. [S,B,F][S,B,G][S,C,G][S,C,H]
8. [S,B,G][S,C,G][S,C,H] D E F G G H

INTRODUCTION TO AI 23
Properties of BFS
Since BFS always explores the shallow nodes before the deeper
ones, BFS finds the shallowest path leading to the goal state.
 In BFS, we reach a goal node with minimum number of edges
from a source vertex.
BFS is a complete algorithm where if the goal exists in the tree, BFS
will certainly find it.
BFS is more suitable for searching vertices which are closer to the
given source.

INTRODUCTION TO AI 24
Improvement to BFS Algorithm
Suppose that we are searching a uniform tree with each node having b
successors
The root of the tree generates b nodes at the first level, each of which
generates b more nodes for a total of b2 nodes at the 2nd level
Suppose the solution is at level d, in which we will have bd nodes
In the worst case, the solution could be the last node at level d
The current algorithm will generate bd+1-b nodes before reaching the
solution
If we check the node if it is a goal when generated before adding it to
the queue, the algorithm will be more efficient
INTRODUCTION TO AI 25
Uninformed Search - Depth-First Search
Depth-First Search (DFS)[Definition]:
Depth-first search always expands the deepest node in
the current frontier of the search tree and if two nodes
have the same depth take the one on the left by
convention. Keep repeating until you find the node you
are looking for.

INTRODUCTION TO AI 26
Depth Fist Search Conceptual Approach
1. The algorithm starts at the
root node and extends each
branch as far as a leaf node.
2. When reaching a leaf node
and the goal was not found
backtrack to the node closest
to the leaf node where a
branch exists that was not
explored.
3. Repeat 1, 2 until goal is found
or stop when there are no
more nodes to explore.

INTRODUCTION TO AI 27
Depth First Search Overview
DFS can be implemented using a Stack.
The front node of the Stack is represented by Stack.Head
The expanded path is placed at the head of the Stack
The inputs to this algorithm are:
 Graph,
 Start node , and
 Goal node 𝐺
If goal is reached it returns success.
If goal is not found and we have traversed the entire tree, the algorithm
reports a failure and exits.
INTRODUCTION TO AI 28
Depth First Search Algorithm
Algorithm: DFS (Input: Graph, S, G)
1: Stack = [S]
2: repeat
3: if Stack.Head = G then
4: return success
5: end if
6: generate children set C of Stack.Head
7: delete Stack.Head
8: insert C into Stack
9: until Stack = []
10: return fail

INTRODUCTION TO AI 29
Depth First Search Example
If the DFS is applied for this example, Initial State is S
the content of the stack is generated Goal State is G
as follows :
1. [S]
2. [S,A][S,B]
3. [S,A,B][S,A,D][S,B]
4. [S,A,B,C][S,A,D][S,B]
5. [S,A,B,C,E][S,A,D][S,B]
6. [S,A,D][S,B]
7. [S,A,D,𝐺][S,B]

INTRODUCTION TO AI 30
Depth First Search Additional Example
For the graph shown, show the content Initial State is S S
Goal State is G
of the stack when using the DFS
algorithm A B C

The content of the stack step by step is: D E F G H


1. [S] Directed Graph

2. [S,A][S,B][S,C]
3. [S,A,D][S,A,E][S,B][S,C] S
4. [S,A,E][S,B][S,C]
5. [S,B][S,C] A B C
6. [S,B,F][S,B,G][S,C]
D E F G G H
7. [S,B,G][S,C]
INTRODUCTION TO AI 31
Comparison Between DFS and BFS
BFS (Breadth First Search) DFS (Depth First Search)
BFS uses Queue data structure for reaching the goal DFS uses Stack data structure for reaching the goal
node where the expanded node is placed at the tail of node where the expanded node is placed at the Head
the queue. of the stack.
BFS can be used to find single source shortest path in In DFS, we might traverse through more edges to
an unweighted graph. reach a goal node from the source node.
BFS is more suitable for searching vertices which are DFS is more suitable when there are solutions away
closer to the given source. from source.
In BFS, sibling nodes are visited before the children In DFS, children are visited before the siblings
Ignores the edge weights Ignores the edge weights

Note:
1. Sibling nodes are nodes that share the same parent.
2. Unweighted graphs are graphs in which the numerical weight of edge is ignored.

INTRODUCTION TO AI 32
Uniform-Cost Search
Uniform-Cost Search [Definition]:
Uniform-cost search is an uninformed search algorithm
for searching a weighted tree or graph that uses the
lowest cumulative cost to find a path from the source to
the destination. Nodes are expanded, starting from the
root, according to the minimum cumulative cost. It is
also named as Branch and Bound algorithm.

INTRODUCTION TO AI 33
Uniform-Cost Search Conceptual Approach
It expands the node
with least cost first.
Least cumulative cost is
used to determine the
next node to be
expanded.

INTRODUCTION TO AI 34
Uniform Cost Search Overview
It can be implemented using a priority queue, named here as Priority_Queue.
The priority of an item in the priority queues will be the cost incurred.
The front node of the queue is represented by Priority_Queue.Head
The last node of the queue is represented by Priority_Queue.Tail
The cost of the head node is Priority_Queue.Head.Cost
The inputs to this algorithm are:
 Weighted Graph denoted by (wGraph),
 Start node , and
 Goal node 𝐺
If goal is reached it returns success.
If goal is not found and we have traversed the entire tree, the algorithm reports a failure
and exits.

INTRODUCTION TO AI 35
Uniform Cost Search (UCS) Algorithm
Algorithm: UCS (Input: wGraph, S, G)
1: Priority_Queue= [S]
2: repeat
3: if Priority_Queue.Head = G then
4: return success
5: end if
6: generate children set C of Priority_Queue.Head
and calculate their cumulative cost
7: delete Priority_Queue.Head
8: insert C into the Priority_Queue based on the
new cumulative cost
9: until Priority_Queue = []
10: return fail

INTRODUCTION TO AI 36
Uniform-Cost Search using Priority Queues
1. Queue: [S]
Cost: 0
2. Queue: [S,A] [S,B]
Cost: 3 5
3. Queue: [S,B] [S,A,D] [S,A,B]
Cost: 5 6 7
4. Queue: [S,A,D] [S,A,B] [S,B,A] [S,B,C]
Cost: 6 7 9 9
5. Queue: [S,A,B] [S,B,A] [S,B,C] [S,A,D,G]
Cost: 7 9 9 11
6. Queue: [S,B,A] [S,B,C] [S,A,B,C] [S,A,D,G]
Cost: 9 9 11 11
7. Queue: [S,B,C] [S,A,B,C] [S,A,D,G] [S,B,A,D]
Cost: 9 11 11 12
8. Queue: [S,A,B,C] [S,A,D,G] [S,B,A,D] [S,B,C,E]
Cost: 11 11 12 16
9. Queue: [S,A,D,G] [S,B,A,D] [S,B,C,E] [S,A,B,C,E]
Cost: 11 12 16 18

INTRODUCTION TO AI 37
Uniform-Cost Search using Priority Queues
1. Queue: [S]
Cost: 0
2. Queue: [S,A] [S,B]
Cost: 1 4
3. Queue: [S,A,D] [S,B] [S,A,C]
Cost: 3 4 4
4. Queue: [S,B] [S,A,C] [S,A,D,G] [S,A,D,F]
Cost: 4 4 6 7
5. Queue: [S,A,C] [S,A,D,G] [S,A,D,F] [S,B,G]
Cost: 4 6 7 9
6. Queue: [S,A,D,G] [S,A,D,F] [S,A,C,E] [S,B,G]
Cost: 6 7 9 9

INTRODUCTION TO AI 38
Notes on Search
Search ≠ Tree or Graph or Map
Search ≡ choice you make when you have multiple options
A tree or a graph or a map are just representations that make it
easy for humans to understand

INTRODUCTION TO AI 39
Large Map Example
Suppose you are visiting Romania and
you want to go from the city Arad to
the city Bucharest using a car. What is
the best path to take?
1. Each city is represented as a node.
2. Each path is represented by an
edge joining the respective cities.
3. Each path has a cost associated
with it (in our example it’s the
distance)
How would you tell a computer
program to find a path between both
cities? (this is a search problem that
we will discuss)
INTRODUCTION TO AI 40
Large Map Example Solution
Ignore Redundant paths as they never
add any values but rather increase cost
Redundant path is having more than
one way to get from one node to
another. This is achieved as follows:
For BFS and DFS, do not add a node to
the queue/stack that has been either
extended or in the queue/stack
For UCS, do not extend or enqueue a
node that has been extended

INTRODUCTION TO AI 41
Large Map Example Solution
BFS (visual representation where tree is constructed on the map itself)

Highlighted nodes show nodes that have been added to the queue

INTRODUCTION TO AI 42
Large Map Example Solution
DFS (visual representation where tree is constructed on the map itself)

Uniform Cost Search Highlighted nodes show nodes


This exercise is left for students to perform. that have been added to the stack

INTRODUCTION TO AI 43
Breadth-First Search avoiding Redundant Paths
Initial State is S
If the BFS is applied for this example with the Goal State is G
condition applied to avoid redundant paths,
the content of the queue is generated as
follows :
1. [S]
2. [S,A][S,B]
3. [S,B][S,A,D]
4. [S,A,D][S,B,C]
5. [S,B,C] [S,A,D,G]
6. [S,A,D,G] [S,B,C,E]
INTRODUCTION TO AI 44
Depth First Search avoiding Redundant Paths
Initial State is S
If the DFS is applied for this example Goal State is G
with the condition applied to avoid
redundant paths, the content of the
stack is generated as follows :
1. [S]
2. [S,A][S,B]
3. [S,A,D][S,B]
4. [S,A,D,G][S,B]

INTRODUCTION TO AI 45
Summary of Uninformed Search
Breadth First Search Algorithm expands the search level by level,
exploring all possibilities of one level before going down.
Depth First Search Algorithm starts by going down one level from
the left by convention until the goal is reached. The program goes
back up to the previous node if the goal is not reached, a process
called “back up” or “backtracking“.
BFS and DFS may not result with the same path from the root node
to goal node.

INTRODUCTION TO AI 46
Summary of Uninformed Search
The big advantage of DFS is that it has much lower memory
requirements than BFS, because it is not necessary to store all of
the child pointers at each level.
Depending on the data and what you are looking for, either DFS or
BFS could be advantageous.
Use uniform cost search when you want to find the path in a graph
that takes you from the root node to some goal node with the least
considered cost function.
Redundant paths can be avoided using extended list and additional
condition for adding children in the queue/stack.
INTRODUCTION TO AI 47
Informed Search
Heuristics are strategies often used to find a solution that is not
perfect but is within an acceptable degree of accuracy for the needs
of the process. Simply put it is a shortcut.
Heuristic solutions often find a good solution, not necessarily an
optimal one, out of the available options.
Heuristic knowledge is the less rigorous, more experiential and
more judgmental knowledge of performance or what commonly
constitutes the rules of "good judgement" or the art of "good
guessing" in a field.

INTRODUCTION TO AI 48
Informed Search algorithms
They consider quantitative values such as heuristics while searching
Heuristics are defined in the form of a function based on the nature of
the problem
Heuristic are used to make more informed decisions regarding which
paths to traverse.
order the nodes to be visited such that most promising nodes are
explored first
They reduce the search space
They do not follow the exhaustive search approach, making them far
more efficient than the uninformed search methods
INTRODUCTION TO AI 49
Heuristic Search
Heuristic search refers to a search strategy that attempts to optimize a problem
by iteratively improving the solution based on a given heuristic function or a
cost measure.
It makes decisions by ranking every available choice at each branch of a search
and then chooses the best option of those presented.
Heuristic approach emphasizes speed over accuracy, and therefore finds the
most acceptable option within a reasonable time limit or within the allocated
memory space.
A heuristic function is said to be admissible if it never overestimates the cost of
reaching the goal, i.e., the estimated cost to reach the goal is not higher than
the lowest possible cost from the current point in the path(i.e. a lower bound)

INTRODUCTION TO AI 50
Informed Search - A* Search
A* Search is a search method that minimizes the total estimated
solution cost
The A* search is composed of the following:
1. Uniform Cost Search (also known as Branch and Bound)
2. Extended List
3. Admissible Heuristics

INTRODUCTION TO AI 51
Uniform Cost Search + Extended List
The same as uniform cost search with the following enhancement:
If you reach a node that was extended before with lower or equal
cost, do not extend it and discard it because you already reached
this node using some other path with less or equal cost
Do not enqueue a node that has been previously extended with
lower or equal cost.
Note that these two conditions are the ones used to avoid
redundant paths and the added condition “with lower or equal
cost” is satisfied by design for uniform cost search
This extra condition is required due to the use of admissible
heuristics
INTRODUCTION TO AI 52
Uniform Cost Search + Extended List
1. Queue: [S]
Cost: 0
2. Queue: [S,A] [S,B]
Cost: 3 5
3. Queue: [S,B] [S,A,D] [S,A,B]
Cost: 5 6 7
4. Queue: [S,A,D] [S,A,B] [S,B,C]
Cost: 6 7 9
5. Queue: [S,A,B] [S,B,C] [S,A,D,G]
Cost: 7 9 11
6. Queue: [S,B,C] [S,A,D,G]
Cost: 9 11
7. Queue: [S,A,D,G] [S,B,C,E]
Cost: 11 16
Note: Red nodes indicate that this node has been extended before
INTRODUCTION TO AI 53
Uniform Cost Search + Extended List + Admissible Heuristics
First look for a good heuristic that
takes you closer to the goal
In a map the Euclidean distance to
the goal is a good heuristic (shown
as dashed lines)
Use the heuristic to expand the
node that leads you closer to the
goal
like Uniform Cost Search with the
difference that weight of the node
has the admissible heuristics
included
INTRODUCTION TO AI 54
A* = Uniform Cost Search + Extended List + Admissible Heuristics
The estimated
remaining distance to the D 7
E S 9.8
3+7.6 = 10.6
S
5+6.5 = 11.5

goal is added to the 4 4 A B A B

extended path. B
6.5
G B D A C B D A C

5 4
4
C G D E C G D E
5

Only the shortest (path S 3


A 3
D 3 E
Step 1
G E
Step 2
G

extended + remaining S S S
estimated distance) is A B A
5+6.5 = 11.5
B A
5+6.5 = 11.5
B

extended until the goal is B D A C 3+4+6.5


= 13.5
B
3+3+5=
D 11
A C
3+4+6.5
= 13.5
B D A C

reached. C G D E C G D E C G D E
3+3+5=

All longer paths are E G E


Step 3
G E 11
Step 4
G

discarded. The white color nodes indicate enqueued nodes

INTRODUCTION TO AI 55
A* Search – Using Priority Queue
1. Queue: [S]
Cost: 9.8
2. Queue: [S,A] [S,B]
Cost: 10.6 11.5
3. Queue: [S,A,D] [S,B] [S,A,B]
Cost: 11 11.5 13.5
4. Queue: [S,A,D,G] [S,B] [S,A,B]
Cost: 11 11.5 13.5

INTRODUCTION TO AI 56
A* = Uniform Cost Search + Extended List + Admissible Heuristics
Consider the graph shown below and the heuristic table showing
the used heuristic function

INTRODUCTION TO AI 57
A* = Uniform Cost Search + Extended List + Admissible Heuristics
1. Queue: [S]
Cost: 7
2. Queue: [S,B] [S,A]
Cost: 6 7
3. Queue: [S,A] [S,B,C]
Cost: 7 7
4. Queue: [S,A,B] [S,A,C] [S,B,C] [S,A,G]
Cost: 5 7 7 12
5. Queue: [S,A,B,C] [S,A,C] [S,B,C] [S,A,G]
Cost: 6 7 7 12
6. Queue: [S,A,C] [S,B,C] [S,A,B,C,G] [S,A,G]
Cost: 7 7 8 12
7. Queue: [S,B,C] [S,A,B,C,G] [S,A,G]
Cost: 7 8 12
8. Queue: [S,A,B,C,G] [S,A,G]
Cost: 8 12

INTRODUCTION TO AI 58
A* Search
Properties of A* search:
If a solution exists for the given problem, the first solution found by A* is an
optimal solution.
A* is also complete algorithm, meaning if a solution exists, the answer is bound
to be found in a finite amount of time.
Advantages of A* Search Disadvantages of A* Search
It is optimal search algorithm in terms of This algorithm is complete if the
heuristics branching factor is finite, and every
It is one of the best heuristic search techniques action has fixed cost
It is used to solve complex search problems The performance of A* search is
There is no other optimal algorithm guaranteed dependent on accuracy of heuristic
to extend fewer nodes than A* algorithm used

INTRODUCTION TO AI 59
Large Map with A*
Find the optimum path between Arad City Distance to City Distance to
and Bucharest using A* Bucharest Bucharest
Arad 366 Mehadia 241
Bucharest 0 Neamt 234
Craiova 160 Oradea 280
Drobeta 242 Pitesti 100
Eforie 161 Rimnicu Vilcea 193
Fagaras 176 Sibiu 253
Giurgiu 77 Timisoara 329
Hirsova 151 Urziceni 80
Iasi 266 Vaslui 199
Lugoj 244 Zerind 374

The distance between cities is calculated using great circle distance, which is the shortest distance between two points on
the surface of a sphere.
INTRODUCTION TO AI 60
Solution of the Large Map Problem

12/23/2022 INTRODUCTION TO AI 61
Comparison Between Search Algorithms
DFS DFS
BFS
BSF

Uniform Cost Search + Extended List A*

INTRODUCTION TO AI 62
Adversarial Search
In Adversarial search, we tackle problems that arise when we try to plan in a
world were other agents are playing against us.
 We want intelligent agents to play games such as Tic-Tac-Toe or chess.

How humans play the game?


mixing analysis, strategy, tactics to choose a move
Programming an agent to be intelligent we need to:
1. Use some sort of If-Then rules (construct a tree)
2. Look ahead and evaluate (as far as possible)
3. Reduce the search space (branch and bound)
How will we make the computer choose so that it can win?
12/23/2022 INTRODUCTION TO AI 63
Adversarial Search
A game can be formally defined as a kind of search problem as follows:
Initial state: initial board position + whose move it is
Operators: legal moves a player can make
Goal (terminal test): game over?
Utility function (payoff): measures the outcome of the game and its
desirability
Search objective:
Find the sequence of player’s decisions (moves) maximizing its utility
(payoff)
Consider the opponent’s moves and their utility

INTRODUCTION TO AI 64
Mini-max – Toy Example
Mini-max is a kind of backtracking
algorithm that is used in decision
making and game theory to find
the optimal move for a player, if
your opponent also plays optimally.
Consider a game which has 4 final
states and paths to reach from root
of a tree as shown. Terminal values: part of the game

INTRODUCTION TO AI 65
Mini-max - Toy Example
Best Strategy
If the Max player chooses A, then
the Min player will choose C (Utility
of 2)
If the Max player chooses B, then the
Min Player will choose E (Utility of 1)
Obviously, the Max player will
choose A to get 2, that is greater
than 1 if he chooses B. The Minimax algorithm tries to predict the
opponent's behavior.
How will you tell the computer to do It predicts that the opponent will take the
this? worst action from our viewpoint.

INTRODUCTION TO AI 66
Mini-max - Toy Example
An agent strategy will be:
Go from bottom-up ↑
Min player
 Utility(A) = min(C,D) = min(2,7) = 2
 Utility(B) = min(E,F) = min(1,8) = 1
Max player
 Utility(S) = max(A,B) = max(2,1) = 2
Decision of Max player is to choose
node A

INTRODUCTION TO AI 67
Mini-max – Large Example
Let's take a game with a bigger tree:

Utility

INTRODUCTION TO AI 68
Mini-max – Large Example
From the tree, the minimizer will have the last step and attempt to
minimize final “Utility”

INTRODUCTION TO AI 69
Mini-max – Large Example
The maximizer will go next and attempt to maximize the final
“Utility”

INTRODUCTION TO AI 70
Mini-max – Large Example
The minimizer will get the second chance and attempt to minimize
the final “Utility”

INTRODUCTION TO AI 71
Mini-max – Large Example
Finally, the maximizer will attempt to maximize the “Utility”

INTRODUCTION TO AI 72
Mini-max – Large Example
Therefore, the Mini-max algorithm will take the following path:

INTRODUCTION TO AI 73
Mini-max Algorithm Pseudocode
function minimax(node, depth, maximizingPlayer) is
if depth = 0 or node is a terminal node then
return the heuristic value of node Depth is the
if maximizingPlayer then
value := −∞
maximum depth
for each child of node do that the algorithm
value := max(value, minimax(child, depth − 1, FALSE)) needs to traverse
return value
else (* minimizing player *)
value := +∞
for each child of node do
value := min(value, minimax(child, depth − 1, TRUE))
return value

(* Initial call *)
minimax(origin, depth, TRUE)

12/23/2022 INTRODUCTION TO AI 74
Tic-Tac-Toe Game
A game in which two
players alternately put Xs
and Os in compartments of
a figure formed by three
vertical lines crossing three
horizontal lines and each
tries to get a (row, column,
diagonal) of three Xs or
three Os before the
opponent does.
The tree of the game can
be constructed as shown
INTRODUCTION TO AI 75
Mini-max Technique for Tic-Tac-Toe
Let us define the following
If I win, I get 10 points (+10)
If I lose, I lose 10 points (-10)
If the game is a tie, then I get no points (0)
What is the objective of the game?
I get the maximum Utility (points) (+10)
My opponent gets the minimum Utility (points) (-10)
How would the game go?
Starting Agent (x) will try to maximize his Utility
In the second turn, the opponent Agent (o) will try to minimize the utility of
the starting agent (x)
INTRODUCTION TO AI 76
Tic-Tac-Toe
Number of vertices in the tree is
bounded by 9! (since there are 9
possible ways of placing the first
mark, 8 remaining ways of placing
the second, 7 the third, ..., and 1
in the last. This would be 9! =
362880.
Number of Actual games that can
be played = 255,168 games (not
all games need to fill all 9 blocks)
A subtree of the game is shown.

INTRODUCTION TO AI 77
Notion of Complexity
In a tree of possibilities:
If the branching factor for every node is (𝑏) and the depth (𝑑) is the
number of levels
Then the number of leaf nodes (or terminal nodes) is 𝑏𝑑
Chess
To construct the tree of Chess, we need to use the following information
 Average branching factor is 35 (i.e., a player on the average has 35 moves to choose
from)
 In a professional game, each player plays on the average 50 moves (steps)  full
game is about 100 moves in total
The search tree is on the average of the size

INTRODUCTION TO AI 78
Notion of Complexity
Can we make a tree this big?
Can a supercomputer find an optimal path in this tree?
Can we utilize cloud computing to solve the problem?
Let us take a professional Chess game with average tree of 10154
Lets get the notion of how long will this take to compute using some
information that we know. To do this we need the following information:
How many Atoms are there in our known Universe?
1082
How many nanoseconds are there in a year?
3×1016≈1016
What is the age of the known universe?
≈10 10

INTRODUCTION TO AI 79
Notion of Complexity
If each atom did a calculation every nanosecond from the start of
the known universe till today, the number of computations will be:
Number of Atoms × Number of nanosecond per year × Age of the
universe ≈ 1082×1016×1010=10108
We will be 1046 times short
Do you still think that any human made system can do the
calculations?
Use techniques to cut down the search space
People are also looking for Quantum Computing (maybe it can
perform such computations in a reasonable amount of time)
INTRODUCTION TO AI 80
Notion of Complexity
How did “Deep Blue” system beat the world
champion if we cannot compute the values in the
entire Mini-Max tree?
Answer: It uses tree pruning and depth-limited
search.
Pruning: is a data compression technique in
machine learning and search algorithms that
reduces the size of decision trees by removing
sections of the tree that are non-critical and
redundant to classify instances.

INTRODUCTION TO AI 81
Example on Cutting the Search Space

No need to compute
INTRODUCTION TO AI 82
Depth-Limited Search
Depth-limited search is like depth-first search with a
predetermined limit.
It can solve the drawback of the infinite path in the
depth-first search.
In Depth-limited search algorithm, the node at the depth
limit will be treated as if it has no successor nodes
further.
Advantages:
Depth-limited search is Memory efficient.
Disadvantages:
Depth-limited search is incomplete. https://www.javatpoint.com/ai-uninformed-search-algorithms

It may not be optimal if the problem has more than one
solution.

12/23/2022 INTRODUCTION TO AI 83
Resource Limits
Problem: In realistic games, cannot
search to leaves!
Solution: Depth-limited search
Instead, search only to a limited depth
in the tree
Replace terminal utilities with an
evaluation function for non-terminal
positions
Guarantee of optimal play is gone

INTRODUCTION TO AI 84
Evaluation Functions
Evaluation functions score non-terminals in depth-limited search

Ideal function: returns the actual minimax value of the position


In practice: typically weighted linear sum of features:

e.g. f1(s) = (num white queens – num black queens), etc.


INTRODUCTION TO AI 85
Conclusion
Search is a powerful tool to solve problems where we want to find
series of actions to reach to a goal.
A tree is a good tool to visualize search but that does not mean
that we cannot perform search without a tree.
We often revert to Uninformed search in problems we do not have
any prior information about.
Informed search leverages past knowledge to solve a problem.
Adversarial search examines problems that arise when we try to
plan in a world where other agents are planning against us.

INTRODUCTION TO AI 86

You might also like