CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Advertisements

CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
COSC 2006 Data Structures I Recursion III
CS 206 Introduction to Computer Science II 03 / 02 / 2009 Instructor: Michael Eckmann.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
CS 106 Introduction to Computer Science I 04 / 02 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 06 / 2006 Instructor: Michael Eckmann.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 13 / 2008 Instructor: Michael Eckmann.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 29 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 16 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 08 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 09 / 2008 Instructor: Michael Eckmann.
Recursion Recitation – 11/(6,7)/2008 CS 180 Department of Computer Science, Purdue University.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 07 / 2010 Instructor: Michael Eckmann.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 8 Recursion Modified.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 10 / 2008 Instructor: Michael Eckmann.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 15 Recursion.
Chapter 15 Recursion.
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Data Structures.
The Hanoi Tower Problem
Dr. Sampath Jayarathna Cal Poly Pomona
Presentation transcript:

CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2007 Today’s Topics Comments and/or Questions? DecimalFormat class ternary conditional operator recursion

Recursion Understand that the Factorial example, was for instructional purposes only, since it is a very simple example of recursion. For the factorial method, in practice, we would always prefer the non-recursive (iterative) solution. It is more efficient in terms of both time and space. Recursive solutions often take more time to run, because making a method call is slower and requires more memory than doing the same statements without a method call.

Recursion Direct vs. indirect recursion – When a method calls itself, it is direct recursion – When method1 calls method2 and method2 calls method1, it is indirect. – There can be more than just two methods involved in the indirection.

Towers of Hanoi Three pegs Start with different size discs on first peg Move them all to the third peg using the following rules – Move only one disc at a time – Can't place a larger disc on a smaller disc – All discs must be on some peg except for the disc in transit between pegs Let's see an applet on the web.

Towers of Hanoi Do a solution with 3 discs. Do a solution with 4 discs. See any patterns emerging? Is it recursive?

Towers of Hanoi To move N discs from first peg to third – Move topmost N-1 discs from first peg to second – Move the largest disc from the first peg to third – Move the N-1 discs from the second peg to the third

Towers of Hanoi During the solution, each peg takes on a role – Either the start peg, end peg, or intermediate peg. – Notice: In the first and third parts, notice that they are the same as the whole, with the roles of the pegs changing. First part: – Move from: first peg (start) – To: second peg (end) – Using as intermediate: third peg (intermediate) Third part: – Move from: second peg (start) – To: third peg (end) – Using as intermediate: first peg (intermediate)

Towers of Hanoi void moveTower(int numDiscs, int start, int end, int intermed) { if (numDiscs == 1) moveOneDisc(start, end); else { moveTower(numDiscs – 1, start, intermed, end); moveOneDisc(start, end); moveTower(numDiscs – 1, intermed, end, start); } void moveOneDisc(int p1, int p2) { System.out.println(“Move the disc from peg “ + p1 + “ to peg “ + p2); }

Towers of Hanoi Let's put this in a program and run it. Does anyone think that an iterative solution would be possible? If so, do you think it'll be easier? You can think about this on your own if you wish and try to come up with an iterative (that is, non-recursive) solution.

Card/Deck implementation Purpose of these classes will be to create a general Card class and general Deck class that could be used for an arbitrary card game. So, nothing game specific is desired to be in the classes. What data describes a Card? What data describes a Deck? What behaviors does a Card exhibit? What behaviors does a Deck exhibit?

Card/Deck implementation What data describes a Card? – Value and suit (types for these?) What data describes a Deck? – An ordered group of some fixed number of cards What behaviors does a Card exhibit? – None really independent of a Deck What behaviors does a Deck exhibit? – Can shuffle it – Can cut it – Can deal a card from it

Shuffle method Ideas for the shuffle method – try to simulate human shuffling – or just swap 2 cards a bunch of times to get a mixed deck – or other ideas?

Cut method Ideas for the cut method Have another array of 52 Cards to copy into (then later copy this array back into original.) Generate some random number between 0 and 51 and use that as the place to cut the cards. Keeping track of starting and ending indicies is a challenge but its doable if we're careful. Draw copying idea on board. – How many loops and what will each one do?