## Monday, 20 March 2017

### Chapter 2 Exercise 2.2, Introduction to Algorithms, 3rd Edition Thomas H. Cormen

2.2-1 Express the function n3/1000100n2100n+3 in terms of
θnotation.

Solution:
The leading term of the function ignoring the constant coefficient is n3 . So, the function in θnotation will be θ(n3).

2.2-2 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 1elements of A. Write pseudocode for this algorithm, which is known as selection sort. What loop invariant does this algorithm maintain? 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? Justify your answers.

Solution:

Pseudocode

SELECTION-SORT(A):
for i = 1 to A.length - 1
min = i
for j = i + 1 to A.length
if A[j] < A[min]
min = j
temp = A[i]
A[i] = A[min]
A[min] = temp


Loop invariants

At the start of each iteration of the inner for loop, A[min] is the smallest number in the subarray A[i..j−1].

Why n−1 elements?
In the final step, the algorithm will be left with two elements to compare. It will store the smaller one in A[n−1] and leave the larger in A[n]. The final one will be the largest element of the array, since all the previous iteration would have sorted all but the last two elements (the outer loop invariant). If we do it n times, we will end up with a redundant step that sorts a single-element subarray.

Running times
In the best-case time (the array is sorted), the body of the if is never invoked. The number of operations (counting the comparison as one operation) is:
$$(n - 1)(\frac{n + 2}{2} + 4)$$
In the worst-case time (the array is reversed), the body of the if is invoked on every occasion, which doubles the number of steps in the inner loop, that is:
(n-1)(n+6)
Both are clearly Θ(n2).

2.2-3 Consider linear search again (see Exercise 2.1-3). How many elements of the input sequence need to be checked on the average, assuming that the element being searched for is equally likely to be any element in the array? How about in the worst case? What are the average-case and worst-case running times of linear search in ‚Θ-notation? Justify your answers.

Solution:

If the element is present in the sequence, half of the elements are likely to be checked before it is found in the average case. In the worst case, all of them will be checked. That is, n/2 checks for the average case and n for the worst case. Both of them are Θ(n).

2.2-4 How can we modify almost any algorithm to have a good best-case running time?

Solution:

We can modify it to handle the best-case efficiently. For example, if we modify merge-sort to check if the array is sorted and just return it, the best-case running time will be Θ(n).

## Friday, 17 March 2017

### Chapter 2 Exercise 2.1, Introduction to Algorithms, 3rd Edition Thomas H. Cormen

2.1-1 Using Figure 2.2 as a model, illustrate the operation of INSERTION -SORT on the array A=⟨31,41,59,26,41,58⟩.

Solution:

2.1-2 Rewrite the INSERTION -SORT procedure to sort into nonincreasing instead of non-decreasing order.

Solution:

Insertion-Sort(A)


for j = 2 to A.length
key = A[j]
// Insert A[j] into the sorted sequence A[1..j − 1]
i = j − 1
while i > 0 and A[i] < key
A[i + 1] = A[i]
i = i − 1
A[i + 1] = key


2.1-3 Consider the searching problem:
Input: A sequence of n numbers A=<a1,a2,...,an> and a value v.
Output: An index i such that v=A[i] or the special value NIL if v does not appear in A.
Write pseudocode for linear search, which scans through the sequence, looking for v. Using a loop invariant, prove that your algorithm is correct. Make sure that your loop invariant fulfills the three necessary properties.

Solution:

Linear-Search(A,v):
for i = 1 to A.length
if A[i] == v
return i

return NIL


Initialization: Initially the subarray is empty. So, none of its’ elements are equal to v.
Maintenance: In i-th iteration, we check whether A[i] is equal to v or not. If yes, we terminate the loop or we continue the iteration. So, if the subarray A[1..i1] did not contain v before the i-th iteration, the subarray A[1..i] will not contain v before the next iteration (unless i-th iteration terminates the loop).
Termination: The loop terminates in either of the following cases,
• We have found index i such that v = Ai
• We reached the end of the array, i.e. we did not find v in the array A. So, we return NIL
In either case, our algorithm does exactly what was required, which means the algorithm is correct.

2.1-4 Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an .n C 1/-element array C . State the problem formally and write pseudocode for adding the two integers.

Solution:

Input: Two n-bit binary integers stored in two n-element array of binary digits (either 0 or 1)
A=<a1,a2,...,an> and B=<b1,b2,...,bn>.
Output: A (n+1)-bit binary integer stored in (n+1)-element array of binary digits (either 0 or 1) C=c1,c2,...,cn+1⟩ such that C=A+B.

Binary-Add(A,B):
n = A.length
for i = 1 to (n + 1)
C[i] = 0

carry = 0
for i = n to 1
C[i] = (A[i] + B[i] + carry) % 2
carry = (A[i] + B[i] + carry) / 2
C[i] = carry

return C


## Thursday, 16 March 2017

### Chapter 1 Problems, Introduction to Algorithms, 3rd Edition Thomas H. Cormen

1-1 Comparison of running times
For each function f(n) and time t in the following table, determine the largest size n of a problem that can be solved in time t, assuming that the algorithm to solve the problem takes f(n) microseconds.

Solution:

 1 second 1 minute 1 hour 1 day 1 month 1 year 1 century lg n n nlg n 62746 2801417 133378058 2755147513 71870856404 797633893349 68654697441062 1000 7745 60000 293938 1609968 5615692 56175382 100 391 1532 4420 13736 31593 146677 19 25 31 36 41 44 51 n! 9 11 12 13 15 16 17