Lecture 12 Oct 13, 2008 Some simple recursive programs recursive problem solving, connection to induction Some examples involving recursion Announcements.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Tirgul 10 Solving parts of the first two theoretical exercises: –T1 Q.1(a,b) –T1 Q.4(a,e) –T2 Q.2(a) –T2 Q.3(b) –T2 Q.4(a,b)
Lecture 2 Feb 3, 2009 goals: continue recursion more examples of recursive programs.
CSE 373: Data Structures and Algorithms
Lecture 2 Aug goals: Introduction to recursion examples of recursive programs.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs.
Algorithm Design Techniques: Induction Chapter 5 (Except Sections 5.6 and 5.7)
Simple Sorting Algorithms
General Announcements Project Due Friday, 1/30 Labs start Wednesday & Thursday – Java review – Weiss 1.19, 1.20 – You may show up & hand in Workshops.
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
Lecture 4 Feb 5 completion of recursion (inserting into a linked list as last item) analysis of algorithms – Chapter 2.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Complexity Analysis (Part III ) Analysis of Some Sorting Algorithms. Analysis of Recursive Algorithms.
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
Simple Recursion. COMP104 Lecture 35 / Slide 2 Recursion: Example 0 * What does the following program do? #include using namespace std; int fac(int n){
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.
Recursion Chapter 5 Outline Induction Linear recursion –Example 1: Factorials –Example 2: Powers –Example 3: Reversing an array Binary recursion –Example.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
EFFICIENCY & SORTING CITS1001. Listen to the sound of sorting Various algorithms Quicksort
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
MATH 224 – Discrete Mathematics
Introduction to Recursion by Dr. Bun Yue Professor of Computer Science 2013
1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
February 4, 2005 Searching and Sorting Arrays. Searching.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
Objectives - 11  We will work with processing Arrays.  Objectives:  Describe the concept of an array and its benefits.  Define the terms index, traverse,
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Lecture 2 Jan Goals: Introduction to recursion Examples of recursive programs Reading: Chapter 1 of the text.
Basic Mathematics Chapter 1 (1.2 and 1.3) Weiss. Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition.
CS101 Computer Programming I Chapter 4 Extra Examples.
Data Structure Introduction.
Comparison-Based Sorting & Analysis Smt Genap
CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Data Structures and Algorithm Analysis Introduction Lecturer: Ligang Dong, egan Tel: , Office: SIEE Building.
Chapter 1: Introduction
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 1 Class 14 - Review: Sorting & searching r What are sorting and searching? r Simple sorting algorithms.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
Loop Invariants and Binary Search Chapter 4.4, 5.1.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Discrete Maths: Invariant/2 1 Discrete Maths Objectives – –to show the use of induction for proving properties of code involving loops use induction.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Induction and Recursion CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
COP 3503 FALL 2012 Shayan Javed Lecture 16
CSE 1342 Programming Concepts
Applied Discrete Mathematics Week 9: Integer Properties
Presentation transcript:

Lecture 12 Oct 13, 2008 Some simple recursive programs recursive problem solving, connection to induction Some examples involving recursion Announcements module final exam Oct 15 from 10 to 12 noon. project # 2 due Oct 20 (next Monday).

Problem 1 – iterative solution Write a program that takes as input as odd number k and print the product 1 x 3 x 5 x … k. If the input is NOT odd, the program should print –1. Iteration: int chainProduct(int k) { if (k % 2 == 0) return –1; int Result:= 1; for (j = 3; j <= k; j = j+2) { Result:= Result * j; Return Result; }

Induction and recursion These two are related concepts. Induction is a proof technique, recursion is a related programming concept. Induction proof: assume it is true for n – 1, and show it for n using the assumption. recursion: assume you know how to solve the problem when the input size n – 1, and design a solution for the case n using the solution for n - 1.

Induction Proof Example: Prove the formula n = n(n+1)/2 induction hypothesis: n – 1 = (n – 1)n/2 induction step: n = ( n – 1) + n = (n – 1)n /2 + n = n ( n +1)/2 and this completes the proof.

Key facts to remember about recursion If the problem involves only one input parameter n, then you should determine how to compute f(n) given the value of f(n – 1). When writing the code, you can call the function recursively to get the value of f(n – 1). Sometimes, f(n) may depend on f(n – 1) as well as f(n – 2) etc. This is fine, so long as f(n) does not depend on f(n+1) … Always provide exit from recursion. (The simplest base cases such as k = 1 does not need recursion and should be handled without recursive calls.)

Recursive Solution to the same problem int chainProduct(int k) { if (k % 2 == 0) return -1; else if (k == 1) return 1; else return k * chainProduct(k-2); } Here is the main program: int main() { // to compute f(15)‏ cout << “f(15) is “ << f(15) << endl; }

Another simple example The following problem is discussed in Chapter 7 of Weiss. Compute the sum … + n. Code is as shown: long s( int n ) { if( n == 1 ) return 1; else return s(n-1) + n; }

Recursive evaluation of x n Write a recursive procedure to compute g(x, n) = x n. A simple solution: g(x, n) = 1 if n = 0 x if n = 1 g(x, n – 1) * x else. This recursive solution is not as efficient as it can be. We will next see a faster recursive solution. This is the first example of the power of recursion. Iteratively implementing this idea harder and trickier.

g(x, n) = 1 if n = 0 x if n = 1 g(x, n – 1) * x if n > 1 and n is odd g(x, n/2) 2 if n > 1 and n is even int power(int x, int n) { if (n == 0) return 1; else if (n == 1) return x; else if (n % 2 == 1) return x * power(x, n-1); else { int temp = power(x, n/2); return temp*temp; } }

Prime number testing Write a program to print all the prime numbers below a given integer N. Recall that m is a prime if and only if there is no integer x, 2 <= x < m such that (m % x == 0). Write a recursive procedure isDivisible(m, k) that returns true if there is an integer x, k <= x < m such that (m % x == 0).

Prime number testing – recursive solution isDivisible(m, k) returns true if there is an integer x, k <= x < m such that (m % x == 0).

Question: Given the procedure isDivisible(x, k), how to write a procedure isPrime(m) that returns true if and only if m is a prime? static boolean isPrime(int n) { return !isDivisible(n, 2); }

Recursive version of selection sort Selection Sorting Algorithm: During the j-th pass (j = 0, 1, …, n – 2), examine the elements of the array a[j], a[j+1], …, a[n-1] and determine the smallest of them. Suppose the smallest of a[j], a[j+1], …, a[n-1] is in position min. Swap a[min] and a[j]. Recursive structure of the algorithm: selection_sort(int[] a, int j) // j-th pass { if (j = a.length) return; // only one element in array min = smallest of a[j], a[j+1], …, a[n-1]; swap a[min] and a[j]; selection_sort(a, j+1); }