a lgorithms & d ata s tructures for g ames lecture 3 1 minor games programming
TRANSCRIPT
![Page 1: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/1.jpg)
ALGORITHMS & DATA STRUCTURES FOR GAMES
Lecture 3
1
Minor Games Programming
![Page 2: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/2.jpg)
NEXT AD COLLEGE
Monday
2
![Page 3: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/3.jpg)
ALGORITHMS AND DATA STRUCTURES
Feedback previous lectures Theory: Dynamic Programming Theory: Randomized Algorithms Theory: Backtracking
3
![Page 4: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/4.jpg)
THEORY: DYNAMIC PROGRAMMING
10.3 Dynamic Programming 10.3.1 Using a Table instead of Recursion
4
![Page 5: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/5.jpg)
10.3 DYNAMIC PROGRAMMING
Rewrite the recursive algorithm as a non recursive algorithm that systematically records the answers to the subproblems in a table.
5
![Page 6: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/6.jpg)
10.3.1 USING A TABLE INSTEAD OF RECURSION
The natural recursive program to compute the Fibonacci numbers is very inefficient. (Running time is exponential).
6
![Page 7: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/7.jpg)
10.3.1 USING A “TABLE” INSTEAD OF RECURSION
a linear solution
7
![Page 8: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/8.jpg)
FIBONACCI
8
![Page 9: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/9.jpg)
ANOTHER VERY NICE EXAMPLESolve the recurrence:
with C(0) = 1
And what about C(2) and C(3)?
9
![Page 10: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/10.jpg)
FIGURE 10.43 RECURSIVE SOLUTION(WHAT IS THE RUNNING TIME?)
10
![Page 11: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/11.jpg)
FIGURE 10.45 SOLUTION WITH A TABLE (WHAT IS THE RUNNING TIME?)
11
![Page 12: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/12.jpg)
RUNNING TIME
12
![Page 13: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/13.jpg)
THEORY: RANDOMIZED ALGORITHMS
10.4 Randomized Algorithms 10.4.2 Skip Lists
And extra topic: How about Primes….
13
![Page 14: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/14.jpg)
10.4 RANDOMIZED ALGORITHMS
At least once during the algorithm, a random number is used to make a decision. The running time depends on the particular input, but also on the random numbers that occur.
A sample application is: 10.4.2 Skip Lists
14
![Page 15: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/15.jpg)
10.4.2 SKIP LISTS
A data structure that supports both searching and insertion in O(log N) expected time.
Also see:http://en.wikipedia.org/wiki/Skip_list
15
![Page 16: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/16.jpg)
LINKED LISTS WITH EXTRA LINKS TO CELLS AHEAD
16
![Page 17: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/17.jpg)
SEARCHING FOR THE VALUE OF 8
17
![Page 18: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/18.jpg)
A SKIP LIST
18
![Page 19: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/19.jpg)
BEFORE AND AFTER AN INSERTION IN A SKIP LIST
19
![Page 20: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/20.jpg)
BEAUTIFUL APPLETS !!Please run:http://people.ksp.sk/~kuko/bak/big/
And you might take a look at:http://iamwww.unibe.ch/~wenger/DA/SkipList/
20
![Page 21: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/21.jpg)
PRIMES
Do you know a method to generateprime numbers?
See: Sieve_of_Eratosthenes21
![Page 22: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/22.jpg)
PRIMES
bool isPrime (const long aNumber){// Do you know a method to test// whether a number is prime?
}
And what about the running time ?
22
![Page 23: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/23.jpg)
PRIMES
Long nextPrime (const long aPrime){
// Do you know a method to generate// the next prime following aPrime?
}
And what about the running time ? 23
![Page 24: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/24.jpg)
WELL KNOWN STUDY: A RANDOM WALK
Imagine now a drunkard walking randomly in a city.
The city is infinite and arranged in a square grid, and at every intersection, the drunkard chooses one of the four possible routes (including the one he came from) with equal probability.
Formally, this is a random walk on the set of all points in the plane with integer coordinates.
24
![Page 25: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/25.jpg)
RANDOM WALK (2)
Will the drunkard ever get back to his home from the bar?
It turns out that he will (almost surely).
25
![Page 26: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/26.jpg)
RANDOM WALK (3)
26
![Page 27: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/27.jpg)
ALGORITHMS AND DATA STRUCTURES FOR GAMES
Backtracking
![Page 28: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/28.jpg)
BACKTRACKING IS A FORM OF RECURSION.
The usual scenario is that you are faced with a number of options, and you must choose one of these.
After you make your choice you will get a new set of options; just what set of options you get depends on what choice you made.
This procedure is repeated over and over until you reach a final state.
If you made a good sequence of choices, your final state is a goal state; if you didn't, it isn't.
28
![Page 29: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/29.jpg)
AN EXAMPLE …
29
![Page 30: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/30.jpg)
THE STEPS:
Starting at Root, your options are A and B. You choose A.
At A, your options are C and D. You choose C. C is bad. Go back to A. At A, you have already tried C, and it failed.
Try D. D is bad. Go back to A. At A, you have no options left to try. Go back
to Root. At Root, you have already tried A. Try B. At B, your options are E and F. Try E. E is good. Congratulations!
30
![Page 31: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/31.jpg)
IN PSEUDO CODE
boolean solve(Node n){if n is a leaf node { if the leaf is a goal node,
return true else return false }else { for each child c of n { if solve(c) succeeds,
return true } return false }}
31
![Page 32: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/32.jpg)
CINDY’S PUZZLE, THE GOAL IS TO REVERSE THE POSITIONS OF THE MARBLES:
32
The black marbles can only move to the right, and the white marbles can only move to the left (no backing up). At each move, a marble can either:• Move one space ahead, if that space is clear, or• Jump ahead over exactly one marble of the opposite
color, if the space just beyond that marble is clear.
![Page 33: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/33.jpg)
CINDY’S PUZZLE
33
SEE: ..\..\Projects\BacktrackingCindyCS\BacktrackingCindyCS.sln
![Page 34: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/34.jpg)
A LITTLE EXERCISE
Can you describe an backtracking algorithm to solve the next problem?
The problem is to write an integer as the sum of 4 squares. This is always possible !
So: 70 = 64 + 4 + 1 + 1 And: 12345 = 11664 + 676 + 4 + 1
See: ..\..\Projects\Backtracking4SquaresCS\Backtracking4SquaresCS.sln
34
![Page 35: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/35.jpg)
bool solve(int value, int num) { if (value == 0) return true; // Are we
done?
// we have no more numbers to work with if (num == 0) return false;
// Start at 1 and work up for (int i = 1; i * i <= value; i++) { int sq = i * i; // Place guess if (solve(value - sq, num - 1))
return true; }
return false; // Nothing worked: Backtrack }
35
![Page 36: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/36.jpg)
ANOTHER EXAMPLE: SUDOKU
See: ..\..\Projects\SudokuCS\SudokuCS.sln
36
![Page 37: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/37.jpg)
THE FAMOUS 8 QUEEN’S PUZZLE HTTP://EN.WIKIPEDIA.ORG/WIKI/EIGHT_QUEENS_PUZZLE
37
![Page 38: A LGORITHMS & D ATA S TRUCTURES FOR G AMES Lecture 3 1 Minor Games Programming](https://reader035.vdocuments.site/reader035/viewer/2022062320/56649d6f5503460f94a503be/html5/thumbnails/38.jpg)
HOMEWORK AND PRACTICE
Study the slides and the corresponding text in your study book.
Try to program one of the backtracking examples (Cindy’s puzzle, Sudoku, eight queens)
The practice is about a backtracking problem to be solved (using C#) 38