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

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 

Kürzlich hochgeladen (20)

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 

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