330 likes | 514 Views
Recursion and Induction. Algorithm : Design & Analysis [3]. In the last class …. Asymptotic growth rate The Sets , and Complexity Class An Example: Searching an Ordered Array Improved Sequential Search Binary Search Binary Search Is Optimal. Recursion and Induction.
E N D
Recursion and Induction Algorithm : Design & Analysis [3]
In the last class… • Asymptotic growth rate • The Sets , and • Complexity Class • An Example: Searching an Ordered Array • Improved Sequential Search • Binary Search • Binary Search Is Optimal
Recursion and Induction • Recursive Procedures • Analyzing the Recursive Computation. • Induction over Recursive Procedures • Proving Correctness of Procedures
Thinking Recursively: Problem 1 • Towers of Hanoi • How many moves are need to move all the disks to the third peg by moving only one at a time and never placing a disk on top of a smaller one. T(1) = 1 T(n) = 2T(n-1) +1
Solution of Towers of Hanoi • T(n) = 2T(n-1) + 1 • 2T(n-1) = 4T(n-2) + 2 • 4T(n-2) = 8T(n-3) + 4 • ……. • 2n-2T(2) = 2n-1T(1) + 2n-2 T(n) = 2n-1
Thinking Recursively: Problem 2 • Cutting the plane • How many sections can be generated at most by n straight lines with infinite length. L(0) = 1 L(n) = L(n-1) +n Intersecting all n-1 existing lines to get as most sections as possible Line n
Solution of Cutting the Plane L(n) = L(n-1)+n = L(n-2)+(n-1)+n = L(n-3)+(n-2)+(n-1)+n = …… = L(0)+1+2+……+(n-2)+(n-1)+n L(n) = n(n+1)/2 + 1
Method99 If the task is to write a procedure p for a problem of size 0 to 100, and There exists a ready-to-use subroutine p99, that can provide the solution for the same problem of size 0-99, then Just figure out a way to write p by calling p99 when needed. IntList delete(IntList L, int x) IntList newL, fixedL; if (L==nil) newL=L; else if (x==first(L)) newL=rest(L); else fixedL=delete99(rest(L),x); newL=cons(first(L),fixedL) return newL Recursion: General Pattern
Thinking Recursively: Problem 3 J(1) =1 J(2n) = 2J(n)-1 J(2n+1) = 2J(n)+1 • Josephus’s problem • Find the position to live, or die! 1 3 1 2n+1 1 3 11 5 2 2 10 2n-1 10 3 5 3 7 9 Same situation in new numbers 9 J(2m+l) =2l+1 4 8 4 8 5 7 5 7 6 6
Recursion: an Implementation View • Activation Frame • Basic unit of storage for an individual procedure invocation at run time • Providing a frame of reference in which the procedure executes for this invocation only • Space is allocated in a region of storage called the frame stack • Activation trace
Fibonacci Function: An Example fib n: 3 f1:1 f2:1 f: 2 int fib(int n) int f, f1, f2; if (n<2) f=n; else f1=fib(n-1); f2=fib(n-2); f=f1+f2; return f; fib n: 2 f1:1 f2:0 f: 1 fib n: 1 f1: f2: f: 1 fib n: 1 f1: f2: f: 1 fib n: 0 f1: f2: f: 0 Activation Tree Activation frame creation is in preorder
Loop-free Complexity • In a computation without while or for loops, but possibly with recursive procedure calls, • The time that any particular activation frame is on the top of the frame stack is O(L), where L is the number of lines in the procedure that contain either a simple statement or a procedure call. (Note: L is a constant) • The total computation time is (C), where C is the total number of procedure calls that occur during the computation. (Note: for an algorithm, max{Li} is a constant.)
Valid over any set if: The set is partially ordered. There is no infinite chain of decreasing elements in the set. Structural Induction A F B E C D B E A C B A A D C B C A B C D E F Base cases
2-Tree Common Binary Tree 2-Tree internal nodes Both left and right children of these nodes are empty tree external nodes no child any type
External Path Length • The external path length of a 2-treet is defined as follows: • The external path length of a leaf, which is a 2-tree consisting of a single external node, is 0 • If t is a nonleaf 2-tree, with left subtree L and right subtree R, then the external path length of t is the sum of: • the external path length of L; • the number of external node of L; • the external path length of R; • the number of external node of R; • In fact, the external path length of t is the sum of the lengths of all the paths from the root of t to any external node in t.
Calculating the External Path Length EplReturn calcEpl(TwoTree t) EplReturn ansL, ansR; EplReturn ans=new EplReturn(); 1. if (t is a leaf) 2. ans.epl=0; ans.extNum=1; 3. else 4. ansL=calcEpl(leftSubtree(t)); 5. ansR=calcEpl(rightSubtree(t)); 6. ans.epl=ansL.epl+ansR.epl+ansL.extNum +ansR.extNum; 7. ans.extNum=ansL.extNum+ansR.extNum 8. Return ans; TwoTree is an ADT defined for 2-tree EplReturn is a organizer class with two field epl and extNum
Correctness of Procedure calcEpl • Let t be any 2-tree. Let epl and m be the values of the fields epl and extNum, respectively, as returned by calcEpl(t). Then: • 1. epl is the external path length of t. • 2. m is the number of external nodes in t. • 3. eplmlg(m) (note: for 2-tree with internal n nodes, m=n+1)
Proof on Procedure calcEpl • Induction on t, with the “subtree” partial order: • Base case: t is a leaf. (line 2) • Inductive hypothesis: the 3 statements hold for any proper subtree of t, say s. • Inductive case: by ind. hyp., eplL, eplR, mL, mR,are expected results for L and R(both are proper subtrees of t), so: • Statement 1 is guranteed by line 6 • Statement 2 is guranteed by line 7 (any external node is in either L or R) • Statement 3: by ind.hyp. epl=eplL+eplR+mmLlg(mL)+mRlg(mR)+m, note f(x)+f(y)2f((x+y)/2) if f is convex, and xlgxis convex for x>0, so, epl 2((mL+mR)/2)lg((mL+mR)/2)+m = m(lg(m)-1)+m =mlgm.
if (condition) block1 THEN f(x,y) block z=f(x1,x2) true- block false- block block2 sequence alternation procedure call Elementary Control Structures ELSE Each structure has its special correctness lemma form: Preconditions vs. Postconditions
Proving Correctness: Case 1 Block precondition implies Block 1 precondition implies Block 1 postcondition implies Block 2 precondition implies Block 2 postcondition implies Block postcondition block1 block2 sequence
Proving Correctness: Case 2 Block precondition And (condition) And not(condition) implies implies if (condition) True-block precondition False-block precondition THEN implies implies true- block false- block False-block postcondition True-block postcondition implies implies Block precondition alternation
Proving Correctness: Case 3 Block precondition implies Preconditions of the called procedure with its actual parameters f(x,y) z=f(x1,x2) implies Preconditions of the called procedure with its actual parameters implies Block postcondition procedure call
Single-Assignment Paradigm • Goto statement and assignment statement are major difficulties in correctness proving. • Single-assignment paradigm • A variable, after its creation, may receive only one assignment; • Being applied to local variable of loop-free code; • To achieve single-assignment by defining new additional local variables • Not practical for using with array or objects having instance fields that need to be updated.
Example of Single-Assignment y=y+1 y1=y+1 All later references to y in this branch must be changed to y1 x=y+1; y1=z x=y+1; y=z 1. if (y<0) 2. y1=0; 3. else 4. y1=y 5. x=2*y1 1. if (y<0) 2. y=0; 3. x=2*y
Sequential Search Input: an unordered array E with n entries, a key K to be matched Output: the location of K in E (or fail) Procedure: Int seqSearch(int[] E, int n, int K) int ans, index; ans=-1; for (index=0; index<n; index++) if (K==E[index]) ans=index; break; Return ans; Loop-Free int seqSearchRec(int[] E, int m, int num, int K) int ans 1. if (mnum) 2. ans=-1; 3. elseif (E[m]==K) 4. ans=m; 5. else 6. ans=seqSearchRec(E,m+1,num,K); 7. return ans; (Top-level call: ans=seqRearchRec(E,0,num,K) Conversion to Loop-Free
Correctness of seqSearchRec • Preconditions: • m0. • For m i num, E[i] is initialized. • Postconditions: • If ans=-1, then for m i num E[i]K. • If ans-1, then mansnum and E[ans]=K. • Proving seqSearchRec using correctness lemma form for Alternation: • Line 2 reached: mnum and ans=-1; • Line 4 reached: ans=m and E[ans]=K; • Line 6 reached: using correctness lemma form for Procedure Call. (Only proved is “Whenever line 7 is reached…) Note: ans is single-assigned.
int factLoop(int n) int k,f; 1. k=1; 2. f=1; 3. while (kn) 4. { 5. int fnew=f*k; 6. int knew=k+1; 7. k=knew; f=fnew; 8. } 9. return f; int fact(int n) 9. return factRec(n,1,1) int factRec(int n, int k, int f) 3a. if (k>n) 3b. ans=f; 4. else 5. int fnew=f*k; 6. int knew=k+1; 7. ans=factRec(n,knew,fnew); return ans; Procedures with Loops passive parameter active parameters terminator updated value Preprocessing: 1. following single-assignment as possible 2. updating values at the end of the loop
Total Correctness • “If all preconditions hold when the block is entered, then all postconditions hold when the block exit.” is partial correctness. • Termination arguments: the structure or “problem size” being passed to the recursive call is small than(in the sense of partial order) that of the caller.
Binary Search: Review int binarySearch(int[] E, int first, int last, int K) 1. if (last<first) 2. index=-1; 3. else 4. int mid=(first+last)/2 5. if (K==E[mid]) 6. index=mid; 7. else if (K<E[mid]) 8. index=binarySearch(E, first, mid-1, K) 9. else 10. index=binarySearch(E, mid+1, last, K) 11.return index;
Binary Search: Correctness • Correctness of binarySearch: • For all n0, if binarySearch(E, first, last, K) is called, and if: • the problem size is (last-first+1)=n • E[first], …, E[last] are in nondecreasing order • then: • it returns -1 if K does not occur in E within the range first, …, last • it returns index if K=E[index]
Binary Search: Proof of Correctness • Induction on n, the problem size. • Base case: n=0, line 2 excuted, returning -1. • For n>0(that is: firstlast), assume that binarySearch(E,f,l,K) satisfies the correctness on problems of size k(k=0,1,2,…n-1), and k=l-f+1. • mid=(first+last)/2 must be within the search range, so: • if line 5 is true, line 6 executed. • otherwise, the induction hypothesis applies for both recursive calls, on line 8 and 10. What we have to do is to verify that the preconditions for the procedure hold, and return value of the recursive call satisfies the postconditions of the problem.
Proving Binary Search (cont.) • Case that line 5 is false, i.e. KE[mid] • Since firstmidlast, we have • (mid-1)-first+1n-1 • Last-(mid+1)+1n-1 • So, the inductive hypothesis applies for both recursive calls on line 8 and 10 • For both calls, only one actual parameter is changed and decreased, so, line 8 or line 10 will return the appropriate results
Home Assignments • pp.141- • 3.3 • 3.4 • 3.6