DSA2 Chapter 1 Math Revision
DSA2 Chapter 1 Math Revision
1
Course Outline
• Mathematical Foundations
– Log function
– Exponentionals
• Proof By Induction
– Sum of squares
– Fibonacci numbers
• Proof By Counter Example
• Proof By Contradiction
• Recursion
• Recursion vs Iteration 2
Mathematical Foundations
– Needed for the analysis of algorithms complexities.
(geometric series):
1 + r+ r2 + r3 +………rN-1 = (1- rN)/(1-r)
1/(1-r), r < 1, large N
Sum of squares:
1 + 22 + 32 +………N2 = N(N + 1)(2N + 1)/6
3
Properties of a log Function
logxa = b iff xb = a
(we will use base 2 mostly, but may use other bases
occasionally)
am+n = am an
5
Proof By Induction
8
Sum of Squares
Now we show that
1 + 22 + 32 +………n2 = n(n + 1)(2n + 1)/6
1(1+1)(2+1)/6 = 1
Thus the property holds for n = 1 (base case)
10
Fibonacci Numbers
Sequence of numbers, F0 F1 , F2 , F3 ,…….
F0 = 1, F1 = 1
Fi = Fi-1 + Fi-2
F2 = 2
F3 = 3
F4 = 5
F5 = 8 11
Prove that Fn+1 < (5/3)n+1
Base case: F2 = 2 < (5/3 )2
Let the property hold for 1,…k
Thus Fk+1 < (5/3)k+1 ? Fk < (5/3)k
Fk+2 = Fk + Fk+1 ,
< (5/3)k + (5/3)k+1
= (5/3)k (5/3 + 1)
< (5/3)k (5/3)2
12
Proof By Counter Example
• Want to prove something is not true!
• Give an example to show that it does not
hold!
• Example: Is FN N2 ?
No, F11 = 144 > 121!
• However, if you were to show that FN N2
then you would need to show for all N, and
not just one number. 13
Proof By Contradiction
• Suppose you want to prove something.
• Assume that what you want to prove does not
hold.
• Then show that you arrive at an impossibility.
• Example: The number of prime numbers is not
finite!
14
Suppose the number of primes is finite, k.
The primes are P1, P2 , … , Pk
The largest prime is Pk
Consider the number N = 1 * P1 * P2 * ….. * Pk
N is larger than Pk , thus N is not prime (hypothesis)
So N must be the product of some primes.
However, none of the primes P1, P2 , … , Pk divide
N exactly. So N is not a product of primes.
(contradiction) 15
Recursion
• A subroutine which calls itself, with different
parameters.
• Need to evaluate factorial(n)
factorial(n) = n.(n-1)…2.1
= n factorial(n-1)
• Suppose routine factorial(p) can find factorial of
p for all p < m. Then factorial(m+1) can be
calculated as follows:
factorial(m+1) = (m+1) factorial(m)
Anything missing?
16
Factorial(m)
{
If m = 1 Factorial(m) = 1
else Factorial(m) = m Factorial(m-1);
}
Basic rules of Recursion :
• There should be a base case for which the subroutine
does not call itself.
• For the general case: the subroutine does some
operations, calls itself, gets result and does some
operations with the result
• The subroutine should progressively move towards the
17
base case.
Printing numbers digit by digit
• We wish to print out a positive integer, n. Our routine
will have the heading printOut(n). Assume that the
only I/O routine available printDigit(m) will take a
single-digit number and outputs it.
void printOut( int n ) // Print nonnegative n
{
if( n >= 10 )
printOut( n / 10 );
printDigit( n % 10 );
}
See proof of algorithm correctness in the textbook. 18
Recursion Versus Iteration
• Factorial of n (n>0) can be iteratively computed
as follows:
factorial = 1
for j=1 to n
factorial factorial j
• Compare to the recursive version.
• In general, iteration is more efficient than
recursion because of maintenance of state
information. 19
Towers of Hanoi