Download presentation
Presentation is loading. Please wait.
Published byPierce Hill Modified over 8 years ago
1
Design & Analysis of Algorithm CSG3F3 Correctness proofs: RECURSIVE
2
Correctness proof of recursive Fibonacci number: F 0 =0, F 1 =1, and for all n 2, F n =F n-1 + F n-2 Function fib(n) Comment return F n 1. If n 1 then return (n) 2. Else return (fib(n-1)+fib(n-2)) 13/2/07 RMB/correctness proof2
3
13/2/07 RMB/correctness proof3 Claim: for n 0, fib(n) F n Base: n=0 fib(n) =0, n=1 fib(n) =1 Induction: Suppose n 2 and for all 0 m<n, fib(m) =F m (Berdasarkan klaim algoritma) RTP fib(n) returns F n. What does fib(n) return ?
4
fib(n) = fib(n-1) + fib(n-2) = F n-1 + F n-2 = F n 13/2/07 RMB/correctness proof4
5
13/2/07 RMB/correctness proof5 Correctness proof of recursive Recursive maximum function maximum(n) // comment return max of A[1..n] 1.if n<=1 then return A[1] else 2.return max(maximum(n- 1),A[n])
6
13/2/07 RMB/correctness proof6 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]}
7
13/2/07 RMB/correctness proof7 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]}
8
13/2/07 RMB/correctness proof8 Exercise #1 Prove that the following recursive algorithms are correct. Function sum(n) comment return sum of A[1..n] 1.if n 1 then return (A[1]) else 2.return (sum(n-1)+A[n])
9
Claim For n>=1, sum(n) returns sum{A[1..n]}. Proof by induction on n>=1. Base n=1, sum(n) returns A[1]. Induction Suppose n>=1 dan sum(n) returns sum{A[1..n]}. RTP sum(n+1) returns sum{A[1..n+1]}. 13/2/07 RMB/correctness proof9
10
What does sum(n+1) return ? sum(n+1)=sum(n)+A[n+1] = sum{A[1..n]}+A[n+1] = sum{A[1..n+1]} 13/2/07 RMB/correctness proof10
11
13/2/07 RMB/correctness proof11 Exercise #2 Factorial: Fact 0 =1, Fact 1 =1, and for all n 2, Fact n =n*Fact n-1. Function factorial(n) comment return Fact n 1.if n 1 then return 1 else 2.else return (n.factorial(n-1))
12
Claim For all n>=0, factorial(n) returns Fact n. Base n<=1, factorial(n)=1. Induction Suppose n 2 and for all 0 m<n, factorial(m) returns 1 Fact m. RTP factorial(n) returns Fact n. 13/2/07 RMB/correctness proof12
13
Factorial(n) = n.factorial(n-1) = n.Fact n-1 = Fact n 13/2/07 RMB/correctness proof13
14
13/2/07 RMB/correctness proof14 Exercise #3 Function g(n): G 0 =0, G 1 =1. For all n 2, G n = 5. G n-1 6. G n-2 Function g(n) comment return the value of 3 n -2 n for all n 0 1.if n 1 then return n 2.else return (5.g(n-1)- 6.g(n-2))
15
Claim For all n 0, g(n) returns 3 n 2 n Base n=0 returns g(n)=0, n=1 returns g(n)=1. Induction Suppose n 2 and for all 0 m n, g(m) returns G m. RTP g(n) returns G n. What does g(n) return ? 13/2/07 RMB/correctness proof15
16
g(n) = 5.g(n-1)-6.g(n-2) = 5.G n-1 6.G n-2 = G n 13/2/07 RMB/correctness proof16
17
13/2/07 RMB/correctness proof17 Reference Standish, Thomas A. Data structures, Algorithms, & Software Principles in C. Addison wesley publishing company. 1995
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.