45
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & King Fahd University of Petroleum & Minerals Minerals Information & Computer Science Department Information & Computer Science Department

ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Embed Size (px)

Citation preview

Page 1: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

ICS 253: Discrete Structures I

Induction and Recursion

King Fahd University of Petroleum & MineralsKing Fahd University of Petroleum & Minerals

Information & Computer Science DepartmentInformation & Computer Science Department

Page 2: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Reading Assignment• K. H. Rosen,

Discrete Mathematics and Its Applications, 6th Ed., McGraw-Hill, 2006. • Chapter 4 Sections 4.1, 4.2 and 4.3

2

Page 3: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Introduction

• Many mathematical statements assert that a property is true for all positive integers.• e.g. n! nn

• Many functions are defined based on certain “rules”• i.e. recursively.

3

Page 4: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Section 4.1: Mathematical Induction

4

Page 5: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Mathematical Induction

• Mathematical induction is an extremely important proof technique that is used extensively to prove results about a large variety of discrete objects. • Complexity of algorithms• Correctness of certain types of computer

programs• Theorems about graphs and trees, • A wide range of identities and inequalities…etc.

5

Page 6: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Mathematical Induction

• Mathematical induction is used to prove propositions of the form: n P(n), where the universe of discourse is the set of positive integers

6

Page 7: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

First Form of Mathematical Induction

• Basis Step: P(1) is shown to be true• Inductive Step: The implication

P(n) P(n+1) is shown to be true for every positive integer n

• Question: Is this the same as circular reasoning???

• As a rule of inference:[P(1) n (P(n) P(n+1))] n P(n)

Inductive Hypothesis

7

Page 8: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples

• Prove that

Proof:

8

1

1

2

n

i

n ni

Page 9: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples

• Prove that the sum of the first n odd positive integers is n2

9

Page 10: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples

• Prove that n < 2n n

10

Page 11: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples

• Prove that 2n < n! for n 4

11

Page 12: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples

• Prove that n3 – n is divisible by 3 n Z+

12

Page 13: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples

• Use mathematical induction to show that

n=0,1,2,…

where

13

,2

12

nH n

kH k

1...

3

1

2

11

Page 14: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples

• Use mathematical induction to show that if S is a finite set with n elements where n is a nonnegative integer, then S has 2n subsets.

14

Page 15: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples• Use mathematical induction to prove the

following generalization of one of De Morgan's

laws: whenever A1, A2 , . . . , An are

subsets of a universal set U and n 2.

15

1 1

n n

j jj j

A A

Page 16: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples

• Let n be a +ve integer. Show that any 2n 2n chessboard with one square removed can be tiled using L-shaped pieces, where these pieces cover three squares at a time as shown below

16

Page 17: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Sketch of The Solution

Basis Step:

InductionStep:

InductiveHypothesis

Subsquare having the real hole

Subsquare with ‘artificial’hole at lower right corner

Subsquare with ‘artificial’hole at lower left corner

Subsquare with ‘artificial’ hole at upper left corner

17

Page 18: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Section 4.2: Strong Induction and Well Ordering

• Strong induction is similar to the mathematical induction in both requiring a basis step.

• Strong induction differs in the inductive step, where we assume that the statement P(j) is true for all j k, and then prove that P holds for j=k+1.

• The validity of both mathematical induction and strong induction follow from the well-ordering property.• In fact, mathematical induction, strong induction,

and well-ordering are all equivalent principles.

18

Page 19: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Strong Induction

• Basis Step: P(1) is shown to be true

• Inductive Step:

[P(1) P(2) … P(n)] P(n+1)

is shown to be true for every positive integer n

19

Page 20: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples

• Suppose we can reach the first and second rungs of an infinite ladder, and we know that if we can reach a rung, then we can reach two rungs higher. • Can we prove that we can reach every rung using

the principle of mathematical induction? • Can we prove that we can reach every rung using

strong induction?

20

Page 21: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples

• Show that if n is an integer greater than 1, then n can be written as the product of primes.

21

Page 22: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples

• Prove that every amount of postage of 12 cents or more can be formed using just 4-cent and 5-cent stamps.

22

Page 23: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Well Ordering Property

• Mathematical induction follows from the following fundamental axiom

The Well Ordering Property: Every non-empty set of nonnegative integers has a least element

• The well ordering property can often be used directly in proofs.

23

Page 24: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Section 4.3: Recursive Definitions and Structural Induction

24

• Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object in terms of itself. This process is called recursion.

Page 25: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Recursive Definitions and Structural Induction• Recursion can be used to define sequences,

functions and sets• Sequences: can also be defined as

• Functions: Similar to sequences• After all, sequences are, themselves, functions!

• Sets

25

2nna

1

1 0

2 1nn

na

a n

Page 26: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Recursively Defined Functions

• Q3 pp 308: Find f(2), f(3), f(4), and f(5) if f is defined recursively by f(0) = –1, f(l) = 2 and for n = 1,2, ...a) f(n + 1) = f(n) + 3f(n – 1).

b) f(n + 1) = f(n)2 f(n – 1).

c) f(n + 1) = 3f(n)2 – 4f(n – 1)2.

d) f(n + 1) = f(n – l)/f(n).

26

Page 27: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Recursively Defined Functions

• Give an inductive definition of the factorial function F(n) = n!.

• Give a recursive definition of

27

0

n

kk

a

Page 28: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Fibonacci Numbers

• Definition: The Fibonacci numbers, f0, f1, f2, . . . , are defined by the equations f0 = 0, f1 = 1, and fn = fn – l + fn – 2 for n = 2, 3, 4, . . . .

• Show that whenever n 3, fn > n – 2 , where

28

1 5

2

Page 29: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Recursively Defined Sets and Structures• In recursively defined sets, the basis step defines

some initial elements and the recursive step defines a rule for constructing new elements from those already in the set.

• Recursive definitions may also include an exclusion rule, which specifies that a recursively defined set contains nothing other than those elements specified in the basis step or generated by applications of the recursive step.• We will assume that it always holds.

• To prove results about recursively defined sets we use a method called structural induction.

29

Page 30: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Example

• Consider the subset S of the set of integers defined by • Basis step: 3 S.• Recursive step: If x S and y S, then

x + y S.

• Show that S is the set of all positive integers that are multiples of 3.

30

Page 31: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Strings Over Alphabet

• Definition: The set * of strings over the alphabet can be defined recursively by• Basis step: * (where is the empty string

containing no symbols).• Recursive step: If w* and x, then wx*.

• Example: What are the strings formed over ={0,1}?

31

Page 32: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Concatenation of Strings

• Definition: 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* and w2* and x,

then w1 · (w2 x ) = (w1 . w2)x.

• Usually, w1 . w2 is denoted by w1w2

32

Page 33: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Length of a String

• Give a recursive definition of l(w), the length of the string w.

• Solution:

The length of a string can be defined by• l() = 0;• l(wx) = l(w) + 1 if w * and x .

• Use structural induction to prove that l(xy) = I(x) + l(y).

33

Page 34: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Well-Formed Formulae• Well-Formed Formulae for Compound Statement Forms

• We can define the set of well-formed formulae for compound statement forms involving T, F, propositional variables, and operators from the set {,,,,} as follows

• Basis step: T, F, and s, where s is a propositional variable, are well-formed formulae.

• Recursive step: If E and F are well-formed formulae, then (E), (E F), (E F), (E F), and (E F) are well-formed formulae.

• Is (p q) (q F) a well-formed formula?• Is p q a well-formed formula?• Can you similarly define well-formed formulae for operators

and operands?• Show that every well-formed formulae for compound

propositions contains an equal number of left and right parentheses.

34

Page 35: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Rooted Trees

• Definition: 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

disjoint rooted trees with roots rl , 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. .

35

Page 36: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Building Up Rooted Trees

36

Page 37: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Extended Binary Trees

• Definition: The set of extended binary trees can be defined recursively by these steps:• Basis step: The empty set is an extended binary tree.

• Recursive step: If T1 and T2 are disjoint extended binary trees, there is an extended binary tree, denoted by Tl · T2 , consisting of a root r together with edges connecting the root to each of the roots of the left subtree Tl and the right subtree T2 when these trees are nonempty.

37

Page 38: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Building Extended Binary Trees

38

Page 39: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Full Binary Trees

• Definition: The set of full binary trees can be defined recursively by these steps:• Basis step: There is a full binary tree consisting

only of a single vertex r.

• Recursive step: If Tl and T2 are disjoint full binary trees, there is a full binary tree, denoted by Tl · T2 , consisting of a root r together with edges connecting the root to each of the roots of the left subtree Tl and the right subtree T2 .

39

Page 40: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Examples of Full Binary Trees

40

Page 41: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Full Binary Trees

• Denote the number of nodes in full binary tree T as n(T).• Can you provide a recursive definition of n(T)?

• Definition: The height h(T) of a full binary tree T recursively as follows• Basis step: The height of the full binary tree T

consisting of only a root r is h(T) = 0.

• Recursive step: If T1 and T 2 are full binary trees, then the full binary tree T = T1 · T2 has height h(T) = 1 + max(h(T 1 ), h(T2))

41

Page 42: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Full Binary Trees

• Theorem: If T is a full binary tree, then

Proof (By Structural Induction):

42

( ) 1( ) 2 1h Tn T

Page 43: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Full Binary Trees

• Definition: A leaf node is a node that has no children.

• Definition: An internal node is a node that has one or more children.

• Prove that in any full binary tree, the number of leaf nodes is one more than the number of internal nodes.

43

Page 44: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Generalized Induction

• Instead of using induction on Z+ or N, we can extend it to any set having the well-ordering property

• For example, consider N N with the following property (called lexicographic ordering)• (x1, y1) is less than or equal to (x2, y2) if either x1<x2,

or x1=x2 and y1<y2;

44

Page 45: ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department

Induction and RecursionICS 253: Discrete Structures I

Example

• Suppose that am,n is defined recursively for (m, n) N N

Show that am,n = m + n(n + 1)/2 for all (m,n)N N

45

, 1,

, 1

0 if 0

1 if 0and 0

if 0m n m n

m n

m n

a a n m

a n n