Академический Документы
Профессиональный Документы
Культура Документы
Correctness of Algorithms.
Efficiency of Algorithms
Correctness of Algorithms
Partial correctness
IF this point is reached,
Output
Algorithm
Total correctness
INDEED this point is reached,
Algorithm
Output
Assertions
To prove partial correctness we associate a
number of assertions (statements about the state
of the execution) with specific checkpoints in the
algorithm.
Loop Invariants
Invariants assertions that are valid any time
they are reached (many times during the
execution of an algorithm, e.g., in loops)
We must show three things about loop invariants:
A[1j] sorted
Summations
2
(
j
1
)
=
O
(
n
)
j =2
10
Proof by Induction
We want to show that property P is true for all
integers n n0
Basis: prove that P is true for n0
Inductive step: prove that if P is true for all k
such that n0 k n 1 then P is also true for n
Example
n
n(n + 1)
S ( n) = i =
for n 1
2
i =0
Basis
1(1 + 1)
S (1) = i =
2
i =0
1
11
12
Solution
13
Proof:by induction on n.
Base Case: Let n = 1. When SUM-ODD(n) is called with n =
1, the if condition is true, thus, SUM-ODD(n) returns 1. Also,
Inductive Conclusion (to show): Assume that SUMODD(k+1) returns a value equal to
14
Inductive Step: First note that k >1. Thus, the else case is
executed.
15
Binary Search
16
17
be true initially:
Pre: a b + 1 and A[a..b] is a sorted array
found = BinarySearch(A, a, b, x);
The postcondition states what is to be true about
the result
Post: found = x A[a..b] and A is unchanged
DSA lecture 7 - T.U.Cluj-Napoca - M. Joldos
18
Base case: n = b a + 1 = 0
The array is empty, so a = b + 1
The test a > b succeeds and the algorithm correctly
returns false
19
If
If
20
Summing an Array
Problem: Given an array of numbers A[a..b] of
size n = b a + 1 0, compute their sum.
// Pre: a b + 1
1 i a, sum 0
2 while i b + 1 do // exit condition, called guard G
3 sum sum + A[i]
4 ii+1 b
A[ j ]
// Post: sum =
j =a
21
Guess a condition I
Prove by induction that I is a loop invariant
Prove that I G Postcondition
Prove that the loop is guaranteed to terminate
22
Initialized to i = a and
sum = 0. Therefore
i 1
A[ j ] = 0
sum = A[ j ]
j =a
Base Case: k = 1
j =a
Inductive hypothesis:
Assume at the start of
the loops k-th
execution
i 1
sum = A[ j ]
j =a
23
i ' 1
j =a
j =a
j =a
24
sum =
b +11
j =a
j =a
A[ j ] = A[ j ] Postcondition
25
Summary on Correctness
Induction
Proving an algorithm:
Precondition
Postcondition
26
Efficiency of algorithms
Algorithms for solving the same problem can differ
dramatically in their efficiency.
Much more significant than the differences due to
hardware and software.
Comparison of two sorting algorithms (n=106 numbers):
27
Substitution method
Recursive tree method
Master method (master theorem)
Provides bounds for: T(n) = aT(n/b)+f(n) where
28
By recursion tree.
29
Solve T(n)=2T(n/2)+n
Guess the solution: T(n)=O(nlg n),
i.e., T(n)
cnlg n for some c.
30
31
Revise guess
Guess is correct, but induction proof does not work.
Problem is that inductive assumption not strong
enough.
Solution: revise the guess by subtracting a lowerorder term.
Example: T(n)=T(n/2)+T(n/2)+1.
32
Avoid pitfalls
wrongly !!!!
33
Changing Variables
Suppose T(n)=2T(
n)+lg n.
Rename m=lg n. So T(2m)=2T(2m/2)+m.
Rename S(m)=T(2m), so S(m)=2S(m/2)+m.
34
Steps:
1. Draw the tree based on the recurrence
2. From the tree determine:
35
Steps:
4. Recognize the sum or look for a closed form
solution for the summation created in 3).
5. Apply that closed form solution to your
summation coming up with your guess in terms
of Big-O, or , or (depending on which type of
asymptotic bound is being sought).
6. Then use Substitution Method or Master Method
to prove that the bound is correct.
36
37
T(n)
cn2
c(n/4)2
c(n/4)2
c(n/4)2
(a)
(b)
cn2
c(n/4)2
log 4n
(c)
c(n/4)2
cn2
(3/16)cn2
c(n/4)2
T(1)T(1)T(1)
(3/16)2cn2
T(1)T(1)T(1)
nlog 43
(d)
DSA lecture 7 - T.U.Cluj-Napoca - M. Joldos
(nlog 43)
Total O(n2)
38
Solution to T(n)=3T(n/4)+(n2)
The height is log 4n,
#leaf nodes = 3log 4n= nlog 43. Leaf node cost: T(1).
Total cost T(n)=cn2+(3/16) cn2+(3/16)2 cn2+
+(3/16)log 4(n1) cn2+ (nlog 43)
=(1+3/16+(3/16)2++(3/16)log 4n-1) cn2 + (nlog 43)
<(1+3/16+(3/16)2++(3/16)m+) cn2 + (nlog 43)
=(1/(1-3/16)) cn2 + (nlog 43)
=16/13cn2 + (nlog 43)
=O(n2).
DSA lecture 7 - T.U.Cluj-Napoca - M. Joldos
39
T(n)=3T(n/4)+
(n2) =O(n2).
Show T(n) dn2 for some d.
T(n) 3(d (n/4)2) +cn2
3(d (n/4)2) +cn2
= 3/16(dn2) +cn2
dn2, as long as d (16/13)c.
40
Master Method/Theorem
Three cases:
1. If f(n)=O(nlogba) for some > 0,
then T(n)= (nlogba).
2. If f(n)= (nlogba), then T(n)= (nlogba lg n).
3. If f(n)=
(nlogba+) for some > 0, and
if af(n/b) cf(n) for some c < 1 and all
sufficiently large n, then T(n)= (f(n)).
DSA lecture 7 - T.U.Cluj-Napoca - M. Joldos
41
42
43
T(n) = 3T(n/4)+nlg n;
a=3,b=4, f(n) =nlg n
nlogba = nlog43 = (n0.793)
f(n)= (nlog43+) for 0.2
By case 3, T(n) =
(f(n))=
(nlg n).
DSA lecture 7 - T.U.Cluj-Napoca - M. Joldos
44
T(n) = 2T(n/2)+n lg n;
a
log
f(n)/n b
= lg n, which is asymptotically
less than n for any >0.
45
Gaps
f(n), case 3, at least polynomially larger
n
c1
f(n), case 2: within constant distances
c2
nlogba
n
46
Reading
AHU, chapter 8
Preiss, chapter: Algorithm Analysis,
Asymptotic Notation
CLR, CLRS, chapters 2, 3, 4
Notes
47