Professional Documents
Culture Documents
in Artificial Intelligence
Marcus Gerhold (marcus.gerhold@gmail.com)
Recursion
Minimax Algorithm
Tic Tac Toe
In Java/Processing
class GridElement {
boolean visited;
boolean marked;
boolean wall_n, wall_e, wall_w,
wall_s;
int xcoord, ycoord;
GridElement north, east, west,
south;
GridElement parent;
}
Recursion - recursive data structures
In C++
class ListElement { recursive data structures
are useful for
public: dynamic structures such as
ListElement(int); lists, trees, graphs,…
int getValue() const;
void setValue(int);
ListElement* getNext() const;
void setNext(ListElement *);
private:
int value;
ListElement *next;
};
Recursion - recursive functions
void ListElement::printList()
{
std::cout << value;
Note, that this
if (next != NULL) { This is
member
std::cout << " "; called
function calls
next->printList(); recursion.
itself.
}
else {
std::cout << std::endl;
}
}
NULL
Recursion
void solveIt(the-Problem)
{
. . .
solveIt(the-Problem-a-bit-reduced);
}
Of course, there’s a lot more to it than this.
In recursion,
the same computation recurs,
or occurs repeatedly,
as the problem is solved.
Iterative definition:
6! = 1 ⋅ 2 ⋅ 3 ⋅ 4 ⋅ 5 ⋅ 6
Recursive definition:
6! = 6 ⋅ 5!
Exercise
Iterative definition:
n! = 1 ⋅ 2 ⋅ 3 ⋅ ... ⋅ n
Recursive definition:
n! = n ⋅ (n-1)! if n>1
1! = 1;
Exercise
Iterative Recursive
for the smallest
int f_iterative(int n) { int f_recursive(int n){ instance of the
int result = 1; if (n <= 1) { problem we have to
return 1; give a solution.
for (int i = 1; i <= n; i++) }
{
result = result*i; return n * f_recursive(n - 1);
} }
return result;
} reduction to a
smaller problem
Recursion vs looping
Some vague rules of thumb:
What is mystery(4)?
int mystery(int n) {
if (n <= 1) {
return 1;
}
int result = mystery(n - 1);
return result + n * n;
}
You need prototypes
Mutual recursion bool odd(int);
bool even(int);
At least one
Pseudocode
Merge sort
split
merge
Tree recursion – Multiple recursive calls
Merge sort
Pseudocode
merge_sort(list) {
if (the list is only one element)
return that list of one element
else {
split the list in two parts;
merge_sort(first part of the list);
merge_sort(second part of the list);
merge(the two sorted lists into one);
}
}
Recursion Classic - Towers of Hanoi
Problem:
move a tower of disks from pillar 1 to pillar 2
only move one disk at a time
only put a smaller disk on top of a higher
Recursion Classic - Towers of Hanoi
Pseudo code
move_tower (int height, position from_A, position
to_B){
if (height==0) return; The legend mentions 64
else{ disks in a temple. If true,
move_tower (height-1, from_A, to_C); and moving a disk would
move_disc (from_A, to_B); take 1 second, it would
move_tower (height-1, from_C, to_B); take 585 billion years to
} finish
}
Recursion - Efficiency n=4
●
Fibonacci Numbers
n=1
n=2 n=5
n=3
...
Recursion - Efficiency
Fibonacci Numbers
Each value in the sequence is the sum of the two preceding
values.
The first ten terms of the sequence are:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55,…
Defined by equations
f(1) = 1
f(2) = 1
f(n) = f(n-1)+f(n-2)
Recursion - Efficiency
int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
Recursion - Efficiency
Fibonacci Numbers
Call graph of straight forward solution:
Lots of
unnecessary
repetitions
Recursion - Efficiency
Fibonacci Numbers
Better solution: This is close to the
iterative solution.
int subfib(int term, int val, int prev) {
if (term == 0) {
return prev; This solution uses so-
} called tail recursion.
if (term == 1) {
return val;
}
Outside of the scope of
return subfib(term - 1, val + prev, val);
this course.
}
int fib(int n) {
return subfib(n, 1, 0);
}
Recursion
...
At this stage it is
easy to determine
who wins
How to almost finish
If it is your
() turn, play
for win.
Assume the
best outcome
How to almost finish
The other
player also
plays to win.
Assume the
worst
outcome
Min Max Strategy Given a position, how
find the best move.
has the turn
Choose to maximize
turn own chances
Choose to
minimize
o turn opponents chances
Choose to maximize
own chances
turn
Min Max Strategy How to evaluate the
moves?
How to evaluate
the other
turn moves?
+1 for win
0 for draw
How to do this
Min Max Strategy How to evaluate the
moves? recursively?
e !
km
Pic
turn Maximize your
-1 -1 0 own score
turn +1 0 0 +1
Maximize your
own score
Min Max Strategy
Setting
each board position determines a node
there is an initial board position
in each level there is one player to take the moves
the possibilities of the game describe a tree
each game is a path in the tree
Pseudo Code
int maximise(Node node, int player) {
if (node is winning for player) return 1;
if (node is draw) return 0; mutual
if (node is loosing for player) return -1; recursion
int evaluation = -∞;
for all children child of the node :
evaluation = max(evaluation, minimise(child, other
player));
make the child successor if it has the max evaluation;
return evaluation;
}
Min Max Strategy - Player o’s turn
Pseudo Code
int minimise(Node node, int player) {
if (node is winning for player) return -1;
if (node is draw) return 0; mutual
if (node is loosing for player) return +1; recursion
int evaluation = ∞;
for all children child of the node :
evaluation = min(evaluation, maximise(child, other
player));
make the child successor if it has the min evaluation;
return evaluation;
}
Min Max Strategy
maximize(...,1)
minimize(...,1)
maximize(...,1)
Min Max Strategy
maximize(...,1)
minimize(...,1)
maximize(...,1)
minimize(...,1)
Min Max Strategy
maximize(...,1)
minimize(...,1)
maximize(...,1)
minimize(...,1)
Min Max Strategy
maximize(...,1)
minimize(...,1)
maximize(...,
1)
Min Max Strategy
maximize(...,1)
minimize(...,1)
maximize(...,1)
minimize(...,1)
Min Max Strategy And so
on ...
maximize(...,1)
minimize(...,1)
maximize(...,1)
minimize(...,1)
Min Max Strategy – Simple recursive
Merged version
int minimax(Node node, int player, int maxPlayer) {
if the node is a terminal evaluate it and return evaluation
if (player is the maxPlayer) {
int evaluation = -∞;
for all children child of the node :
evaluation = max(evaluation, minimax(child, other player,
maxPlayer));
make the child successor if it has the max evaluation;
return evaluation;
}
else {
int evaluation = ∞;
for all children child of the node :
evaluation = min(evaluation, minimax(child, other player,
maxPlayer));
make the child successor if it has the min evaluation;
return evaluation;
}
} Two turns in one
Min Max Strategy
Addendum : Other 2p games
Example: Nim
● Multiple heaps of matches/objects
● 2 players take turns
● Remove as many as you want from one heap
● The one to remove the last match, wins
● Can you win, no matter what?
Addendum : Other 2p games
Admin