Homework 1

Due Date: Sep 19, 2014

1. Do CLRS Problem 3-2.

2. Do CLRS Problem 3-3a for all functions that do not involve the lg* function, i.e., for the following functions.

(√2)lg n      n2         n!        (lg n)!     (3/2)n
   n3      lg2 n       lg(n!)        22n       n1/lg n
ln ln n     n2n        nlg lg n      ln n        1
 2lg n    (lg n)lg n      en         4lg n      (n+1)!
√(lg n)   2√(2 lg n)      n           2n        n lg n
 22n+1

3. (cf CLRS Exercise 2.2-2) Here is a pseudocode for Selection Sort.

selection_sort(A) {
    n ← A.length
    for i ← 1 to n-1 do
    {
        minIndex ← i
        for j ← i+1 to n do
            if A[j] < A[minIndex] then minIndex ← j
        temp ← A[i]
        A[i] ← A[minIndex]
        A[minIndex] ← temp
    }
    return A
}
  1. Label each statement with the (tight big-O estimate) amount of time it takes to execute once (like we did in class).
  2. Give a tight big-O estimate of the running time of the algorithm.
(Here is a python code for iterative selection sort.)

4. Here is a pseudocode for a recursive Selection Sort. It differs from the iterative version of the previous problem in that it repeatedly selects the maximums (instead of the minimums) and puts them in the correct place.

main() {
    A ← input array
    return selection_sort(A, A.length)
}

selection_sort(A, n) {
    if n > 1 then
    {
        maxindex ← select(A, n)
        temp ← A[maxindex];  A[maxindex] ← A[n];  A[n] ← temp
        selection_sort(A, n-1)
    }
    return A
}

select(A, n) {
    if n = 1 then
        return 1
    else
    {
        maxindex ← select(A, n-1)
        if A[n] > A[maxindex] then
            return n
        else
            return maxindex
    }
}
Give a tight big-O estimate of the running time of main() using the method done in class. To do this, you need to first analyze the running time of select(A, n), selection_sort(A, n), and main(), in that order. Is your answer different from the previous problem? (Here is a python code for recursive selection sort.)

5. Modify the algorithm on page 4 of Handout #A4 so that the worst-case running time of main is Θ((n-1)!). Explain why your algorithm takes time Θ((n-1)!).
Here is a python code for TSP. You should at least write up your modified algorithm in python by modifying this given code, and test your modified code to make sure that it gives the correct answer and better running time than the original code.