0% found this document useful (0 votes)
755 views

Assignment # 3

This document provides exercises and solutions related to time complexity and Big-O notation. It includes exercises analyzing the time complexity of algorithms with different functions, proving statements about Big-O notation, and comparing the performance of algorithms. The solutions analyze the dominant terms, state the Big-O complexity, and derive formulas to calculate running times.

Uploaded by

Shahzael Mughal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
755 views

Assignment # 3

This document provides exercises and solutions related to time complexity and Big-O notation. It includes exercises analyzing the time complexity of algorithms with different functions, proving statements about Big-O notation, and comparing the performance of algorithms. The solutions analyze the dominant terms, state the Big-O complexity, and derive formulas to calculate running times.

Uploaded by

Shahzael Mughal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1 Exercises and Solutions

Most of the exercises below have solutions but you should try first to solve
them. Each subsection with solutions is after the corresponding subsection
with exercises. T

1.1 Time complexity and Big-Oh notation: exercises


1. A sorting method with “Big-Oh” complexity O(n log n) spends exactly 1
millisecond to sort 1,000 data items. Assuming that time T (n) of sorting
n items is directly proportional to n log n, that is, T (n) = cn log n, derive
a formula for T (n), given the time T (N ) for sorting N items, and estimate
how long this method will sort 1,000,000 items.
2. A quadratic algorithm with processing time T (n) = cn2 spends T (N )
seconds for processing N data items. How much time will be spent for
processing n = 5000 data items, assuming that N = 100 and T (N ) = 1ms?
3. An algorithm with time complexity O(f (n)) and processing time T (n) =
cf (n), where f (n) is a known function of n, spends 10 seconds to process
1000 data items. How much time will be spent to process 100,000 data
items if f (n) = n and f (n) = n3 ?
4. Assume that each of the expressions below gives the processing time T (n)
spent by an algorithm for solving a problem of size n. Select the dominant
term(s) having the steepest increase in n and specify the lowest Big-Oh
complexity of each algorithm.

Expression Dominant term(s) O(. . .)


5 + 0.001n3 + 0.025n
500n + 100n1.5 + 50n log10 n
0.3n + 5n1.5 + 2.5 · n1.75
n2 log2 n + n(log2 n)2
n log3 n + n log2 n
3 log8 n + log2 log2 log2 n
100n + 0.01n2

0.01n + 100n2

2n + n0.5 + 0.5n1.25

0.01n log2 n + n(log2 n)2

100n log3 n + n3 + 100n

0.003 log4 n + log2 log2 n

1
5. The statements below show some features of “Big-Oh” notation for the
functions f ≡ f (n) and g ≡ g(n). Determine whether each statement is
TRUE or FALSE and correct the formula in the latter case.

Is it TRUE If it is FALSE then write


Statement or FALSE? the correct formula

Rule of sums:
O(f + g) = O(f ) + O(g)

Rule of products:
O(f · g) = O(f ) · O(g)

Transitivity:
if g = O(f ) and h = O(f )
then g = O(h)

5n + 8n2 + 100n3 = O(n4 )

5n+8n2 +100n3 = O(n2 log n)

6. Prove that T (n) = a0 + a1 n + a2 n2 + a3 n3 is O(n3 ) using the formal


definition of the Big-Oh notation. Hint: Find a constant c and threshold
n0 such that cn3 ≥ T (n) for n ≥ n0 .
7. Algorithms A and B spend exactly TA (n) = 0.1n2 log10 n and TB (n) =
2.5n2 microseconds, respectively, for a problem of size n. Choose the al-
gorithm, which is better in the Big-Oh sense, and find out a problem size
n0 such that for any larger size n > n0 the chosen algorithm outperforms
the other. If your problems are of the size n ≤ 109 , which algorithm will
you recommend to use?

8. Algorithms A and B spend exactly TA (n) = cA n log2 n and TB (n) = cB n2


microseconds, respectively, for a problem of size n. Find the best algorithm
for processing n = 220 data items if the algoritm A spends 10 microseconds
to process 1024 items and the algorithm B spends only 1 microsecond to
process 1024 items.
9. Algorithms A and B spend exactly TA (n) = 5·n·log10 n and TB (n) = 25·n
microseconds, respectively, for a problem of size n. Which algorithm is
better in the Big-Oh sense? For which problem sizes does it outperform
the other?
10. One of the two software packages, A or B, should be chosen to process
very big databases, containing each up to 1012 records. Average process-
ing time of the package A is TA (n) = 0.1 · n · log2 n microseconds, and the
average processing time of the package B is TB (n) = 5 · n microseconds.

2
Which algorithm has better performance in a ”Big-Oh” sense? Work out
exact conditions when these packages outperform each other.

11. One of the two software packages, A or B, should be chosen to process


data collections, containing each up to 109 records. Average processing
time of the package A is TA (n) = 0.001n milliseconds
√ and the average
processing time of the package B is TB (n) = 500 n milliseconds. Which
algorithm has better performance in a ”Big-Oh” sense? Work out exact
conditions when these packages outperform each other.

12. Software packages A and B of complexity O(n log n) and O(n), respec-
tively, spend exactly TA (n) = cA n log10 n and TB (n) = cB n milliseconds
to process n data items. During a test, the average time of processing
n = 104 data items with the package A and B is 100 milliseconds and 500
milliseconds, respectively. Work out exact conditions when one package
actually outperforms the other and recommend the best choice if up to
n = 109 items should be processed.
13. Let processing time of an algorithm of Big-Oh complexity O(f (n)) be
directly proportional to f (n). Let three such algorithms A, B, and C
have time complexity O(n2 ), O(n1.5 ), and O(n log n), respectively. During
a test, each algorithm spends 10 seconds to process 100 data items. Derive
the time each algorithm should spend to process 10,000 items.
14. Software packages A and B have processing time exactly TEP = 3n1.5 and
TWP = 0.03n1.75 , respectively. If you are interested in faster processing of
up to n = 108 data items, then which package should be choose?

1.2 Time complexity and Big-Oh notation: solutions


T (N )
1. Because processing time is T (n) = cn log n, the constant factor c = N log N ,
and T (n) = T (N ) Nn log
log n
N.
Ratio of logarithms of the same base is indepen-
dent of the base (see Appendix in the textbook), hence, any appropriate
base can be used in the above formula (say, base of 10). Therefore, for
n = 1000000 the time is T (1, 000, 000) = T (1, 000) · 1000000 log10 1000000
1000 log10 1000 =
1000000·6
1 · 1000·3 = 2, 000 ms
2
T (N ) n n2
2. The constant factor c = N2 , therefore T (n) = T (N ) N 2 = 10000 ms and
T (5000) = 2, 500 ms.
T (1000) 10
3. The constant factor c = f (1000) = f (1000) milliseconds per item. There-
f (n)
fore, T (n) = ms and T (100, 000) = 10 f (100,000)
10 f (1000) f (1000) ms. If f (n) = n
then T (100, 000) = 1000 ms. If f (n) = n , then T (100, 000) = 107 ms.
3

3
Expression Dominant term(s) O(. . .)
5 + 0.001n3 + 0.025n 0.001n3 O(n3 )
500n + 100n1.5 + 50n log10 n 100n1.5 O(n1.5 )
0.3n + 5n1.5 + 2.5 · n1.75 2.5n1.75 O(n1.75 )
n2 log2 n + n(log2 n)2 n2 log2 n O(n2 log n)
n log3 n + n log2 n n log3 n, n log2 n O(n log n)
3 log8 n + log2 log2 log2 n 3 log8 n O(log n)
4. 100n + 0.01n2 0.01n2 O(n2 )

0.01n + 100n2 100n2 O(n2 )

2n + n0.5 + 0.5n1.25 0.5n1.25 O(n1.25 )

0.01n log2 n + n(log2 n)2 n(log2 n)2 O(n(log n)2 )

100n log3 n + n3 + 100n n3 O(n3 )

0.003 log4 n + log2 log2 n 0.003 log4 n O(log n)

If it is FALSE then
Is it TRUE
Statement write
or FALSE?
the correct formula

Rule of sums: O(f + g) =


O(f + g) = O(f ) + O(g) FALSE max {O(f ), O(g)}

Rule of products:
O(f · g) = O(f ) · O(g) TRUE

5. Transitivity: if g = O(f ) and


if g = O(f ) and h = O(f ) FALSE f = O(h) then
then g = O(h) g = O(h)

5n + 8n2 + 100n3 = O(n4 )


TRUE

5n + 8n2 + 100n3 =
5n + 8n2 + 100n3 = O(n3 )
O(n2 log n) FALSE

6. It is obvious that T (n) ≤ |a0 | + |a1 |n + |a2 |n2 + |a3 |n3 . Thus if n ≥ 1,
then T (n) ≤ cn3 where c = |a0 | + |a1 | + |a2 | + |a3 | so that T (n) is O(n3 ).
7. In the Big-Oh sense, the algorithm B is better. It outperforms the algo-

You might also like