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

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

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

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Fall 2016 Test 1 with Solutions

CS 170 Algorithms Fall 2014 David Wagner MT2

CSI 4105 MIDTERM SOLUTION

CS 170 Algorithms Spring 2009 David Wagner Final

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

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

Midterm Exam 2 Solutions

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

University of New Mexico Department of Computer Science. Final Examination. CS 362 Data Structures and Algorithms Spring, 2007

CS173 Lecture B, November 3, 2015

Assignment 4. CSci 3110: Introduction to Algorithms. Sample Solutions

CS60007 Algorithm Design and Analysis 2018 Assignment 1

P, NP, NP-Complete, and NPhard

Dynamic Programming: Shortest Paths and DFA to Reg Exps

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

More on NP and Reductions

Lecture 18: More NP-Complete Problems

Limitations of Algorithm Power

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Computational Complexity and Intractability: An Introduction to the Theory of NP. Chapter 9

COL106: Data Structures and Algorithms (IIT Delhi, Semester-II )

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Final Exam Version A December 16, 2014 Name: NetID: Section: # Total Score

Midterm II : Formal Languages, Automata, and Computability

CPSC 320 Sample Final Examination December 2013

Final Exam (Version B) December 16, 2014 Name: NetID: Section: 1 2 3

CSE 202 Homework 4 Matthias Springer, A

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):

Data Structures and Algorithms Chapter 3

Math 391: Midterm 1.0 Spring 2016

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

CS 161 Summer 2009 Homework #2 Sample Solutions

CSE 421 Introduction to Algorithms Final Exam Winter 2005

CS 6505, Complexity and Algorithms Week 7: NP Completeness

Algorithms Re-Exam TIN093/DIT600

Algorithms Exam TIN093 /DIT602

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

NP and Computational Intractability

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:

Instructor N.Sadagopan Scribe: P.Renjith. Lecture- Complexity Class- P and NP

Mid-term Exam Answers and Final Exam Study Guide CIS 675 Summer 2010

CSCI3390-Lecture 17: A sampler of NP-complete problems

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

Design and Analysis of Algorithms April 16, 2015 Massachusetts Institute of Technology Profs. Erik Demaine, Srini Devadas, and Nancy Lynch Quiz 2

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.

Essential facts about NP-completeness:

Connections between exponential time and polynomial time problem Lecture Notes for CS294

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

CS Fall 2011 P and NP Carola Wenk

CSCI3390-Second Test with Solutions

Branching. Teppo Niinimäki. Helsinki October 14, 2011 Seminar: Exact Exponential Algorithms UNIVERSITY OF HELSINKI Department of Computer Science

9.1 HW (3+3+8 points) (Knapsack decision problem NP-complete)

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Quiz 1 Date: Monday, October 17, 2016

Technische Universität München Summer term 2010 Theoretische Informatik August 2, 2010 Dr. J. Kreiker / Dr. M. Luttenberger, J. Kretinsky SOLUTION

Introduction to Divide and Conquer

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

Admin NP-COMPLETE PROBLEMS. Run-time analysis. Tractable vs. intractable problems 5/2/13. What is a tractable problem?

Methods for solving recurrences

Module 1: Analyzing the Efficiency of Algorithms

CSE 355 Test 2, Fall 2016

Complexity Theory Final Exam

Complexity Theory VU , SS The Polynomial Hierarchy. Reinhard Pichler

Outline. Complexity Theory EXACT TSP. The Class DP. Definition. Problem EXACT TSP. Complexity of EXACT TSP. Proposition VU 181.

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

Lecture 12 : Recurrences DRAFT

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

NP-Hardness reductions

1 Primals and Duals: Zero Sum Games

Name: Student ID: Instructions:

Examination paper for TDT4120 Algorithms and Data Structures

Algorithms and Their Complexity

RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 616 FINAL EXAM WINTER 2015

Correctness of Dijkstra s algorithm

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

NP-Complete Problems. More reductions

Algorithm Design and Analysis

CS2223 Algorithms D Term 2009 Exam 3 Solutions

COMP 382: Reasoning about algorithms

COL 352 Introduction to Automata and Theory of Computation Major Exam, Sem II , Max 80, Time 2 hr. Name Entry No. Group

Aditya Bhaskara CS 5968/6968, Lecture 1: Introduction and Review 12 January 2016

Chapter 4 Divide-and-Conquer

NP-Complete Reductions 2

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

Summer School on Introduction to Algorithms and Optimization Techniques July 4-12, 2017 Organized by ACMU, ISI and IEEE CEDA.

1 More finite deterministic automata

Complexity theory for fellow CS students

CS361 Homework #3 Solutions

Discrete Optimization 2010 Lecture 10 P, N P, and N PCompleteness

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

NP-Completeness. Andreas Klappenecker. [based on slides by Prof. Welch]

Transcription:

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer 2009. Instructor: Dr. Lior Malka Final Examination, July 24th, 2009 Student ID: INSTRUCTIONS: There are 6 questions printed on pages 1 7. Exam duration: 2 hours and 30 minutes. No aids are allowed, except for 1 letter size, double-sided cheat sheet. Start with easy questions and do not spend too much time on any section. The marks for each question appear in square bold brackets next to the question. Before you answer a question, read all of its sections and make sure that you fully understand them. You are always encouraged to ask for clarifications. When asked to give an algorithm, the intention is that you provide the asymptotically most efficient implementation and describe it concisely. Unless otherwise stated, no pseudocode is necessary. Unless otherwise stated, when asked to provide lower or upper bounds, the intention is that you provide the tightest bounds possible under worst case analysis. Answers are to be written on examination paper only. You can use another piece of paper as a draft. If you also use the examination paper as a draft, do not forget to cross your notes. Do not use red pen. Good Luck! Q1 Q2 Q3 Q4 Q5 Q6 Total 1

1. [10] General. Let A be an unsorted array of n distinct integers. The algorithm SortAndP rint(a, i, j) first sorts A and then prints all the elements between index i to index j in A. Describe an algorithm that outputs the same numbers, possibly in a different order, but in time Θ(n). The input to your algorithm is an unsorted array A of n distinct elements and indices i, j such that 1 i, j n. Informally explain why your algorithm is correct and why it runs in time Θ(n). Answer. Let A be the array A after sorting. The element A [i] is an element of A whose order is i. Thus, the required algorithm should output all the numbers in A whose order is between i to j. Intuitively, we achieve running time O(n) by using the Select algorithm and then printing everything in between the end points. Formally, on input an array A and integer i, algorithm Select outputs the i-th order element of A in time Θ(n). We store x =Select(A, i) and y =Select(A, j), and then we scan A, printing each element A[k] if x A[k] y. Two invocations of Select are 2 Θ(n) and scanning A is Θ(n). The total is Θ(n). 2. [15] Recurrences. (a) [5] Using the Master Theorem, solve T (n) = 9T ( n 3 )+n. Provide the values you are using (e.g., a, b, c, ɛ, f). Answer. In this case we have a = 9, b = 3, f(n) = n, and n log b (a) = n 2. For ɛ = 1 2 we get that f(n) = O(n log b (a) ɛ ), and therefore the first case of the master theorem applies. That is, T (n) = Θ(n log b (a) ) = Θ(n 2 ). (b) [10] Using the Guess and Test Method, prove an upper bound on T (n) = 9T ( n 3 ) + n. Your bound should apply to any n 1, and you can define T for the base case in any way. Answer. We already know from the previous section that T (n) O(n 2 ), so we try to prove the induction step first, assuming T (k) k 2 for all k < n: T (n) = 9 T ( n 3 ) + n 9 ( n 3 ) 2 + n = n 2 + n. Since we need to show that T (n) n 2, the induction step does not work and we need to revise it. We try proving that T (n) n 2 n. The induction step is now T (n) = 9 T ( n ( 3 ) + n 9 ( n 3 )2 n ) + n = n 2 2n n 2 n, 3 which coincides with the induction hypothesis. Turning our attention to the base case, we need to find an n N for which T (n) = 9 T ( n 3 ) + n n2 n holds. It turns out that if we take T (0) = 0, then T (0) = 9 T (0) + 0 0 holds. We conclude that our guess T (n) n 2 n can be formally proved using induction by defining T (n) = 0 if n = 0 and T (n) = 9 T ( n 3 ) + n. 1

3. [20] Dynamic Programming. Let A be an array of numbers a 1, a 2,..., a n N. The longest increasing subsequence of A is the largest ordered subset of a 1, a 2,..., a n in which each element is strictly greater than the previous one. For example, the longest increasing subsequence of 5, 2, 8, 6, 3, 6, 9, 7 is 2, 3, 6, 9. The length of this subsequence is 4. We can compute the length of the longest increasing subsequence by creating an array M, initializing it with default values, and then computing new entries based on previous ones. (a) [4] Explain why the problem of computing the length of the longest increasing subsequence can be solved using dynamic programming. Answer. There are two reasons: optimal substructure, and overlapping subproblems. The optimal substructure is due to the fact that we can compute the length of the longest increasing subsequence for the numbers a 1,..., a i by looking at the values of the longest increasing subsequence for the numbers a 1,..., a i 1. The overlapping subproblems are due to the fact that at stage i we recompute more than once the same solutions for subproblems from stages i < i. (b) [8] Define the size of the array M, the initial default values of entries in M, and the recursive formula that computes new entries in M based on previous ones. Answer. We define M[i, j] to be the length of a longest increasing subsequence in the subarray A[1,..., i] such that the corresponding longest increasing subsequence ends at index j. 1 The value of i ranges from 1 to n, and so is the value of j. Thus, M is of size n by n. Intuitively, the length of the longest increasing subsequence in a 1,..., a i is computed by looking at the values a 1,..., a i 1. For each of these, we look at A[j] and decide whether A[i] could be appended to the sequence or not. Then, we take the maximum over these values. Formally, null if j > i or j = 0 M[i, j] = 1 if i = 1 and j = 1 max 1 k j (δ k ) otherwise, where δ k = M[i 1, k] if A[k] A[i] and δ k = M[i 1, k] + 1 otherwise. (c) [8] After computing M, what entry contains the length of the longest increasing subsequence in A? Prove it using induction. Answer. The index i for which M[n, j] takes the maximal value (over all 1 j n) is the length of the longest increasing subsequence in A. Proof: by induction on i, we prove that M[i, j] is the length of a longest increasing subsequence in A[1,..., i] whose corresponding longest increasing subsequence ends at index j. The base case trivially holds when i = 1 because j is also 1. Assume the induction hypothesis for all numbers smaller than i. For the 1 Notice that saying that the subsequence ends at index j is not the same as saying that the subsequence ends at the number A[j]. 2

induction step, assume towards contradiction that there is j i and a longest increasing subsequence S in A[1,..., i] ending at index j such that M[i, j ] < S. (Case 1:) If A[j ] A[i], then S is also a longest increasing subsequence in A[1,..., i 1] ending at index j i 1. By the definition of M, it follows that M[i, j] = M[i 1, j ] < S, and contradiction to the induction hypothesis for i 1. (Case 2:) If A[j ] < A[i], then S {A[i]} is a longest increasing subsequence in A[1,..., i 1]. The length of this sequence is S 1, and it ends at some index j i 1. By the definition of M, it follows that M[i 1, j] < S 1 for all j such that A[j] < A[i]. Thus, in particular M[i 1, j ] < S 1, and contradiction to the induction hypothesis for i 1. The induction follows. 4. [15] Amortized Analysis. A stack is a data structure with the operation push(x), which pushes an element into the stack, and pop(), which pops an element out of the stack. Let k N. A third operation called debug is added to the stack. This operation prints the content of the stack to the screen. It is triggered automatically by the stack after each sequence of k push or pop operations. (a) [9] Assuming the size of the stack never exceeds k, describe the amortized cost of each operation and use these costs to upper bound the total cost of n stack operations. Answer. In a sequence of n stack operations there are at most n k debug operations, each costing at most k steps, for a total of n k k = n steps. Thus, we assign $ 2 to the push operation, $ 0 to the pop operation, $ 0 to the debug operation, and $ n to the stack (alternatively, we can assign $ n to the first debug operation and $ 0 to the following debug operations, and specify that the remaining credit goes to the stack). Now, in each sequence of n of the 3 operations, every push and pop operation costs 1 step. Thus, these operations are covered by the $ 2 assigned to the push operation. Debug operations are also covered by the stack because there are n k of them, each of cost at most k. Thus, the time complexity of a sequence of n operations is 2n + n = O(3n). (b) [3] Give a sequence of n stack operations that shows that your upper bound from Section (a) is also a lower bound. Assume again that the size of the stack never exceeds k. Answer. There are several sequences showing that the lower bound is Ω(n). For example, a sequence of alternating push and pop operations requires 2 n 2 = n steps. (c) [3] Without limiting the size of the stack, give a lower bound on the cost of n stack operations. Answer. Consider a sequence of n push operations. The first debug costs k steps, the second costs 2k steps, and so on until the last debug, which costs n k k. The total cost is therefore lower bounded by k n/k i=1 i = n 2 ( n n2 k + 1) = Ω( k ). 3

5. [20] Graph algorithms. Let G = V, E be an undirected connected graph with positive weights on the edges and let s V. Recall that after invoking Dijkstra(G, s), each vertex v has a distance value d(v). We define a graph G from G by adding exactly one arbitrary edge u, v of weight w u,v > 0. That is, G = V, E, where E = E { u, v } and u, v / E. In the new graph G, we denote by d (v) the weight of a shortest path from any vertex v V to s. (a) [6] Prove that the distance of at least one of u and v is unaffected by the new edge. Formally, prove that either d (u) = d(u) or d (v) = d(v). Answer. To prove this claim we show that if d (u) < d(u), then d (v) = d(v) (the other case is symmetric). If d (u) < d(u), then the shortest path from u to s must use the edge u, v, or else contradiction to the choice of d(u). Thus, the shortest path from u to s starts at u, goes to v on the edge u, v, and then continues to s on the shortest path from v to s. The path cannot return to u because all the edges have positive weight. Since the shortest path from v to s in G does not pass through u, the shortest path from v to s in G does not use the edge u, v. Hence, d (v) < d(v) contradicts the choice of d(v) as the weight of a shortest path in G. Thus, d (v) = d(v). (b) [6] Prove that if w u,v d(u) d(v), then the distance of u and v is unaffected by the new edge (formally, d (u) = d(u) and d (v) = d(v)). Answer. Assume towards contradiction that either d (u) < d(u) or d (v) < d(v). Suppose d (u) < d(u) (the other case is symmetric). As we saw in the previous section, the shortest path from u to v in G starts at u, goes to v, and then continues to s on the shortest path from v to s in G. Thus, d(u) > d (u) = w u,v + d (v) = w u,v + d(v). This leads to the inequality w u,v < d(u) d(v), and contradiction. (c) [8] Prove that if w u,v d(u) d(v), then the distances of all vertices are unaffected (formally, d(t) = d (t) for any t V ). Hint: show that no shortest path in G contains the edge u, v. Answer. Assume towards contradiction that there is t V such d (t) < d(t), and consider the shortest path p t,s from t to s in G. This path must use the edge u, v, or else contradiction to the choice of d (t). Suppose the path starts at t, passes through u, then uses the edge u, v, and continues on the shortest path p v,s from v to s. This implies that the weight of the shortest path from u to s in G is w u,v + d (v) = w u,v + d(v) < d(u), which contradicts w u,v d(u) d(v) 6. [20] NP-Completeness. Consider the efficient compiler from 3-SAT to CLIQUE. The input to the compiler is a 3-CNF formula ϕ, and the output is G(ϕ), k, where G(ϕ) is a graph and k is a number. (a) [3] We represent ϕ using an array A with 3 columns. The number of rows equals the number of clauses in ϕ. For each j {1, 2, 3} and i, the value of A[i, j] is t (respectively, -t) if the literal 4

x t (respectively, x t ) appears in the j-th position of the i th clause. Draw A for the formula ϕ = (x 1 x 3 x 4 ) (x 3 x 5 x 5 ) (x 2 x 7 x 9 ) (x 6 x 2 x 5 ) (x 7 x 8 x 9 ) Answer. -1 3 4 3 5-5 -2 7 9 6 2 5-7 -8 9 (b) [10] Give pseudocode for the efficient compiler from 3-SAT to CLIQUE. On input an array A representing a 3-CNF formula ϕ as in Section (a), the compiler outputs B, k, where B is a matrix representation of the graph G(ϕ) and k is a number. Briefly and informally explain what your algorithm does and why it is correct. Answer. The algorithm numbers entry i, j in A as vertex 3(i 1) + j in B. It then connects all 3n vertices, unless the literals corresponding to the entries are complements, or they are in the same row. Correctness follows from the fact that in G(ϕ) we do not connect complementing literals or literals in the same clause in ϕ. Thus, A (which represents ϕ) has a satisfying assignment if and only if B (which represents G(ϕ)) has a clique of size k. Compiler(A) n = A.rows B is a 3n by 3n matrix For i = 1 to n For j = 1 to 3 // the number 3(i 1) + j is a label for a vertex in G(ϕ) For i = 1 to n For j = 1 to 3 // the number 3(i 1) + j is a label for a vertex in G(ϕ) if i i and A[i, j] A[i, j ] B[3(i 1) + j, 3(i 1) + j ] = 1 return B, n (c) [2] What is the running time of your compiler? Answer. On input a 3 by n matrix A of length m = 3n, the compiler scans the entire matrix A for each cell of A, resulting in time 3n 3n, which is Θ(m 2 ). (d) [5] Assume that your compiler from Section (b) is correct. Denote the length of the matrix A by m, and the length of B, k by n. Prove that if there is c N such that CLIQUE can be solved in time n c, then there is a N such that 3-SAT can be solved in time O(m a ). Prove that your 5

algorithm is correct and provide the value of a. Answer. We define an algorithm that on input a 3-CNF ϕ (represented as a matrix A of size m) compiles A into G(ϕ), k (represented as a pair B, k of size n = O(m 2 )) and then outputs the answer of the CLIQUE algorithm on G(ϕ), k. The running time of the CLIQUE algorithm is O(n c ) = O((m 2 ) c ) = O(m 2c ), which implies that the running time for our algorithm is O(m 2 ) + O(m 2c ) = O(m 2c ). Thus, a = 2c. To prove correctness, notice that if ϕ is satisfiable, then G(ϕ) has a clique of size k, which implies that the CLIQUE algorithm will output 1. Conversely, if ϕ is not satisfiable, then G(ϕ) does not have a clique of size k, which implies that the CLIQUE algorithm will output 0. Hence, our algorithm solves 3-SAT. 6