COSC 600: Advanced Data Structure and Algorithm Lecture Note - 1 (Chapter 1) In order to analyze a given algorithm, we need to have some mathematical background. Some of formulas that aid in analysis are as follows 1) Exponents Definition The base X raised to power N is equal to product of X multiplied N many times. The power N in this case is exponent X N = X · X · X ·....· X Exponent Rules X A.X B = X A+B for example 25.2 7 = 25+7 = 212 (X A )B = X A.B for example (25 )3 = 215 X N + X N = X N+1 for example 25 + 25 = 26 2) Logarithms Definition X A = B if and only if log X B = A Theorem 1.1 log A B = logC B logC A where A, B, C > 0, A ¹1 Theorem 1.2 log AB = log A + log B where A, B > 0 3) Series Let A1, A2 , A3,........., AN be a sequence then the sum of the sequence is called series denoted as N åA = A + A i 1 2 + A3 +......... + AN i=1 The easiest formula to remember is N å2 = (21 + 2 2 + 23 + 2 4 +..... + 2 N ) = 2 N+1 -1 i i=1 N if å2 i = (2 0 + 21 + 2 2 +..... + 2 N ) = 2 N+1 i=0 The companion formula is N åA i = (A1 + A 2 + A3 + A 4 +..... + A N ) = i=1 if 0 < A < 1, A N+1 -1 1 = A -1 1- A N å Ai = lim lim N®¥ N®¥ i=1 A N+1 -1 where A ¹1 Þ A -1 Important formulas used for analysis N N(N +1) N 2 1) åi =1+ 2 + 3.... + N = » 2 2 i=1 2) N(N +1)(2N +1) N 3 » åi =1 + 2 + 3 +.... + N = 6 3 i=1 3) N(N +1) 2 N 4 åi =1 + 2 + 3 +.... + N = ( 2 ) » 4 i=1 N 2 2 2 2 2 N 3 3 3 3 3 N 4) N k+1 åi =1 + 2 + 3 +.... + N » | k +1| where k ¹ -1 i=1 k k N-1 for example åi = i=1 for example k k k (N -1)(N -1+1) N(N -1) = 2 2 N N N N N i=1 i=1 i=1 i=1 i=1 å(2i -1) = 1+ 3+ 5 +.... + (2N -1) = å 2i - å1 = 2åi - å1 = 2 · if N = 100 100 å(2i -1) = (1+ 3+ 5 +.... +199) = 10, 000 i=1 N(N +1) - N = N2 2 Rules Rule 1 N N N N i=1 i=1 i=1 i=1 å f (N ) = f (N )· å1 = N · f (N ) ¹ å f (i) ¹ f (i)å1 = Nf (i) Rule 2 N N 0 -1 N 0 -1 i=N 0 i=1 i=1 å f (i) = å f (i) - å f (i) 100 for example å i = (10 +11+12 +.... +100) = (1+ 2 +.... +10 +11+....100) - (1+ 2 +.... + 9) i=10 100 9 i=1 i=1 = åi - åi = N 100.101 10.11 2 2 å i = (1+ 2 + 2 1 1 1 2 i=1 + 1 1 +.... + N ) » log e N 3 2 2 for example 2 + 7 +12 +17 + 22 +.... + (5N + 2) N = å (5i + 2) i=0 N = å (5i + 2) + 2 i=1 N N i=1 i=1 N+1 = å (5i - 3) i=1 N+1 N+1 i=1 i=1 = 5å i - 3å1 5(N +1)(N + 2) - 3(N +1) 2 = 5å i + 2å1 + 2 = 5N(N +1) + 2N + 2 2 5N 2 + 5N + 4N + 4 = 2 2 5N + 9N + 4 = 2 5N 2 +15N +10 - 3N - 3 2 5N 2 +15N +10 - 6N - 6 = 2 2 5N + 9N + 4 = 2 = = 4) Induction Method Principle of Mathematical Induction Let P be a property of positive integers such that 1) Base Case: P (1) is true, and 2) Inductive Step: if P (n) is true, then P (n+1) is true. Then P (n) is true for all positive integers. The premise P (n) in the inductive step is called Induction Hypothesis. Steps for proof by Induction Method 1) Base condition: prove the theorem for base condition. For example N=1 2) Assume that the given theorem is true for N=k case 3) Using assumption, prove the theorem is true for N=k+1 case step by step Example I N For example, proof of åi = i=1 Proof N(N +1) for any positive integer N 2 Base condition: For N=1 1 åi = 1 = L.H.S i=1 1(1+1) =1 = R.H.S 2 k N(N +1) k(k +1) Assume that åi = for N = k Þ åi = 2 2 i=0 i=1 N For N=k+1 case, k+1 åi = (1+ 2 +.... + k + (k +1)) i=1 k(k +1) + (k +1) 2 k = (k +1)( +1) 2 k+2 = (k +1)( ) 2 (k +1)(k + 2) = 2 = Þ Theorem is true for n=k+1 case Þ Theorem is true for any positive integer N Example II For example, proof of Fibonacci number Let Fi be Fibonacci number (F0 =1, F1 =1, F2 = 2, F3 = 3, F4 = 5,...., Fi = Fi-1 + Fi-2 ) Prove Fi < ( 5 3 )i for i ³1 Proof Base condition: For i=1 case, F1 =1 < 5 3 is true Assume that Fk < ( 5 )k for any k 3 For i=k+1 case, Fk+1 = Fk-1 + Fk < ( 5 )k-1 + ( 5 )k 3 3 k-1 = ( 5 ) (1+ 5 ) 3 3 k-1 = (5 ) (8 ) 3 3 9 8 = ( 5 )k+1 ( ´ ) 3 25 3 k+1 = ( 5 ) ( 3 )2 .( 8 ) 3 5 3 5 28 5 = ( )k+1 ( ) < ( )k+1 3 25 3 i = k +1 case is true 5) Proof by Counterexample This method of proof involves giving an example that fails the stated theorem, and hence, disproves the theorem. For example, let Fi is Fibonacci number Fk £ k 2 is false F0 =1, F1 =1, F2 = 2, F3 = 3,..... Proof: F11 =144 £121 is not true 6) Proof by Contradiction This method of proof begins with the assumption that the stated theorem is false, and during the process shows that the assumption was wrong and, hence proves the stated theorem. For example, Theorem: There is an infinite number of prime. e.g. 2,3,5,7,13,17,19,… Proof: Assume that there is finite number of prime Let, Pk be the largest prime Let, N = P1 ·P2 ·P3 ·........·Pk +1 None of P1, P2 , P3........, Pk divide N exactly ( there will be a remainder of 1) Þ N is a prime number Þ N> Pk ( Pk is not the largest prime which is contradiction to initial assumption that Pk is the largest prime) Þ Theorem is true i.e. there is an infinite number of prime 7) Recursion Any function that is defined in terms of itself is called recursive function. For example, Divide & Conquer Like Merge Sort for traversing an array, whole array is divided in half, and, then each half is further divided into half, until search item is found. Rules of recursion: 1. Base cases-must always have some base case that can be solved without recursion 2. Making progress-must always to be a case that makes a progress toward a base case Example I For example, consider following program Public static int f (int n) { if (n == 0) return 0; else return f (n/3 +1) + n-1; } If we pass value of n as 10, the program will return f (4)+9, here we do not know what is f (4) so, the program gets called again with value of n= 4. The program now returns f (2)+3, the program gets called again with value of n=2 to yield f (1)+2. The program gets called again with n=1 to yield f (1)+0. The program gets called again and again with n=1. This is bad example. To make this example good one we can change if (n==0) to if (n==1). F (10) F (4) +9 F (2) +3 F (1) +2 F (1) + 0 Example II Consider another example, Hanoi Tower Problem. This problem involves moving different size disk from source to destination tower with temporary tower in between following condition that bigger size disk cannot be placed over smaller as it will crush smaller disk. In this case, the number of movements needed to move disks from source, to destination along with movements to and from temporary tower increases with increase in number of disks and is given by formula N (number of disk movements)= 2 k -1(k is number of disc). A solution to the problem is as follows T (n, a, b, c) //n= number of disc, a=source tower, b=temporary tower, c= destination tower { if (n=1) { move disk from source to destination } else { } } T (n-1, A, C, B) Move nth disc from source A to destination C T (n-1, B, A, C) References 1) LERMA, M. (2003, February 8). MATHEMATICAL INDUCTION. Retrieved September 6, 2014, from http://www.math.northwestern.edu/~mlerma/problem_solving/results/in duction.pdf