CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Similar documents
IS 709/809: Computational Methods in IS Research Fall Exam Review

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

COMPSCI 611 Advanced Algorithms Second Midterm Exam Fall 2017

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:

COMPSCI 611 Advanced Algorithms Second Midterm Exam Fall 2017

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 561 Data Structures and Algorithms Fall, 2013

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

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

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

Math 391: Midterm 1.0 Spring 2016

Limitations of Algorithm Power

Exam EDAF August Thore Husfeldt

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

Exam EDAF May 2011, , Vic1. Thore Husfeldt

Quiz 1 Date: Monday, October 17, 2016

Practice Final Solutions. 1. Consider the following algorithm. Assume that n 1. line code 1 alg(n) { 2 j = 0 3 if (n = 0) { 4 return j

Divide-and-Conquer Algorithms Part Two

Examination paper for TDT4120 Algorithms and Data Structures

CS361 Homework #3 Solutions

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

CS 170 Algorithms Spring 2009 David Wagner Final

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

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

CSCE 551 Final Exam, April 28, 2016 Answer Key

CPSC 320 Sample Final Examination December 2013

Review Of Topics. Review: Induction

Preliminaries. Graphs. E : set of edges (arcs) (Undirected) Graph : (i, j) = (j, i) (edges) V = {1, 2, 3, 4, 5}, E = {(1, 3), (3, 2), (2, 4)}

6.045 Final Exam Solutions

CS 350 Algorithms and Complexity

Breadth-First Search of Graphs

CS 350 Algorithms and Complexity

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

Midterm 1 for CS 170

CSE613: Parallel Programming, Spring 2012 Date: May 11. Final Exam. ( 11:15 AM 1:45 PM : 150 Minutes )

MIDTERM Fundamental Algorithms, Spring 2008, Professor Yap March 10, 2008

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

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

Linear Time Selection

University of Washington March 21, 2013 Department of Computer Science and Engineering CSEP 521, Winter Exam Solution, Monday, March 18, 2013

Combinatorial Optimization

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

Selection and Adversary Arguments. COMP 215 Lecture 19

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

Problem Set 2 Solutions

data structures and algorithms lecture 2

Week 5: Quicksort, Lower bound, Greedy

Exam EDAF January 2012, , Sparta:A D. Thore Husfeldt

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

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

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

10.4 The Kruskal Katona theorem

Out-colourings of Digraphs

Algorithms Exam TIN093 /DIT602

CSE 321 Solutions to Practice Problems

Fundamental Algorithms 11

Data Structures and Algorithms

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

CMSC Discrete Mathematics SOLUTIONS TO FIRST MIDTERM EXAM October 18, 2005 posted Nov 2, 2005

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

Disjoint-Set Forests

CSE548, AMS542: Analysis of Algorithms, Fall 2016 Date: Nov 30. Final In-Class Exam. ( 7:05 PM 8:20 PM : 75 Minutes )

CS 161 Summer 2009 Homework #2 Sample Solutions

Fall 2016 Test 1 with Solutions

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Massachusetts Institute of Technology 6.042J/18.062J, Fall 02: Mathematics for Computer Science Professor Albert Meyer and Dr.

Data selection. Lower complexity bound for sorting

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

P P P NP-Hard: L is NP-hard if for all L NP, L L. Thus, if we could solve L in polynomial. Cook's Theorem and Reductions

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

2. (25%) Suppose you have an array of 1234 records in which only a few are out of order and they are not very far from their correct positions. Which

i times p(p(... (p( n))...) = n ki.

Hon.Algorithms (CSCI-GA 3520), Professor Yap Fall 2012 MIDTERM EXAM. Oct 11, 2012 SOLUTION

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

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science ALGORITHMS FOR INFERENCE Fall 2014

CS1800 Discrete Structures Fall 2016 Profs. Gold & Schnyder April 25, CS1800 Discrete Structures Final

How many hours would you estimate that you spent on this assignment?

5. DIVIDE AND CONQUER I

COMP Analysis of Algorithms & Data Structures

Randomized Algorithms

Recap & Interval Scheduling

HOMEWORK #2 - MATH 3260

7.4 DO (uniqueness of minimum-cost spanning tree) Prove: if all edge weights are distinct then the minimum-cost spanning tree is unique.

Introduction to Algorithms March 10, 2010 Massachusetts Institute of Technology Spring 2010 Professors Piotr Indyk and David Karger Quiz 1

MIDTERM I CMPS Winter 2013 Warmuth

CSE 202 Homework 4 Matthias Springer, A

COE428 Notes Week 4 (Week of Jan 30, 2017)

Part V. Intractable Problems

Exercises NP-completeness

CS 70 Discrete Mathematics and Probability Theory Fall 2016 Seshia and Walrand Midterm 1 Solutions

Problem Set 1. MIT students: This problem set is due in lecture on Wednesday, September 19.

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

MAT 271E Probability and Statistics

Complexity Theory VU , SS The Polynomial Hierarchy. Reinhard Pichler

Exam EDAF May Thore Husfeldt

On shredders and vertex connectivity augmentation

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

Final exam (practice) UCLA: Math 31B, Spring 2017

Transcription:

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005 Do all problems. Put your answers on blank paper or in a test booklet. There are 00 points total in the exam. You have 80 minutes. Please note the following: This exam is open book, open notes, but no electronic devices. There are eleven regular questions, plus one question that counts for Quiz. All regular questions are worth the same amount, regardless of their difficulty. Your lowest score on a regular question will be dropped. This makes each regular question worth % of your grade. The quiz question is worth 5% of your grade. In general, unless I say otherwise, you need not show your scratch work to get full credit for a correct answer. If your answer is incorrect, however, showing your work may earn you partial credit. You may take as given any fact proved in either of the two textbooks (CLRS or GJ).

. Show that the sum diverges. (Cf. CLRS A.2-) n=2 n lg n Answer There are at least two standard ways of doing this that I know of: (a) Split the sum up into blocks of size, 2,, 8, 6, etc. and lower-bound each term with the smallest term in its block: ( n=2 n lg n = 2 lg 2 + lg + ) ( + lg 5 lg 5 + + ) + 8 lg 8 ( 2 lg 2 + lg + ) ( + lg 8 lg 8 + + ) + 8 lg 8 = ( ) + ( lg 2 2 lg + ) + ( lg 8 8 + + ) + 8 = ( 2 lg 2 + lg + ) lg 8 + = ( + 2 2 + ) + = 2 k k= =. (b) Use an integral approximation with a change of variables u = ln x. Let k 2 be an integer. Noting that /(n lnn) is monotone decreasing as a function of n, we have k n=2 k n lg n = (ln 2) n=2 n ln n k+ dx (ln 2) 2 x ln x ln(k+) du = (ln 2) ln 2 u = (ln 2)[ln ln(k + ) lnln 2]. Taking the limit as k shows that the sum diverges, albeit very slowly. 2. Draw a recursion tree and give a tight asymptotic bound (i.e., using Θ( ) notation) on the solution of the recurrence T(n) = 2 n/2 T(n/2) + 2 n. You need not prove your answer. (Cf. CLRS.2-)

Answer Here is the tree. 2 n 2 n/2 2 n/2 2 n/2 2 n/ 2 n/ 2 n/ 2 n/ 2 n/.. The tree is full and balanced with depth lg n. For each nonleaf node x in the tree, the value at x is equal to the sum of the values at all of x s children. (This can be seen easily by induction.) Thus, the sum of values at each level is the same, namely 2 n. Therefore, T(n) = Θ(2 n lg n).. During the running of the procedure Randomized-Quicksort, how many calls are made to the random-number generator Random in the worst case? Give your answer using Θ-notation. (Cf. CLRS 7.-2).. Answer Note that worst case here refers to a case that maximizes the number of calls to Random. This may or may not coincide with a case that maximizes the total running time. There are Θ(n) calls to Random in the worst case. This is also true for the best case. Each call to random chooses a pivot. Each pivot value is chosen only once, after which it does not participate in further partitions. With n items in the list, there can be at most n pivots, hence at most n calls to Random. There is clearly a case where there are n calls to Partition (and hence n calls to Random), namely, when the pivot is always chosen to be an extreme value in the list to be partitioned.. Explain why any comparison-based algorithm that finds the median of a list of n numbers must always make at least n comparisons, even in the best case. (Cf. CLRS 8.-) Answer Note: we will assume that duplicate values are not allowed in the input list. The statement is false otherwise. (For example, on input a, a 2, a, a, a 5, an algorithm might compare a with a 2, a 2 with a, and a with a and thus find that a = a 2 = a ; the median must be this common value, and so the algorithm outputs a with only three comparisons.) The idea is that with fewer than n comparisons, there are groups of elements with no comparisons made between them. By tweaking one or another of these groups, we can change the median without changing the output of A, thus making A incorrect on at least one input.

Here is a more careful proof using this idea. Let A be any comparison-based algorithm that outputs one of the values in its input list (this is certainly true of any comparison-based median-finding algorithm). Suppose that there is some n-element input list a,...,a n on which A makes strictly fewer than n comparisons. We ll explain why there must be some input list b,..., b n on which A does not output the median (and hence A is not a median-finding algorithm). Consider the decision tree of A for inputs of size n, and let p be the path taken by A on input a,...,a n. By assumption, there are at most n 2 comparisons made along p. I claim that the elements of the input list can be divided into two disjoint, nonempty sets X and Y such that no element of X is compared with any element of Y along the path p. [Consider the input elements as vertices of an undirected graph. For each comparison of elements u and v along p, draw an edge connecting u and v. The resulting graph has at most n 2 edges, and so it is not connected. Let X be the vertices in any connected component of the graph, and let Y be the rest of the vertices.] Let M be the largest value in the input list a,..., a n, and let m be the smallest value. Set R = M m, the range of values in the list a,...,a n. With this list as input, algorithm A outputs some element a i at the end of path p. Suppose that a i X. (The argument for a i Y is the same but with the roles of X and Y reversed.) Let b,...,b n be the list that results from a,...,a n by adding R to every element in Y, and let c,...,c n be similar except that we subtract R from every element in Y (the elements in X stay the same). Note the following two facts: b,...,b n and c,...,c n have different medians. This follows from the fact that all Y -elements of the former are larger than all X-elements, and vice versa for the latter list. A outputs the same value (a i ) on the three inputs a,...,a n, b,...,b n, and c,..., c n. This is because all comparison along the path p give the same results among the three inputs, so A takes path p for all three, and outputs the same element a i, which, being in X, is the same in all three inputs. Thus A does not output the median for at least one of the input lists b,...,b n and c,...,c n. 5. An ordinary six-sided die is rolled three times in a row. What is the probability that the values come in strictly increasing order? (Cf. CLRS C.2-) Answer Here s the slick way. There are 6 possible outcomes (i.e., elementary events) in the sample space Ω = D D D, where D = {, 2,,, 5, 6} is the sample space for a single die roll. The set of outcomes where the three numbers occur in strictly increasing order can be placed in one-to-one correspondence with the set of three-element subsets of D. Thus there are ( ) 6 many possibilities. Each possible way is equally likely, with

probability /6 = /26. Thus the probability is ( 6 ) 6 = 5 5. 6. Show the red-black trees that result after successively inserting the keys,, 5,, 2 in that order into an initially empty red-black tree. Be sure to show the color of each node. (Cf. CLRS.-) Answer There are five trees in all. 5 5 2 5 7. Find an optimal parenthesization of a matrix-chain product whose sequence of dimensions is 5, 2,, 0,, 6. (Cf. CLRS 5.2-) Answer The optimal parenthesization is (A (A 2 (A A )))A 5. Here are the tables. m-table: 76 6 8 60 6 8 0 60 0 60 0 0 0 0 0 5 2 0 6 s-table: 2 2 A A 2 A A A 5 8. A sequence of n operations is performed on a data structure. The ith operation costs i if i is a perfect square, and otherwise. Using any method you like (e.g., aggregate analysis, accounting method, or potential method), determine the amortized cost per operation. Show your work. (Cf. CLRS 7.-, 7.-2)

Answer Aggregate Method Let n > 0 be an integer. The total cost of the first n operations is n n( n ) C = n + (i ) n + < n/2. 2 i= The amortized cost per operation is C/n, which is thus at most /2 = O(). Potential Method The idea is that the distance between two successive perfect squares, say i 2 and (i + ) 2, is 2i +, so we can amortize the cost of the next expensive operation by tacking on a constant additional charge (at least /2 per operation) to the cheap operations. Let c be any constant greater than or equal to /2, and for i =, 2,,... define Φ(i) = c(i i 2 ). That is, if we let m be the largest integer such that m 2 i and let k be such that i = m 2 + k, then Φ(i) = ck. We have two cases. k > 0. The amortized cost of the ith operation is + Φ(i) φ(i ) = + ck c(k ) = + c = O(). k = 0. We have i = m 2, and so the amortized cost of the ith operation is m+φ(i) Φ(i ) = m+0 c[m 2 (m ) 2 ] = ( 2c)m+2c = O(). Thus the amortized cost per operation is O(). 9. Show the binomial min-heap that results when the element 2 is removed of H (below): head[h] 0 20 5 0 26 9 6 2 5 0 28 9 2 (Cf. CLRS 9.2-) Answer We must merge the two heaps

head[h ] 0 20 5 26 9 0 6 head[h 2 ] 5 0 2 28 9 to get head[h] 5 0 2 0 20 5 28 9 26 9 6 0 0. An undirected graph G = (V, E) is bipartite if V can be partitioned into two disjoint sets V and V 2 with V = V V 2 such that every edge in E connects a vertex from V with a vertex from V 2. [This may not be relevant, but it is well-known that G is bipartite iff G has no cycles of odd length.] Explain in words how to use Breadth- First Search to find such a V and V 2 (which may not be unique) if G is bipartite, or else output, G is not bipartite. Keep your description high-level English, without pseudocode. The procedure you describe need not be the most efficient, but it should at least run in polynomial time. [Hint: Suppose G is bipartite with vertex partition V, V 2. Fix any vertex v V. Suppose WLOG that v V. What can you say about the (unweighted) distance between v and any vertex in V? Between v and any vertex in V 2?] (Cf. 22.2-6) Answer The idea is that if G is bipartite with partition V, V 2 and v V, then the unweighted distance between v and any vertex in V is even (or possibly ), and the distance between v and any vertex in V 2 is odd (or possibly ). Choose any v V. Run BFS to find the unweighted distance from v to other vertices. If not all vertices are reachable from v, then repeat BFS with an unreached vertex, and so on, until all vertices have finite distances. Let V be the set of vertices whose distances are even, and let V 2 be the set of vertices whose distances are odd. Now, for

each pair of distinct vertices u, w V, check if (u, w) E, and do the same for each pair of distinct vertices in V 2. If any adjacencies are found this way, say that G is not bipartite. Otherwise, output V and V 2. Here s an alternate approach that works. Starting with V and V 2 being empty, do the following repeatedly until all vertices are placed into either V or V 2 : Starting with an unplaced vertex v, put v into V and then run BFS from v. During the BFS, whenever a vertex w is updated as a result of being adjacent to u, check if u and w are in the same partition (either both in V or both in V 2 ). If yes, stop immediately and output, G is not bipartite. Otherwise, place w into the opposite set (the one that does not contain u) if it is not already in there. Finally, return V and V 2.. Give the final d and π values of the vertices obtained by running Dijkstra s algorithm on the directed graph below with source A: 6 B 20 C A D E 6 9 F (Cf. CLRS Exercise 2.-) Answer v d[v] π[v] A 0 nil B 8 E C E D A E 7 D F C Quiz credit: Recall the problem HAMILTONIAN CIRCUIT (HC) that we showed in class to be NP-complete: Instance: An undirected graph G. Question: Is there a cycle in G that includes each vertex of G exactly once? Consider the following problem: HAMILTONIAN PATH (HP) Instance: An undirected graph G. Question: Is there path in G that includes each vertex of G exactly once?

HP is clearly in NP. Show that HP is NP-complete by giving a polynomial reduction from HC to HP. That is, show how to easily transform an arbitrary undirected graph G into an undirected graph G such that G has a Hamiltonian circuit if and only if G has a Hamiltonian path. You may assume that G has at least three vertices. [Hint: Get G by adding a constant number of vertices to G along with some new edges, and possibly remove some edges from G.] Answer: Given an arbitrary undirected graph G with at least three vertices, we construct G as follows: Choose an arbitrary vertex v of G (it does not matter which one). Let N (the neighborhood of v) be the set of all vertices adjacent to v. Add three new vertices a, b, c to the vertex set of G. Add edges between a and v, between b and c, and between c and each vertex in N. Let G be the resulting graph. This transformation is clearly polynomial-time. Suppose G has a Hamiltonian circuit p. This circuit must pass through v and two of v s neighbors, x, y N. By removing (v, x) from p and adding the edges (a, v), (b, c), and (c, x) to p, we clearly get a Hamiltonian path in G. Conversely, suppose p is a Hamiltonian path in G. The path p must be of the form a, v,..., x, c, b for some x N, because a and b both have degree one. Then the subpath p = v,...,x of p must be a Hamiltonian path in G. Also, v and x are adjacent in G, but the edge (x, v) is not in p since G has at least three vertices. So adding the edge (x, v) to the end of p makes it a Hamiltonian circuit in G. We have shown that G has a Hamiltonian circuit if and only if G has a Hamiltonian path. Thus the map G G is a polynomial reduction from HC to HP.