0% found this document useful (0 votes)
50 views4 pages

Algorithm Design: Spring 2016 Problem Set 1

The document is a problem set for an algorithms course. It contains 6 problems regarding time complexity analysis of algorithms: 1) Analyzing time complexity statements for true/false/sometimes true. 2) Comparing input sizes for algorithms with time complexities of O(log n), O(n), O(nk), and O(2n) when computation speed doubles. 3) Ordering functions by time complexity with f1 = O(f2), ..., fn = O(f1). 4) Finding the time complexity of Horner's rule for polynomial evaluation. 5) Analyzing the best/average/worst case time complexity of the Gnome sort algorithm.

Uploaded by

katayoun
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)
50 views4 pages

Algorithm Design: Spring 2016 Problem Set 1

The document is a problem set for an algorithms course. It contains 6 problems regarding time complexity analysis of algorithms: 1) Analyzing time complexity statements for true/false/sometimes true. 2) Comparing input sizes for algorithms with time complexities of O(log n), O(n), O(nk), and O(2n) when computation speed doubles. 3) Ordering functions by time complexity with f1 = O(f2), ..., fn = O(f1). 4) Finding the time complexity of Horner's rule for polynomial evaluation. 5) Analyzing the best/average/worst case time complexity of the Gnome sort algorithm.

Uploaded by

katayoun
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/ 4

I SFAHAN U NIVERSITY OF T ECHNOLOGY

Algorithm Design
Spring 2016
Problem Set 1

Soroush Zargar - Saleh Ashkboos

Due Date: 26 Feb (7 Esfand)

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 )

• f (n) + g (n) = Θ(max( f (n), g (n))

• f (n) + O( f (n)) = Ω( f (n))

• 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

• Problem 5: Stupid Sort


Gnome sort (or Stupid sort) is a sorting algorithm firstly proposed by Hamid Sarbazi-Azad
in 2000 and called "stupid sort", and then later on described by Dick Grune and named
"gnome sort" from the observation that it is "how a gnome sorts a line of flower pots".
Find the Best/Avarage/Worst case complexity for this algorithm. Explain you answer.

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

That’s why in cryptography we say it is computationally hard


to find if a 1024 bit number is prime or not!

Good Luck!

You might also like