You are on page 1of 28

Mathematical Analysis of

Nonrecursive Algorithms

Assistant Prof. Dr. Tuğba Özacar Öztürk

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

Bu algoritmanın basic operation’ı büyüktür küçüktür


Best case worst case’i yok çünkü en büyüğünü bulmak için bütün array’i tarayacak

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:

• The comparison A[i]>maxval


• The assignment maxvalA[i]
• Which operation is basic?

• Comparison is executed on each repetititon of the loop BASIC


• Assignment is not
• Comparisons will be same for all arrays of size n; therefore,
there is no need to distinguish among the worst, average,
and best cases here.
n −1
C ( n ) = ∑ 1 = n − 1 ∈ Θ( n )
i =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.
Time efficiency of nonrecursive algorithms

General Plan for Analysis



Decide on parameter n indicating input size

• Identify algorithm’s basic operation



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

• Simplify the sum using standard formulas and rules (see


Appendix A)

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

Bu algoritmada best case worst case var


En iyi ihtimal 1, en kötü ihtimal n2

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

• The number of element comparisons depends not only on n


• We will limit our investigation to the worst case only
n − 2 n −1 n−2
(n − 1)n 1 2 2
∑ ∑ 1 = ∑ ( n − 1 − i ) = ( n − 1) + ( n − 2 ) + ... + 1 = ≈ n ∈ Θ ( n )
i = 0 j =i +1 i =0 2 2

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

Basic operation çarpma işlemi, iç içe 3 tane for döngüsü olduğu


için algoritmanın complexity’si n^3
Best case worst case’den bahsedemeyiz

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

It cannot be investigated the way the previous examples are.

Basic operation bölme, logaritmik bir fonksiyon log2n+1 complexitysi

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

• What does this algorithm compute?


• What is its basic operation?
• How many times is the basic operation
executed?
• What is the efficiency class of this
algorithm?

Basic operation karşılaştırma, complexity’si 2n, lineer

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

• What does this algorithm compute?


• What is its basic operation?
• How many times is the basic
operation executed?
• What is the efficiency class of this
algorithm?

Simetri olup olmadığını kontrol ediyor, basic operation


karşılaştırma, best case worst case var, complexity n2

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

• Find the time efficiency class of this algorithm


• What glaring inefficiency does this code contain, and how can it be eliminated?
• Estimate the reduction in run time.

Basic operation çarpma, complexity n3 , iyileştirme yapılabilir;


İşaretli yer K’dan bağımsız olduğu ve sabit bir sayı olduğu için yukarıdaki
döngüde bir kez hesaplanır, n kere bölme işleminden kurtulunur

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

Assistant Prof. Dr. Tuğba Özacar Öztürk

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

Recursive definition of n!: F(n) = F(n-1) ⋅ n for n ≥ 1 and


F(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(0) = 0


= [M(n-2)+1]+1=M(n-2)+2
= [M(n-3)+1]+2=M(n-3)+3

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

Her azalmada basic operation cn kere


execute ediliyor, for döngüsü var

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ı

Basic operation karşılaştırma- comparison


C değeri 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.
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.

You might also like