0% found this document useful (0 votes)
2K views62 pages

Discrete Mathematics and Its Application - Chapter 4

Uploaded by

Funkey dunkey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views62 pages

Discrete Mathematics and Its Application - Chapter 4

Uploaded by

Funkey dunkey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 62

Chapter 4 (Part 1):

Induction & Recursion

• Proof Strategy (4.1)

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2011
Proof Strategy (4.1)
• Introduction

– Provide advice on how to find a proof of a


theorem

– How proofs are constructed

– Method of proof by cases

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
2 CS 210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)

• Proof Strategies

– Proof of a statement

• Replace terms by their definition

• Analyze what the hypotheses and conclusions


mean

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
3 CS 210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)

– Prove the result using one of the available methods of


proof

– Example: p  q

1. Go from p and derive q

2. Assume q false and p true and derive a contradiction

3. Prove the contrapositive q  p.

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
4 CS 210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)

– Strategy of proof of an implication

1. The forward reasoning

– Start with hypotheses

– Use axioms and known theorem with hypotheses

– Use a sequence of logical steps to derive the


conclusion

 This is the most common type of reasoning to prove


relatively simple results.

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
5 CS 210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)

2. The backward reasoning

– Start with the negation of the conclusion

– Use a sequence of logical steps to derive the


negation of the hypotheses

Unfortunately, forward reasoning is often difficult to use to


prove more complicated results. We often use backward
reasoning.

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
6 CS 210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)

– Example: Prove that the arithmetic mean is


always greater than the geometric mean for
positive real numbers.

Proof: (a + b)/2 > ab


(a + b)2/4 > ab
(a + b)2 > 4ab
a2 + 2ab + b2 > 4ab
a2 – 2ab + b2 > 0
(a – b)2 > 0

since this last inequality is always true if a  b and


since all these inequalities are equivalent, it follows
that
(a + b)/2 > ab (a  b).
7 © by Kenneth H.CS
Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)
– Example: Show that there are no solutions in
integers x and y of x2 + 3y2 = 8.

Proof: when |x|  3  x2 > 8


when |y|  2  3y2 > 8

The remaining cases are those when x takes one of the


values –2, -1, 0, 1 or 2 and y takes one of the values –1, 0 or
1. By computing x2 and 3y2 with all these possible values and
by combining the sum x2 + 3y2, we find that the largest sum is
equal to 7. Therefore, it is impossible for x2 + 3y2 = 8 to hold
when x and y are integers.
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
8 CS 210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)

• Conjecture and Proof


– Conjecture: Statement whose truth value is
unknown
• No matter how a conjecture was made, once it
has been formulated, the goal is to prove or
disprove it
• When mathematicians believe a conjecture may
be true, they try to find a proof
• If proofs cannot be found by mathematicians, they
may look for a counterexample
• Few conjecture resist attack for hundred of years
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
9 and leadCS to210
the Discrete
development Structures, of new parts4of(Part 1)
Chapter
Proof Strategy (4.1) (cont.)

– Example:
Conjecture: Do other primes of the special form an –1, where a
and n are positive integers, exist?

26 –1 = 63, 28 –1 = 255
34 –1 = 80, 45 –1 = 1023

 These numbers are not prime!

 We cannot find any primes beside the Mersenne


primes (2p-1)

 We can conjecture that an-1 is composite when a > 2


or when a = 2 and n is composite

10 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)

Proof: We need to find a factor of an-1 when


a > 2 or a = 2 and n is composite.

an-1 = (a –1) (an-1 + an-2 + … + a + 1)


 when a =2  a –1 = 2 –1 = 1 we cannot
conclude!

 when a > 2  a –1 is a factor of an –1 with


1 < a –1 < an –1  an –1 is not prime.

 when a = 2 and n is composite  r and s with


1 < r < n and 1 < s < n such that n = rs
an-1 = ars –1 = (ar –1) (ar(s-1) + …+ ar +1)
 a r –1 is a factor of an –1.
11 © by Kenneth H.CS
Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)

• Theorem 1:

The integer an-1 is composite when a > 2


or when a = 2 and n is composite

12 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)

• Conjectures & Counterexamples

– The role of open problems

• One famous problem unsolved for approximately


three hundred years led to the development of an
entire branch of number theory

• This problem asked whether the statement


known as Fermat’s last theorem is true

13 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)

– Theorem 2:

Fermat’s Last Theorem


The equation xn + yn = zn has no solutions in
integers x, y, and z with xyz  0 whenever n is an
integer with n > 2.

Proof: Refer to Andrew Wiles (Theory of Elliptic


Curves)

14 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)

– Example: The 3x + 1 conjecture

x
 if x is an even integer
f ( x )  2
 3x  1 if x is an odd integer

When we repeatedly apply the function f, we


will eventually reach the integer 1.

15 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) (cont.)

x = 13  f(13) = 3 * 13 + 1 = 40
x = 40  f(40) = 40/2 = 20
x = 20  f(20) = 20/2 = 10
x = 10  f(10) = 10/2 = 5
x = 5  f(5) = 3 * 5 + 1 = 16
x = 16  f(16) =16/2 = 8
x = 8  f(8) = 8/2 = 4
x = 4  f(4) = 4/2 =2
x = 2  f(2) = 1

This conjecture has been verified for all integers x up


to
5 * 6 *
© by 10 13H.. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
16 Kenneth
CS 210 Discrete Structures, Chapter 4 (Part 1)
Exercises
• Section 3.1:

#6, 8, 10 and 12.

17 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 1)
Chapter 4 (Part 2):
Mathematical Reasoning, Induction
& Recursion

• Recursive Definitions (3.4)

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2011
Introduction

• Define an object in
terms of itself: this
process is called
recursion

19 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 2)
Introduction (cont.)

• We can use recursion to define


sequences, functions and sets

• Example: the sequence of powers of 2 is


given by
an = 2n for n = 0, 1, 2, … (explicitly
defined)
a0 = 1; an+1 = 2an for n = 0, 1, 2, …
(recursively defined)

20 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 2)
Recursively Defined Functions
• We use 2 steps to define a function
recursively:

– Basis step: specify the the value of the


function at 0

– Recursive step: give a rule for determining its


value at an integer from its value at smaller
integers

– Such a definition
© by Kenneth is called a recursive or 20112)
21 H. Rosen, Discrete Mathematics
CS 210 Discrete&Structures, its Applications, Seventh Edition, Mc Graw-Hill,
Chapter 4 (Part
Recursively Defined Functions (cont.)

• Example: f defined recursively as:


f(0) = 3
f(n + 1) = 2 f(n) + 3
determine f(1), f(2), f(3) and f(4)

Solution: by definition:
f(1) = 2f(0) + 3 = 2 * 3 + 3 = 9
f(2) = 2f(1) + 3 = 2 * 9 + 3 = 21
f(3) = 2f(2) + 3 = 2 * 21 + 3 = 45
f(4) = 2f(3) + 3 = 2 * 45 + 3 = 93

22 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 2)
Recursively Defined Functions (cont.)

• Example: Give an inductive definition of


f(n) = n!

Solution:
Basis step: Specify the initial value of
this function; f(0) = 1.

Recursive step: give a rule for


determining
f(n© + 1) from f(n)
23 by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 2)
Recursively Defined Functions (cont.)
n

 ak of
• Example: Give a recursive definition
k 0

Solution:
0

a k  a0
Basis step:
k 0

n 1
 n 

k 0
ak    ak   an 1
 k 0 
Recursive step:

24 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 2)
Recursively Defined Functions (cont.)

• Definition 1:

The Fibonacci numbers, f0, f1, f2, …, are


defined by the equations f0 = 0, f1 = 1,
and
fn = fn-1 + fn-2
for n = 2, 3, 4, …

25 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 2)
Recursively Defined Functions (cont.)

• Example: Determine the Fibonacci


numbers
f2, f3, f4, f5 and f6

Solution:
Basis step: f0 = 0 and f1 = 1
Recursive step: fn = fn-1 + fn-2 
f2 = f1 + f0 = 1 + 0 = 1
f3 = f2 + f1 = 1 + 1 = 2
f4 = f3 + f2 = 2 + 1 = 3
f5 = f4 + f3 = 3 + 2 = 5
26 © by Kenneth H.CS f6 =Discrete
f5 + f4 =Structures,
5 + 3 = 8 Chapter 4 (Part 2)
Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
210
Recursively Defined Functions (cont.)

• Question: How can sets be defined


recursively?
Example: Consider the subset S of the
set of integers defined by:
Basis step: 3  S
Recursive steps: if x  S  y  S  x + y  S.

3  S (basis step)
3  S  3  S  3 + 3 = 6  S (recursive
step)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
27 CS 210 Discrete Structures, Chapter 4 (Part 2)
Recursively Defined Functions (cont.)
• Definition 2:

The set * of strings over the alphabet  can be defined by

Basis step:   * (where  is the empty string containing no


symbols)

Recursive step: if w  * and x  , then wx  *.

 Example: if  = {0, 1}
Basis step:   *
Recursive step: 0, 1 (0  *, 1  *)
Recursive step: 00 01 10 11  *
28 © by Kenneth H.CS
Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
210 Discrete Structures, Chapter 4 (Part 2)
Recursively Defined Functions (cont.)
• Definition 3
Two strings can be combined via the operation of
concatenation. Let  be a set of symbols and * the set of
strings formed from symbols in . We can define the
concatenation of two strings, denoted by  recursively as
follows:

Basis step: if w  *, then w   = w, where  is the empty


string.

Recursive step: if w1  *, w2  * and x  , then


w1  (w2x) = (w1  w2) x.

 Example: w1= abra w2 = cadabra


29 © byw 1w2 = abracadabra
Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
Recursively Defined Functions (cont.)

• Definition 4

The set of rooted trees, where a rooted tree consists of a set


of vertices containing a distinguished vertex called the root,
and edges connecting these vertices, can be defined
recursively by these steps:

Basis step: a single vertex r is a rooted tree.

Recursive step: Suppose that T1, T2, …, Tn are rooted trees


with roots r1, r2, …, rn, respectively. Then the graph formed
by starting with a root r, which is not in any of the rooted
trees T1, T2, …, Tn , and adding an edge from r to each of the
vertices r1, r2, …, rn, is also a rooted tree.
30 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CS 210 Discrete Structures, Chapter 4 (Part 2)
Building up rooted trees
Basis step

Step 1

Step 2

… …
31 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
Chapter 4 (Part 3):
Mathematical Reasoning, Induction
& Recursion

• Recursive Algorithms (4.4)

•Program Correctness (4.5)

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2011
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

33 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
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 an; a  0, n>0

Solution:
©
an+1 = a*an for n>0 a 0 = 1
34 by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
Recursive algorithm (4.4) (cont.)

• A recursive algorithm for computing an

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)

35 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
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).

36 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
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)

37 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
Recursive algorithm (4.4) (cont.)
• Example: Express the linear search as a
recursive procedure: search x in the search
sequence
aiai+1… aj.

• A Recursive linear search algorithm

procedure search(i, j, x)
if ai = x then
location := i
else if i = j then
location := 0
38else
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
Recursive algorithm (4.4) (cont.)

• Recursion & iteration

– We need to express the value of a function at


a positive integer in terms of the value of the
function at smaller integers

– Example: Compute a recursive procedure for


the evaluation of n!

39 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
Recursive algorithm (4.4) (cont.)

– A recursive procedure for factorials

procedure factorial(n: positive


integer
if n = 1 then
factorial(n) := 1
else
factorial(n) := n *
factorial(n - 1)

40 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
Recursive algorithm (4.4) (cont.)

• Recursion & iteration (cont.)

– 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

41 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
Recursive algorithm (4.4) (cont.)

– 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!}

42 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
Recursive algorithm (4.4) (cont.)

• Recursion & iteration (cont.)

– 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)
43 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
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
end
{y is the nth Fibonacci
© by Kenneth
44 number}
H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
Program Correctness (4.5)
• Introduction
Question: How can we be sure that a program
always produces the correct answer?
– The syntax is correct (all bugs removed!)
– Testing a program with a randomly selected
sample of input data is not sufficient
– Correctness of a program should be proven!
– Theoretically, it is never possible to
mechanize the proof of correctness of
complex programs
45 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
Program Correctness (4.5) (cont.)

• Program verification

– 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

46 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
Program Correctness (4.5) (cont.)

• Program verification (cont.)

– 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
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
47
respect to the initial CSE 504 Discrete Structures
assertion p and the & Foundations
final of
Program Correctness (4.5) (cont.)

– This definition of partial correctness (has


nothing to do whether a program terminates)
is due to Tony Hoare

– 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:
© by Kenneth H.pRosen,
is Discrete  x& =
trueMathematics 1  Seventh 2
y :=Edition, z :=2011 3
48 its Applications,
CSE 504 Discrete Structures & Foundations of Mc Graw-Hill,
Program Correctness (4.5) (cont.)

• Rules of inference
– Goal: Split the program into a series of
subprograms and show that each
subprogram is correct. This is done through
a rule of inference.

– The program S is split into 2 subprograms S1


and S2
(S = S1; S2)

– Assume that we have S1 correct w.r.t. p and


q (initial
© by Kennethand
H. Rosen,final assertions)
49 CSE Discrete
504 Mathematics
Discrete & its Applications, Seventh Edition, Mc Graw-Hill, 2011
Structures & Foundations of
Program Correctness (4.5) (cont.)
– It follows:

“if p is true  (S1 executed and terminates) then q


is true”
“if q is true  (S2 executed and terminates) then r
is true”
“thus, if p = true and S = S1; S2 is executed and
terminates then r = true”

This rule of inference is known as the


composition rule.

– It is written as:
© by Kenneth
50 H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
Program Correctness (4.5) (cont.)

• Conditional Statements

– 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© bymust show Discrete that when p is trueEdition,and
51 Kenneth H. Rosen, Mathematics & its Applications,
CSE 504 Discrete Structures & Foundations ofSeventh Mc Graw-Hill, 2011
Program Correctness (4.5) (cont.)
– We can summarize the conditional statement as:
(p  condition) {S}q
(p  condition)  q

p {if condition then S) q

– 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
52 ©and x>y
by Kenneth
CSEisDiscrete
H. Rosen, true
504 then
Mathematics
Discrete the final
& its Applications,
Structures &assertion
Seventh Edition, y of
Mc Graw-Hill, 2011
Foundations
Program Correctness (4.5) (cont.)
2. “if condition then S1 else S2”
if condition is true then S1 executes; otherwise
S2 executes

This piece of code is correct if:

a) If p = true  condition = true  q = true after S1


terminates
b) If p = true  condition = false  q = true after S2
terminates

(p  condition) {S1}q
(p  condition) {S2}q
© by Kenneth H. Rosen, Discrete Mathematics &
53 its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
Program Correctness (4.5) (cont.)

– 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
© by Kenneth H. Rosen,with definition of Seventh
absEdition, Mc Graw-Hill, 2011
54 Discrete Mathematics & its Applications,
CSE 504 Discrete Structures & Foundations of
Program Correctness (4.5) (cont.)
• Loop invariants
– In this case, we prove codes that contain the
while loop: “while condition S”

– 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

– 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
(p 
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
write55the rule of inference as:
Program Correctness (4.5) (cont.)

– 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
Rosen,factorial := Seventh
factorial Mc Graw-Hill, 2011*
56 © by Kenneth H.CSE Discrete Mathematics & its Applications,
504 Discrete Structures &Edition,
Foundations of
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

57 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
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

inew = i + 1

factorialnew = factorial * (i + 1) = (i + 1)! = inew!

Since i < n  inew = i + 1  n

 p true at the end of the execution of the loop


58 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504 Discrete Structures & Foundations of
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
© by Kenneth H. Rosen, that& itsifStructures
the while loop
59 CSE Discrete Mathematics
504 Discrete Applications, Seventh Edition, Mc Graw-Hill, 2011
& Foundations of
Chapter 5: Discrete Probability

• Historic
• Introduction (5.1)
• Discrete Theory (5.2)
• Expected Value &
Variance (5.3)

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2011
61
Historic
• The theory of probability was first developed
more than three hundred years ago by Blaise
Pascal for gambling purposes

• Later, the French mathematician Laplace, who


also was interested in gambling defined the
probability of an event

• The probability of an event led to the


development of much of basic probability theory

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
Dr. Djamel CSE 504 Discrete Structures & Foundations of
62
Historic (cont.)

• Probability theory plays an important role in


genetics where the goal is to help understand
the inheritance of traits

• In computer science, it is used in complexity


theory (average complexity of algorithms,
expert systems for medical diagnosis, etc)

• Unlike in deterministic algorithms, in


probabilistic algorithms, the output of a program
may change given the same input (random
choices are taken!)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
Dr. Djamel CSE 504 Discrete Structures & Foundations of

You might also like