110 likes | 123 Views
This article provides a correctness proof for the recursive Fibonacci number algorithm using mathematical induction. It explains how the algorithm accurately returns the Fibonacci number for a given input.
E N D
Design & Analysis of Algorithm CSG3F3 Correctness proofs: RECURSIVE
Correctness proof of recursive Fibonacci number: F0=0, F1=1, and for all n2, Fn=Fn-1 + Fn-2 Function fib(n) Comment return Fn 1. If n 1 then return (n) 2. Else return (fib(n-1)+fib(n-2)) RMB/correctness proof
Claim: for n0, fib(n) Fn Base: n=0 fib(n) =0, n=1 fib(n) =1 Induction: Suppose n2 and for all 0m<n, fib(m) =Fm (Berdasarkan klaim algoritma) RTP fib(n) returns Fn. What does fib(n) return ? RMB/correctness proof
fib(n) = fib(n-1) + fib(n-2) = Fn-1 + Fn-2 = Fn RMB/correctness proof
Correctness proof of recursive Recursive maximum function maximum(n) // comment return max of A[1..n] • if n<=1 then return A[1] else • return max(maximum(n-1),A[n]) RMB/correctness proof
Claim: n1, maximum(n) max{A[1], A[2],…,A[n]}. Proof by induction on n1. Base: n=1, maximum(n) returns A[1] Induction: Suppose n1 & maximum(n) returns max{A[1], A[2],…,A[n]}. RTP Maximum(n+1) max{A[1], A[2],…,A[n+1]} RMB/correctness proof
What does maximum(n+1) return ? Maximum(n+1) = max(maximum(n),A[n+1]) = max(max{A[1], A[2],…,A[n]},A[n+1]) = max{A[1],…,A[n+1]} RMB/correctness proof
Exercise #1 Prove that the following recursive algorithms are correct. Function sum(n) comment return sum of A[1..n] • if n 1 then return (A[1]) else • return (sum(n-1)+A[n]) RMB/correctness proof
Exercise #2 Factorial: Fact0=1, Fact1=1, and for all n2, Factn=n*Factn-1. Function factorial(n) comment return Factn • if n1 then return 1 else • else return (n.factorial(n-1)) RMB/correctness proof
Exercise #3 Function g(n): G0=0, G1=1. For all n2, Gn= 5. Gn-1 6. Gn-2 Function g(n) comment return the value of 3n-2n for all n0 • if n1 then return n • else return (5.g(n-1)-6.g(n-2)) RMB/correctness proof
Reference • Standish, Thomas A. Data structures, Algorithms, & Software Principles in C. Addison wesley publishing company. 1995 RMB/correctness proof