CompSci 001 14.1 Today’s topics Java Recursion Upcoming Graphics Reading Great Ideas, Chapter 4 (begin Chapter 5 for graphics)

Slides:



Advertisements
Similar presentations
CompSci Today’s topics Java Recursion in Graphics Writing a Class Simple Animation Upcoming Simulation Reading Great Ideas, Chapters 5.
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Computer Science II Recursion Professor: Evan Korth New York University.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Csci1300 Introduction to Programming Recursion Dan Feng.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
CompSci Today’s topics Java The Database Program Upcoming Recursion Reading Great Ideas, Chapter 4.
CPS Today’s topics Java Language Inheritance Upcoming Electric Circuits (not in text) Reading Great Ideas, Chapters 5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
CPS Today’s topics Java Information Retrieval Upcoming Database Programs Reading Great Ideas, Chapter 4.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
1Recursion. 2 Outline thinking recursively recursive algorithms iteration vs. recursion recursive functions integer exponentiation (pow) infinite recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
CompSci Today’s topics Java Review Just a bunch of headings meant to trigger questions A contraction of previous notes Upcoming Midterm Exam Reading.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
CompSci Today’s topics Java Applications Simulation Upcoming Software Engineering (Chapter 7) Reading Great Ideas, Chapters 6.
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
SNU OOPSLA Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes.
CompSci Today’s topics Java Numbers Iteration Upcoming More Java Reading Great Ideas, Chapter 3.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
CompSci Today’s topics Java Writing Functions/Methods Upcoming Information Retrieval Reading Great Ideas, Chapter 4.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Today’s topics Java Input More Syntax Upcoming Decision Trees More formal treatment of grammers Reading Great Ideas, Chapter 2.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Today’s topics Java Looping Upcoming Arrays in Java Reading Great Ideas, Chapter 3.
CompSci Today’s Topics Computer Science Noncomputability Upcoming Special Topic: Enabled by Computer -- Decoding the Human Genome Reading Great.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Recursion.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Today’s topics Java Information Retrieval Upcoming Database Programs
Introduction to Recursion
Abdulmotaleb El Saddik University of Ottawa
RECURSION.
Decrease-and-Conquer Approach
Today’s topics Java Arrays Upcoming Functions Reading
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.
Unit 3 Test: Friday.
Algorithms An algorithm is a set of instructions used to solve a specific problem In order to be useful, an algorithm must have the following properties:
Presentation transcript:

CompSci Today’s topics Java Recursion Upcoming Graphics Reading Great Ideas, Chapter 4 (begin Chapter 5 for graphics)

CompSci Recursion  Actually have been using it in everyday life  Dictionary Example  Need dictionary to use a dictionary o Look up word o Definition may use words we don’t understand o Look up those words o Etc.  Use Clone Model to sort this out  Like using multiple dictionaries  Recursion implies a self-referential process  In computing we say a function invokes itself

CompSci Recursion  Recursion is a Divide and Conquer strategy  Decomposing problem 1.Base Case ( halting case) 2.Recursive case (which must get us closer to solution )  If we don’t have base case, infinite recursion o Very much like an infinite loop o (very bad!)  Factorial Example  Definition of N Factorial ( domain is non-negative integers ) o N! = N * (N-1)! o 0! = 1  Note we defined factorial (or !) in terms of !  Which part corresponds to the base case?  In class demo…

CompSci Factorial Program public class RecFact extends java.applet.Applet implements ActionListener { TextField mInstruct, mResults; IntField gN; Button bFact; public void init() { mInstruct = new TextField(70); mInstruct.setText( "Enter N, then press button for factorial"); gN = new IntField(10); gN.setLabel("N"); bFact = new Button("Factorial"); mResults = new TextField(70); add(mInstruct); add(gN); add(bFact); add(mResults); bFact.addActionListener(this); }

CompSci Factorial Program.2 public int fact(int n) { if (n == 0) { return 1; } return n * fact(n - 1); } public void actionPerformed(ActionEvent event) { int k; k = gN.getInt(); mResults.setText(k + " factorial = " + fact(k)); }

CompSci Using the Clone Model for f = fact(5) public int fact(int n){ if (n==0) return 1; return n * fact(n-1); } public int fact(int n){ if (n==0) return 1; return n * fact(n-1); } public int fact(int n){ if (n==0) return 1; return n * fact(n-1); } public int fact(int n){ if (n==0) return 1; return n * fact(n-1); } public int fact(int n){ if (n==0) return 1; return n * fact(n-1); } public int fact(int n){ if (n==0) return 1; return n * fact(n-1); }

CompSci Recursive vs Iterative  Notice that recursive solution required No Loop  Repetition is implicit in the process  Could have used iterative (looping) approach: public int fact(int n) { int prod = 1; while(n > 0) { prod = prod * n; n = n - 1; } return prod; }  Is actually simpler for this problem  For some problems, recursion is much easier (when comfortable with it)  Watch the SIZE OF THE NUMBERS ! ! ! !

CompSci Exponentiation  Want to calculate X to the N th power  Also written as X N  Brute force approach  X N = X*X*X* … X*X  How many multiplications?  Can we do better?  How would you calculate 7 64 with simple 4-function calculator?  Might calculate 49=7*7. Then can use  How many multiplications now?  Carry on with this idea: 2401 = 49*49. o Leaves us with

CompSci Exponentiation Recursively  Want to calculate X to the N th power recursively  Base case: N = 0 X 0 = 1.0  Recursive case: N is an even number X N = X N/2 * X N/2  Recursive case: N is an odd number X N = X * X N/2 * X N/2  Ready to put this into code

CompSci Recursive Expon public class Recurse extends java.applet.Applet implements ActionListener { IntField gN; DoubleField gX; Label lN, lX; Button bFact, bExp; TextField mResults; int k, n; double x; public void init() { lN = new Label("N"); lX = new Label("X"); mInstruct = new TextField(60); mInstruct.setText( "Enter N and X, then press button for function");

CompSci Recursive Expon.2 gN = new IntField(10); gX = new DoubleField(10); bFact = new Button("Factorial"); bExp = new Button("Exponential"); mResults = new TextField(60); bFact.addActionListener(this); bExp.addActionListener(this); add(mInstruct); add(lN); add(gN); add(lX); add(gX); add(bFact); add(bExp); add(mResults); } public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == bFact) { n = gN.getInt(); x = gX.getDouble(); mResults.setText(n + " factorial = " + fact(n)); }

CompSci Recursive Expon.3 if (cause == bExp) { n = gN.getInt(); x = gX.getDouble(); mResults.setText( x + " to the " + n + " power = " + expon(x, n)); } int fact(int n) { if (n<=1) { return 1; } return n * fact(n-1); }

CompSci Recursive Expon.4 double expon(double x, int n) { double xnot; if (n == 0) { return 1.0; } xnot = expon(x, n/2); if ( n == 2*(n/2)) { // or if (n%2 == 0) i.e., is it even? return xnot * xnot; } else { return x * xnot * xnot; }

CompSci Other uses of Recursion  Recursion sometime associated with self-similar structure  Fractals are a graphic instantiation of similar ideas  Will look at this later  Processing folders (directories)  Each folder may contain files or other folders  Folder containing folders is self-referential  Processing tree-like data structures  Important in computer science  (think of your family tree)  Many other applications  Recursion can be expensive  Each invocation of a method (function) incurs overhead  Use iteration when this is obvious solution (e.g. N!)  For many complicated problems, recursive solution is easier!

CompSci Church-Markov-Turing Thesis Any non-trivial computer language that one can invent is apparently capable of computing no more and no fewer functions than all other nontrivial programming languages. This part of Java lets you solve all kinds of problems and implement all computer algorithms. See how far we have gotten!