1 / 38

A lgorithms & D ata Structures for Games

Minor Games Programming. A lgorithms & D ata Structures for Games. Lecture 3 . Next AD college. Algorithms and Data Structures. Feedback previous lectures Theory: Dynamic Programming Theory: Randomized Algorithms Theory: Backtracking. Jan Verhoeven j.verhoeven@windesheim.nl.

adora
Download Presentation

A lgorithms & D ata Structures for Games

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Minor Games Programming Algorithms & Data Structuresfor Games Lecture 3

  2. Next AD college

  3. Algorithms and Data Structures • Feedback previous lectures • Theory: Dynamic Programming • Theory: Randomized Algorithms • Theory: Backtracking Jan Verhoeven j.verhoeven@windesheim.nl

  4. Theory: Dynamic Programming • 10.3 Dynamic Programming • 10.3.1 Using a Table instead of Recursion

  5. 10.3 Dynamic Programming • Rewrite the recursive algorithm as a non recursive algorithm that systematically records the answers to the subproblems in a table.

  6. 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).

  7. 10.3.1 Using a “Table” instead of Recursion a linearsolution

  8. Fibonacci

  9. Another very nice example Solve the recurrence: with C(0) = 1 And what about C(2) and C(3)?

  10. Figure 10.43 Recursive solution(what is the running time?)

  11. Figure 10.45 solution with a table(what is the running time?)

  12. Running time

  13. Theory: Randomized Algorithms • 10.4 Randomized Algorithms • 10.4.2 Skip Lists • And extra topic: How about Primes….

  14. 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

  15. 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

  16. Linked lists with extra links to cells ahead

  17. Searching for the value of 8

  18. A skip list

  19. Before and after an insertion in a skip list

  20. 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/

  21. Primes Do you know a method to generate prime numbers? See: Sieve_of_Eratosthenes

  22. Primes boolisPrime (const long aNumber) { // Do you know a method to test // whether a number is prime? } And what about the running time ?

  23. Primes Long nextPrime(const long aPrime) { // Do you know a method to generate // the next prime following aPrime? } And what about the running time ?

  24. 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.

  25. Random Walk (2) • Will the drunkard ever get back to his home from the bar? • It turns out that he will (almost surely).

  26. Random Walk (3)

  27. Algorithms and Data Structures for games

  28. 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.

  29. An example …

  30. 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!

  31. In pseudo code booleansolve(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 } }

  32. Cindy’s puzzle, the goal is to reverse the positions of the marbles: • 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.

  33. Cindy’s puzzle SEE: ..\..\Projects\BacktrackingCindyCS\BacktrackingCindyCS.sln

  34. 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

  35. boolsolve(intvalue, 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 }

  36. Another example: SUDOKU • See: • ..\..\Projects\SudokuCS\SudokuCS.sln

  37. The famous 8 queen’s puzzlehttp://en.wikipedia.org/wiki/Eight_queens_puzzle

  38. 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#)

More Related