ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
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.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
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 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
Recursion Apan Qasem Texas State University Spring 2011.
© 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.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
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.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
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,
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 9 gcc Compiler Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 52 Generic Algorithm Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 53 Generic Algorithms Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 3 Numbers Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
ECE 103 Engineering Programming Chapter 45 Pointers to Functions Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursion.
Chapter 19: Recursion.
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Chapter 15 Recursion.
Programming Fundamentals Lecture #7 Functions
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Chapter 10.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
Stack Frames and Functions
ECE 103 Engineering Programming Chapter 32 Array Parameters
Chapter 11 Recursion.
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 62 Stack Implementation
Yan Shi CS/SE 2630 Lecture Notes
ECE 103 Engineering Programming Chapter 18 Iteration
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Java Software Solutions Foundations of Program Design Sixth Edition
Presentation transcript:

ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE

Syllabus Recursion Process Fibonacci Sequence Quicksort Sierpinski Triangle

2 Recursion Process A recursive function is a function that invokes itself. Recursion is a powerful and elegant method to solve certain types of problems. Recursion decomposes a problem into smaller subproblems of exactly the same form as the original problem.

3 Towers of Hanoi Koch Snowflake

4 Function call mechanics: When a function is called, these items are saved (pushed) onto a call stack:  Return address  Function parameters  Local data When the function is done:  The stored items are removed (popped) from the call stack.  Control returns back to the code following the original call.

5 Recursive function call: Pending function data are placed on the call stack each time the function invokes itself. A base case determines when recursion stops.  It is a conditional test that halts recursion when: The problem cannot be decomposed any further. A predefined recursion depth is reached. Once the base case is reached, the recursion begins backtracking to return pending values.

6 #include /* This function adds up the numbers from 1 to n using ITERATION */ int sum (int n) { int psum = 0; /* Partial sum accumulator */ int k; for (k = 1; k <= n; k++) /* Iteration */ psum = psum + k; return psum; } int main (void) { printf("\nFinal sum(5) = %d\n", sum(5)); return 0; } ACTUAL OUTPUT: sum(5) = 15

7 #include /* This function adds up the numbers from 1 to n using RECURSION */ int sum (int n) { printf("sum(%d) = ", n); if (n <= 1) /* Base case */ { printf("1\n"); return 1; } else /* Recursion */ { printf("%d + sum(%d)\n", n, n-1); return n + sum(n - 1); } int main (void) { printf("\nFinal sum(5) = %d\n", sum(5)); return 0; } ACTUAL OUTPUT: sum(5) = 5 + sum(4) sum(4) = 4 + sum(3) sum(3) = 3 + sum(2) sum(2) = 2 + sum(1) sum(1) = 1 → sum(5) = 15

8 Potential Problems: Deep recursion may require excessive memory storage. Recursion can be inefficient due to excessive recomputation. If the base case is never satisfied, the recursion is infinite (until all memory becomes exhausted). Recursion may not converge if the subproblems do not become smaller.

9 Fibonacci Sequence F n = { 0 for n=0, 1 for n=1, and F n-1 + F n-2 for n>=2 } → 0, 1, 1, 2, 3, 5, 8, 13, 21, … /* This is the iterative (non-recursive) version */ long int fib (int n) { long int previous = -1; long int result = 1; long int sum; int i; for (i = 0; i <= n; ++i) { sum = result + previous; previous = result; result = sum; printf("i=%d sum=%ld previous=%ld result=%ld\n", i, sum, previous, result); } return result; } ACTUAL OUTPUT FOR fib(5): i=0 sum=0 previous=1 result=0 i=1 sum=1 previous=0 result=1 i=2 sum=1 previous=1 result=1 i=3 sum=2 previous=1 result=2 i=4 sum=3 previous=2 result=3 i=5 sum=5 previous=3 result=5

10 /* This is the recursive version */ long int fib (int n) { if ( n == 0 || n == 1 ) /* base case */ return n; else /* recursion */ return fib(n-1) + fib(n-2); } Order of calls for fib(5): fib(5)=fib(4) + fib(3)  fib(4)=fib(3) + fib(2)  fib(3)=fib(2) + fib(1)  fib(2)=fib(1) + fib(0)  fib(1)= 1 fib(0) = 0 fib(1)= 1 fib(2)=fib(1) + fib(0)  fib(1)= 1 fib(0) = 0 fib(3)=fib(2) + fib(1)  fib(2)=fib(1) + fib(0)  fib(1)= 1 fib(0) = 0 Tail-recursive functions can always be rewritten as iterative functions. (Tail-recursive means the recursion occurs in the last statement.)

11 Quicksort Quicksort is a sorting algorithm developed by Tony Hoare in On average, it makes O(n log n) comparisons to sort n items. Quicksort partitions data into two smaller sub-lists and then recursively sorts each sub-list.

12 From: Wikipedia article Quicksort (in-place version) Partition Function // left is the index of the leftmost element of the subarray // right is the index of the rightmost element of the subarray (inclusive) // number of elements in subarray = right-left+1 function partition (array, left, right, pivotIndex) pivotValue := array[pivotIndex] swap array[pivotIndex] and array[right]// Move pivot to end storeIndex := left for i from left to right – 1 // left ≤ i < right if array[i] <= pivotValue swap array[i] and array[storeIndex] storeIndex := storeIndex + 1 swap array[storeIndex] and array[right] // Move pivot to its final place return storeIndex

13 Sorting Function function quicksort(array, left, right) // If the list has 2 or more items if left < right choose any pivotIndex such that left ≤ pivotIndex ≤ right // Get lists of bigger and smaller items and final position of pivot pivotNewIndex := partition(array, left, right, pivotIndex) // Recursively sort elements smaller than the pivot quicksort(array, left, pivotNewIndex - 1) // Recursively sort elements at least as big as the pivot quicksort(array, pivotNewIndex + 1, right)

14 Sierpinski Triangle From: Each triangle is recursively divided into four sub-triangles: one at each corner one in the center

15 Graphics Support: Using a graphics library, we write a function to draw a single triangle: void draw_triangle (float x1, float y1, float x2, float y2, float x3, float y3) { /* Replace these with commands of actual graphics library */ draw_line( x1,y1, x2,y2 ); draw_line( x2,y2, x3,y3 ); draw_line( x3,y3, x1,y1 ); } (x 1,y 1 ) (x 2,y 2 ) (x 3,y 3 )

16 Step 1 of recursion function: Define the drawing step. void fractal_triangle (float x1, float y1, float x2, float y2, float x3, float y3) { /* Base case will go here */ /* Drawing step */ draw_triangle( x1,y1, x2,y2, x3,y3 ); /* Recursive step will go here */ } (x 1,y 1 ) (x 2,y 2 ) (x 3,y 3 )

17 Step 2 of recursion function: Define the recursion step. void fractal_triangle (float x1, float y1, float x2, float y2, float x3, float y3) { /* Base case will go here */ /* Drawing step */ draw_triangle( x1,y1, x2,y2, x3,y3 ); /* Recursive step */ fractal_triangle( x1,y1, (x1+x2)/2,(y1+y2)/2, (x1+x3)/2,(y1+y3)/2 ); fractal_triangle( (x1+x2)/2,(y1+y2)/2, x2,y2, (x2+x3)/2,(y2+y3)/2 ); fractal_triangle( (x1+x3)/2,(y1+y3)/2, (x2+x3)/2,(y2+y3)/2, x3,y3 ); } (x 1,y 1 ) (x 2,y 2 ) (x 3,y 3 )

18 Step 3 of recursion function: Define the base case. void fractal_triangle (float x1, float y1, float x2, float y2, float x3, float y3) { /* Base case step */ if ( fabs(x2 – x1) < LIMIT ) /* Stop when side length gets too short */ return; /* Drawing step */ draw_triangle( x1,y1, x2,y2, x3,y3 ); /* Recursive step */ fractal_triangle( x1,y1, (x1+x2)/2,(y1+y2)/2, (x1+x3)/2,(y1+y3)/2 ); fractal_triangle( (x1+x2)/2,(y1+y2)/2, x2,y2, (x2+x3)/2,(y2+y3)/2 ); fractal_triangle( (x1+x3)/2,(y1+y3)/2, (x2+x3)/2,(y2+y3)/2, x3,y3 ); } (x 1,y 1 ) (x 2,y 2 ) (x 3,y 3 )

20 Julius Tree