1 / 11

Design & Analysis of Algorithm

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.

rhallmark
Download Presentation

Design & Analysis of Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design & Analysis of Algorithm CSG3F3 Correctness proofs: RECURSIVE

  2. Correctness proof of recursive Fibonacci number: F0=0, F1=1, and for all n2, 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

  3. Claim: for n0, fib(n) Fn Base: n=0  fib(n) =0, n=1 fib(n) =1 Induction: Suppose n2 and for all 0m<n, fib(m) =Fm (Berdasarkan klaim algoritma) RTP fib(n) returns Fn. What does fib(n) return ? RMB/correctness proof

  4. fib(n) = fib(n-1) + fib(n-2) = Fn-1 + Fn-2 = Fn RMB/correctness proof

  5. 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

  6. Claim: n1, maximum(n)  max{A[1], A[2],…,A[n]}. Proof by induction on n1. Base: n=1, maximum(n) returns A[1] Induction: Suppose n1 & 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

  7. 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

  8. 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

  9. Exercise #2 Factorial: Fact0=1, Fact1=1, and for all n2, Factn=n*Factn-1. Function factorial(n) comment return Factn • if n1 then return 1 else • else return (n.factorial(n-1)) RMB/correctness proof

  10. Exercise #3 Function g(n): G0=0, G1=1. For all n2, Gn= 5. Gn-1 6. Gn-2 Function g(n) comment return the value of 3n-2n for all n0 • if n1 then return n • else return (5.g(n-1)-6.g(n-2)) RMB/correctness proof

  11. Reference • Standish, Thomas A. Data structures, Algorithms, & Software Principles in C. Addison wesley publishing company. 1995 RMB/correctness proof

More Related