Knightstour

361 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
361
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Knightstour

  1. 1. CS301 Peter Kemper R 104A, phone 221-3462, email:kemper@cs.wm.edu Today: Knight’s Tour Project Reference: Project Assignment 1 1
  2. 2. Knight’s Tour Problem: A Chess Problem Source: http://en.wikipedia.org/wiki/Knight's_tour 2
  3. 3. Project Overview GUI = Graphical User Interface  provides elementar ydescribes available methods for graphic for i/o any  GUI  class that implements Tour interface shows 5 x 5  board picks random initial position  has a Interface:
  4. 4. Project Assignment Class KnightsTourP1 does not work! Add a solution algorithms that given an initial position on an n x n board (here n=5)  compute an open Knight’s tour if one exists  recognize if a tour does not exist  Typical questions in math Does a given problem have a solution?  If it does, is a solution unique?  How do you find a solution?  Typical questions in CS
  5. 5. Knight’s Tour Problem --> Programming Problem What is a good point of view to implement an algorithm that solves this problem? Map to a formalism that is closer to programming! Suggestions? Source: http://en.wikipedia.org/wiki/Knight's_tour 5
  6. 6. Knight’s Tour Problem How do you solve this? 5 x 5 matrix  permutation of numbers 1,2,3,...,25  distance between consecutive numbers i and i+1 is 2 steps in one direction plus 1 step in orthogonal direction  6 open tour means that distance between 25 and 1 is arbitrary
  7. 7. Knight’s Tour Solution  Note  Not all configurations on a 5 x5 board have a solution!  Chess board is larger such that computation takes longer! 7
  8. 8. Knight’s Tour Problem How do you solve this? How do you solve this with a program? 8
  9. 9. How to solve this on a computer? 5 x 5 = 25 fields with (1,2,...,25) in some order Naive: Try all permutations of (1,2,...,25) and see which one fits all constraints  Constraints:     rules of the game some fields have given, fixed values Why is this naive?  Does not involve any real insight + better concepts exist  Number of permutations is high Other ideas? 9
  10. 10. How to solve this on a computer Backtracking algorithm function backtrack(P, c) if (invalid(P,c)) then return // recognize failure, backtrack if (complete(P,c) then output(P,c) ; stop // termination while (hasMoreCandidates(P,c)) through all candidates // go { s = nextCandidate(P,c) ; // get next candidate backtrack(P,s) ; // recursion, try next candidate undoChanges(P,s) ; // failed, need to recover state of P } Problem P, candidate c Note: undoChanges() only necessary if function manipulates shared data, e.g. P 10
  11. 11. Backtracking Illustrate Knight’s Tour backtracking as tree! Do you see ways to improve the performance of the algorithm? 11
  12. 12. Recursion A method calls itself Classic example: Factorial function  Computes: N! = N x (N-1) x ... x 1 Shows two essential components Base case: returns value without recursion, i.e. terminates recursive method calls  Reduction step: relates function at particular input to same function at some other inputs.  Key for termination is that the sequence of Source : Sedgewick & Wayne: http://introcs.cs.princeton.edu/java/23recursithe base parameter values in reduction step converges to 1 case. 2 
  13. 13. Pitfalls of Recursion Missing base case Recursion will not terminate  Example: Faulty method for Harmonic numbers  No guarantee of convergence  Recursive call to a problem that is not smaller Source : Sedgewick & Wayne: http://introcs.cs.princeton.edu/java/23recursi on/ 1 3
  14. 14. Pitfalls of Recursion Excessive space requirements Recursive methods calls build up on execution stack  Java: eventually results in StackOverflowError, e.g. at N=5000  Excessive recomputation Recursive calls may imply that results are computed over and over again  Example: Fibonacci sequence  Note: storing results leads to concept of Dynamic Programming  Source : Sedgewick & Wayne: http://introcs.cs.princeton.edu/java/23recursion1/4
  15. 15. Problem solving techniques Divide and conquer split a problem into a set of smaller ones  solve smaller problems  combine solutions of smaller problems to solution of main problem  matches with recursion  Change point of view  Alain Kay: “A change in perspective is worth 80 IQ points.”  other quotes from Kay see http://en.wikiquote.org/wiki/Alan_Ka y  e.g. “Actually I made up the term "object-oriented", and I can tell you I did not have C++ in mind.” Try to solve more restricted special case(s)  get a solution, try to generalize from here Try to solve a more general problem Nice to read: George Polya, How to solve it,
  16. 16. Getting back to CS 301 Learning objectives for Project 1 Develop Java programming skills: basic UI,  recursion  Understand Unit testing with Junit  Project contains a substantial set of test cases as examples Recognize Test-driven development  Develop code to pass a set of tests 16

×