CPS 001 8.1 Today’s topics Programming Recursion Invariants Reading Great Ideas, p. 180-186 Brookshear, Section 5.5 6.3 Upcoming Copyrights, patents, and.

Slides:



Advertisements
Similar presentations
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Advertisements

Kavita Math231 Recursion and Iteration. Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Algorithm Design Techniques: Induction Chapter 5 (Except Sections 5.6 and 5.7)
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursion Gordon College CPS212
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.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
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.
Recursion Chapter 5.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
CPS Today’s topics Programming Recursion Invariants Digital Intellectual Property Issues Reading Brookshear, Chapter 6 Chapter 1 of Jessica Litman,
CompSci 100E 10.1 Solving Problems Recursively  Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
CPS 100, Spring From Recursion to Self Reference public int calc(int n){ return n*calc(n-1); } l What is the Internet?  A network of networks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 8 Recursion Modified.
CompSci 100E 12.1 Solving Problems Recursively  Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
CompSci 100e 6.1 Hashing: Log ( ) is a big number l Comparison based searches are too slow for lots of data  How many comparisons needed for a.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Compsci 100, Fall From Recursion to Self Reference public int calc(int n){ return n*calc(n-1); } l What is the Internet?  A network of networks.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
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.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
A Computer Science Tapestry 10.1 Solving Problems Recursively l Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems.
CompSci Review of Recursion with Big-Oh  Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
CPS Today’s topics Programming Recursion Copyrights, patents, and digital media Reading Great Ideas, p Brookshear, Section Online.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Recursion Fundamentals of Recursion l Base case (aka exit case)  Simple case that can be solved with no further computation  Does not make a recursive.
CPS Solving Problems Recursively l Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved simply.
A Computer Science Tapestry 10.1 Solving Problems Recursively l Recursion is an indispensable tool in a programmer’s toolkit ä Allows many complex problems.
CPS Today’s topics Digital Intellectual Property Issues Programming Recursion Invariants Reading Intellectual Property Readings Intellectual property.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Plan for the Week Review for Midterm Source code provided
Solving Problems Recursively
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
Java Software Structures: John Lewis & Joseph Chase
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 12 Recursion (methods calling themselves)
Chapter 8: Recursion Java Software Solutions
Announcements Final Exam on August 19th Saturday at 16:00.
Announcements Final Exam on December 25th Monday at 16:00.
Chapter 11 Recursion.
Chapter 8: Recursion Java Software Solutions
Announcements HW3 grades will be announced this week
Dr. Sampath Jayarathna Cal Poly Pomona
Solving Problems Recursively
Java Software Solutions Foundations of Program Design Sixth Edition
Recursive example 1 double power(double x, int n) // post: returns x^n
Recursive Thinking.
Solving Problems Recursively
Presentation transcript:

CPS Today’s topics Programming Recursion Invariants Reading Great Ideas, p Brookshear, Section Upcoming Copyrights, patents, and digital media Security

CPS Solving Problems Recursively l Recursion is an indispensable tool in a programmer’s toolkit ä Allows many complex problems to be solved simply ä Elegance and understanding in code often leads to better programs: easier to modify, extend, verify ä Sometimes recursion isn’t appropriate, when it’s bad it can be very bad---every tool requires knowledge and experience in how to use it l The basic idea is to get help solving a problem from coworkers (clones) who work and act like you do ä Ask clone to solve a simpler but similar problem ä Use clone’s result to put together your answer l Need both concepts: call on the clone and use the result

CPS Fundamentals of Recursion l Base case (aka exit case) ä Simple case that can be solved with no further computation ä Does not make a recursive call l Reduction step (aka Inductive hypothesis) ä Reduce the problem to another smaller one of the same structure ä Make a recursive call, with some parameter or other measure that decreases or moves towards the base case Ensure that sequence of calls eventually reaches the base case “Measure” can be tricky, but usually it’s straightforward l The Leap of Faith ! ä If it works for the reduction step is correct and there is proper handling of the base case, the recursion is correct. l What row are you in?

CPS Classic examples of recursion l For some reason, computer science uses these examples: ä Factorial: we can use a loop or recursion, is this an issue? ä Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21, … F(n) = F(n-1) + F(n-2), why isn’t this enough? What’s needed? Classic example of bad recursion, to compute F(6), the sixth Fibonacci number, we must compute F(5) and F(4). What do we do to compute F(5)? Why is this a problem? ä Towers of Hanoi N disks on one of three pegs, transfer all disks to another peg, never put a disk on a smaller one, only on larger Every solution takes “forever” when N, number of disks, is large ä Reversing strings Append first character after the rest is reversed

CPS Exponentiation l Computing x n means multiplying n numbers (or does it?) ä What’s the easiest value of n to compute x n ? ä If you want to multiply only once, what can you ask a clone? double Power(double x, int n) // post: returns x^n { if (n == 0) { return 1.0; } return x * Power(x, n-1); } l What about an iterative version?

CPS Faster exponentiation l How many recursive calls are made to compute ? ä How many multiplies on each call? Is this better? double Power(double x, int n) // post: returns x^n { if (n == 0) { return 1.0; } double semi = Power(x, n/2); if (n % 2 == 0) { return semi*semi; } return x * semi * semi; } l What about an iterative version of this function?

CPS Loop Invariants l Want to reason about the correctness of a proposed iterative solution l Loop invariants provide a means to effectively about the correctness of code while !done do { // what is true at every step // Update/iterate // maintain invariant }

CPS Bean Can game l Can contains N black beans and M white beans initially l Emptied according the following repeated process ä Select two beans from the can ä If the beans are: same color: put a black bean back in the can different colors : put a white bean back in the can ä Player who chooses the color of the remaining bean wins the game l Analyze the link between the initial state and the final state l Identify a property that is preserved as beans are removed from the can ä Invariant that characterizes the removal process

CPS Recursive example 1 double power(double x, int n) // post: returns x^n { if (n == 0) { return 1.0; } return x * power(x, n-1); } x: n: Return value:

CPS Recursive example 2 double fasterPower(double x, int n) // post: returns x^n { if (n == 0) { return 1.0; } double semi = fasterPower(x, n/2); if (n % 2 == 0) { return semi*semi; } return x * semi * semi; } x: n: Return value:

CPS Recursive example 3 String mystery(int n) { if (n < 2) { return "" + n; } else { return mystery(n / 2) + (n % 2); } n: Return value: