Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kontraktbaseret Programmering 1 Induction and Recursion Jens Bennedsen.

Similar presentations


Presentation on theme: "Kontraktbaseret Programmering 1 Induction and Recursion Jens Bennedsen."— Presentation transcript:

1 Kontraktbaseret Programmering 1 Induction and Recursion Jens Bennedsen

2 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

3 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

4 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

5 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.

6 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

7 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.

8 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.

9 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.

10 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

11 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

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

13 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”).

14 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

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

16 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

17 Ingeniørhøjskolen i Århus Slide 17 Fibonacci numbers Developed by Leonardo Pisano in 1202. –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?

18 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:

19 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

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

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

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

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

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

25 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.

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

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

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

29 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

30 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

31 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 )

32 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); } }

33 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

34 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

35 Ingeniørhøjskolen i Århus Slide 35 1. 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.

36 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.

37 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.

38 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.

39 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

40 Ingeniørhøjskolen i Århus Slide 40 2. 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

41 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.

42 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

43 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 

44 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”.

45 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


Download ppt "Kontraktbaseret Programmering 1 Induction and Recursion Jens Bennedsen."

Similar presentations


Ads by Google