CSCI-GA.1170-001, 003 Fundamental Algorithms September 6, 2023 Problem Set 1 Lecturer: Yevgeniy Dodis Due: 5 pm on Thursday, September 14 Problem 1-1 (Asymptotic Comparisons) 10 points For each of the following pairs of functions f (n) and g(n), state whether f is O(g); whether f is o(g); whether f is Θ(g); whether f is Ω(g); and whether f is ω(g). (More than one of these can be true for a single pair or none of it!) (a) f (n) = 1 n 4 + 3n17 ; g(n) = 16log2 n . 5 (b) f (n) = (nn )5 ; g(n) = n(n ) . (c) f (n) = nlog2 c ; g(n) = clog2 n . (d) f (n) = log2 (n5 + 5n4 + 4n3 + 3n2 + 2n + 1); (e) f (n) = 4log2 n ; g(n) = log2 (n10 + n8 + n6 + n4 + n2 + 100). g(n) = 0.8n2 + 10n(log2 n)42 . Problem 1-2 (Order of Growth) 12 points (a) (8 points) For each of the following functions f (n), find a “canonical” function1 g(n) such that f (n) = Θ(g(n)). For example, 3200n + 2n2 log24 (n2 ) = Θ(n2 log24 n). Briefly justify your answers (and I mean briefly). 3n +7n73 , 3n+log3 n , log(n34 +5), 22n , p n4 − 5n n5 + 30n4 , , log2 n+33, n2 log3 n+n3 log2 n 55555 (b) (4 points) Based on your answers in part (a), sort the resulting “canonical” (not original)2 functions in asymptotically increasing order. Problem 1-3 (Selection Sort) 7 points Consider sorting n numbers stored in array A by first finding the largest element of A and exchanging it with the element in A[n]. Then find the second largest element of A and exchange it with A[n − 1]. Continue in this manner for the first n − 1 elements of A. (a) (4 points) Write a (non-recursive) pseudocode for this algorithm, which is known as selection sort. Give the best-case and worst-case running times of selection sort in Θ-notation. 1 2 I.e., function of the form an nb logc n for constants a ≥ 1 and b, c ≥ 0. This way even if you got part (a) wrong, you can still have correct solution to part (b). PS 1, Page 1 (b) (3 points) In this question, you will need to prove the correctness of your algorithm from part (a). In order to do this, you will first state a suitable loop invariant. You will then prove correctness using this loop invariant and induction. Problem 1-4 (Insertion Sort) 14 points Let A[1, . . . , n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair of indices (i, j) is called an inversion of A. (a) (2 points) List all inversions of the array [8, 5, 2, 7, 9]. (b) (3 points) In the lecture, we discussed Insertion-Sort algorithm. This is a pseudocode implementation of the algorithm: Insertion-Sort(A, n) for j = 2 to n key = A[j] i=j−1 while i > 0 and A[i] > key A[i + 1] = A[i] i=i−1 A[i + 1] = key Let I(n) be the function representing the number of inversions of an input array A[1, . . . , n]. Then, formulate an expression for: (i) S(n) where S(n) is the function representing the number of swaps that the algorithm performs, in terms of n and I(n). (ii) T (n) where T (n) is the function representing the running time of the algorithm, in terms of I(n) and n. Justify your answers! (Hint: You may find it useful to first express T (n) as a function of S(n).) (c) (4 points) The Insertion-Sort algorithm discussed in lecture and shown in part (b) works by “shifting in” the new element into the sorted portion from the right. That is, it compares the key element to the largest element in the sorted portion of the array, and moving that element to the right, if it was larger than the key, and then comparing the key to successively smaller elements until the right position is found. In this question, you will write the pseudocode of a different variant of insertion sort that “shifts in” the new element from the left. That is, your implementation should work by comparing the key to the smallest element in the sorted portion of the array and then iterate by comparing to successively larger elements. (At each point in time, the sorted portion should still be at the beginning of the array.) PS 1, Page 2 (d) (3 points) We will repeat part (b) but now for the modified Insertion-Sort algorithm from part (c). Let I(n) be the function representing the number of inversions of an input array A[1, . . . , n]. Then, formulate an expression for: (i) S(n) where S(n) is the function representing the number of swaps that the algorithm performs, in terms of n and I(n). (ii) T (n) where T (n) is the function representing the running time of the algorithm, in terms of I(n) and n. Justify your answers. (Hint: You may find it useful to first express T (n) as a function of S(n).) (e) (2 points) Use parts (b) and (d) to determine how each of the algorithm performs for the following cases: (i) I(n) = Θ(n2 ) (ii) I(n) = o(n2 ) Justify your answer. Use this to conclude which algorithm is the better choice? PS 1, Page 3