Kontraktbaseret Programmering 1 Induction and Recursion Jens Bennedsen.

Slides:



Advertisements
Similar presentations
MAT 4 – Kompleks Funktionsteori MATEMATIK 4 INDUKTION OG REKURSION MM 1.4 MM 1.4: Induktion og Rekursion Topics: Mathematical induction Example of Towers.
Advertisements

22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Recursive Definitions and Structural Induction
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Induction and recursion
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
1.  The set N = {1,2,3,4,……..} is known as natural numbers or the set of positive integers  The natural numbers are used mainly for :  counting  ordering.
Recursion Chapter 8. 2 Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an.
Induction Sections 4.1 and 4.2 of Rosen Fall 2010
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSE115/ENGR160 Discrete Mathematics 03/22/12 Ming-Hsuan Yang UC Merced 1.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
Discrete Structures Chapter 5: Sequences, Mathematical Induction, and Recursion 5.2 Mathematical Induction I [Mathematical induction is] the standard proof.
CSE115/ENGR160 Discrete Mathematics 03/29/11 Ming-Hsuan Yang UC Merced 1.
1 Strong Mathematical Induction. Principle of Strong Mathematical Induction Let P(n) be a predicate defined for integers n; a and b be fixed integers.
Induction and Recursion by: Mohsin tahir (GL) Numan-ul-haq Waqas akram Rao arslan Ali asghar.
Induction and recursion
Advanced Counting Techniques
ICS 253 Presents Mathematical Induction Sultan Almuhammadi muhamadi
Mathematical Maxims and Minims, 1988
Induction and recursion
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Discrete Mathematics, 1st Edition Kevin Ferland
Mathematics Review Exponents Logarithms Series Modular arithmetic Proofs.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
1 Introduction to Abstract Mathematics Chapter 4: Sequences and Mathematical Induction Instructor: Hayk Melikya 4.1- Sequences. 4.2,
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Chapter 4: Induction and Recursion
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
CSC201 Analysis and Design of Algorithms Asst.Proof.Dr.Surasak Mungsing Oct-151 Lecture 2: Definition of algorithm and Mathematical.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Section 3.3: Mathematical Induction Mathematical induction is a proof technique that can be used to prove theorems of the form:  n  Z +,P(n) We have.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
CompSci 102 Discrete Math for Computer Science March 1, 2012 Prof. Rodger Slides modified from Rosen.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
 STACK STACK  STACK OPERATIONS STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  USES OF STACK USES OF STACK  THE TOWER.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Foundations of Discrete Mathematics Chapters 5 By Dr. Dalia M. Gil, Ph.D.
Inductive Proofs and Inductive Definitions Jim Skon.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
Mathematical Induction
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Chapter 5. Section 5.1 Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If we can reach.
Section 1.7. Definitions A theorem is a statement that can be shown to be true using: definitions other theorems axioms (statements which are given as.
Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If we can reach a particular.
1 Discrete Mathematical Mathematical Induction ( الاستقراء الرياضي )
Advanced Algorithms Analysis and Design
6/12/2016 Prepared by Dr.Saad Alabbad1 CS100 : Discrete Structures Proof Techniques(2) Mathematical Induction & Recursion Dr.Saad Alabbad Department of.
Mathematical Induction What it is? Why is it a legitimate proof method? How to use it?
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Mathematical Induction I Lecture 19 Section 4.2 Mon, Feb 14, 2005.
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Advanced Algorithms Analysis and Design
Induction and recursion
Mathematical Induction Recursion
Chapter 5 Induction and Recursion
Induction and recursion
Applied Discrete Mathematics Week 9: Integer Properties
1A Recursively Defined Functions
Advanced Analysis of Algorithms
Mathematical Induction
Mathematical Induction
Recursion: The Mirrors
Presentation transcript:

Kontraktbaseret Programmering 1 Induction and Recursion Jens Bennedsen

Ingeniørhøjskolen i Århus Slide 2 Agenda  Syllabus for this week Recursive definitions of sets Recursive definitions of algorithms Mathematical Induction – First Principle Mathematical Induction – Second Principle

Ingeniørhøjskolen i Århus Slide 3 Syllabus for this week Discrete Structures Web Course Material –Section 5A: Recursive Definitions –Section 5D: Proof by Induction –But these web pages also contain material about Propositional logic Predicate logic Sets Relations and Functions –Including on-line interactive exercises –May be handy for repetition before exam

Ingeniørhøjskolen i Århus Slide 4 Agenda Syllabus for this week  Recursive definitions of sets Recursive definitions of algorithms Mathematical Induction – First Principle Mathematical Induction – Second Principle

Ingeniørhøjskolen i Århus Slide 5 Recursive Definition of a set The basis clause: –What are the basic starting elements of the set. The inductive clause: –In which ways can existing elements be combined to produce new elements. The extremal clause: –Unless an object can be shown to be a member of the set by applying the first two clauses it is not a member of the set.

Ingeniørhøjskolen i Århus Slide 6 Example: The natural numbers The set of the natural numbers can be defined as: –Basis clause: 0  N. –Inductive clause: For any element x in N, x + 1 is in N. –Extremal clause: Nothing is in N unless it is obtained from either the basis or the inductive clauses. The “x + 1” is also called “succ(x)” in the literature This was formalized by the mathematician Peano

Ingeniørhøjskolen i Århus Slide 7 How to define the even natural numbers? The set of the even natural numbers (NE) can be defined as: –Basis clause: 0  NE. –Inductive clause: For any element x in NE, x + 2 is in NE. –Extremal clause: Nothing is in NE unless it is obtained from either the basis or the inductive clauses.

Ingeniørhøjskolen i Århus Slide 8 How to define the even integers? The set of the even integers (EI) can be defined as: –Basis clause: 0  EI. –Inductive clause: For any element x in EI, x + 2 and x -2 are in EI. –Extremal clause: Nothing is in EI unless it is obtained from either the basis or the inductive clauses.

Ingeniørhøjskolen i Århus Slide 9 How to define propositional forms? Let V = {p,q,r,…} be a set of propositional variables, where V does not contain any of the following symbols: (, ), , , , , , T and F. Then the set of the propositional forms can be defined as: –Basis clause: T and F are propositional forms and if x  V then x is a propositional form. –Inductive clause: If E1 and E2 are propositional forms then (  E1), (E1  E2), (E1  E2), (E1  E2), (E1  E2) are all propositional forms. –Extremal clause: Nothing is a propositional form unless it is obtained from either the basis or the inductive clauses.

Ingeniørhøjskolen i Århus Slide 10 Agenda Syllabus for this week Recursive definitions of sets  Recursive definitions of algorithms Mathematical Induction – First Principle Mathematical Induction – Second Principle

Ingeniørhøjskolen i Århus Slide 11 Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that would be difficult to solve in other ways Recursion splits a problem into one or more simpler versions of itself

Ingeniørhøjskolen i Århus Slide 12 Recursion in practice

Ingeniørhøjskolen i Århus Slide 13 Recursion Recipe 1.Handle the “base case”, where a recursive call is not made. 2.For the other cases, make a recursive call (the recursive “leap of faith”), which must make progress towards a goal (towards the base case) 3.Typically no need for an extremal clause If the base case is not handled, or the recursive call does not progress towards the base case, then the method will call itself infinitely (it will “diverge”).

Ingeniørhøjskolen i Århus Slide 14 Recursive Definitions of Algorithms The factorial 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

Ingeniørhøjskolen i Århus Slide 15 Recursive Definitions 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1!

Ingeniørhøjskolen i Århus Slide 16 A recursive C++ algorithm for factorial // Precondition n >= 1 // Postcondition result is n! int recursiveFac(int n) { if(n==1) return 1; else return n*recursiveFac(n-1); } Base case Recursive case

Ingeniørhøjskolen i Århus Slide 17 Fibonacci numbers Developed by Leonardo Pisano in –Investigating how fast rabbits could breed under idealized circumstances. –Assumptions A pair of male and female rabbits always breed and produce another pair of male and female rabbits. A rabbit becomes sexually mature after one month, and that the gestation period is also one month. –Pisano wanted to know the answer to the question how many rabbits would there be after one year?

Ingeniørhøjskolen i Århus Slide 18 Fibonacci Numbers The Fibonacci Numbers are defined by –The first two numbers are 1 and 1. –Each subsequent number is the sum of the preceding 2 numbers. 1,1,2,3,5,8,13,21,34,55,etc. Recursively it is defined as: Fibonachi(n) = 1 if n = 0 Fibonachi(n) = 1 if n = 1 Fibonachi(n) = Fibonachi(n-2) + Fibonachi(n-1) otherwise Non-recursively:

Ingeniørhøjskolen i Århus Slide 19 The fibonacci Method // Precondition: n >= 0 // Postcondition: The corresponding fibonachi number is // returned long fibonacci(long n){ int fib; if (n <= 1) return 1; else return fibonacci(n-1) + fibonacci(n-2); } Base case Recursive case

Ingeniørhøjskolen i Århus Slide 20 Execution Trace (decomposition) fibonacci(3) fibonacci(2) fibonacci(1)

Ingeniørhøjskolen i Århus Slide 21 Execution Trace (decomposition) fibonacci(3) fibonacci(2) fibonacci(1) fibonacci(0) fibonacci(1)

Ingeniørhøjskolen i Århus Slide 22 Execution Trace (composition) fibonacci(3) fibonacci(2) fibonacci(1) fibonacci(0)->1 fibonacci(1)->1 + +

Ingeniørhøjskolen i Århus Slide 23 Execution Trace (composition) fibonacci(3) fibonacci(2)->2 fibonacci(1)->1 +

Ingeniørhøjskolen i Århus Slide 24 Execution Trace (composition) fibonacci(3)->3

Ingeniørhøjskolen i Århus Slide 25 Towers of Hanoi In the great temple of Brahma in Benares, on a brass plate under the dome that marks the center of the world, there are 64 disks of pure gold that the priests carry one at a time between these diamond needles according to Brahma's immutable law: No disk may be placed on a smaller disk. In the beginning of the world all 64 disks formed the Tower of Brahma on one needle. Now, however, the process of transfer of the tower from one needle to another is in mid course. When the last disk is finally in place, once again forming the Tower of Brahma but on a different needle, then will come the end of the world and all will turn to dust. R. Douglas Hofstadter. Metamagical themas. Scientific American, 248(2):16-22, March 1983.

Ingeniørhøjskolen i Århus Slide 26 Towers of Hanoi Problem with Three Disks

Ingeniørhøjskolen i Århus Slide 27 Towers of Hanoi: Three Disk Solution

Ingeniørhøjskolen i Århus Slide 28 Towers of Hanoi: Three Disk Solution

Ingeniørhøjskolen i Århus Slide 29 Towers of Hanoi: Recursive Function The general algorithm to solve this problem is 1. Move the top n-1 disks from needle 1 to needle 2 using needle 3 as the intermediate needle 2. Move disk number n from needle 1 to needle 3 3. Move the top n-1 disks from needle 2 to needle 3 using needle 1 as the intermediate needle

Ingeniørhøjskolen i Århus Slide 30 Recursive Towers of Hanoi in C++ // Precondition: count >= 0 // Postcondition: count disks are moved from from_needle // to to_needle using via_needle for help void moveDisks(int count, int from_needle, int to_needle, int via_needle) { if(count > 0) { moveDisks(count - 1, from_needle, via_needle, to_needle); cout<<"Move disk "<<count<<“ from "<<from_needle <<“ to "<<to_needle<<"."<<endl; moveDisks(count - 1, via_needle, to_needle, from_needle); } } Recursive call

Ingeniørhøjskolen i Århus Slide 31 General problem solving strategy Divide and conquer solve(p: Problem) { if solution = else res1 = solve(p 1 ) res2 = solve(p 2 ) solution = join(p 1, p 2 )

Ingeniørhøjskolen i Århus Slide 32 Example: search private bool search(int m, List p) { if (p.Count == 0) return false; else if (p.Count == 1) return p[0] == m; else { List p1 = new List (); List p2 = new List (); for (int i = 0; i < p.Count / 2; i++) p1[i] = p[i]; for (int j = (p.Count / 2) + 1; j < p.Count; j++) p2[j - ((p.Count / 2) + 1)] = p[j]; return search(m, p1) || search(m, p2); } }

Ingeniørhøjskolen i Århus Slide 33 Agenda Syllabus for this week Recursive definitions of sets Recursive definitions of algorithms  Mathematical Induction – First Principle Mathematical Induction – Second Principle

Ingeniørhøjskolen i Århus Slide 34 Mathematical Induction In the mathematical induction we have the law already formulated. We must prove that it holds generally The basis for mathematical induction is the property of the well-ordering principle for the natural numbers

Ingeniørhøjskolen i Århus Slide principle of Mathematical Induction Also called weak induction Suppose P(n) is a statement involving an integer n Then to prove that P(n) is true for every n ≥ n 0 it is sufficient to show that these two things hold: 1.P(n 0 ) is true 2.For any k ≥ n 0, if P(k) is true, then P(k+1) is true –In general we have a basis step –Followed by an inductive step.

Ingeniørhøjskolen i Århus Slide 36 Example Induction Proof Prove that for any natural number n it holds that: 0+1+…+n = n(n + 1)/2 –Basis step: If n = 0 then LHS = 0 and RHS = 0(0+1)/2=0 –Induction step: Let the induction hypothesis be: For an arbitrary k it holds that 0+1+…+k = k(k + 1)/2 Let us try to express LHS for (k+1). Using the induction hypothesis the LHS = k(k+1)/2 + (k+1) If we factor out (k+1) we can rewrite this to: (k+1)(k+2)/2 This is identical to the RHS for (k+1) QED.

Ingeniørhøjskolen i Århus Slide 37 Example Induction Proof Prove that for any natural number n it holds that: 1+3+…+(2n+1) = (n + 1) 2 –Check for n = 0: 2*0 + 1 = (0 + 1) 2 –Assume that: For an arbitrary k it holds that 1+3+…+(2k+1) = (k + 1) 2 Then for k+1 we need to prove: 1+3+…+(2k+1)+(2(k+1)+1) = ((k+1)+1) 2 Using the induction hypothesis we get: 1+3+…+(2k+1)+(2(k+1)+1) = (k+1) 2 + (2(k+1) +1) Since (k+1) 2 + 2(k+1)+1 = (k+1+1) 2 we have proved our objective. QED.

Ingeniørhøjskolen i Århus Slide 38 More on Inductive Proofs General strategy: Clarify on which variable you are going to do the induction Calculate some small cases n=0,1,2,3,… (Come up with your conjecture) Make clear what the induction step n  n+1 is Prove it, and say that you proved it.

Ingeniørhøjskolen i Århus Slide 39 Agenda Syllabus for this week Recursive definitions of sets Recursive definitions of algorithms Mathematical Induction – First Principle  Mathematical Induction – Second Principle

Ingeniørhøjskolen i Århus Slide principle of Mathematical Induction Also called strong induction Suppose P(n) is a statement involving an integer n Then to prove that P(n) is true for every n ≥ n 0 it is sufficient to show that this holds: 1.P(n 0 ) is true 2.For any k ≥ n 0, if P(x) is true for all x smaller than k, then P(k) is true

Ingeniørhøjskolen i Århus Slide 41 Strong induction Weak mathematical induction assumes P(k) is true, and uses that (and only that!) to show P(k+1) is true Strong mathematical induction assumes P(1), P(2), …, P(k) are all true, and uses that to show that P(k+1) is true.

Ingeniørhøjskolen i Århus Slide 42 Strong induction example Show that any number > 1 can be written as the product of primes Base case: P(2) –2 is the product of 2 (remember that 1 is not prime!) Inductive hypothesis: P(1), P(2), P(3), …, P(k) are all true Inductive step: Show that P(k+1) is true

Ingeniørhøjskolen i Århus Slide 43 Strong induction example Inductive step: Show that P(k+1) is true There are two cases: –k+1 is prime It can then be written as the product of k+1 –k+1 is composite It can be written as the product of two composites, a and b, where 2 ≤ a ≤ b < k+1 By the inductive hypothesis, both P(a) and P(b) are true –QED 

Ingeniørhøjskolen i Århus Slide 44 Induction in Computer Science Inductive proofs play an important role in computer science because of their similarity with recursive algorithms. Analyzing recursive algorithms often require the use of recurrent equations, which require inductive proofs. Also, recursive algorithms are constructive such that we often get a solution “for free”.

Ingeniørhøjskolen i Århus Slide 45 Summary The notion of Recursion and Induction –Recursive definitions of sets –Recursive definitions of algorithms –Mathematical Induction Read Section 5A: Recursive Definitions and Section 5D: Proof by Induction from the web pages on slide 3 Carry out as many exercises as possible from the web pages