Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Slides:



Advertisements
Similar presentations
© 2006 Pearson Education Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William.
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
Chapter 8: Recursion Java Software Solutions
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
CISH 4960 Introduction to Programming Lecture 101 Introduction to Programming Lecture 10 Recursion/Data Structures Tom Blough
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
Chapter Day 25. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 25 Problem set 5 Posted (Last one)  Due Dec 8 Friday Capstones Schedule.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Chapter Day 25. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 25 Problem set 5 Posted (Last one)  Due Dec 8 Capstones Schedule  3rd.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
Recursion Chapter 5.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Arrays Arrays are objects that help us organize large amounts of information.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
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,
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
CS212: Data Structures and Algorithms
Recursion.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion -- Introduction
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Abdulmotaleb El Saddik University of Ottawa
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
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
Chapter 8: Recursion Java Software Solutions
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Chapter 11 Recursion.
Chapter 8: Recursion Java Software Solutions
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Java Software Solutions Foundations of Program Design Sixth Edition
Recursion (part 1) October 25, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Administrative Stuff HW8 is out (due this 8pm) HW9 is out (due next 8pm) Midterm 2 results

Compare with Midterm 1

Top Midterm Scores (out of 135) Gupta Shishir ( )129 Gibson Richard ( )127.5 Rehfuss Nathan( )127 Schenck Connor( )127 Studer Sean ( )126 Anderson Michael ( )125.5 Githens Katie ( )124 Staley Nathan( )123.5 Wells Kevin( )121.5 Jiang Miao ( )120 Klinge Titus ( )119.5

Chapter 10 (Recursion)

Recursion (part 2) CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

Examples of Recursion

[

The von Koch Curve and Snowflake [

Divide it into three equal parts [

Replace the inner third of it with an equilateral triangle [

Repeat the first two steps on all lines of the new figure [

[

Quick Review of the Last Lecture (with more in depth examples)

© 2004 Pearson Addison-Wesley. All rights reserved Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems.

© 2004 Pearson Addison-Wesley. All rights reserved Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word, a recursive definition is often not helpful But in other situations, a recursive definition can be an appropriate way to express a concept Before applying recursion to programming, it is best to practice thinking recursively

© 2004 Pearson Addison-Wesley. All rights reserved Circular Definitions Debugger – a tool that is used for debugging

© 2004 Pearson Addison-Wesley. All rights reserved Recursive Definitions Consider the following list of numbers: 24, 88, 40, 37 Such a list can be defined as follows: A LIST is a: number or a: number comma LIST That is, a LIST is defined to be a single number, or a number followed by a comma followed by a LIST The concept of a LIST is used to define itself

© 2004 Pearson Addison-Wesley. All rights reserved Recursive Definitions The recursive part of the LIST definition is used several times, terminating with the non-recursive part: number comma LIST 24, 88, 40, 37 number comma LIST 88, 40, 37 number comma LIST 40, 37 number 37

© 2004 Pearson Addison-Wesley. All rights reserved Infinite Recursion All recursive definitions have to have a non- recursive part If they didn't, there would be no way to terminate the recursive path Such a definition would cause infinite recursion This problem is similar to an infinite loop, but the non-terminating "loop" is part of the definition itself The non-recursive part is often called the base case

© 2004 Pearson Addison-Wesley. All rights reserved Recursive Definitions N!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusive This definition can be expressed recursively as: 1! = 1 N! = N * (N-1)! A factorial is defined in terms of another factorial Eventually, the base case of 1! is reached

© 2004 Pearson Addison-Wesley. All rights reserved Recursive Definitions 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1!

Example: Factorial_Iterative.c int factorial(int a) { int i,result; result=1; for(i=1;i<=a;i++) result=result*i; return result; }

Example: Factorial_Recursive.c int factorial(int a) { if(a==1) return 1; else return a*factorial(a-1); }

Recursive Execution 6! (6 * 5!) (6 * (5 * 4!)) (6 * (5 * (4 * 3!))) (6 * (5 * (4 * (3 * 2!)))) (6 * (5 * (4 * (3 * (2 * 1!))))) (6 * (5 * (4 * (3 * (2 * (1 * 0!)))))) (6 * (5 * (4 * (3 * (2 * (1 * 1)))))) (6 * (5 * (4 * (3 * (2 * 1))))) (6 * (5 * (4 * (3 * 2)))) (6 * (5 * (4 * 6))) (6 * (5 * 24)) (6 * 120) 720

Recursive Programming A function in C can invoke itself; if set up that way, it is called a recursive function The code of a recursive function must be structured to handle both the base case and the recursive case Each call to the function sets up a new execution environment, with new parameters and local variables As with any function call, when the function completes, control returns to the function that invoked it (which may be an earlier invocation of itself)

© 2004 Pearson Addison-Wesley. All rights reserved Recursive Programming Consider the problem of computing the sum of all the numbers between 1 and any positive integer N This problem can be recursively defined as:

Example: Sum_Iterative.c int sum(int a) { int i,result; result=0; for(i=1;i<=a;i++) result=result+i; return result; }

Example: Sum_Recursive.c int sum(int a) { if(a==1) return 1; else return a+sum(a-1); }

© 2004 Pearson Addison-Wesley. All rights reserved Recursive Programming // This function returns the sum of 1 to num int sum (int num) { int result; if (num == 1) result = 1; else result = num + sum (n-1); return result; }

sum(1); sum(1)sum(2) Recursive Control Flow In Recursive calls methods can call themselves, but typically with different arguments each time return 1;

Recursive Control Flow In Recursive calls methods can call themselves, but typically with different arguments each time sum(2) sum(1) sum(1); sum(2); sum(3) main() return 1; sum(3);

© 2004 Pearson Addison-Wesley. All rights reserved Recursive Programming main sum sum(3) sum(1) sum(2) result = 1 result = 3 result = 6

Fibonacci Sequence #include int fib(int n) { int ret; if (n < 2) { ret = 1; } else{ ret= fib(n-1) + fib(n-2); } return ret; } int main() { int i; for(i=0;i<10;i++) printf("%d ", fib(i)); system("pause"); }

Recursion: Fibonacci Numbers The sequence: {0,1,1,2,3,5,8,13,...}

Mathematical notation v.s. C code int fib(int n) { if(n <= 1) return n; //base case else return fib(n-1) + fib(n-2); }

Execution Trace

Mystery Recursion void mystery1(int a, int b) { if (a <= b) { int m = (a + b) / 2; printf(“%d “, m); mystery1(a, m-1); mystery1(m+1, b); } } int main() { mystery1(0, 5); system(“pause”); }

Think of recursion as a tree …

… an upside down tree

(a=0, b=5) (a=0, b=1)(a=3, b=5) (a=0, b=-1)(a=1, b=1)(a=3, b=3)(a=5, b=5) (a=1, b=0)(a=2, b=1)(a=3, b=2)(a=4, b=3)(a=6, b=5)(a=5, b=4)

(a=0, b=5) (a=0, b=1)(a=3, b=5) (a=0, b=-1)(a=1, b=1)(a=3, b=3)(a=5, b=5) (a=1, b=0)(a=2, b=1)(a=3, b=2)(a=4, b=3)(a=6, b=5)(a=5, b=4) 2 m=(a+b)/2 => print 2

(a=0, b=5) (a=0, b=1)(a=3, b=5) (a=0, b=-1)(a=1, b=1)(a=3, b=3)(a=5, b=5) (a=1, b=0)(a=2, b=1)(a=3, b=2)(a=4, b=3)(a=6, b=5)(a=5, b=4)

(a=0, b=5) (a=0, b=1)(a=3, b=5) (a=0, b=-1)(a=1, b=1)(a=3, b=3)(a=5, b=5) (a=1, b=0)(a=2, b=1)(a=3, b=2)(a=4, b=3)(a=6, b=5)(a=5, b=4) Traversal Order

Questions?

Other stuff for the labs this week

Stack Animation animations/tsang/html/STACK/stack1024.html

Memory Organization [

The stack during a recursive call to factorial

The stack during a recursive call to gcd [

Recursive Programming Note that just because we can use recursion to solve a problem, doesn't mean we should For instance, we usually would not use recursion to solve the sum of 1 to N problem, because the iterative version is easier to understand However, for some problems, recursion provides an elegant solution, often cleaner than an iterative version You must carefully decide whether recursion is the correct technique for any problem

Maze Traversal We can use recursion to find a path through a maze From each location, we can search in each direction Recursion keeps track of the path through the maze The base case is an invalid move or reaching the final destination

Traversing a maze

Towers of Hanoi The Towers of Hanoi is a puzzle made up of three vertical pegs and several disks that slide on the pegs The disks are of varying size, initially placed on one peg with the largest disk on the bottom with increasingly smaller ones on top The goal is to move all of the disks from one peg to another under the following rules:  We can move only one disk at a time  We cannot move a larger disk on top of a smaller one

Towers of Hanoi Original ConfigurationMove 1Move 3Move 2

Towers of Hanoi Move 4Move 5Move 6Move 7 (done)

Animation of the Towers of Hanoi WangApr01/RootWang.html

Towers of Hanoi An iterative solution to the Towers of Hanoi is quite complex A recursive solution is much shorter and more elegant

Indirect Recursion A method invoking itself is considered to be direct recursion A method could invoke another method, which invokes another, etc., until eventually the original method is invoked again For example, method m1 could invoke m2, which invokes m3, which in turn invokes m1 again This is called indirect recursion, and requires all the same care as direct recursion It is often more difficult to trace and debug

Indirect Recursion m1m2m3 m1m2m3 m1m2m3

THE END