SlideShare ist ein Scribd-Unternehmen logo
1 von 16
CS301
Peter Kemper
R 104A, phone 221-3462, email:kemper@cs.wm.edu

Today: Knight’s Tour Project
Reference: Project Assignment 1

1
Knight’s Tour Problem: A Chess Problem

Source: http://en.wikipedia.org/wiki/Knight's_tour 2
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:
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
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
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
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
Knight’s Tour Problem

How do you solve this?
How do you solve this
with a program?

8
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
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
Backtracking
Illustrate Knight’s Tour backtracking as tree!

Do you see ways to improve the performance of the
algorithm?
11
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

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
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
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,
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

Weitere ähnliche Inhalte

Was ist angesagt?

Lecture 2 role of algorithms in computing
Lecture 2   role of algorithms in computingLecture 2   role of algorithms in computing
Lecture 2 role of algorithms in computingjayavignesh86
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsVenkatesh Iyer
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchAmrinder Arora
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms ISri Prasanna
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: IntroductionAndres Mendez-Vazquez
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Dr. Pankaj Agarwal
 
Symbolic Execution as DPLL Modulo Theories
Symbolic Execution as DPLL Modulo TheoriesSymbolic Execution as DPLL Modulo Theories
Symbolic Execution as DPLL Modulo TheoriesQuoc-Sang Phan
 
Introduction to Approximation Algorithms
Introduction to Approximation AlgorithmsIntroduction to Approximation Algorithms
Introduction to Approximation AlgorithmsJhoirene Clemente
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
A calculus of mobile Real-Time processes
A calculus of mobile Real-Time processesA calculus of mobile Real-Time processes
A calculus of mobile Real-Time processesPolytechnique Montréal
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsSwapnil Agrawal
 
2010 3-24 cryptography stamatiou
2010 3-24 cryptography stamatiou2010 3-24 cryptography stamatiou
2010 3-24 cryptography stamatiouvafopoulos
 

Was ist angesagt? (20)

Lecture 2 role of algorithms in computing
Lecture 2   role of algorithms in computingLecture 2   role of algorithms in computing
Lecture 2 role of algorithms in computing
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First Search
 
chapter 1
chapter 1chapter 1
chapter 1
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction
 
NP completeness
NP completenessNP completeness
NP completeness
 
Unit 5
Unit 5Unit 5
Unit 5
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Daa notes 2
Daa notes 2Daa notes 2
Daa notes 2
 
ARIC Team Seminar
ARIC Team SeminarARIC Team Seminar
ARIC Team Seminar
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
Symbolic Execution as DPLL Modulo Theories
Symbolic Execution as DPLL Modulo TheoriesSymbolic Execution as DPLL Modulo Theories
Symbolic Execution as DPLL Modulo Theories
 
Introduction to Approximation Algorithms
Introduction to Approximation AlgorithmsIntroduction to Approximation Algorithms
Introduction to Approximation Algorithms
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
A calculus of mobile Real-Time processes
A calculus of mobile Real-Time processesA calculus of mobile Real-Time processes
A calculus of mobile Real-Time processes
 
Chap14
Chap14Chap14
Chap14
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
2010 3-24 cryptography stamatiou
2010 3-24 cryptography stamatiou2010 3-24 cryptography stamatiou
2010 3-24 cryptography stamatiou
 

Andere mochten auch

Andere mochten auch (18)

Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studio
 
Android wear notes
Android wear notesAndroid wear notes
Android wear notes
 
Rational rose tutorial
Rational rose tutorialRational rose tutorial
Rational rose tutorial
 
Mobile development xamarain
Mobile development xamarainMobile development xamarain
Mobile development xamarain
 
7 embed systcasestudy
7 embed systcasestudy7 embed systcasestudy
7 embed systcasestudy
 
Chapter 02 collaborative recommendation
Chapter 02   collaborative recommendationChapter 02   collaborative recommendation
Chapter 02 collaborative recommendation
 
Intelli j idea-report
Intelli j idea-reportIntelli j idea-report
Intelli j idea-report
 
Programming
ProgrammingProgramming
Programming
 
Big data-and-the-web
Big data-and-the-webBig data-and-the-web
Big data-and-the-web
 
(148065320) dijistra algo
(148065320) dijistra algo(148065320) dijistra algo
(148065320) dijistra algo
 
Running your database in the cloud presentation
Running your database in the cloud presentationRunning your database in the cloud presentation
Running your database in the cloud presentation
 
Javanotes5 linked
Javanotes5 linkedJavanotes5 linked
Javanotes5 linked
 
How to use sq lite with java using net beans
How to use sq lite with java using net beansHow to use sq lite with java using net beans
How to use sq lite with java using net beans
 
Cloudcomputing basics
Cloudcomputing basicsCloudcomputing basics
Cloudcomputing basics
 
Unit ii-ppt
Unit ii-pptUnit ii-ppt
Unit ii-ppt
 
Running your database in the cloud presentation
Running your database in the cloud presentationRunning your database in the cloud presentation
Running your database in the cloud presentation
 
Filtering content bbased crs
Filtering content bbased crsFiltering content bbased crs
Filtering content bbased crs
 
Apidays efficient-131204080056-phpapp02
Apidays efficient-131204080056-phpapp02Apidays efficient-131204080056-phpapp02
Apidays efficient-131204080056-phpapp02
 

Ähnlich wie Knightstour

9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13Terry Yoast
 
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021Peng Cheng
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - RecursionAdan Hubahib
 
CS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and CullingCS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and CullingMark Kilgard
 
Parallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel applicationParallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel applicationGeoffrey Fox
 
lecture4-recursion.pptx
lecture4-recursion.pptxlecture4-recursion.pptx
lecture4-recursion.pptxLizhen Shi
 
lecture01_lecture01_lecture0001_ceva.pdf
lecture01_lecture01_lecture0001_ceva.pdflecture01_lecture01_lecture0001_ceva.pdf
lecture01_lecture01_lecture0001_ceva.pdfAnaNeacsu5
 
Methods of Manifold Learning for Dimension Reduction of Large Data Sets
Methods of Manifold Learning for Dimension Reduction of Large Data SetsMethods of Manifold Learning for Dimension Reduction of Large Data Sets
Methods of Manifold Learning for Dimension Reduction of Large Data SetsRyan B Harvey, CSDP, CSM
 
Model-Based Decision Making: On Supporting Deliberation with Metaheuristics
Model-Based Decision Making: On Supporting Deliberation with MetaheuristicsModel-Based Decision Making: On Supporting Deliberation with Metaheuristics
Model-Based Decision Making: On Supporting Deliberation with MetaheuristicsStevenOKimbrough
 
Linear Discrimination Centering on Support Vector Machines
Linear Discrimination Centering on Support Vector MachinesLinear Discrimination Centering on Support Vector Machines
Linear Discrimination Centering on Support Vector Machinesbutest
 
Pointcuts and Analysis
Pointcuts and AnalysisPointcuts and Analysis
Pointcuts and AnalysisWiwat Ruengmee
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardAnimesh Chaturvedi
 
Ads unit 3 ppt
Ads unit 3 pptAds unit 3 ppt
Ads unit 3 pptpraveena p
 

Ähnlich wie Knightstour (20)

Ppt chapter12
Ppt chapter12Ppt chapter12
Ppt chapter12
 
Cis068 08
Cis068 08Cis068 08
Cis068 08
 
N Queens problem
N Queens problemN Queens problem
N Queens problem
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13
 
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - Recursion
 
CS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and CullingCS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and Culling
 
Parallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel applicationParallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel application
 
lecture4-recursion.pptx
lecture4-recursion.pptxlecture4-recursion.pptx
lecture4-recursion.pptx
 
ASE02.ppt
ASE02.pptASE02.ppt
ASE02.ppt
 
ANSSummer2015
ANSSummer2015ANSSummer2015
ANSSummer2015
 
lecture01_lecture01_lecture0001_ceva.pdf
lecture01_lecture01_lecture0001_ceva.pdflecture01_lecture01_lecture0001_ceva.pdf
lecture01_lecture01_lecture0001_ceva.pdf
 
modeling.ppt
modeling.pptmodeling.ppt
modeling.ppt
 
Methods of Manifold Learning for Dimension Reduction of Large Data Sets
Methods of Manifold Learning for Dimension Reduction of Large Data SetsMethods of Manifold Learning for Dimension Reduction of Large Data Sets
Methods of Manifold Learning for Dimension Reduction of Large Data Sets
 
Model-Based Decision Making: On Supporting Deliberation with Metaheuristics
Model-Based Decision Making: On Supporting Deliberation with MetaheuristicsModel-Based Decision Making: On Supporting Deliberation with Metaheuristics
Model-Based Decision Making: On Supporting Deliberation with Metaheuristics
 
Linear Discrimination Centering on Support Vector Machines
Linear Discrimination Centering on Support Vector MachinesLinear Discrimination Centering on Support Vector Machines
Linear Discrimination Centering on Support Vector Machines
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Pointcuts and Analysis
Pointcuts and AnalysisPointcuts and Analysis
Pointcuts and Analysis
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 
Ads unit 3 ppt
Ads unit 3 pptAds unit 3 ppt
Ads unit 3 ppt
 

Kürzlich hochgeladen

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 

Kürzlich hochgeladen (20)

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 

Knightstour

  • 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. Knight’s Tour Problem: A Chess Problem Source: http://en.wikipedia.org/wiki/Knight's_tour 2
  • 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. 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. 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. 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. 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. Knight’s Tour Problem How do you solve this? How do you solve this with a program? 8
  • 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. 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. Backtracking Illustrate Knight’s Tour backtracking as tree! Do you see ways to improve the performance of the algorithm? 11
  • 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. 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. 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. 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. 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