Homework 1 CSE 246 Analysis of Algorithms, Spring 2015 Due
Transcription
Homework 1 CSE 246 Analysis of Algorithms, Spring 2015 Due
Homework 1 CSE 246 Analysis of Algorithms, Spring 2015 Due 20.03.2015, 14:30 1. Solve the following recurrence relations using backward substitution method: (a) T(n)=T(n-2)+n for n>1, T(1)=1, T(2)=2. (b) T(n)=4T(βπ/2β)+n for n>1, T(1)=1. (solve for π = 2π ). (c) T(n)=T(n-1)+2n, T(0)=0. π (d) T(n)=T(βπ)+1, T(2) = 1. (solve for π = 22 ). 2. (n+2)!, 3nlog(n+5)5, 2n-1, 115n8+n2n +1, ln2(βπ), βπ + 4 log 3 π , log10 (2βπ ) β 5 (a) For each of the above functions, indicate the class Ξ(g(n)) the function belongs to. (Use the simplest g(n) possible in your answers.) (b) List the above functions according to their order of growth from the lowest to the highest. 3. How many lines, as a function of n (in Ξ( · ) form), does the following program print? Write a recurrence relation and solve it. (Note: You need to give two different recurrence relations and two different solutions for odd and even values of n.) function func1(n) if n = 0: print_line("Hayatta en büyük hata, kendini hatasΔ±z sanmaktΔ±r. [Y.Emre]") else if n mod 2 = 0: //For even values of n func1(n-1) func1(n-2) else: //For odd values of n func1(n-1) 4. Consider the following function: function func2(A[1..n]) if n = 1 return A[1]; else: h = func2(A[1..n-1]); if h > A[n] return h; else return A[n]; What is the output of this algorithm? Give a recurrence relation for its time complexity. Solve the recurrence relation. What is the design technique of this algorithm? Is this algorithm better than the brute-force algorithm that gives the same result? Why? 5. Consider the following function: function func3(n) i = 1; t = 1; while i < n do i = i + t; t = t + 1; end while What is the time complexity of this algorithm? State your answer in big-Oh notation. 6. Implement both recursive and iterative algorithms to find nth Fibonacci number. Compare the running time of these algorithms for various values of n. Illustrate your results in a table. Comment on your results. Do they match to the theoretical epectations? 7. (a) An array A[0..n-2] contains n-1 integers from 1 to n in increasing order. (Thus one integer in the range is missing.) Design the most efficient algorithm you can to find the missing integer and indicate its time efficiency. (b) Repeat (a) for an unsorted array (instead of an array in increasing order). (c) Now consider A[0..n-3] which contains n-2 integers from 1 to n in increasing order. (Thus two integer in the range are missing.) Design the most efficient algorithm you can to find the missing integers and indicate its time efficiency. 8. Consider the following directed graph. a b e f d c g h i (a) Write down the adjacency matrix and adjacency lists specifying this graph. (Assume that the matrix rows and columns and vertices in the adjacency lists follow in the alphabetical order of the vertex labels.) (b) Starting at vertex a, and resolving ties by the vertex alphabetical order, traverse the graph by depth-first-search (DFS) and construct the corresponding depth-first search tree (or forest). Give the order in which the vertices were reached for the first time (pushed onto the traversal stack) and the order in which the vertices became dead ends (popped off the stack). (c) Using your answer in (b), give a topological order of the vertices. (d) Describe an algorithm based on DFS, to decide whether it is possible to reach from a vertex to all other vertices or not (in a directed graph). Apply your algorithm to the above graph. Note 1: You are supposed to answer all the questions but selected questions will be graded. Note 2: You may bring your homeworks to the class (on Friday) or give it directly to TA Muhammed Nur Avcil.