Algorithm Design: Spring 2016 Problem Set 1
Algorithm Design: Spring 2016 Problem Set 1
Algorithm Design
Spring 2016
Problem Set 1
1 T IME C OMPLEXITY
In computer science, the time complexity of an algorithm quantifies the amount of time
taken by an algorithm to run as a function of the length of the string representing the input.
The time complexity of an algorithm is commonly expressed using big O notation, which
excludes coefficients and lower order terms. In computer science, big O notation is used
to classify algorithms by how they respond (e.g., in their processing time or working space
requirements) to changes in input size.
Big O: We say f (x) = O(g (x)) if and only if there exists a positive real number M and a real
number x 0 such that,
| f (x)| ≤ M |g (x)| for all x ≥ x 0
1
• Problem 1
For each of the following statements, decide whether it is always true, never true, or some-
times true for asymptotically nonnegative functions f and g . If it is always true or never
true, explain why. If it is sometimes true, give one example for which it is true, and one for
which it is false
• f (n) = O( f (n)2 )
• Problem 2
Suppose for solving problem A with input size n we have four algorithms with time com-
plexities given below, Imagine that (by a revolution in technology or etc) now the amount
of computation in a constant time have got double. Describe the difference of the input
size of each algorithm to be solved in the same time.
1. O(l og n)
2. O(n)
3. O(n k )
4. O(2n )
• Problem 3
Find an ordering f 1 , f 2 , ..., of the following functions such that for all 1 ≤ i ≤ n,
f i = O( f i +1 )
p
(1.1)n , 1, 2, ( 2)n , n 2 , log∗ n, (l g n)!, n log n, n log n , n n , n!, 2n
2
• Problem 4: Horner’s Rule
The following pseudo-code implements the Horner’s rule for evaluating a polynomial
n
a k x k = a 0 + x(a 1 + x(a 2 + ... + x(a n−1 + xa n ))...)
P
P (x) =
i =0
The x and coefficients are given as input, find the asymptotic running time for this code:
Data: x, a 0 , a 1 , ..., a n
Result: P (x)
y ← 0;
i ← n;
while i ≥ 0 do
y ← a i + x.y0;
i ← i − 1;
end
Algorithm 1: Horner’s Rule
Data: Array a
pos := 1; while pos < l eng t h(a) do
if a[pos] >= a[pos-1] then
pos := pos + 1;
end
else
swap a[pos] and a[pos-1];
if pos > 1 then
pos := pos - 1;
end
end
end
Algorithm 2: Stupid Sort
3
• Problem 6: Prime Detection
A number n is prime if and only if it is dividable to only 1 and itself.
1. First of all, write an algorithm which receives a number n and returns if it’s prime or
not. Then for the given algorithm find it’s time complexity class.
2. Suppose your algorithm is running on a machine which receives the number in bi-
nary form. Find the order of space you need to represent the input.
In such problems (Numerical Problems), we call it m ∈ O(g (n)) where n is the value
of input. So, m is the length we need to represent n to our machine.
(We call this representation, a coding for n )
3. Find the running time complexity for you algorithm in the way that your given time
complexity is only a function to m, Not n
Good Luck!