MA THEMA TICAL INDUCTION � Induction and Deduction � Mathematical Induction (its strength) � Examples { Sum of �rst n naturual numb ers Prove b y induction that the sum of the �rst n natural numb ers is n ( n + 1) = 2. i.e. n X n � ( n + 1) S ( i ) : i = 2 i =1 { Sum of p o w ers of t w o Prove b y induction that the sum of the �rst n n +1 p o w ers of t w o (0..n) is 2 � 1. i.e. n X i n +1 S ( i ) : 2 = 2 � 1 i =0 k x { nth derivative of e k x Prove b y induction that the nth derivative of e n k x is k e . i.e. n d k x n k x S ( n ) : e = k e n dx 1
� Arithmetic and Geometric p rogressions. { Derivations of general fo rms { Pro ofs b y induction { An exp ression fo r the sum of the �rst n o dd numb ers and p rove it using Mathematical In- duction. { Pro cedure fo r deriving an exp ression fo r the sum P n of a p olynomial series: p ( n ) where p ( n ) k k i =0 k k � 1 is a p olynomial in n of o rder k : a n + a n + 1 2 � � � + a k � General template fo r Induction p ro ofs: � One mo re example: Erro r co rrecting co des S(n): C is any set of bit strings of length n that n n � 1 is erro r detecting , then C contains at most 2 n strings. { Assume S(n) { Divide C into t w o sets of strings 1x and 0y n +1 { x & y obviously b e erro r detecting themselves. n � 1 { By hyp othesis, x/y contain no mo re than 2 strings. { Thus maximum total numb er of strings is 2 � n � 1 n +1 � 1 2 = 2 2
Complete (strong) Vs W eak induction � Complete (o r strong) induction: Uses t w o o r mo re assumptions from the basis up to n . � Examples: Prove that there exist integers a and b such that 8 n 2 Z \ n > 0 : n = 2 a + 3 b . Prove that there exist no integers x; y ; z such that n n n 8 n 2 Z \ n > 2 : x + y = z . � F allacies and getting ca rried a w a y with induction n (All ma rbles a re red, 8 n 2 Z : a = 1) � Structural induction: Proving p rop erties ab out a structure b y using a basis case and an inductive assumption. � Examples: In a colony of aphids rep ro ducing asexually , let the status of an aphid b e rep resented b y the numb er of its children. Sho w that the total numb er of aphids in the colony is one mo re than the sum of the sta- tuses of all the aphids. Assume that a pa rticula r implementation of Bina ry trees uses the No de with right and left child p oint- ers. In such a bina ry tree, p rove that the numb er of NULL p ointers is one mo re than the total numb er of no des. 3
Proving Program Prop erties What a re lo op inva riants? A Selection So rt p rogram: (1) for (i = 0; i < n-1; i++) { (2) small = i; (3) for (j = i+1; j < n; j++) (4) if (A[j] < A[small]) (5) small = j; (6) swap(&A[small], &A[i]); (7) } S ( k ): If w e reach the test fo r j < n on line (3) with j = k then smal l indexes smallest of A [ i::k � 1]. 4
Basis: S ( i + 1): smal l indexes smallest of A [ i::i ]. Induction: Pro of of S ( k + 1) assuming S ( k ). S ( k ): When testing fo r j < n at line (3), smal l indexes smallest of A [ i::k � 1]. No w consider what happ ens in the lo op b o dy when j = k , sp eci�cally in the \if " statement of lines (4) and (5). if A [ k ] is less than the smallest of A [ i + 1 ::k � 1] then smal l = k . Then smal l indexes smallest of A [ i + 1 ::k ]. If A [ k ] is not less than the smallest of A [ i + 1 ::k � 1] then the value of small is unchanged. So small will no w b e the index of the smallest of A [ i + 1 ::k ]. Thus, in either case, if the lo op test is reached subse- quently with the value of j incremented from k to k + 1, smal l indexes the smallest of A [ i + 1 ::k ] which p roves S ( k ) holds fo r all values of k � i + 1. Question: what is the truth value of S ( k ) when k > n ) 5
No w consider the whole SelectionSo rt function. The follo wing assertion is made ab out it. T ( m ): If w e reach the test i < n � 1 at line 1, with i = m , then 1. A [0 ::m � 1] a re in non-decreasing o rder. 2. All of A [ m::n � 1] a re greater than o r equal to any of A [0 ::m � 1]. Basis : m = 0. 1. A [0 :: � 1] a re obviously in so rted o rder. 2. All of A [0 ::n � 1] � any of A [0 :: � 1] Induction : Pro of of T(m+1) assuming T(m). Consider what happ ens when i = m . W e kno w (b y the IH) that A [0 ::m � 1] a re in so rted o rder and no element of A [0 ::m � 1] is greater than any element of A [ m::n � 1]. w e exit the lo op of lines (3){(5), the lo op va riable j w ould have the value n and so at that p oint smal l will index the smallest of A [ m::n � 1], b y the p revious induc- tion. 6
No w note t w o things ab out the situation: 1. By the �rst condition of the IH, w e kno w that A [0 ::m � 1] a re already in so rted o rder. 2. By the second condition, w e kno w that smal l is greater than o r equal to any item in A [0 ::m � 1]. Thus, 1. After sw apping A [ m ] with A [ smal l ], incrementing i to m + 1, p ro ceeding a round the lo op, just b efo re the test, w e can assert that A [0 ::m ] is so rted which is condition 1. 2. A [ smal l ] is the smallest of A [ m::n � 1] and that b oth A [ m ] and all elements of A [0 ::m � 1] a re less than o r equal to any element in A [ m + 1 ::n � 1] which is condition 2. Note that when m = n , w e have exited the outer lo op and so, A [0 ::n � 1] a re in so rted o rder. 7
Pro of numb er 2: F acto rial function. (1) int factorial(int n) { (2) int i = 2; (3) int fact = 1; (4) while (i <= n) { (5) fact = fact * i; (6) i++; } (7) return fact; } First { Pro of of termination: Chose E = n � i + 1. Second | Pro of of co rrectness: S ( k ): If w e reach the test i < = n at line (4), with the value of the lo op va riable i set to k , then fact will contain the value ( k � 1)! Basis: The basis is S (2). i = 2 ! f act = 1 b y assign- ment. Induction: Assume S ( k ). Prove S ( k + 1). 8
Proving p rop erties of recursive functions: (1) int fact(int n) (2) if (n <= 1) (3) return 1; (4) else (5) return n * fact(n-1); W e w ant to p rove the follo wing inductive assertion on i . S ( i ) : If the function fact is called with the a rgument i , it returns the value i ! Basis: Consider S (1). Line (2) p erfo rms a test which succeeds and so facto rial returns 1 which is indeed 1! Induction: Assume S ( k ), i.e. fact(k) returns k !. No w consider what will happ en if fact() is called with a rgu- ment k + 1 with k � 1 The test on line (2) fails and so the else pa rt is executed. Thus the call of facto rial returns the p ro duct of ( k + 1) � f act ( k ). Ho w ever, b y the IH, fact(k) will return the value k !. Thus the value returned b y the p resent call will b e ( k + 1) � k ! which b y the de�nition of the facto rial function is ( k + 1)!. QED. 9
Recursive Selection so rt: (1) void recSS(int A[], int i, int n) { (2) if (i < n-1) { (3) small=i; (4) for (j = i; j < n; j++) (5) if (A[j] < A[small]) (6) small = j; (7) swap(&A[small], &A[i]); (8) recSS(A, i+1, n); (9) } (10) } S ( k ): If the function recSS() is called with i = k , then when it returns it will leave A [ k ::n � 1] in non-descending o rder. Basis: S ( n � 1). The IH is fo r S ( k ) where 0 � k < n . W e must sho w that S ( k � 1) holds given S ( k ). 10
Merge So rt: LIST MakeList() { int x; LIST pNewCell; if (scanf(``%d'', &x) == EOF) return NULL; else { pNewCell = (LIST) malloc(sizeof(CELL)); pNewCell->next = MakeList(); pNewCell->element = x; return pNewCell; } } LIST MergeSort(LIST list) { LIST SecondList; if (!list || !list->next) return list; else { SecondList = split(list); return merge(MergeSort(list), MergeSort(SecondList)); } } 11
Recommend
More recommend