Recursion I CSE 120 Spring 2017 Instructor: Teaching Assistants:

Slides:



Advertisements
Similar presentations
New Mexico Computer Science For All
Advertisements

1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
Invitation to Computer Science, Java Version, Second Edition.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Function Recursion to understand recursion you must understand recursion.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Recursion ● Recursion is a computer programming technique that allows programmers to divide problems into smaller problems of the same type. ● You can.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Recursion Powerful Tool
Recursion.
Nested Loops & Arrays CSE 120 Spring 2017
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Creativity of Algorithms & Simple JavaScript Commands
Expressions & Control Flow CSE 120 Spring 2017
Recursion Version 1.0.
to understand recursion you must understand recursion
Algorithms and Problem Solving
Recursion II CSE 120 Spring 2017
Lesson #6 Modular Programming and Functions.
Processing Introduction CSE 120 Spring 2017
Lesson #6 Modular Programming and Functions.
10.3 Details of Recursion.
Developing an App CSE 120 Spring 2017
Functions in Processing CSE 120 Spring 2017
Lightbot and Functions CSE 120 Winter 2017
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Mid-Quarter Review CSE 120 Winter 2018
Functions in Processing CSE 120 Winter 2018
to understand recursion you must understand recursion
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Basic Input and Output CSE 120 Winter 2018
Lesson #6 Modular Programming and Functions.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Processing and Drawing CSE 120 Winter 2018
Applied Algorithms (Lecture 17) Recursion Fall-23
Chapter 12 Recursion (methods calling themselves)
Mid-Quarter Review CSE 120 Spring 2017
More Selections BIS1523 – Lecture 9.
Expressions & Control Flow CSE 120 Winter 2018
Algorithm design and Analysis
Lecture 17 Recursion part 1 Richard Gesick.
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Unit-2 Divide and Conquer
Fundamentals of Programming
Lightbot and Functions CSE 120 Winter 2018
Nested Loops, Arrays CSE 120 Winter 2018
Coding Concepts (Basics)
Recursion Chapter 11.
Basics of Recursion Programming with Recursion
Algorithms and Problem Solving
Lesson #6 Modular Programming and Functions.
Recursion Taken from notes by Dr. Neil Moore
Recursion Chapter 11.
Lecture 12 Recursion part 1 CSE /26/2018.
Divide & Conquer Algorithms
Hour of Code Code.org/lightbot
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Recursion I CSE 120 Spring 2017 Instructor: Teaching Assistants: Justin Hsia Anupam Gupta, Braydon Hall, Eugene Oh, Savanna Yee The Abusive ‘Pranks’ of YouTube Family Vloggers DaddyOFive is a fairly popular YouTube channel – it boasts around 750,000 subscribers. [It] would often put up “typical” family videos, but their most popular videos are loosely executed pranks. What the family calls “pranking,” though, can look an awful lot like abuse. Estimates of DaddyOFive’s income range from $200,000 to $350,000 annually. Though many young YouTube stars essentially work as child entertainers, the conditions of and income from their labor are not regulated. Most rely entirely on the generosity of their parents, who receive automated payments from ad revenue. http://nymag.com/selectall/2017/04/daddyofive-youtube- abuse-controversy-explained.html

Administrivia Assignments: Midterm grades released on Gradescope Controlling Elli due tonight (5/1) Mid-Quarter Survey due Wednesday (5/3) Living Computers Museum Report (5/14) Midterm grades released on Gradescope Average: 21.81/30, Std Dev: 5.31 Regrade requests via Gradescope due Wednesday (5/3) Make sure to submit separate requests for each subproblem Make sure you understand your mistakes

Growth vs. Fixed Mindset Students can be thought of as having either a “growth” mindset or a “fixed” mindset (based on research by Carol Dweck) “In a fixed mindset students believe their basic abilities, their intelligence, their talents, are just fixed traits. They have a certain amount and that's that, and then their goal becomes to look smart all the time and never look dumb.” “In a growth mindset students understand that their talents and abilities can be developed through effort, good teaching and persistence. They don't necessarily think everyone's the same or anyone can be Einstein, but they believe everyone can get smarter if they work at it.”

Bloom’s Two Sigma Problem 1984 Educational Researcher paper http://web.mit.edu/5.95/readings/bloom-two-sigma.pdf Core observation: An “average” student learning in a 1 on 1 format achieves results similar to the top 2% of a lecture based class (two standard deviations above the mean). Very strongly implies that the “fixed” mindset is wrong!

Outline Processing: translate() and rotate() Recursion Solving Problems Using Recursion Variable Scope Revisited

Manipulating the Coordinate Grid We have always used the default coordinate grid: (0,0) is the upper left corner Increasing x-position moves towards the right Increasing y-position moves towards the bottom rect(20,20,40,40);

Manipulating the Coordinate Grid translate(xShift,yShift) moves the origin (0,0) now refers to the pixel (xShift,yShift) on the drawing canvas Can use negative coordinates translate(60,80); rect(20,20,40,40);

Manipulating the Coordinate Grid rotate(angle) rotates the grid clockwise around the origin Increasing x-position now moves at angle from horizontal Increasing y-position now moves at angle from vertical rotate(radians(45)); rect(40,40,40,40);

Peer Instruction Question Which combination of commands, when followed by rect(20,20,20,20) produces the picture shown below? Vote at http://PollEv.com/justinh rotate(radians(45)); translate(width/2,height/2); translate(width/2,height/2); rotate(radians(45)); rotate(radians(-45)); translate(width/2,height/2); translate(width/2,height/2); rotate(radians(-45)); We’re lost…

Outline Processing: translate() and rotate() Recursion Solving Problems Using Recursion Variable Scope Revisited

Recursion Recursion is an algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task A function is recursive if the body of the function calls the function itself We’ve seen this before! Lightbot recursion add(x,y) function from “Beauty in Computer Science” Algorithm for Selection Sort

Building Blocks of Algorithms Sequencing The application/execution of each step of an algorithm in the order given Iteration Repeat part of algorithm a specified number of times Selection Use of conditional to select which instruction to execute next Recursion Algorithm calls itself to help solve the problem on smaller parts fill(255); rectMode(CORNERS); rect(-r, -r, 0, r); ellipse(0, -r/2, r, r); if(mousePressed) { fill(0,0,255); } for(int i=20; i<400; i=i+60) { line(i,40,i+60,80); }

Why Recurse? In its most boring form, recursion is simply an alternative to iteration/looping Anything you can do with iteration, you can do with recursion (an vice versa) However, it is a profoundly powerful way of thinking! Tremendously useful when the problem is self-similar No more powerful than iteration, but often leads to more concise and “better” code

Examples of Recursion

Examples of Recursion

Examples of Recursion https://xkcd.com/244/

Peer Instruction Question What will happen when we try to run the following code in Processing? Vote at: http://PollEv.com/justinh It prints out 3, 2, 1, 0 It runs forever Error occurs before execution Error occurs during execution We’re lost… void draw() { countdown(3); noLoop(); } void countdown(int n) { println(n); countdown(n-1);

Fixing the Countdown Without using loops, how would you modify countdown() to stop at 0? A conditional is needed to stop the infinite loop! void countdown(int n) { println(n); countdown(n-1); } void countdown(int n) { if(n<0) { // do nothing } else { println(n); countdown(n-1); }

Outline Processing: translate() and rotate() Recursion Solving Problems Using Recursion Variable Scope Revisited

Recursive Solutions Base case(s) Recursive case(s) When the problem is simple enough to be solved directly Needed to prevent infinite recursion Recursive case(s) Function calls itself one or more times on “smaller” problems Divide the problem into one or more simpler/smaller parts Invoke the function (recursively) on each part Combine the solutions of the parts into a solution for the problem Depending on the problem, any of these may be trivial or complex

Add Divide: y is reduced by 1 Invoke: call to add(x+1,y-1) Combine: none Base: y==0 (nothing left to add) int add(int x, int y) { if(y==0) { return x; } else { return add(x+1,y-1); }

Add (alternate) Divide: y is reduced by 1 Invoke: call to add(x,y-1) Combine: add 1 to result Base: y==0 (nothing left to add) int add(int x, int y) { if(y==0) { return x; } else { return 1 + add(x,y-1); }

Selection Sort Algorithm: Find smallest number in an array and move that to the front Repeat this entire procedure, but for the rest of the array Divide: array “size” reduced by 1 Invoke: call selection sort on smaller array Combine: smallest number in front of sorted array Base: array of size 1 To selection sort this: 7 3 1 8 4 Move 1 to the front: 1 3 7 8 4 Then selection sort this: 1 3 7 8 4

Drawing a Recursive Tree Divide: draw smaller tree (fewer levels, shorter branches) Invoke: draw tree as “right branch” and “left branch” Combine: draw branch rotated from end of “trunk” Base: “leaf branches” at end

Recursive Drawing (video) “Recursive Drawing is an exploration of user interface ideas towards the development of a spatially-oriented programming environment.” Create drawings without any lines of code! Created by Toby Schachman http://recursivedrawing.com

Summary A recursive function calls itself to solve a problem Always have a base case and recursive case Recursion adds no “power” to programming Anything that can be done with iteration can be done with recursion and vice versa But it makes some things much easier and more elegant Particularly problems with “self-similarity”