16
CS301 Peter Kemper R 104A, phone 221-3462, em ail: k emper@cs . wm . edu Today: Knight’s Tour Project Reference: Project Assignment 1 1

Knightstour

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 3: Knightstour

describes available methods for any

class that implements Tour interface

Interface:

Project Overview

GUI = Graphical User Interface

provides

elementary

graphic for i/o

GUI

shows 5 x 5 board picks random initial position has a solves open Knight’s tour problem contains/has Tour object

Interface: Tour

Tour object

Tour

KnightsTourP1 implements

GUI operates

on

KnightsTourP1

Knightsobject (almost) without knowing TourP1

Methods compute a valid solution (if possible)

3

Page 4: Knightstour

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 How do you find a solution: find an algorithm. Does the algorithm always terminate? What is the time and space complexity of the algorithm?

4

Page 5: Knightstour

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

Page 6: Knightstour

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

open tour means that distance between 25 and 1 is arbitrary

start position given

Page 7: Knightstour

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!

Page 8: Knightstour

Knight’s Tour Problem

How do you solve this?

How do you solve thiswith a program?

8

Page 9: Knightstour

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

Page 10: Knightstour

How to solve this on a computer

Backtracking algorithmfunction backtrack(P, c)

if (invalid(P,c)) then return // recognize failure, backtrack

if (complete(P,c) then output(P,c) ; stop // terminationwhile (hasMoreCandidates(P,c)) // go through all candidates

{

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 cNote: undoChanges() only necessary if function manipulates shared data, e.g. P

10

Page 11: Knightstour

Backtracking

Illustrate Knight’s Tour backtracking as tree!

Do you see ways to improve the performance of the algorithm?

11

Page 12: Knightstour

Source : Sedgewick & Wayne: http://introcs.cs.princeton.edu/java/23recursi12

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 parameter values in reduction step converges to the base case.

on/

Page 13: Knightstour

Source : Sedgewick & Wayne: http://introcs.cs.princeton.edu/java/23recursi13

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

on/

Page 14: Knightstour

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

Page 15: Knightstour

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

g/wiki/Alan_Kay 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, see also: http://en.wikipedia.org/wiki/How_to_Solve_I1t5

Page 16: Knightstour

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