0% found this document useful (0 votes)
6 views22 pages

Lec - 2 Running Time

Uploaded by

amrayyad728
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views22 pages

Lec - 2 Running Time

Uploaded by

amrayyad728
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CS313

Dr. Sondos Fadl


 The running time of an algorithm on a particular input is
the number of primitive operations or “steps” executed
before termination.

Arithmetic Data movement


operations
Approximate not exact !
comparison

Decision making
PRIMITIVE OPERATIONS
 Ex: Find min value of an array?

Min(A, 1 , n)
m=A(1)
for i=2 to n
if A(i) <m
m=A(i)
Array size 106
 Ex: sorting numbers:

Ahmed’s computer=109 operations /sec


Ahmed’s Algorithm=2𝑛2 operations
𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚 𝑐𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 2×(106 )2
Time= = = 2000 𝑠𝑒𝑐
𝑑𝑒𝑣𝑖𝑐𝑒 𝑠𝑝𝑒𝑒𝑑 109

Mohamed’s computer=107 operations /sec


20 times better !!!
Mohamed’s Algorithm=50𝑛 log 𝑛 operations
𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚 𝑐𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 50×106 log 106
Time= = = 100 𝑠𝑒𝑐
𝑑𝑒𝑣𝑖𝑐𝑒 𝑠𝑝𝑒𝑒𝑑 107
 1 (constant)
 log 𝑛 (logarithmic)
 𝑛𝑘 (polynomial)
 𝑛 linear
 𝑛2 quadratic
 𝑛3 cubic

 2𝑛 (exponential)
Input size 𝑛 𝑛2 log10 𝑛

10 10 100 1

100 100 10000 2

106 106 (106 )2 6


exponential
polynomial

logarithmic

constant
𝑛3 > 𝑛2

𝑛2 > log 𝑛
𝑛2 log 𝑛 > n log 𝑛2

𝑛 log 𝑛 < 𝑛2
For i=1 to n
𝑛 print ‘Hi’
For i=1 to n
if i <11

10 print ‘Hi’
else
break
Nested loop:
For i=1 to n
𝑛
Dependent nested loop:
𝑛 𝑛2 for j=1 to n
𝑖=1 𝑛 For i=1 to n
print ‘Hi’
𝑛 for j=1 to I 𝑖 = 1, 1
𝑖 = 2, 2
𝑖 print ‘Hi’ .
𝑖=1
.
.
𝑖 = 𝑛, 𝑛
Why study summations?
1. In general, the running time of a loop can be expressed as the sum
of the running time of each iteration.
2. Summations come up in solving recurrences.
𝑛 𝑛(𝑛+1)
 𝑖=1 𝑖 =
2

𝑛
 𝑖=1 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 𝑜𝑟 𝑛 = ∗ = 𝑛2

log2 𝑛 𝑖
 𝑖=1 2 = 2𝑛 − 1
log2 𝑛
 𝑖=1 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 = ∗

𝑛
 𝑖=𝑗 1 =𝑛−𝑗+1

𝑛 𝑥 𝑛+1 −1
 𝑖=1 𝑥 𝑖
= x constant
𝑥−1

𝑛 2 2𝑛3 +3𝑛2 +𝑛
 𝑖=1 𝑖 =
6

𝑛 1
 𝑖=1 𝑖 = log 𝑛 + 1
For (i=1, i≤ 𝑛,i++)
𝑛

1 Print i
𝑖=1
end

𝑇 𝑁 = 1= 1 ∗ 𝑛 =𝑛
𝑖=1
𝑛−1 For (i=n-1, i≥ 1,i--)
1 Print i
𝑖=1
end

𝑛−1
𝑇 𝑁 = 1= 1 ∗ 𝑛−1 =𝑛−1
𝑖=1
log2 𝑛 For (i=1, i≤ 𝑛,i*=2)
1 Print i
𝑖=1
end

log2 𝑛
𝑇 𝑁 = 1= 1 ∗ log 2 𝑛 = log 2 𝑛
𝑖=1
log2 𝑛 For (i=1, i≤ 𝑛,i/=2)
1 Print i
𝑖=1
end

log2 𝑛
𝑇 𝑁 = 1= 1 ∗ log 2 𝑛 = log 2 𝑛
𝑖=1
𝑛
For (i=1, i≤ 𝑛,i++)
𝑡 For (j=1, j≤ 𝑛,j++) 𝑛

𝑖=1 1
print “hi”
𝑖=1
end
end 𝑛 𝑛 𝑛

𝑇 𝑁 = 1= 𝑛 = 𝑛2
𝑖=1 𝑗=1 𝑖=1
 Our first algorithm, insertion sort, solves the sorting problem.
𝑛

𝑇 𝑁 = 𝑡
𝑗=2

The best case occurs The worst case occurs

 If the array is already sorted, Thus 𝑡𝑗  If the array is in reverse sorted order
= 1 for j = 2;3; … ;n, and the best-case that is, in decreasing order—the
running time is worst case results. We must compare
each element A[j] with each element
𝑛 in the entire sorted subarray A[1 .. j -
𝑇 𝑁 = 1=𝑛 1], and so 𝑡𝑗 = j for j = 2;3; … ;n
𝑗=2
𝑛
𝑛(𝑛 + 1) 𝑛2 + 𝑛
𝑇 𝑁 = 𝑗= =
2 2
𝑗=2
Ο(𝑛)
Ο(𝑛2 )

You might also like