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.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Lecture 2 Based on Chapter 1, Weiss. Mathematical Foundation Series and summation: ……. N = N(N+1)/2 (arithmetic series) 1 + r+ r 2 + r 3 +………r.
MATH 224 – Discrete Mathematics
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Recursion COL 106.
Recursion.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursion.
Programming Searching Arrays. COMP102 Prog. Fundamentals: Searching Arrays/ Slide 2 Copyright © 2000 by Broks/Cole Publishing Company A division of International.
Programming Recursion.
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.
Recursion COMP171 Fall Recursion / Slide 2 Recursion * In some problems, it may be natural to define the problem in terms of the problem itself.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
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.
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
1 Chapter 1: Introduction What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types -efficient algorithms.
Recursion Gordon College CPS212
Analysis of Recursive Algorithms
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
Simple Recursion. COMP104 Lecture 35 / Slide 2 Recursion: Example 0 * What does the following program do? #include using namespace std; int fac(int n){
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
CSE 220 (Data Structures and Analysis of Algorithms) Instructor: Saswati Sarkar T.A. Dimosthenes Anthomelidis
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
1 Chapter 1 RECURSION. 2 Chapter 1  Subprogram implementation  Recursion  Designing Recursive Algorithms  Towers of Hanoi  Backtracking  Eight Queens.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
Recursion. Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
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.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Math & Recursion COMP171 Fall Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition * Theorem.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
6/12/2016 Prepared by Dr.Saad Alabbad1 CS100 : Discrete Structures Proof Techniques(2) Mathematical Induction & Recursion Dr.Saad Alabbad Department of.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
CS212: Data Structures and Algorithms
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 19: Recursion.
Recursion DRILL: Please take out your notes on Recursion
Recursion and Logarithms
Mathematical Induction Recursion
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursion.
Announcements Final Exam on August 19th Saturday at 16:00.
Applied Discrete Mathematics Week 9: Integer Properties
Recursion.
Presentation transcript:

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 * Theorem 1.2: log AB = log A + log B n Proof: again apply the definition * log A : default is base 2 n log 2 = 1, log 1 = 0, … a log n = n log a log (a/b ) = log a - log b a mn = (a m ) n = (a n ) m a m+n = a m a n

Recursion / Slide 3 Series and summation: ……. N = N(N+1)/2 (arithmetic series) 1 + r+ r 2 + r 3 +………r N-1 = (1- r N )/(1-r), (geometric series)  1/(1-r), r < 1, large N Sum of squares: ………N 2 = N(N + 1)(2N + 1)/6 (proof by induction)

Recursion / Slide 4 Proof By Induction 1. Prove that a property holds for input n= 1 (base case) 2. Assume that the property holds for input size 1,…n. Show that the property holds for input size n Then, the property holds for all input sizes, n. (2  n) 0.5 (n/e) n  n   (2  n) 0.5 (n/e) n + (1/12n) ?

Recursion / Slide 5 Try this: Prove that the sum of 1+2+…..+n = n(n+1)/2 Proof: 1(1+1)/2 = 1 Thus the property holds for n = 1 (base case) Assume that the property holds for n=1,…,m, Thus …..+m = m(m+1)/2 We will show that the property holds for n = m + 1, that is ….. + m + m + 1 = (m+1)(m+2)/2 This means that the property holds for n=2 since we have shown it for n=1 Again this means that the property holds for n=3 and then for n=4 and so on.

Recursion / Slide 6 Now we show that the property holds for n = m + 1, that is ….. + m + m + 1 = (m+1)(m+2)/2 assuming that …..+m = m(m+1)/ …..+m + (m+1) = m(m+1)/2 + (m+1) = (m+1)(m/2 + 1) = (m+1)(m+2)/2

Recursion / Slide 7 Now we show that ………n 2 = n(n + 1)(2n + 1)/6 1(1+1)(2+1)/6 = 1 Thus the property holds for n = 1 (base case) Assume that the property holds for n=1,..m, Thus ………m 2 = m(m + 1)(2m + 1)/6 and show the property for m + 1, that is show that ………m 2 +(m+1) 2 = (m+1)(m + 2)(2m + 3)/6

Recursion / Slide ………m 2 + (m+1) 2 = m(m + 1)(2m + 1)/6 + (m+1) 2 =(m+1)[m(2m+1)/6 +m+1] = (m+1)[2m 2 + m + 6m +6]/6 = (m+1)(m + 2)(2m + 3)/6

Recursion / Slide 9 Sequence of numbers, F 0 F 1, F 2, F 3,……. F 0 = 1, F 1 = 1, F i = F i-1 + F i-2, F 2 = 2, F 3 = 3, F 4 = 5, F 5 = 8 Fibonacci numbers

Recursion / Slide 10 Prove that F n+1 < (5/3) n+1, F 2 < (5/3 ) 2 Let the property hold for 1,…k Thus F k+1 < (5/3) k+1, F k < (5/3) k F k+2 = F k + F k+1, < (5/3) k + (5/3) k+1 = (5/3) k (5/3 + 1) < (5/3) k (5/3) 2

Recursion / Slide 11 Proof By Counter Example Want to prove something is not true! Give an example to show that it does not hold, it is false! Is F N  N 2 ? No, F 11 = 144 However, if you were to show that F N  N 2 then you need to show for all N, and not just one number.

Recursion / Slide 12 Proof By Contradiction Suppose, you want to prove something. Assume that what you want to prove does not hold. Then show that you arrive at an impossibility. Example: The number of prime numbers is not finite!

Recursion / Slide 13 Suppose the number of primes is finite, k. The primes are P 1, P 2….. P k The largest prime is P k Consider the number N = 1 + P 1, P 2….. P k N is larger than P k Thus N is not prime. So N must be product of some primes. However, none of the primes P 1, P 2….. P k divide N exactly. So N is not a product of primes. (contradiction)

Review on Recursion

Recursion / Slide 15 Introduction What does the following program do? #include using namespace std; int fac(int n){ int product; if(n <= 1) product = 1; else product = n * fac(n-1); return product; } void main(){ int number; cout << "Enter a positive integer : " << endl;; cin >> number; cout << fac(number) << endl; }

Recursion / Slide 16 Assume the number typed is 3. fac(3): has the final returned value 6 3<=1 ? No. product 3 = 3*fac(2) product 3 =3*2=6, return 6, fac(2): 2<=1 ? No. product 2 = 2*fac(1) product 2 =2*1=2, return 2, fac(1): 1<=1 ? Yes. return 1 Tracing the program …

Recursion / Slide 17 void three(…) { … } void two (…) { three(); } void one (…) { two(…); } void main() { one(…); } l Functions are calling (DIFFERENT) functions l One function (three) is the last ‘stopping function’ int fac(int n){ int product; if(n <= 1) product = 1; else product = n * fac(n-1); return product; } void main(){ fac(3); } l … calling the SAME function ( with different parameters) … l The ‘stopping function’ is already included as a ‘condition’ Normal (non-recursive) functionsRecursive function

Recursion / Slide 18 Recursive function A recursive function is just a function which is calling one (or more) other functions which happen to be the same!!! l Though the function is the same, ‘parameters’ are always ‘smaller’ l There is always at least one stopping case to terminate It is a kind of ‘loop’, even more powerful as a general problem-solving technique! --- thinking recursively!

Recursion / Slide 19 Recursion: a programming and problem solving technique * Recursion is one way to decompose a task into smaller subtasks. * At least one of the subtasks is a smaller example of the same task. * The smallest example of the same task has a non-recursive solution. A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself. Remember: The general top-down programming and problem solving: Example: The factorial function n! = n * (n-1) * (n-2) *... * 1 or n! = n * (n-1)! and 1! = 1

Recursion / Slide 20 Recursion vs. Iteration (non-recursive) * A recursive solution may be simpler to write (once you get used to the idea) than a non- recursive solution.  But a recursive solution may not be as efficient as a non-recursive solution of the same problem.

Recursion / Slide 21 Iterative Factorial // Non-recursive factorial function // Compute the factorial using a loop int fac(int n){ // Assume n >= 0 int k, product; if(n <=1) product = 1; product = 1; for(k=1; k<=n; k++) product = k*product; return product; }

Recursion / Slide 22 Other Recursive Examples * Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. * Recursive definition: n F(0) = 0 n F(1) = 1 n F(n) = F(n-1) + F(n-2)

Recursion / Slide 23 Other Recursive Examples Binary search: n Compare search element with middle element of the array: If not equal, then apply binary search to half of the array (if not empty) where the search element would be.

Recursion / Slide 24 Recursion General Form How to write recursively? func-type function(parameters) { func-type value; if(stopping conditions) value = stopping value; else value = g(function((revised parameters))); return value; }

Recursion / Slide 25 Recursion: Example 1 How to write exp(int x, int y) recursively? int exp(int x, int y) { int power; if(y==0) power = 1; else power = x * exp(x, y-1); return power; }

Recursion / Slide 26 Recursion: Example 2 Write a recursive function that takes a double array and its size as input and returns the sum of the array: double asum(int a[], int size){ double sum; if(size==0) sum=0; else sum=asum(a,size-1)+a[size-1]; return sum; }

Recursion / Slide 27 Recursion: Example 3 Write a recursive function that takes a double array and its size as input and returns the product of the array: double aprod(int a[], int size) { doulbe prod; if(size==0) prod=1; else prod=aprod(a,size-1)*a[size-1]; return prod; }

Recursion / Slide 28 Recursion: Example 4 * Write a recursive function that counts the number of zero digits in a non-negative integer  zeros(10200) returns 3 int zeros(int n){ int z; if (n<10) if (n==0) z=1; else z=0; else z=zeros(n/10)+zeros(n%10); return z; } l n/10  the number n with the last digit removed l n%10  the last digit of n

Recursion / Slide 29 Recursion: Example 5 Write a recursive function to determine how many factors m are part of n. For example, if n=48 and m=4, then the result is 2 (48=4*4*3). int factors(int n, int m){ int f; if(n%m != 0) f=0; else f=1+factors(n/m, m); return f; }

Recursion / Slide 30 Recursion: example 6 Fibonacci numbers * Fibonacci numbers: Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. * Recursive definition: n F(0) = 0; n F(1) = 1; n F(number) = F(number-1)+ F(number-2);

Recursion / Slide 31

Recursion / Slide 32 //Calculate Fibonacci numbers using recursive function. //A very inefficient way, but illustrates recursion well int fib(int number) { if (number == 0) return 0; if (number == 1) return 1; return (fib(number-1) + fib(number-2)); } int main(){// driver function int inp_number; cout << "Please enter an integer: "; cin >> inp_number; cout << "The Fibonacci number for "<< inp_number << " is "<< fib(inp_number)<<endl; return 0; }

Recursion / Slide 33 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc.

Recursion / Slide 34 Trace a Fibonacci Number * Assume the input number is 4, that is, num=4: fib(4): 4 == 0 ? No; 4 == 1?No. fib(4) = fib(3) + fib(2) fib(3): 3 == 0 ? No; 3 == 1? No. fib(3) = fib(2) + fib(1) fib(2): 2 == 0? No; 2==1? No. fib(2) = fib(1)+fib(0) fib(1): 1== 0 ? No; 1 == 1? Yes. fib(1) = 1; return fib(1) ; int fib(int num) { if (num == 0) return 0; if (num == 1) return 1; return (fib(num-1)+fib(num-2)); }

Recursion / Slide 35 Trace a Fibonacci Number fib(0): 0 == 0 ? Yes. fib(0) = 0; return fib(0); fib(2) = = 1; return fib(2); fib(3) = 1 + fib(1) fib(1): 1 == 0 ? No; 1 == 1? Yes fib (1) = 1; return fib (1) ; fib(3) = = 2; return fib(3)

Recursion / Slide 36 Trace a Fibonacci Number fib(2): 2 == 0 ? No; 2 == 1?No. fib(2) = fib(1) + fib(0) fib(1): 1== 0 ? No; 1 == 1? Yes. fib(1) = 1; return fib(1); fib(0): 0 == 0 ? Yes. fib(0) = 0; return fib(0); fib(2) = = 1; return fib(2); fib(4) = fib(3) + fib(2) = = 3; return fib(4);

Recursion / Slide 37 Fibonacci number w/o recursion //Calculate Fibonacci numbers iteratively //much more efficient than recursive solution int fib(int n) { int f[n+1]; f[0] = 0; f[1] = 1; for (int i=2; i<= n; i++) f[i] = f[i-1] + f[i-2]; return f[n]; }

Recursion / Slide 38 Example 3: Binary Search n Search for an element in an array  Sequential search  Binary search n Binary search  Compare the search element with the middle element of the array  If not equal, then apply binary search to half of the array (if not empty) where the search element would be.

Recursion / Slide 39 Binary Search with Recursion // Searches an ordered array of integers using recursion int bsearchr(const int data[], // input: array int first, // input: lower bound int last, // input: upper bound int value // input: value to find )// output: index if found, otherwise return –1 { int middle = (first + last) / 2; if (data[middle] == value) return middle; else if (first >= last) return -1; else if (value < data[middle]) return bsearchr(data, first, middle-1, value); else return bsearchr(data, middle+1, last, value); }

Recursion / Slide 40 Binary Search int main() { const int array_size = 8; int list[array_size]={1, 2, 3, 5, 7, 10, 14, 17}; int search_value; cout << "Enter search value: "; cin >> search_value; cout << bsearchr(list,0,array_size-1,search_value) << endl; return 0; }

Recursion / Slide 41 Binary Search w/o recursion // Searches an ordered array of integers int bsearch(const int data[], // input: array int size, // input: array size int value // input: value to find ){ // output: if found,return // index; otherwise, return -1 int first, last, upper; first = 0; last = size - 1; while (true) { middle = (first + last) / 2; if (data[middle] == value) return middle; else if (first >= last) return -1; else if (value < data[middle]) last = middle - 1; else first = middle + 1; }

Recursion / Slide 42 Example 7: Towers of Hanoi n Only one disc could be moved at a time n A larger disc must never be stacked above a smaller one n One and only one extra needle could be used for intermediate storage of discs

Recursion / Slide 43 void hanoi(int from, int to, int num) { int temp = 6 - from - to; //find the temporary //storage column if (num == 1){ cout << "move disc 1 from " << from << " to " << to << endl; } else { hanoi(from, temp, num - 1); cout << "move disc " << num << " from " << from << " to " << to << endl; hanoi(temp, to, num - 1); }

Recursion / Slide 44 int main() { int num_disc; //number of discs cout << "Please enter a positive number (0 to quit)"; cin >> num_disc; while (num_disc > 0){ hanoi(1, 3, num_disc); cout << "Please enter a positive number "; cin >> num_disc; } return 0; }