CS201: Data Structures and Discrete Mathematics I

Slides:



Advertisements
Similar presentations
Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Advertisements

Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
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.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
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.
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.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
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.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Recursion Chapter 11. How it works “A recursive computation solves a problem by using the solution of the same problem with simpler inputs” Big Java,
Pei Zheng, Michigan State University 1 Chapter 8 Recursion.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Recursion.
CS212: Data Structures and Algorithms
Recursion.
Introduction to Algorithms: Divide-n-Conquer Algorithms
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 19: Recursion.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Data Structures and Algorithms
Decrease-and-Conquer Approach
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Recursion Chapter 10.
Lecture Outline for Recurrences
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion: The Mirrors
Data Structures & Algorithms
Announcements Final Exam on August 19th Saturday at 16:00.
7.Recursion Recursion is the name given for expression anything in terms of itself. Recursive function is a function which calls itself until a particular.
Module 1-10: Recursion.
Basics of Recursion Programming with Recursion
Java Programming: Chapter 9: Recursion Second Edition
Recursion: The Mirrors
CSC 143 Recursion.
Last Class We Covered Recursion Stacks Parts of a recursive function:
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
ITEC324 Principle of CS III
Recursion: The Mirrors
Presentation transcript:

CS201: Data Structures and Discrete Mathematics I Recursion

Recursive Definitions Recursive definition (or inductive definition): A definition in which the item being defined appears as part of the definition. Contain two parts: A base, where some simple cases of the item being defined are given An recursive step, where new cases of the item being defined are given in terms of previous cases. 12/2/2018 CS201

Examples Fibonacci numbers F(1) = 1, F(2) = 1 F(n) = F(n-2) + F(n-1) for n > 2. (1, 1, 2, 3, 5, 8, 13, 21…) Recurrence relation: A rule like F(n), which define a sequence value in terms of one or more earlier values. Define n! recursively 1! = 1 n! = n(n-1)! for n > 1 12/2/2018 CS201

Recursively defined sequences A sequence S represents a list of objects that are enumerated in some order. E.g, 1. S(1) = 2 2. S(n) = 2S(n-1) for n ≥ 2 2, 4, 8, 16, 32, … Another sequence T T(1) = 1 T(n) = T(n-1) + 3 for n ≥ 2 12/2/2018 CS201

Recursively defined sets Define a set of people who are ancestors of James: 1. James parents are ancestors of James. 2. Every parent of an ancestor is an ancestor of James An identifier in a programming language can be alphanumeric strings of any length but must begin with a letter. 1. A single letter is an identifier. 2. If B is an identifier, so is the concatenation of B and any letter or digit. 12/2/2018 CS201

Recursively defined operations A recursive definition of multiplication of two positive integers m and n is 1. m(1) = m 2. m(n) = m(n-1) + m for n ≥ 2 Let x be a string. Define the operation xn (concatenation of x with itself n times) for n ≥ 1 1. x1 = x 2. xn = xn-1x for n ≥ 1 12/2/2018 CS201

Recursive Programming: Recursively defined algorithms Recursively computes the value of S(n) S(integer n) If n = 1 then return 2 else return 2*S(n-1) endif end 12/2/2018 CS201

Recursion programming - Basic Idea When writing recursive programs, we need Base cases: we must always have some base cases, which can be solved without recursion. Making progress: For the cases that are to be solved recursively, the recursive call must always make progress toward a base case. 12/2/2018 CS201

Iteration versus Recursion Most of the time, we can express a problem more elegantly using recursion e.g. summation of numbers from 1 to n sum(n)= n+(n-1)+(n-2)+...+2+1 =  sum(n) for (i=1,sum=0;i<=n;i++) sum=sum+i; return sum; 12/2/2018 CS201

In Recursion Summation of numbers from 1 to n using recursion. sum(n) = n+(n-1)+(n-2)+...+2+1 = 1 if (n==1) n+sum(n-1) if (n>1)  sum(n) if (n==1) return 1; else return n+sum(n-1); 12/2/2018 CS201

Another Example of Recursion Product of numbers from 1 to n using recursion fact(n) = n*(n-1)*(n-2)*...*2*1 = 1 if (n==1) n*fact(n-1) if (n>1) -> fact(n) if (n==1) return 1; else return n*fact(n-1); 12/2/2018 CS201

Visualizing Recursive Execution With nonrecursive programs, it is natural to visualize execution by imagining control stepping through the source code This can be confusing for programs containing recursion Instead, useful to imagine each call of a function generating a copy of the function, so that if the same function is called several times, several copies are present. 12/2/2018 CS201

Scope When a function is called (stack takes role for the process) caller is suspended “state” of caller saved new space allocated for variables of new function … end of new function release the space allocated return to the point next to the caller with the previous “state” recovered With recursive call, same things happen 12/2/2018 CS201

How Recursion Works Given fact(n) = if (n==1) return 1; else return n*fact(n-1); 120 fact(5) fact(4) 5* 24 fact(3) 4* 6 fact(2) 3* 2 fact(1) 2* 1 1 12/2/2018 CS201

Computing xn This is a simple program. float power (float x, int n) { if (!n) /* if (n==0) */ return 1 else return x * power(x, n-1) } 12/2/2018 CS201

What does this program do? This program is not easy to understand. void f () { int ch; if ((ch = getchar()) != ‘\n’) { f(); printchar(ch); } Given the input string “Is it going to work?” 12/2/2018 CS201

Recursion - how to Ask the following How can you solve the problem using the solution of a “simpler” instance of the problem? Can you be sure to have a “simplest” input? (If so, include separate treatment of this case.) Can you be sure to reach the “simplest” input? 12/2/2018 CS201

Another Example: Merge Sort We now use another complex example to show the working of a recursive program. Sorting is the process of rearranging data in either ascending or descending order. (2, 4, 1, 6, 5, 9, 2) => (1, 2, 2, 4, 5, 6, 9) We need sorting because The data in sorted order is required It is the initialization step of many algorithms. 12/2/2018 CS201

Merge Sort: one sorting algorithm A nice example of a recursive algorithm. It is a divide-and-conquer algorithm Divide-and-conquer is an important technique in Computer Science. It solves problem in three steps: Divide Step: divide the large problem into two or more smaller problems. Recursively solve the smaller problems Conquer Step: based on the results of the smaller problems, produce the result of the large problem. 12/2/2018 CS201

Merge Sort Idea Divide Step: Divide the array into two equal halves Recursively sort the two halves Conquer Step: Merge the two halves to form a sorted array 12/2/2018 CS201

An example 7 2 6 3 8 4 5 Divide into two equal halves 7 2 6 3 8 4 5 Recursively sort the halves 2 3 6 7 4 5 8 Merge them 2 3 4 5 6 7 8 12/2/2018 CS201

Merge Sort Algorithm MergeSort(A[i..j]) if (i < j) { mid = (i+j)/2 MergeSort(A[i..mid]); MergeSort(A[mid+1..j]); Merge(A[i..mid], A[mid+1..j]); } 12/2/2018 CS201

Merge Sort of an array of six integers 12/2/2018 CS201

How to merge two subarrays? 2 4 5 3 7 8 2 2 4 5 3 7 8 2 3 2 4 5 3 7 8 2 3 4 2 4 5 3 7 8 2 3 4 5 2 4 5 3 7 8 2 3 4 5 7 8 2 4 5 3 7 8 12/2/2018 CS201

Merge Algorithm Algorithm Merge(A[i..mid], A[mid+1..j]) While both subarrays are not empty, Between the first entries of both subarrays, copy the smaller item into the first available entry in the temporary array T[]. When one subarray is empty, finish off the nonempty subarray Copy the result in T[] back to A[i..j] 12/2/2018 CS201

Tower of Hanoi initial state A C B A B C final state 12/2/2018 CS201

Tower of Hanoi void tower (int cnt, char A,char B,char C) { if (cnt==1) move(A,B); else { tower(cnt-1,A,C,B); tower(cnt-1,C,B,A); }; } 12/2/2018 CS201

Fibonacci Numbers again First two are 0, and 1, rest are obtained by adding the previous two. Naïve method, using recursion: int fib(int n) { if (n < 2) return n; /* else */ return fib(n-1)+fib(n-2); } 12/2/2018 CS201

Tracing Fibonacci Calls Too many duplicate calls!!! fib(6) fib(5) fib(4) fib(3) fib(2) fib(4) fib(3) fib(3) fib(2) fib(2) fib(1) fib(2) fib(1) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) fib(1) fib(0) fib(1) fib(0) fib(1) fib(0) 12/2/2018 CS201

fib(int n) is extremely inefficient Number of additions Number of calls 6 12 25 10 88 177 15 986 1973 20 10945 21891 121392 242785 30 1346268 2692537 12/2/2018 CS201

Much better to write an iterative function int fib(int n) { int fibn=0, fibn1=0, fibn2=1; if (n < 2) return n else for( int i = 2; i <= n; i++ ) { fibn = fibn1 + fibn2; fibn1 = fibn2; fibn2 = fibn; } return fibn; 12/2/2018 CS201

Recursion or Iteration Every recursive procedure can be converted into an iterative version (sometime not a trivial task) No general rules prescribing when to use recursion and when not to. Recursion code is usually easily readable, simpler and clearer. The main problem with recursion is the hidden bookkeeping cost. Recursion is usually less efficient than its iterative equivalent. 12/2/2018 CS201

Summary Inductive proof is perhaps the most commonly used proof technique in CS. Base case Inductive case. Recursion definition A base A recursive step An recursive program is often simpler and clearer, but can be less efficient than its iterative version. 12/2/2018 CS201