You are on page 1of 4

Design and Analysis of Algorithms

CS 302 Spring 2019

Homework # 1
Q1) Suppose we are comparing implementations of insertion sort and merge sort on the same
machine. For inputs of size n, insertion sort runs in 4n2 steps, while merge sort runs in 32 n lg n
steps. For which values of n does insertion sort beat merge sort? [5 Marks]

Ans: N ≥ 50,

N = 50, 4n2 = 10,000, 32 n lg n = 8960

Q2) What is the smallest value of n such that an algorithm whose running time is 100n2 runs
faster than an algorithm whose running time is 2n on the same machine? [5 Marks]

Ans: n= 15, 100n2 = 22500, 2n = 32768

Q3) Prove that function n3/8000 - 800n2 - 400n + 36 is Θ(n3) by finding constants n0, k1 and k2
such that following inequalities hold: [5 Marks]

k2 n3 ≤ n3/8000 - 800n2 - 400n + 36 ≤ k1 n3 for all n ≥ n0

Ans:

k2 n3 ≤ n3/8000 - 800n2 - 400n + 36 ≤ k1 n3

Simplify it by dividing it by n3

k2 ≤ 1/8000 – 800/n – 400/n2 + 36/n3 ≤ k1

Lets find k1

1/8000 – 800/n – 400/n2 + 36/n3 ≤ k1

1/8000 – (800/n + 400/n2 ) + 36/n3 ≤ k1

Notice in above inequality the terms being subtracted (800/n + 400/n2) from 1/8000 are greater
than the term being added (36/n3) so the result will be a value less than 1/8000 so this inequality
will hold for k1 = 1/8000 and n0 = 1
As n gets large (800/n + 400/n2) becomes even greater than 36/n3 so this inequality will be true
for all values of n greater or equal to 1.

Lets find k2

k2 ≤ 1/8000 – 800/n – 400/n2 + 36/n3

k2 = 1/80000
n0 = 64000000

Q4) Consider sorting n numbers stored in array A by first finding the smallest element of A and
exchanging it with the element in A[1]. Then find the second smallest element of A, and
exchange it with A[2]. Continue in this manner for the first n - 1 elements of A. Write pseudo-
code for this algorithm, which is known as selection sort. Why does it need to run for only the
first n - 1 elements, rather than for all n elements? Give the best-case and worst-case running
times of selection sort in Θ-notation. [5 Marks]

Ans: It needs to run for only first n-1 elements because nth element will be the largest element
left in array and it will be already in its right position (last position).

Best case and worst case time will be Θ (n2) since even if array is sorted we need to traverse
entire array in order to kth find smallest element.

Q5) a) Suppose you are given p sorted arrays, each with m elements, and you want to combine
them into a single array of pm elements. Consider the following approach. Using the merge
subroutine, you merge the first 2 arrays, then merge the 3rd given array with this merged version
of the first two arrays, then merge the 4th given array with the merged version of the first three
arrays, and so on until you merge in the final (pth) input array.

What is the running time taken by this successive merging algorithm, as a function of p and m?
[3 Marks]
Ans: First merge = m + m = 2m steps
Second merge = 2m + m = 3m steps
Third merge = 3m + m = 4m
Last merge = (p-1)m + m = pm steps

Arithmetic series = 2m + 3m + 4m + …… + pm
= m ( 2 + 3+ 4 + … p) = m ( p(p+1)/2 - 1) = Theta (mp2)

b) What is a faster way to do the above merge procedure ? [5 Marks]


Ans: Merge all p arrays at same time. Compare first element of each array and find smallest
element in p steps. Write that value in output array and move the pointer of array containing
smaller elements to next element. Repeat this step until all elements are written to output array.

Since every step is taking p time and there will be pm steps in total (there are total pm elements
and in every step one element is written to output array) so total time will be p * pm =
Theta (mp2). This can be made faster by using minHeap to find minimum element from the p
elements in each step. It will take Theta (mplgp ) time using MinHeap.
After every step one element will be extracted and one element will be inserted in minHeap
which take lg p time.

Q 6) Consider the following pseudocode for calculating ab (where a and b are positive integers)

FastPower(a,b) :

if b = 1

return a

otherwise

c := a*a

ans := FastPower(c,[b/2])

if b is odd

return a*ans

otherwise return ans

end
Here [x] denotes the floor function, that is, the largest integer less than or equal to x.
Now assuming that you use a calculator that supports multiplication and division (i.e., you can
do multiplications and divisions in constant time), what would be the overall asymptotic running
time of the above algorithm (as a function of b)?

Ans: O(log2 b). The running time only depends on b. b is divided by 2 in each recursive call so
there will be log2 b levels of recursion tree. There are constant number of operations in each
recursive call.
Q 7) Given a set N of integers and an integer y, determine if there exit two elements in N whose
absolute difference is equal to y and also print those numbers. The algorithm should take O(n lg
n) time. Justify why your algorithm runs in O(n lg n) time.

e.g. Let N = 3 , 7, 2, 1, 4, 10

y=1

there are three pairs of elements in N whose absolute difference is 1

Pair 1 = |3 - 2| = |-1| = 1

Pair 2 = |3 - 4|= |-1| = 1

Pair 3 = |2 -1| = 1

Ans:

1. sort elements (NlogN)


2. for each element x use binary search (logN per element ) to find |x - y| in sorted list.
3. Total time (NlogN + NlogN) = (2NlogN) = Theta(NlogN)

Another algorithm:

The following algorithm solves the problem:


1. Sort the elements in S.
2. Form the set S_ = {z : z = x − y for some y ∈ S}.
3. Sort the elements in S_.
4. If any value in S appears more than once, remove all but one instance. Do the
same for S_.
5. Merge the two sorted sets S and S_.
6. There exist two elements in S whose sum is exactly x if and only if the same value appears in
consecutive positions in the merged output. To justify the claim in step 4, First observe that if
any value appears twice in the merged output, it must appear in consecutive positions. Thus, we
can restate the condition in step 5 as there exist two elements in S whose sum is exactly x if and
only if the same value appears twice in the merged output.
Suppose that some value w appears twice. Then w appeared once in S and once in S_. Because w
appeared in S_, there exists some y ∈ S such that w = x − y, or x = w + y. Since w ∈ S, the
elements w and y are in S and sum to x. Conversely, suppose that there are values w, y ∈ S such
that w + y = x. Then, since x − y = w, the value w appears in S_. Thus, w is in both S and S_, and
so it will appear twice in the merged output.

Steps 1 and 3 require O(n lg n) steps. Steps 2, 4, 5, and 6 require O(n) steps. Thus
the overall running time is O(n lg n).

You might also like