MCS 315 ALGORITHMS WORKSHEET II 1. Write down an algorithm whose worst-case time complexity is O(n2) and best-case time complexity is O(n). 2. Given the following chain of 5 matrices to be multiplied. Find the most efficient parenthesization for it by applying a dynamic programming approach. Show each step of your answer. Matrices A1 A2 A3 A4 A5 Dimensions 5x15 15x7 7x10 10x15 15x5 3. Given the following sequence of 4 matrices to be multiplied. Find the most effective parenthesization for it by applying a dynamic programming approach. Show each step of your answer. Matrices A1 A2 A3 A4 Dimensions 25x15 15x20 20x5 5x15 1 4. For each of the following problems, write down which design strategies was used (in class) a) Binary Search b) Quick Sort c) Merge Sort d) Minimum Spanning Tree (Kruskal’s Algorithm) e) Matrix Multiplication Order 5. Define the dynamic programming strategy. What are the basic approaches to dynamic programming? 6. Given a numeric array A, the partial sum array PSA(A) is an array P that has the same length as A in which P[i] is the sum of all the elements in the array segment A[1..i]. For example, the following figure show an array A and its partial sum array P = PSA(A): 7. Consider the set S = {a, b, c, d, e} of five functions a through e, which are classified as follows: 2 8. Which of the following sets of functions are subsets of O(n2)? 9. Which of the following sets are empty? 10. Show that ank ≠ Θ(np) where a is a constant and k and p are positive integers and k > p. 11. Write down a divide and conquer type algorithm whose division cost is constant, combination cost is defined by O(n3), at each step divides the problem into three equal size subproblems. 12. We are given two sorted lists [a1, a2, ..., an] and [b1, b2, ..., bn], each of size n. That is: a1 < a2 < ... < an and b1 < b2 < ... < bn. Write in pseudocode an algorithm that forms a single sorted list of size 2n. Establish the upper bound for the algorithm. 3 13. Determine the lower bound of finding the median element in a list L[1:n] by a comparison-based algorithm. Assume that the elements in the list are distinct and n is odd. (Median element in L[1:n] is the element which is larger than (n-1)/2 elements of L and smaller than (n-1)/2 elements of L.) 14. The matrix-vector product of an nxn matrix A=(aij)nxn and an ndimensional vector X=(x1,x2,…,xn) is the n-dimensional vector Y=(y1,y2,…,yn), where: n yi a ij x j , i 1,..., n j 1 Write in pseudocode an algorithm that finds the matrix-vector product. Find the complexity. 15. Find a minimum spanning tree of the following graph using Kruskal’s algorithm. 16. Give brief answers to the following questions. a) Give an example problem which is known that it cannot be solved in polynomial time b) What is intractable problem? c) What is the relation between the problems in class NP-Complete ? 17.Answer the following sentences as TRUE or FALSE. 1. (T/F) For a given optimization problem, there exists only one optimal solution. 2. (T/F) Dynamic programming is only applicable when subproblems share common subproblems. 3. (T/F) Greedy algorithms use locally optimal choice. 4. (T/F) Adjacency matrix representation of a graph should be preferred when the graph is sparse. 4 5. (T/F) Adjacency list representation of a graph uses Θ(E+V) amount of memory. 6. (T/F) If the error rate can be controlled, an incorrect algorithm can be useful. 7. (T/F) For an NP-Complete problem, we do not know whether there exist an efficient polynomial time algorithm or not. 8. (T/F) Loop invariants can be used to prove that algorithms are efficient. 9. (T/F) Among two algorithms solving the same problem, when the problem size is n→∞, we must prefer the one having “quadratic” running time function to the one having “linear” running time function. 10. (T/F) For the same algorithm, O(n2) is more informative than Θ(n2). 11. (T/F) 5n3 ≠ o(n3). 12. (T/F) In any case, QuickSort algorithm performs better than the InsertionSort algorithm. 13. (T/F) We may add randomization to an algorithm in order to obtain good average case performance over all inputs. 14. (T/F) Greedy approaches may find optimal solutions. 15. (T/F) In Kruskal’s algorithm, we always select the current safe edge with the “least-weight” that connects two distinct components. 16. (T/F) Some tractable problems can not be solved in polynomial time. 17. (T/F) For class NP, a solution for a given problem can be verified in polynomial time. 18. (T/F) If a graph is not directed, it cannot be searched. 19. (T/F) Both Breadth First and Depth First Searches run in exponential time. 20. (T/F) n2 (2n ) 21. (T/F) n! O(2n ) 22. (T/F) n3 3n2 n 1(n3 ) 5 18. For each of the following pairs of functions f(n) and g(n), state whether f(n) = O(g(n)), f(n) = Ω(g(n)), f(n) = Θ(g(n)) or none of the above. a) f(n) = n , g(n) = lg(n + 3) b) f(n) = 2n − n2, g(n) = n4 + n2 c) f(n) = n n , g(n) = n2 − n d) f(n) = n + n n , g(n) = 4n lg(n2 + 1) e) f(n) = n2 + 3n + 4, g(n) = 6n + 7 6