© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.

Slides:



Advertisements
Similar presentations
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
Advertisements

Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 04/05/11 Ming-Hsuan Yang UC Merced 1.
Elementary Number Theory and Methods of Proof. Basic Definitions An integer n is an even number if there exists an integer k such that n = 2k. An integer.
Discrete Structures Chapter 2 Part B Mathematical Induction
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
4/17/2017 Section 3.6 Program Correctness ch3.6.
Chapter Mathematical Induction
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank. Modified By Mingwu Chen Induction.
Climbing an Infinite Ladder
Induction and recursion
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Discrete Mathematics Chapter 4 Induction and Recursion 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 3 (Part 3): The Fundamentals: Algorithms, the.
Discrete Mathematics, 1st Edition Kevin Ferland
Mathematics Review Exponents Logarithms Series Modular arithmetic Proofs.
Discrete Mathematics and Its Applications Sixth Edition By Kenneth Rosen Chapter 4 Induction and Recursion 歐亞書局.
Discrete Maths Objective to introduce mathematical induction through examples , Semester 2, Mathematical Induction 1.
Week #7 – 7/9/11 October 2002 Prof. Marie desJardins
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.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
INDUCTION AND RECURSION. PRINCIPLE OF MATHEMATICAL INDUCTION To prove that P(n) is true for all positive integers n, where P(n) is a propositional function,
4.3 Recursive Definitions and Structural Induction The PMI, 2PMI, and WOP are all valuable tools in proving the correctness of loops and of recursively.
Module #14: Recursion Rosen 5 th ed., §§ In this class, we will study recursion, one of the most important topics in computer science. In the last.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Induction and recursion
CSI 3125, Axiomatic Semantics, page 1 Axiomatic semantics The assignment statement Statement composition The "if-then-else" statement The "while" statement.
Copyright © Cengage Learning. All rights reserved. CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
1 Introduction to Abstract Mathematics Chapter 3: Elementary Number Theory and Methods of Proofs Instructor: Hayk Melikya Direct.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
Program Correctness. 2 Program Verification An object is a finite state machine: –Its attribute values are its state. –Its methods optionally: Transition.
Inductive Proofs. Mathematical Induction A powerful, rigorous technique for proving that a predicate P(n) is true for every natural number n, no matter.
Chapter 5 With Question/Answer Animations. Section 5.1.
Recursive Algorithms &
Chap 3 –A theorem is a statement that can be shown to be true –A proof is a sequence of statements to show that a theorem is true –Axioms: statements which.
13 Aug 2013 Program Verification. Proofs about Programs Why make you study logic? Why make you do proofs? Because we want to prove properties of programs.
October 3, 2001CSE 373, Autumn Mathematical Background Exponents X A X B = X A+B X A / X B = X A-B (X A ) B = X AB X N +X N = 2X N 2 N +2 N = 2 N+1.
Recursive Algorithm (4.4) An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input.
Chapter 5 Kenneth Rosen, Discrete Mathematics and its Applications, 7th edition, McGraw Hill Instructor: Longin Jan Latecki, Copyright.
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.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Chapter 4: Induction and Recursion
Recursive Algorithms Section 5.4.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
CSE15 Discrete Mathematics 04/12/17
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
CSE 504 Discrete Mathematics & Foundations of Computer Science
Induction and Recursion
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Mathematical Induction Recursion
Chapter 5 Induction and Recursion
Copyright © Cengage Learning. All rights reserved.
Axiomatic semantics Points to discuss: The assignment statement
CSE 311: Foundations of Computing
Applied Discrete Mathematics Week 9: Integer Properties
Chapter 4 Induction and Recursion
Induction and recursion
Recursion.
Program Correctness an introduction.
Presentation transcript:

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (4.4)  Program Correctness (4.5)

2 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive Algorithm (4.4)  Goal: Reduce the solution to a problem with a particular set of input to the solution of the same problem with smaller input values  Example: Greater Common Divisor (gcd) gcd(a,b) = gcd(b mod a, a) gcd(4,8) = gcd(8 mod 4, 4) = gcd(0,4) = 4

3 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.)  Definition 1: An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input.  Example: Give a recursive algorithm for computing a n ; a  0, n>0 Solution: a n+1 = a*a n for n>0 a 0 = 1

4 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.)  A recursive algorithm for computing a n Procedure power(a: nonzero real number, n: nonnegative integer) if n = 0 then power(a, n):= 1 Else power(a,n) := a * power(a, n-1)

5 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.)  Example: Give a recursive algorithm for computing the greatest common divisor of two nonnegative integers a and b (a<b) Solution: gcd(a,b) = gcd(b mod a, a) and the condition gcd(0,b) = b (b>0).

6 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.)  A recursive algorithm for computing gcd(a,b) procedure gcd(a, b: nonnegative integers with a<b) if a = 0 then gcd(a,b) := b else gcd(a,b) := gcd(b mod a, a)

7 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.)  Example: Express the linear search as a recursive procedure: search x in the search sequence a i a i+1 … a j.  A Recursive linear search algorithm procedure search(i, j, x) if a i = x then location := i else if i = j then location := 0 else search(i + 1, j,x)

8 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.)  Recursion & iteration u We need to express the value of a function at a positive integer in terms of the value of the function at smaller integers u Example: Compute a recursive procedure for the evaluation of n!

9 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.) u A recursive procedure for factorials procedure factorial(n: positive integer if n = 1 then factorial(n) := 1 else factorial(n) := n * factorial(n - 1)

10 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.)  Recursion & iteration (cont.) u However, instead of reducing the computation to the evaluation of the function at smaller integers, we can start by 1 and explore larger in an iterative way

11 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.) u An iterative (vs. recursive) procedure for factorials procedure iterative factorial(n: positive integer) x := 1 for i := 1 to n x := i * x {x is n!}

12 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.)  Recursion & iteration (cont.) u An other example is the recursive algorithm for Fibonacci numbers procedure fibonacci(n: nonnegative integer) if n = 0 then fibonacci(0) := 0 else if n = 1 then fibonacci(1) := 1 else fibonacci(n) := fibonacci(n – 1) + fibonacci(n – 2)

13 Recursive algorithm (4.4) (cont.)  An iterative algorithm for computing Fibonacci numbers procedure iterative fibonacci(n: nonnegative integer) if n = 0 then y := 0 else begin x := 0 y := 1 for i := 1 to n – 1 begin z := x + y x := y y := z end {y is the nth Fibonacci number}

14 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5)  Introduction Question: How can we be sure that a program always produces the correct answer? u The syntax is correct (all bugs removed!) u Testing a program with a randomly selected sample of input data is not sufficient u Correctness of a program should be proven! u Theoretically, it is never possible to mechanize the proof of correctness of complex programs u We will cover some of the concepts and methods that prove that “ simple ” programs are corrects

15 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.)  Program verification u To prove that a program is correct, we need two parts: 1.For every possible input, the correct answer is obtained if the program terminates 2.The program always terminates

16 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.)  Program verification (cont.) u Definition 1: A program, or program segment, S is said to be partially correct with respect to the initial assertion p and the final assertion q if whenever p is true for the input values of S and S terminates, then q is true for the output values of S. The notation p{S}q indicates that the program, or program segment, S is partially correct with respect to the initial assertion p and the final assertion q.

17 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) u This definition of partial correctness (has nothing to do whether a program terminates) is due to Tony Hoare u Example: Show that the program segment y := 2 z := x + y is correct with respect to the initial assertion p: x =1 and the final assertion q: z =3. Solution: p is true  x = 1  y := 2  z := 3  partially correct w.r.t. p and q

18 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.)  Rules of inference u Goal: Split the program into a series of subprograms and show that each subprogram is correct. This is done through a rule of inference. u The program S is split into 2 subprograms S 1 and S 2 (S = S 1 ; S 2 ) u Assume that we have S 1 correct w.r.t. p and q (initial and final assertions) u Assume that we have S 2 correct w.r.t. q and r (initial and final assertions)

19 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) u It follows: “ if p is true  (S 1 executed and terminates) then q is true ” “ if q is true  (S 2 executed and terminates) then r is true ” “ thus, if p = true and S = S 1 ; S 2 is executed and terminates then r = true ” This rule of inference is known as the composition rule. u It is written as: p {S 1 }q q {S 2 }r  p {S1; S2) r

20 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.)  Conditional Statements u Assume that a program segment has the following form: 1.“ if condition then S ” where S is a block of statement Goal: Verify that this piece of code is correct Strategy: a)We must show that when p is true and condition is also true, then q is true after S terminates b)We also must show that when p is true and condition is false, then q is true

21 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) u We can summarize the conditional statement as: (p  condition) {S}q (p   condition)  q  p {if condition then S) q u Example: Verify that the following program segment is correct w.r.t. the initial assertion T and the final assertion y  x if x > y then y:= x Solution: a)If T = true and x>y is true then the final assertion y  x is true b)If T = true and x>y is false then x  y is true  final assertion is true again

22 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) 2.“ if condition then S 1 else S 2 ” if condition is true then S 1 executes; otherwise S 2 executes This piece of code is correct if: a)If p = true  condition = true  q = true after S 1 terminates b)If p = true  condition = false  q = true after S 2 terminates (p  condition) {S 1 }q (p   condition) {S 2 }q  p {if condition then S 1 else S 2 ) q

23 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) u Example: Check that the following program segment if x < 0 then abs := -x else abs := x is correct w.r.t. the initial assertion T and the final assertion abs = |x|. Solution: a)If T = true and (x<0) = true  abs := -x; compatible with definition of abs b)If T = true and (x<0)= false  (x  0) = true  abs := x; compatible with abs definition.

24 Program Correctness (4.5) (cont.)  Loop invariants u In this case, we prove codes that contain the while loop: “ while condition S ” u Goal: An assertion that remains true each time S is executed must be chosen: Such an assertion is called a loop invariant. In other words, p is a loop invariant if: (p  condition) {S}p is true u If p is a loop invariant, then if p is true before the program segment is executed, p and  condition are true after termination, if it occurs. We can write the rule of inference as: (p  condition) {S}p  p {while condition S} (  condition  p)

25 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) u Example: Determine the loop invariant that verifies that the following program segment terminates with factorial = n! when n  0. i := 1 factorial := 1 While i < n begin i := i + 1 factorial := factorial * i end

26 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) Solution: Choose p = (factorial = i!  (i  n)) a)Prove that p is in fact a loop invariant b)If p is true before execution, p and  condition are true after termination c)Prove that the while loop terminates

27 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) a)P is a loop invariant: Suppose p is true at the beginning of the execution of the while loop and the while condition holds;  factorial = i!  i < n i new = i + 1 factorial new = factorial * (i + 1) = (i + 1)! = i new ! Since i < n  i new = i + 1  n  p true at the end of the execution of the loop  p is a loop invariant

28 CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) b)Before entering the loop, i = 1  n and factorial :=1 = 1! = i!  (i  n)  (factorial = i!) = true  p = true Since p is a loop invariant  through the inference rule, if the while loop terminates  p = true and i < n false  i = n and factorial = i! = n! c) While loop terminates: Because p is a loop invariant, the rule of inference implies that if the while loop terminates, it terminates with p true and with i<n false. In this case, at the end, factorial = i! and i<=n are true, but i<n is false, in other words, i=n and factorial= i!=n!, as desired.