You are on page 1of 5

Analysis and Design of Algorithms Assignment

Contents
Question 1 1

Question 2 2

Question 3 3

Question 4 3

Question 5 4

Question 6 4
Question 1
Compute the asymptotic order of growth of the following pairs of functions. In each case
determine if:

• f (n) ∈ O(g(n))

• f (n) ∈ Ω(g(n))

• f (n) ∈ θ(g(n))

Ans
f (n) g(n) θ(f (n)) θ(g(n))
2
100n + log(n) n + log(n) θ(n) θ(n)
log(n) log(n2 ) θ(log(n)) θ(log(n))
n2 n2
n(log(n))2 θ( ) θ(n(log(n))2 )
log(n) log(n)
log n log n2 θ(log n) θ(log n)
n2 n 2
log n
n(log n)2 θ( log n
) θ(n(log n)2 )
n
(log n)log n log n
θ((log n)log n ) θ( logn n )
√ √
n (log n)5 θ( n) θ((log n)5 )
n2n 3n θ(n2n ) θ(3n )

log n
√ √
log n

2 n θ(2 ) θ( n)

f (n) ∈ O(g(n)) f (n) ∈ Ω(g(n)) f (n) ∈ θ(g(n))


True True True
True True True
False True False
False True False
False True False
True False False
True False False

1
Question 2
The following algorithm computes a power of 2 with the exponent which is itself a power
of 2. Analyze the algorithm in terms of both the unit cost and bit cost.

Ans

Unit Cost
In unit cost analysis, we consider numbers as fixed byte variables, treating operations
like integer addition and multiplication as constant-time operations.

Hence, it’s evident that the algorithm requires Θ(log m) = Θ(n) time and occupies Θ(1)
space.

Bit Cost
In bit cost analysis, we consider the variable size of numbers, acknowledging that simple
operations like integer multiplication can vary in time complexity. For instance, the mul-
tiplication of two integers each of length n bits may take O(n2 ) time.

Hence, the primary distinction lies in the variable time required for multiplication inside
the loop rather than constant time. At the beginning of each loop iteration, where
i−1
y = 22 , the time complexity is O((log y)2 ) = O(22i ). The overall time complexity of
the loop can be computed using a geometric series:
log m
X 4log m − 1
c2i = 22 c ∗ = O(4log m ) = O(4n ) (1)
i=1
4−1

Additionally, as y eventually equals 2m , having m + 1 bits, the space complexity is


Θ(m) = Θ(2n ).

2
Question 3
Describe and analyze an algorithm that determines whether a given graph is bipartite.
The time complexity should be linear in both the number of nodes and edges of the graph.

Ans
We can execute a Breadth-First Search (BFS) on the graph, assigning two colors (e.g.,
blue and red) to each level of nodes. If an attempt to recolor a node that was already
colored is made during the process, then the graph is not bipartite.

Question 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 O complexity of each algorithm.

Ans
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.5n1.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)
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)

3
Question 5
The statements below show some features of the O notation for the functions f (n) and
g(n). Determine whether each statement is TRUE or FALSE and correct the formula in
the latter case.

Ans

Statement true/false in case of false, write the correct formula


Rule of sums: O(f + g) = O(f ) + O(g) false O(f + g) = O(max(O(f ), O(g)))
Rule of products: O(f · g) = O(f ) · O(g) true
Transitivity: if g = O(f ) and h = O(f ) then g = O(h) false if g = O(f ) and f = O(h) then g = O(h)
5n + 8n2 + 100n3 = O(n4 ) true
5n + 8n2 + 100n3 = O(n2 logn) false O(n3 )

Question 6
Sort each of the following sets of functions in increasing order of asymptotic O complexity:

a)
f1 (n) = n0 .999999 log n
f2 (n) = 10000000n
(1)
f3 (n) = 1.000001n
f4 (n) = n2

b)
1000000
f1 (n) = 22
f2 (n) = 2100000n
(2)
 
n
f3 (n) =
2

f4 (n) = n n

c) √
f1 (n) = n n
f2 (n) = 2n
f3 (n) = n10 · 2n/2 (3)
Xn
f4 (n) = (i + 1)
i+1

Ans
a) f1 (n) < f2 (n) = f3 (n) < f4 (n)
b) f1 (n) < f4 (n) < f3 (n) < f2 (n)
c) f4 (n) < f1 (n) < f3 (n) < f2 (n)

You might also like