ICS 353: Design and Analysis of Algorithms

King Fahd University of Petroleum & Minerals
Information & Computer Science Department
ICS 253: Discrete Structures I
Induction and Recursion
ICS 253: Discrete Structures I
2
Induction and Recursion
Reading Assignment
• K. H. Rosen, Discrete Mathematics and Its
Applications, 6th Ed., McGraw-Hill, 2006.
• Chapter 5 Sections 5.1, 5.2 and 5.3
3
ICS 253: Discrete Structures I
Induction and Recursion
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.
ICS 253: Discrete Structures I
4
Induction and Recursion
Section 5.1: Mathematical Induction
ICS 253: Discrete Structures I
5
Induction and Recursion
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.
ICS 253: Discrete Structures I
6
Induction and Recursion
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
ICS 253: Discrete Structures I
7
Induction and Recursion
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???
Inductive Hypothesis
• As a rule of inference:
[P(1)  n (P(n)  P(n+1))]  n P(n)
8
ICS 253: Discrete Structures I
Examples
n
• Prove that  i 
i 1
Proof:
n  n  1
2
Induction and Recursion
ICS 253: Discrete Structures I
9
Induction and Recursion
Examples
• Prove that the sum of the first n odd positive
integers is n2
10
ICS 253: Discrete Structures I
Examples
• Prove that n < 2n n
Induction and Recursion
ICS 253: Discrete Structures I
11
Examples
• Prove that 2n < n! for n  4
Induction and Recursion
ICS 253: Discrete Structures I
12
Induction and Recursion
Examples
• Prove that n3 – n is divisible by 3 n Z+
13
ICS 253: Discrete Structures I
Induction and Recursion
Examples
• Use mathematical induction to show that
n
n=0,1,2,…
H n  1 ,
2
2
1 1
1
where H k  1    ... 
2 3
k
ICS 253: Discrete Structures I
14
Induction and Recursion
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.
15
ICS 253: Discrete Structures I
Induction and Recursion
Examples
• Use mathematical induction to prove the
following generalization of one of De Morgan's
laws:
n
j 1
Aj 
n
Aj
j 1
whenever A1, A2 , . . . , An are
subsets of a universal set U and n  2.
ICS 253: Discrete Structures I
16
Induction and Recursion
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
17
ICS 253: Discrete Structures I
Induction and Recursion
Sketch of The Solution
Basis Step:
Subsquare with ‘artificial’
hole at lower right corner
Subsquare with ‘artificial’
hole at lower left corner
Induction
Step:
Inductive
Hypothesis
Subsquare having
the real hole
Subsquare with
‘artificial’ hole at upper
left corner
ICS 253: Discrete Structures I
18
Induction and Recursion
Section 5.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.
ICS 253: Discrete Structures I
19
Induction and Recursion
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
ICS 253: Discrete Structures I
20
Induction and Recursion
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?
ICS 253: Discrete Structures I
21
Induction and Recursion
Examples
• Show that if n is an integer greater than 1, then
n can be written as the product of primes.
ICS 253: Discrete Structures I
22
Induction and Recursion
Examples
• Prove that every amount of postage of 12
cents or more can be formed using just 4-cent
and 5-cent stamps.
ICS 253: Discrete Structures I
23
Induction and Recursion
Well Ordering Property
• Mathematical induction follows from the
following fundamental axiom
The Well Ordering Property: Every nonempty set of nonnegative integers has a least
element
• The well ordering property can often be used
directly in proofs.
ICS 253: Discrete Structures I
24
Induction and Recursion
Example
• In a round-robin tournament every player
plays every other player exactly once and
each match has a winner and a loser. We say
that the players p1, p2, . . . , pm form a cycle
if p1 beats p2, p2 beats p3, . . . , pm−1 beats
pm, and pm beats p1. Use the well-ordering
principle to show that if there is a cycle of
length m (m ≥ 3) among the players in a
round-robin tournament, there must be a cycle
of three of these players.
ICS 253: Discrete Structures I
25
Induction and Recursion
Section 5.3: Recursive Definitions and
Structural Induction
• 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.
ICS 253: Discrete Structures I
26
Induction and Recursion
Recursive Definitions and Structural Induction
• Recursion can be used to define sequences,
functions and sets
n
• Sequences: an  2 can also be defined as
n 0
1
an  
2an 1 n  1
• Functions: Similar to sequences
• After all, sequences are, themselves, functions!
• Sets
ICS 253: Discrete Structures I
27
Induction and Recursion
Recursively Defined Functions
• Q2 pp 351: 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).
ICS 253: Discrete Structures I
Induction and Recursion
28
Recursively Defined Functions
• Give an inductive definition of the factorial
function F(n) = n!.
n
• Give a recursive definition of
a
k 0
k
ICS 253: Discrete Structures I
29
Induction and Recursion
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
1 5

2
ICS 253: Discrete Structures I
30
Induction and Recursion
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.
ICS 253: Discrete Structures I
31
Induction and Recursion
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.
ICS 253: Discrete Structures I
32
Induction and Recursion
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}?
ICS 253: Discrete Structures I
33
Induction and Recursion
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
ICS 253: Discrete Structures I
34
Induction and Recursion
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).
ICS 253: Discrete Structures I
35
Induction and Recursion
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 wellformed 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.
ICS 253: Discrete Structures I
36
Induction and Recursion
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. .
ICS 253: Discrete Structures I
37
Building Up Rooted Trees
Induction and Recursion
ICS 253: Discrete Structures I
38
Induction and Recursion
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.
ICS 253: Discrete Structures I
39
Building Extended Binary Trees
Induction and Recursion
ICS 253: Discrete Structures I
40
Induction and Recursion
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 .
ICS 253: Discrete Structures I
41
Induction and Recursion
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))
ICS 253: Discrete Structures I
Induction and Recursion
42
Full Binary Trees
• Theorem: If T is a full binary tree, then
n (T )  2
h (T ) 1
1
Proof (By Structural Induction):
ICS 253: Discrete Structures I
43
Induction and Recursion
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.
ICS 253: Discrete Structures I
44
Induction and Recursion
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;
Induction and Recursion
45
ICS 253: Discrete Structures I
Example
• Suppose that am,n is defined recursively for
(m, n)  N  N
am ,n
0

 am 1,n  1
a
 m ,n 1  n
if m  n  0
if n  0 and m  0
if n  0
Show that am,n = m + n(n + 1)/2 for all
(m,n)N  N