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

5.1 Tree Search

Uploaded by

jeevith.k2053
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)
15 views

5.1 Tree Search

Uploaded by

jeevith.k2053
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/ 26

Game Programming

A. Avinash, Ph.D.,
Assistant Professor
School of Computer Science and Engineering (SCOPE)
Vellore Institute of Technology (VIT), Chennai
Motivati
on
One of the major goals of AI is to help humans in solving
complex tasks
🞑 Which is the shortest way from Milan to Innsbruck?
🞑 Which is the fastest way from Milan to Innsbruck?
🞑 How can I solve my Sudoku game?
🞑 What is the sequence of actions I should apply to win a game?
 Sometimes finding a solution is not enough, you want the
optimal solution
according to some “cost” criteria
 All the example presented above involve looking for a plan
 A plan that can be defined as the set of operations to be
performed of an initial state, to reach a final state that is
considered the goal state
 Thus we need efficient techniques to search for paths, or
sequences of actions, that can enable us to reach the goal
state, i.e. to find a plan
 Such techniques are commonly called Search Methods
Search Space
Representation
Representing the search Nod
Loo
space is the first step to e
Ar p
enable the problem
resolution c
 Search space is mostly
represented through
graphs
 A graph is a finite set of
nodes that are connected
by arcs
 A loop may exist in a
graph, where an arc lead
back to the original node
 In general, such a
graph is not explicitly
given
 Search space is
constructed
during search
Search Space
Representation
A graph is undirected if
undirect direct

arcs do not imply a


direction, direct
otherwise
connected
 A graph is connected
if every pair of disconnected
nodes is connected
by a path
tre
 A connected graph e
with no loop is called
tree
 A weighted graph, is a
graph for which a weighted
value is associated to 1
4
1

each arc 5 6
2 2 1 1
Search
Methods
A search method is defined by picking the order
of node expansion

 Strategies are evaluated along the following


dimensions:
🞑 completeness: does it always find a solution if one
exists?
🞑 time complexity: number of nodes generated
🞑 space complexity: maximum number of nodes in
memory
🞑 optimality: does it always find the shortest path
solution?

 Time and space complexity are measured in


terms of
🞑 b: maximum branching factor of the search tree
Search
Methods
 Uninformed techniques
🞑 Systematically search complete graph,
unguided
🞑 Also known as brute force, naïve, or blind

 Informed methods
🞑 Use problem specific information to guide
search in promising directions
Uninformed
Search
A class of general purpose algorithms that operates in a brute
force way
🞑 The search space is explored without leveraging on any
information on the
problem
 Also called blind search, or naïve search
 Since the methods are generic they are intrinsically inefficient

 E.g. Random Search


🞑 This method selects randomly a new state from the current one
🞑 If the goal state is reached, the search terminates
🞑 Otherwise the methods randomly select an other operator to move
to the next state

 Prominent methods:
🞑 Depth-First Search
🞑 Breadth-First Search
Depth-First
Search
 Depth-First Search (DFS) begins at the root node and exhaustively
search each branch to it maximum depth till a solution is found
🞑 The successor node is selected going in depth using from right to left
(w.r.t. graph
representing the search space)
 If greatest depth is reach with not solution, we backtrack till we find an
unexplored branch to follow

 DFS is not complete


🞑 If cycles are presented in the graph, DFS will follow these cycles
indefinitively
🞑 If there are no cycles, the algorithm is complete
🞑 Cycles effects can be limited by imposing a maximal depth of search (still the
algorithm is incomplete)
 DFS is not optimal
🞑 The first solution is found and not the shortest path to a solution

 The algorithm can be implemented with a Last In First Out (LIFO) stack
or recursion
Depth-First Search:
Example S 1

A B
2
3

S B F F A C D
5
4
6
open={S} closed
={}
S A F D 1. Visit S: open={A,B}, closed={S}
2.Visit A: open={S,B,F,B}, closed={A,S}
3.Visit S: open={B,F,B}, closed={S,A,S}
4.Visit B: open={S,A,F,D,F,B},
closed={B,S,A,S}
5.Visit S: open={A,F,D,F,B},
closed={S,B,S,A,S}
6.Visit A: open={F,D,F,B},
closed={A,S,B,S,A,S}
Depth-First Search:
Example S

A B

S B F F A C D

S A F D

Result is: S->A->B-


>F
Depth-First Search:
Complexity
Time
d=
Complexity
🞑 assume (worst case) that
there is 1 goal leaf at the 0
RHS d=
🞑 so DFS will expand all 1
nodes m=d
G =2
=1 + b + b2+
🞑 where m is+
......... the
bmmax depth of
the tree d=
= O (bm) 0
d=
 Space Complexity 1
🞑 how many nodes can be in
the queue (worst-case)? d=
2
🞑 at each depth l < d we have
d=
b-1
3
nodes
m=d
🞑 at depth m we have b nodes =4
Breadth-First
Search
 Breadth-First Search (BFS) begins at the
root node and explore level-wise al the
branches

 BFS is complete
🞑 If there is a solution, BFS will found it
 BFS is optimal
🞑 The solution found is guaranteed to be the
shortest path possible

 The algorithm can be implemented with a


First In First Out (FIFO) stack
Breadth-First Search:
Example S 1

2
A B
3

4 5
S B F F A C
D
open = {S}, closed={}
1. Visit S: open = {A,B}, closed={S}
S A F D 2. Visit A: open={B,S,B,F}, closed={S,A}
3. Visit B: open={S,B,F,F,A,C,D},
closed={S,A,B}
4. Visit S: open={B,F,F,A,C,D},
closed={S,A,B,S}
5. Visit B:
open={F,F,A,C,D,S,A,C,D},
closed={S,A,B,S,B}
Breadth-First Search:
Example S
A B

S B F F A C D

S A F D

Result is: S->A-


>F
Breadth-First Search:
Complexity
 Time complexity is the same magnitude
as DFS
🞑 O (bm)
🞑 where m is the depth of the solution
 Space Complexity
🞑 how many nodes can be in the queue (worst- d=
case)? 0
d=
🞑 so BFS will
assume store
(worst all that th1 ere is 13 goal leaf
case)
2 1
nodes
at the d=
RHS=1 + b + b +
2
6 7 2
......... + b m
d=
= O (bm)
4 5
3
8 9 10 11 12 13 14 G 15 d=
4
Informed
Search
Blind search methods take O(b m ) in the worst
case

 May make blind search algorithms


prohibitively slow where d is large

 How can we reduce the running time?


🞑 Use problem-specific knowledge to pick which states
are better candidates
Informed
Search
Also called heuristic search

 In a heuristic search each state is assigned a


“heuristic value” (h-value) that the search uses
in selecting the “best” next step

 A heuristic is an operationally-effective nugget


of information on how to direct search in a
problem space

 Heuristics are only approximately correct


Informed Search: Prominent
methods
 Best-First
Search

 A*

 Hill Climbing
Cost and Cost
Estimation
f(n)=g(n)+h(n)
 g(n) the cost (so far) to reach the node
n
 h(n) estimated cost to get from the
node to the goal
 f(n) estimated total cost of path
through n to goal
Informed Search: Best-First
Search
 Special case of breadth-first search
 Uses h(n) = heuristic function as its evaluation
function
 Ignores cost so far to get to that node (g(n))
 Expand the node that appears closest to goal

 Best First Search is complete


 Best First Search is not optimal
🞑 A solution can be found in a longer path (higher h(n) with
a lower g(n) value)

 Special cases:
🞑 uniform cost search: f(n) = g(n) = path to n
🞑 A* search
Best-First Search:
Example S 1

h= h=
1 2 1
A B
h= h=
3
h= h= 2 2 h= h= h=
2 S 2 2 2 2
F F A C
B D
open = {S}, closed={}
1. Visit S: open = {A,B}, closed={S}
S A F D 2. Visit A: open={B,F,B,S}, closed={S,A}
3. Visit B: open={F,B,S,F,A,C,D},
closed={S,A,B}
4. Visit F: Goal Found!

In this case we estimate the cost as the distance from the root node (in
term of nodes)
Best-First Search:
Example S
h=1, h=1,
A w=2 w=1
B
h=2, h=2,
h= h= w=7 w=4 h= h= h=
2 S 2 2 2 2
F F A C D
B

S A F D

Result is: S->A->F!

If we consider real costs, optimal


solution is: S->B->F
A
*
 Derived from Best-First
Search
 Uses both g(n) and h(n)
 A* is optimal
 A* is complete
A* :
Example S 1

h=1, w=2, h=1, w=1,


g=2 g=1
2
A B
h=2 h=2, w=7, h=2, w=4,
, h= g=9 3 g=5
h= h=2, w=4,
w= 2,
2 g=5
2, w= h=
g=4
S 1 B F F 5
w= C 2, D
34
g=
3
A g=
w=
1
4
open = {S}, closed={} g=
2
1. Visit S: open = {B,A}, closed={S}
S A F D 2. Visit B: open={A,C,A,F,D},
closed={S,B}
3. Visit A: open={C,A,F,D,B,S,F},
closed={S,B,A}
4. Visit C: open={A,F,D,B,S,F},
closed={S,B,A,C}
In this case we estimate the 5. Visit
cost as A:
theopen={F,D,B,S,F},
distance from the root node (in
term of nodes) closed={S,B,A,C,A}
6. Visit F: Goal Found!
A* :
Example S
h=1, w=2, h=1, w=1,
g=2 g=1
A B
h=2 h=2, w=7, h=2, w=4,
, h= g=9 g=5
h= h=2, w=4,
w= 2,
2 g=5
2, w= h=
g=4
S 1 B F F A w= C 2, D
3
g= w=
g=
3 1
4
g=
2
S A F D

Result is: S->B-


>F!
Informed Search Algorithm
Comparison
Algorithm Time Space Optimal Complete Derivative
Best First O(bm) O(bm) No Yes BFS
Search
Hill Climbing O() O(b) No No
A* O(2N) O(bd) Yes Yes Best First
Search

b, branching factor
d, tree depth of the
solution
m, maximum tree depth

You might also like