ALGORITHMS
SECTION2: ANALYZING ALGORITHMS
Eng. Dalia Elwi
EXAMPLE 1
Algorithm
i=1
Loop (i <= N)
Application Code
i = i+1
End Loop
Time Complexity
T(n) = θ(N)
EXAMPLE 2
Algorithm
i=1
Loop (i <= N)
Application Code
i = i+2
End Loop
Time Complexity
T(n) = θ(N)
EXAMPLE 3
Algorithm
i=1
Loop (i <= N)
Application Code
i = i*2
End Loop
Time Complexity
T(n) = θ(log 2 𝑁𝑁)
EXAMPLE 4
Algorithm
i=1
Loop (i >= N)
Application Code
i = i/2
End Loop
Time Complexity
T(n) = θ(log 2 𝑁𝑁)
EXAMPLE 5
Algorithm
i=1
Loop (i <= N)
j=1
Loop (j <= N)
Application Code
j = j+1
End Loop
i = i+1
End Loop
Time Complexity
T(n) = θ(𝑁𝑁 2 )
EXAMPLE 6
Algorithm
i=1
Loop (i <= N)
j=1
Loop (j <= N)
Application Code
j = j*2
End Loop
i = i+1
End Loop
Time Complexity
T(n) = θ(𝑁𝑁 log 2 𝑁𝑁)
EXAMPLE 7
• If the algorithm doIt has an efficiency factor of 5N,
calculate the running time of the following
algorithm:
Algorithm
i=1
Loop (i <= N)
doIt(…)
i = i+1
End Loop
Time Complexity
T(n) = 1 + 5𝑁𝑁 2 = θ(𝑁𝑁 2 )
EXAMPLE 8
Algorithm
i=1
Loop (i <= 𝑁𝑁 2)
j=1
Loop (j <= N)
Application Code
j = j+1
i = i+1
End Loop
Time Complexity
T(n) = θ(𝑁𝑁 3 )
EXAMPLE 9
Algorithm
i=1
Loop (i <=N)
j=1
Loop (j <= i)
Application Code
j = j+1
i = i+1
End Loop
Time Complexity
T(n) = θ(𝑁𝑁 2 )
EXAMPLE 10
• What is the asymptotic running time of this
algorithm:
Algorithm
SECRET(Array[N] of real numbers)
Minval = A[0]
Maxval = A[0]
For i = 1 to N-1 do
If A[i] < Minval
Minval = A[i]
If A[i] > Maxval
Maxval = A[i]
Return Maxval - Minval
Time Complexity
T(n) = θ(N)
TYPES OF ASYMPTOTIC ANALYSIS
• Types of Analysis:
1. Best Case (Ω): lower cost
2. Worst Case (O): upper cost
3. Average Case (θ): expected cost for random input
EXAMPLE 11
• For each of the following functions, calculate the
asymptotic Upper tight bound O-notation:
A. (𝒏𝒏𝟐𝟐 + 𝒏𝒏 + 𝟏𝟏)𝟏𝟏𝟏𝟏 = (𝒏𝒏𝟐𝟐 + 𝑶𝑶(𝒏𝒏𝟐𝟐 ) + 𝑶𝑶(𝒏𝒏𝟐𝟐 ))𝟏𝟏𝟏𝟏 = O(𝒏𝒏𝟐𝟐𝟎𝟎 )
B. ( 𝒏𝒏 + 𝟑𝟑 𝒏𝒏 + log 𝒏𝒏)𝟏𝟏𝟏𝟏 = ( 𝒏𝒏 + 𝑶𝑶( 𝒏𝒏) + 𝑶𝑶( 𝒏𝒏))𝟏𝟏𝟏𝟏 = O(𝒏𝒏𝟓𝟓 )
C. 𝒏𝒏𝟏𝟏𝟏𝟏 + 𝟏𝟏. 𝟎𝟎𝟎𝟎𝒏𝒏 = 𝑶𝑶 𝟏𝟏. 𝟎𝟎𝟎𝟎𝒏𝒏 + 𝟏𝟏. 𝟎𝟎𝟎𝟎𝒏𝒏 = O(𝟏𝟏. 𝟎𝟎𝟎𝟎𝒏𝒏 )
D. 𝒏𝒏𝟏𝟏𝟏𝟏 + 𝟎𝟎. 𝟗𝟗𝟗𝟗𝒏𝒏 = 𝑶𝑶 𝒏𝒏𝟏𝟏𝟏𝟏 + 𝟎𝟎. 𝟗𝟗𝟗𝟗𝒏𝒏 = O(𝒏𝒏𝟏𝟏𝟏𝟏 )
E. 𝟐𝟐𝒏𝒏 + 𝒏𝒏! + 𝒏𝒏𝒏𝒏 = 𝑶𝑶 𝒏𝒏𝒏𝒏 + 𝑶𝑶 𝒏𝒏𝒏𝒏 + 𝒏𝒏𝒏𝒏 = O(𝒏𝒏𝒏𝒏 )
F. 𝟐𝟐log 𝒏𝒏 = 𝒏𝒏log 𝟐𝟐 = 𝒏𝒏 = O(𝒏𝒏)
EXAMPLE 12
• For each of the following functions, calculate the
asymptotic Lower tight bound Ω-notation:
A. (𝒏𝒏𝟐𝟐 + 𝒏𝒏 + 𝟏𝟏)𝟏𝟏𝟏𝟏 = (Ω(𝟏𝟏) + Ω 𝟏𝟏 + 1)𝟏𝟏𝟏𝟏 = Ω(𝟏𝟏)
B. ( 𝒏𝒏 + 𝟑𝟑 𝒏𝒏 + log 𝒏𝒏)𝟏𝟏𝟏𝟏 = (Ω(log 𝒏𝒏) + Ω(log 𝒏𝒏) + log 𝒏𝒏)𝟏𝟏𝟏𝟏
= Ω( log 𝒏𝒏𝟏𝟏𝟏𝟏 )
C. 𝒏𝒏𝟏𝟏𝟏𝟏 + 𝟏𝟏. 𝟎𝟎𝟎𝟎𝒏𝒏 = 𝒏𝒏𝟏𝟏𝟏𝟏 + Ω 𝒏𝒏𝟏𝟏𝟏𝟏 = Ω 𝒏𝒏𝟏𝟏𝟏𝟏
D. 𝒏𝒏𝟏𝟏𝟏𝟏 + 𝟎𝟎. 𝟗𝟗𝟗𝟗𝒏𝒏 = Ω 𝟎𝟎. 𝟗𝟗𝟗𝟗𝒏𝒏 + 𝟎𝟎. 𝟗𝟗𝟗𝟗𝒏𝒏 = Ω(𝟎𝟎. 𝟗𝟗𝟗𝟗𝒏𝒏 )
E. 𝟐𝟐𝒏𝒏 + 𝒏𝒏! + 𝒏𝒏𝒏𝒏 = 𝟐𝟐𝒏𝒏 + Ω 𝟐𝟐𝒏𝒏 + Ω(𝟐𝟐𝒏𝒏 ) = Ω(𝟐𝟐𝒏𝒏 )
F. 𝟐𝟐log 𝒏𝒏 = 𝒏𝒏log 𝟐𝟐 = 𝒏𝒏 = Ω(𝒏𝒏)
EXAMPLE 13
• Analyze the following algorithm to find the time
complexity?
Algorithm
ABC(A, n, x):
Input: An array A of n integers
Output: (T) if the element x belongs to A
for (i = 0; i < n; i++) do
if (A[i] == x) then return T
Time Complexity
T(n) = θ(n)
EXAMPLE 14
Algorithm
ABC(A, n):
Input: An array A of n integers
for (i = 1 to n) do
for (j = n downto i + 1) do
if (A[j] < A[ j - 1]) then
SWAP(A[j]; A[j - 1]).
end if
end for
end for
A. What is the output if A=(4, 3, 2, 7, 9, 10, 5)?
B. Analyze the running time for the algorithm?
EXAMPLE 14
• What is the output if A=(4, 3, 2, 7, 9, 10, 5)?
i=1 j=7
4 3 2 7 9 10 5
i=1 j=6
4 3 2 7 9 5 10
i=1 j=5
4 3 2 7 5 9 10
i=1 j=4
4 3 2 5 7 9 10
EXAMPLE 14
• What is the output if A=(4, 3, 2, 7, 9, 10, 5)?
i=1 j=3
4 3 2 5 7 9 10
i=1 j=2
4 2 3 5 7 9 10
i=2 j=7
2 4 3 5 7 9 10
i=2 j=6
2 4 3 5 7 9 10
EXAMPLE 14
• What is the output if A=(4, 3, 2, 7, 9, 10, 5)?
i=2 j=5
2 4 3 5 7 9 10
i=2 j=4
2 4 3 5 7 9 10
i=2 j=3
2 4 3 5 7 9 10
i=3 j=7
2 3 4 5 7 9 10
To The End
EXAMPLE 14
• Analyze the running time for the algorithm?
T(n) = ∑𝑛𝑛𝑖𝑖=1 ∑𝑛𝑛𝑗𝑗=𝑖𝑖+1 1
= ∑𝑛𝑛𝑖𝑖=1(𝑛𝑛 − 𝑖𝑖 + 1 + 1)
= ∑𝑛𝑛𝑖𝑖=1(𝑛𝑛 − 𝑖𝑖)
= ∑𝑛𝑛𝑖𝑖=1 𝑛𝑛 − ∑𝑛𝑛𝑖𝑖=1 𝑖𝑖
= 𝑛𝑛. 𝑛𝑛 − 𝑛𝑛(𝑛𝑛 + 1)/2
2
1 2
≅ 𝑛𝑛 − 𝑛𝑛
2
1
≅ 𝑛𝑛2
2
Time Complexity
T(n) = θ(𝑛𝑛2 )
EXAMPLE 15
Write an algorithm to compute the product of two
matrices A and B, both of n x n and then analyze
your algorithm.
Algorithm
Square-Matrix-Multiply(A,B)
n = A.Rows
Let C be a new n x n matrix
For i = 1 to n
For j = 1 to n
𝑐𝑐𝑖𝑖𝑖𝑖 = 0
For k = 1 to n
𝑐𝑐𝑖𝑖𝑖𝑖 = 𝑐𝑐𝑖𝑖𝑖𝑖 + 𝑎𝑎𝑖𝑖𝑖𝑖 . 𝑏𝑏𝑘𝑘𝑘𝑘
Return C
EXAMPLE 15
Write an algorithm to compute the product of two
matrices A and B, both of n x n and then analyze
your algorithm.
𝑎𝑎11 𝑎𝑎12 𝑏𝑏11 𝑏𝑏12 𝑐𝑐11 𝑐𝑐12
A = 𝑎𝑎 𝑎𝑎22 B= C = 𝑐𝑐 𝑐𝑐22
21 𝑏𝑏21 𝑏𝑏22 21
Step1: 𝑐𝑐11 = 𝑎𝑎11 . 𝑏𝑏11
Step2: 𝑐𝑐11 = 𝑐𝑐11 + 𝑎𝑎12 . 𝑏𝑏21
Time Complexity
T(n) = θ(𝑛𝑛3 )