CPSC 320 Sample Final Examination December 2013

Similar documents
Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Math 391: Midterm 1.0 Spring 2016

Asymptotic Analysis and Recurrences

Review Of Topics. Review: Induction

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2013

Selection and Adversary Arguments. COMP 215 Lecture 19

CSE 21 Practice Exam for Midterm 2 Fall 2017

CS 170 Algorithms Fall 2014 David Wagner MT2

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

CS2223 Algorithms D Term 2009 Exam 3 Solutions

Lecture 2: Divide and conquer and Dynamic programming

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

Algorithm Design CS 515 Fall 2015 Sample Final Exam Solutions

25. Minimum Spanning Trees

Week 5: Quicksort, Lower bound, Greedy

25. Minimum Spanning Trees

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

CS 161 Summer 2009 Homework #2 Sample Solutions

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

CS173 Running Time and Big-O. Tandy Warnow

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

Data Structures and Algorithms

N/4 + N/2 + N = 2N 2.

1 Terminology and setup

Advanced Analysis of Algorithms - Midterm (Solutions)

CMPSCI 611 Advanced Algorithms Midterm Exam Fall 2015

CSC 8301 Design & Analysis of Algorithms: Lower Bounds

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

CS361 Homework #3 Solutions

1 Closest Pair of Points on the Plane

Lecture 4. Quicksort

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

CPS 616 DIVIDE-AND-CONQUER 6-1

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Omega notation. Transitivity etc.

COMP 382: Reasoning about algorithms

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

Fundamental Algorithms

Fundamental Algorithms

CS Analysis of Recursive Algorithms and Brute Force

Insertion Sort. We take the first element: 34 is sorted

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Fundamental Algorithms

data structures and algorithms lecture 2

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case.

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

Consider a complete bipartite graph with sets A and B, each with n vertices.

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Lecture 5: The Principle of Deferred Decisions. Chernoff Bounds

Fast Sorting and Selection. A Lower Bound for Worst Case

Concrete models and tight upper/lower bounds

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

Methods for solving recurrences

Linear Selection and Linear Sorting

Module 1: Analyzing the Efficiency of Algorithms

Divide and Conquer Algorithms

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

Divide-and-conquer. Curs 2015

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!!

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence

CS 231: Algorithmic Problem Solving

Proof Techniques (Review of Math 271)

1 Primals and Duals: Zero Sum Games

Design and Analysis of Algorithms March 12, 2015 Massachusetts Institute of Technology Profs. Erik Demaine, Srini Devadas, and Nancy Lynch Quiz 1

CS325: Analysis of Algorithms, Fall Midterm

Hamiltonian paths in tournaments A generalization of sorting DM19 notes fall 2006

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

CS483 Design and Analysis of Algorithms

CS 350 Algorithms and Complexity

Central Algorithmic Techniques. Iterative Algorithms

Matching Residents to Hospitals

NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt): Name of Neighbor to your right (1 pt):

CS 188 Introduction to Fall 2007 Artificial Intelligence Midterm

COMP Analysis of Algorithms & Data Structures

Analysis of Algorithms - Using Asymptotic Bounds -

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

COMP Analysis of Algorithms & Data Structures

Notes on induction proofs and recursive definitions

1 Basic Combinatorics

Divide and Conquer Algorithms

Midterm 1. Your Exam Room: Name of Person Sitting on Your Left: Name of Person Sitting on Your Right: Name of Person Sitting in Front of You:

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

CSCI 3110 Assignment 6 Solutions

Data Structures in Java

Chapter 4 Divide-and-Conquer

SOLUTION: SOLUTION: SOLUTION:

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Transcription:

CPSC 320 Sample Final Examination December 2013 [10] 1. Answer each of the following questions with true or false. Give a short justification for each of your answers. [5] a. 6 n O(5 n ) lim n + This is false: 6 n ( 6 5 n = lim n 5 = + which means that 6 n ω(5 n ). Therefore 6 n / O(5 n ). [5] b. 1.5 n + n 2 O(1.5 n + n log n) [18] 2. Short Answers ) n This is true, since for n 13 we have n 2 < 1.5 n, which means that 1.5 n + n 2 1.5 n + 1.5 n = 2 1.5 n < 2 (1.5 n + n log n). Hence take c = 2 and n 0 = 13. [3] a. Why is it important to know that a problem is NP-Complete? Because then we know not to waste time trying to find an efficient algorithm for it that always returns the correct answer. [3] b. Explain why the following statement holds: Finding the median of an unordered array is as difficult as finding the i th order statistics of that array, for an arbitrary i. Hint: think of the implementations of algorithm RandomizedQuickSelect In order to find the median of the array, we will most likely end up trying to find the i th smallest element of a subset of m elements, where i m/2. In that sense, in order to be able to find the median, we need to be able to find an arbitrary order statistics. Thus finding the median is not any easier. [3] c. What is the main advantage of RandomizedQuickSelect over Select1? RandomizedQuickSelect performs well on average on every possible input, whereas Select1 performs very well (always) on some inputs, and very badly (always) on other inputs. [3] d. In our divide and conquer algorithm to find the closest pair of points in the plane, why was it sufficient, during the merge step, to consider points that were at most 15 positions apart in the list of points in the vertical strip around the dividing line?

page 2 Because if two points 16 or more positions apart on the list were closer than a distance δ, then it would mean that two points on the same side of the dividing line are closer than δ apart, which isn t possible by the definition of δ (the smallest distance found between two points on the same side of the dividing line). [3] e. Why does the Gale-Shapley (Stable Matching) algorithm discussed in class terminate after at most n 2 iterations? Because at every step a woman proposes to a man she has never proposed to. There are only n 2 possible couples, and hence the loop could not iterate more than n 2 times without repeating a proposal. [3] f. When do we use amortized analysis? When we are trying to prove a good upper-bound on the worstcase running time of a sequence of n operations. [9] 3. A Computer Science researcher has designed a new data structure called a sheap that supports operations insert, findminmax and extractminmax, and decides to use amortized analysis to determine the worst-case running time of a sequence of n operations on an initially empty sheap. She defines a potential function Φ for sheaps, such that Φ(S) 0 for every sheap S, and such that Φ(S) = 0 if the sheap S is empty. After analyzing the running time of the three operations, she has learned that An insert operation on a sheap with n elements takes time log n, and increases the sheap s potential by 2. A findminmax operation on a sheap with n elements takes time x + n, where x is the number of elements examined by the operation. The potential of the sheap goes down by x 1. A extractminmax operation starts by performing a findminmax operation. The remainder of the extractminmax operation takes time log n, and decreases the sheap s potential by 1. Give as tight a bound as possible on the worst-case running time of a sequence of n operations on an initially empty sheap. The amortized cost of insert is log n+2, the amortized cost of findminmax is x + n (x 1) = n + 1 and the amortized cost of extractminmax is n + 1 + log n 1 = n + log n. We thus get an upper bound of n max{log n + 2, n + 1, n + log n} O(n n) i=1 on the worst-case running time of a sequence of n operations.

page 3 [9] 4. For each of the following recurrence relations, determine whether or not the Master Theorem discussed in class can be used. If it can be used, apply it to derive the solution of the recurrence relation using O notation. If the Master Theorem can not be used, explain why briefly. { 2T ( n ) + n if n 2 [3] a. T (n) = 1 if n 1 No, because the term inside the T () is not in the form n/b where b is a constant. { 9T (n/3) + 2n 2 if n 3 [3] b. T (n) = 1 if n 2 Yes: this is case 2 of the Master theorem, and T (n) Θ(n 2 log n). { { [3] c. T (n) = 4T ( n/2 ) + n 2+odd(n) if n 2 1 if n is odd where odd(n) = 1 if n 1 0 if n is even No: this recurrence is sometimes in case 2 (when n is even) and sometimes in case 3 (when n is odd), so the theorem can not be used. [5] 5. Write a recurrence relation that describes the running time of the following algorithm as a function of n. Algorithm DoSomethingConstant runs in Θ(1) time, and algorithm DoSomethingLinear runs in Θ(N) time, Algorithm MysteryP(A, N, n) // // A is an array with N element, n is a value between 0 and N. // if (n = 0) then DoSomethingLinear(A, N) else For i n-1 downto 0 do DoSomethingConstant(A, n, i) MysteryP(A, N, n-1) DoSomethingConstant(A, n, i) endif You may treat N as a constant. Since the for loop statements take a total of Θ(n) time in addition to the recursive calls, we have { nt (n 1) + Θ(n) if n 1 T (n) = Θ(N) if n = 0. Note: this method ends up calling DoSomethingLinear for every permutation of the input array A (assuming that DoSomethingConstant swaps A[n] and A[i]) and runs in Θ(N n!) time.

page 4 [9] 6. Using the guess and test method, prove that the function T (n) defined by 5T (n 2) + 12T (n 3) + 32 if n 4 T (n) = 25 if n = 3 7 if n = 2 1 if n = 1 belongs to O(3 n ). We will show that T (n) c3 n x for some values of x and c that will be specified later. Note that the guess that most people would have started with, namely T (n) c3 n, does not work as one ends up with an extra +32 at the end of the induction step. Let us start by the induction step. Consider k 4. Assume that T (i) c3 i x for every i < k, and look at T (k). We have T (k) = 5T (k 2) + 12T (k 3) + 32 5(c3 k 2 x) + 12(c3 k 3 x) + 32 5c3 k 2 5x + 12c4 k 3 12x + 32 5c3 k 2 + 4c3 k 2 17x + 32 9c3 k 2 17x + 32 c3 k 17x + 32 Now, we observe that as long as x 2, 17x + 32 = x + (32 16x) x and so choosing x = 2 allows the induction step to work. Now let us deal with the base cases for n = 1, we need 1 c3 1 2 which is true for every c 1. for n = 2, we need 7 c3 2 2 which is true for every c 1. for n = 3, we need 25 c3 3 2 which is true for every c 1. Hence we can choose c = 1, and we have successfully shown that T (n) 3 n 2. We therefore conclude that T (n) O(3 n ), as required. [8] 7. Binomial and Fibonacci Heaps [2] a. How do we merge a pair of binomial trees of order k? We make the root with the larger key the leftmost child of the root with the smaller key.

page 5 [1] b. One of the operations supported by binomial heaps is critical, in the sense that it is needed in order to perform most of the other operations. Which one is it? It s the union operation. [2] c. Explain how the operation whose name you wrote in part (b) is used in the implementation of the extractmin operation. We perform extractmin by first deleting the root with the minimum and turning its children into another binomial heap. Then we call union on this new heap and the remainder of the original heap. [3] d. Why does the Fibonacci heap implementation of decreasekey use cascading cuts? Because we do not want a node other than a root to have lost more than one child. [15] 8. The CEO of a software company wants to keep his developers happy by giving them a bonus, but does not want to spend too much money. He thus wants to select as few developers as possible (these will get a bonus, and be happy), chosen so that every other developer likes at least one of those that get a bonus (and hence will be happy for him/her). The CEO s problem can be modeled using a graph G = (V, E): each node in the graph is a developer, and an edge (u, v) means that developer u likes developer v. The CEO is looking for a minimum subset W of the set V of vertices, where for every vertex u / W, there is an edge (u, w) where w W. This is called a minimum dominating set for the graph G. [9] a. Write a greedy algorithm that finds a subset W of V (it does need to be the subset with the fewest elements possible) with that property. Your mark will depend in part on the criterion you use to decide which vertex to add to W. You do not need to give pseudo-code, but you should indicate what data structure you are using to store the vertices that your algorithm has not dealt with yet. We store in each node N whether or not developer N is happy. The algorithm then proceeds as follows: while at least one vertex is not happy do for each vertex v of G set count(v) to 0 if v is happy, and 1 otherwise for each edge (v1, v2) of G if v1 is not happy then

page 6 increment count(v2) let v be the vertex with maximum count mark v happy add v to W for each edge (v1, v) of G mark v1 happy endwhile [3] b. What is the running time of the algorithm you described in part (a)? This algorithm will run in time O( V E ) time. [3] c. There is no dynamic programming algorithm known for the minimum dominating set problem. Knowing this, what can you conclude about the greedy algorithm you gave in part (a)? W ). It does not always return an optimal solution (that is, the smallest [17] 9. Anne and Simon have been arguing about where to have lunch after the CPSC 320 final examination, and decide to solve the decision by playing a sequence of Poker games. The first person to win n games gets to choose where they will have lunch. Anne is a slighly better poker player, and has a 51% chance of winning any individual Poker game. Let P (i, j) be the probability that Anne will be the first person to win n games, given that she only needs to win another i games (that is, Anne has won n i games so far), and that Simon only needs to win another j games (that is, he has won n j games so far). It can be proved that P (i, j) = 0.49P (i 1, j) + 0.51P (i, j 1) (1) Anne is worried about his chances of winning, and asks you to use dynamic programming to compute the probability P (n, n) that she will win n games before Simon does. [3] a. State how big a table you need to answer Anne s question using dynamic programming, and the meaning of each entry in the table. Both i and j will take values from 0 to n, and so we need a n + 1 by n + 1 table. [3] b. Give one possible order that you can use to compute the entries in the table from part (a).

page 7 We can compute them by increasing value of j, and for each j by increasing value of i. [3] c. List all of the base cases that will be needed when you are computing the entries in the table from part (a). That is, list the cases where you can not use equation (1) to compute P (i, j), and the value of P (i, j) for each of them. If i = 0 and j > 0 then P (i, j) = 1 (Anne has already won). If i > 0 and j = 0 then P (i, j) = 0 (Anne has already lost). [8] d. Write pseudo-code for an algorithm that uses dynamic programming to determine the probability that Anne will win n Poker games before Simon does. for i 1 to n do P[i,0] 0 for j 1 to n do P[0,j] 1 for i 1 to n do P[i,j] = 0.49*P[i-1,j] + 0.51*P[i,j-1] return P[n,n]