Professional Documents
Culture Documents
Nonrecursive Algorithms
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 1: Maximum element
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 1 (cont.)
• Input size: number of elements in the array = n
• The operations that are going to be executed most often are
in the for loop:
•
•
Determine worst, average, and best cases for input of size n
•
•
Set up a sum for the number of times the basic operation is
executed
•
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Useful summation formulas and rules
n n
∑ c = c∑1 = c(n − m + 1)
i =m i =m
∑ (a + b ) = ∑ a + ∑ b
i
i i
i
i
i
i
∑ ca
i
i = c ∑ ai
i
n n+k
∑a
i =m
i+k = ∑a
i =m+ k
i
i+k k i
a
∑ i x = x a
∑ i x
i i
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 2: Element uniqueness problem
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 2 (cont.)
• Input size: n
• Basic operation: the comparison of two elements
•
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 3: Matrix multiplication
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 3 (cont.)
• Input size: n
• Multiplication and addition: don’t have to choose between
them, by counting one we automatically count the other.
• The count of multiplications depends only on the size of
input matrices, we do not have to investigate the worst-case,
average-case and best-case efficiencies seperately.
• Total number of multiplications M(n):
n −1 n −1 n −1 n −1 n −1 n −1
2 3
M (n) = ∑∑ ∑1 = ∑∑ n = ∑ n = n
i =0 j =0 k =0 i =0 j =0 i =0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 4: Counting binary digits
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 4 (cont.)
• Basic operation: not inside while loop but rather the
comparison n>1 that determines whether the loop’s body
will be executed
• Number of times the comparison will be executed is larger
than the number of repetitions of the loop’s body by exactly
1
• The value of n is halved on each repetition of the loop so the
answer should be about log2n. The exact formula:
⎣log 2 n⎦ + 1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 6
Sınav sorusu
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 6 (cont.)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 7
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 7 (cont.)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 8
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 8 (cont.)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Mathematical Analysis of Recursive
Algorithms
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example 1: Recursive evaluation of n!
Definition: n ! = 1 ⋅ 2 ⋅ … ⋅ (n-1) ⋅ n for n ≥ 1 and 0! = 1
•
Input Size: n
Basic operation: Multiplication – M(n)
Recurrence relation
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Solving the recurrence for M(n)
M(n)=M(n-1)+1=…=M(n-i)+i=…=M(n-n)+n=n
Lineer
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Plan for Analysis of Recursive Algorithms
• Decide on a parameter indicating an input’s size.
•
Identify the algorithm’s basic operation.
•
Check whether the number of times the basic op. is executed
may vary on different inputs of the same size. (If it may, the
worst, average, and best cases must be investigated
separately.)
• Set up a recurrence relation with an appropriate initial
condition expressing the number of times the basic op. is
executed.
• Solve the recurrence (or, at the very least, establish its
solution’s order of growth) by backward substitutions or
another method.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Termination Conditions
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
T(n)=T(n-1)+c, T(1)=d
Lineer
D ve c sabit sayı olduğu için bi önemi yok
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
T(n)=T(n-1)+cn
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
T(n)=T(n/2)+cŞimdiye kadarT(n)
decrease and concur tekniklerini gördük
t(n-1) tarzı
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
T(n)=T(n/2)+cn, linear function of n
A geometric serie
Complexitysi n
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
T(n)=2T(n/2)+c, linear function of n
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
T(n)=2T(n/2)+cn
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.