Midterm2_Solution_311

pdf

School

University of Massachusetts, Amherst *

*We aren’t endorsed by this school

Course

311

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

10

Uploaded by GeneralEnergyCoyote11

Report
COMPSCI 311: Introduction to Algorithms Second Midterm Exam — SOLUTIONS October 25, 2023 Name: ID: Instructions: Answer the questions directly on the exam pages. Show all your work for each question. Providing more detail including comments and expla- nations can help with assignment of partial credit. If you need extra space, use the blank pages at the end of the exam. No books, notes, calculators or other electronic devices are allowed. Any cheating will result in a grade of 0. If you have questions during the exam, raise your hand. 1
Question 1. ( 4 points ) Topological orderings. List all possible topological orderings for this graph. Solution: a, b, c, d, e, f a, b, d, c, e, f a, b, d, e, c, f a, d, b, c, e, f a, d, b, e, c, f Question 2. ( 5 points ) You are given n numbers and want to order them as v 1 , v 2 , . . . , v n so the value of the ordering, defined as follows, is as small as possible: value of ordering = v 1 + 2 v 2 + 3 v 3 + . . . + nv n 2.1 ( 1 points ): Suppose the numbers are 1, 2, and 5, and are ordered as v 1 = 5 , v 2 = 1 , v 3 = 2 . What is the value of this ordering? Solution: The value is 5 + 2 · 1 + 3 · 2 = 13. Claim : The value is minimized by any non-increasing ordering, i.e., whenever v 1 v 2 . . . v n . Proof : Suppose for contradiction that some ordering v 1 , . . . , v n minimizes v 1 +2 v 2 +3 v 3 + . . . + nv n but does not satisfy v 1 v 2 . . . v n . Then there is some index i such that v i < v i +1 . Then ... 2.2 ( 4 points ): Complete the proof by arguing that the ordering v 1 , . . . , v n does not have minimum value, a contradiction. Solution: Consider swapping the numbers in positions i and i + 1 to get a new ordering. The original value minus the new value is h iv i + ( i + 1) v i +1 i h iv i +1 + ( i + 1) v i i = v i +1 v i > 0 Therefore, the new values is smaller, which contradicts our assumption that the ordering was optimal. 2
Question 3. ( 10 points ) You play on a local game show where you will complete n different challenges and try to make as much money as possible. The i th challenge takes t i minutes to complete. You start off with winnings of $ 1000 but will lose one dollar per minute for each challenge that is not yet completed (i.e., when you have two challenges remaining, you lose two dollars per minute). In other words, if you complete challenge i at time f ( i ), you lose f ( i ) dollars total from that challenge . Your goal is to order the challenges to lose as little as possible. Assume the times t i are short enough so no matter what order you choose your winnings will always be positive. 3.1 ( 2 points ): Suppose t 1 = 2 , t 2 = 5 , t 3 = 1 . What is the optimal ordering? How much do you lose from your initial $ 1000 in winnings? Solution: The optimal ordering is 3, 1, 2. You lose 1 + (1 + 2) + (1 + 2 + 5) = 1 + 3 + 8 = 12. 3.2 ( 3 points ): One of these orderings is guaranteed to be optimal for all problem instances. Circle the correct one. Sort challenges: randomly by increasing t i by decreasing t i in the order listed in the problem input 3.3 ( 5 points ): Prove that the ordering you circled always finds an optimal solution. Use an exchange argument. Solution: We will prove this by an exchange argument. Suppose O is optimal, and that the challenges are numbered according to the order they appear in O . Suppose there is some pair of jobs i and j such that i comes before j in O but t j t i . We will call this an inversion. As in the problems we did in class and on the homework, if there is an inversion, there is a consecutive inversion, i.e., a pair of jobs where i comes immediately before j in O and t j t j . Consider swapping these two jobs. Job j finishes t i time units earlier, so you lose t i fewer dollars for job j . Job i finishes t j time units later, so you lose t j more dollars for job j . Since t j t i , the decreased loss from job i is at least as much as the increased loss from job j . Therefore O is still optimal, and has one fewer inversion. By repeating the argument we can transform O into the greedy solution while preserving optimality; therefore, the greedy solution is optimal. 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Question 4. ( 6 points ) We are given a connected, undirected graph with m edges and n nodes where each edge has weight 1 or 2. A student proposes the following O ( m + n ) time algorithm to find the MST. Replace each weight-2 edge by a two-edge path of weight-1 edges. Now the resulting graph has all edges of weight 1, and we know any spanning tree is an MST, so we return a BFS tree of the new graph. 4.1 ( 3 points ): Does this give us an MST of the original graph? Briefly explain. Solution: No. This construction seems like it may be useful but it doesn’t preserve information about spanning trees in the original graph. For example, the BFS tree might only include “half” of an edge of the original graph. Specifically, suppose the original graph is a tree specific example: if the original graph consists only of two edges ( a, b ) and ( b, c ) of weight 2. 4.2 ( 3 points ): Does this run in O ( m + n ) time, whether it is correct or not? Briefly explain. Solution: Yes, it does. The running time is O ( m + n ) where m and n are the numbers of edges and nodes, respectively, in the new graph. We have m 2 m and n n + m , so m + n 3 m + n , which means O ( m + n ) = O ( m + n ). 4
Question 5. ( 13 points ) Consider this graph when answering the following questions: s a b c d e 3 8 3 5 4 2 1 2 5.1 ( 1 points ): What is the total cost of a minimum spanning tree (MST)? 13 5.2 ( 3 points ): List the weights of the edges added by Kruskal’s algorithm in the order they are added. 1, 2, 3, 3, 4 5.3 ( 2 points ): How many different minimum spanning trees are there? 2. The weights are 1, 2, 2, 3, 4. Either edge of weight 2 can be used. 5.4 ( 3 points ): List the nodes that are attached by Prim’s algorithm starting from s . a, b, d, e, c 5.5 ( 3 points ): List the first three nodes after s that are explored by Dijkstra’s algorithm starting from s . a, b, c 5.6 ( 1 points ): What is the length of the shortest path from s to e ? 10 5
Question 6. ( 7 points ) Consider a recursive algorithm whose running time on an input of size n satisfies: T ( n ) 4 T ( n/ 5) + cn 2 6.1 ( 1 points ): For a problem of size n , what is the size of the subproblem in each recursive call? n/ 5 6.2 ( 1 points ): How many recursive calls does the algorithm make? 4 6.3 ( 1 points ): How much work does the algorithm do outside the recursion? O ( n 2 ) Now, consider an algorithm whose running time on an input of size n satisfies: T ( n ) 2 T ( n/ 3) + cn. 6.4 ( 2 points ): How many subproblems are there at level i of the recursion tree? 2 i 6.5 ( 2 points ): What is the size of a single subproblem at level i of the recursion tree? n/ 3 i 6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Question 7. ( 12 points ) Follow the instructions to solve the recurrences. You need to show the steps in your answer. Final answer is not sufficient. 7.1 ( 3 points ): Consider the recurrence T ( n ) = T ( n 1) + n 2 . Use unrolling to solve this recurrence. What answer do you get? (a) Θ(log n ) (b) Θ( n ) (c) Θ( n 2 ) (d) Θ( n 3 ) Solution: The exact answer is T ( n ) = n i =1 i 2 , which solves to Θ( n 3 ). 7.2 ( 3 points ): Consider the recurrence T ( n ) = 4 T ( n/ 2) + O ( n 3 ) . Solve this recurrence using any technique. (Hint: try the master theorem). (a) Θ( n 2 ) (b) Θ( n 2 log n ) (c) Θ( n 3 ) (d) Θ( n 3 log n ) Solution: Case 1 of the master theorem would give Θ( n 3 ) and Case 3 would give Θ( n log 2 4 ) = Θ( n 2 ). Since the former is larger, the answer is Θ( n 3 ). 7.3 ( 3 points ): Consider the recurrence T ( n ) = 8 T ( n/ 2) + O ( n 3 ) . Solve this recurrence using any technique. (Hint: try the master theorem). (a) Θ( n 2 ) (b) Θ( n 2 log n ) 7
(c) Θ( n 3 ) (d) Θ( n 3 log n ) Solution: Case 1 of the master theorem would give Θ( n 3 ) and Case 3 would give Θ( n log 2 8 ) = Θ( n 3 ). Since these two are equal, the answer comes from Case 2 of the Master theorem, and is Θ( n 3 log n ). 7.4 ( 3 points ): Consider the recurrence T ( n ) = 4 T ( n/ 3) + O ( n 1 / 2 ) . Solve this recurrence using any technique. (Hint: try the master theorem). (a) Θ( n 1 / 2 ) (b) Θ( n 1 / 2 log n ) (c) Θ( n log 3 4 ) (d) Θ( n log 3 4 log n ) Solution: Case 1 of the master theorem would give Θ( n 1 / 2 ) and Case 3 would give Θ( n log 3 4 ). Since the latter is larger, the answer is Θ( n log 3 4 ). 8
Question 8. ( 5 points ) Consider the following recurrence, which describes an algorithm that divides a problem of size n into eight equal-sized subproblems, and then does O ( n 3 ) work outside of the recursive calls: T ( n ) 8 T ( n/ 2) + cn 3 We assume the recurrence holds for n 2 and that T (2) c . Prove by induction that T ( n ) cn 3 log n . You should assume that the logarithm is base 2. Solution: Base Case : We know from the base case of the recurrence T (2) c c · 2 3 log 2 2 = 8 c , so the claim is proven for n = 2. Induction Step. Assume that T ( m ) cm 3 log m for all m n . Then T ( n ) 8 T ( n/ 2) + cn 3 8 c n 2 3 log n 2 + cn 3 = cn 3 (log n 1) + cn 3 = cn 3 log n 9
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Question 9. ( 9 points ) During your time-travels you collect a record of bitcoin prices for the next n years, which you plan to use to get rich. The price in dollars in year i is p ( i ). Your plan is to find the two years i and j such that i < j and the difference p ( j ) p ( i ) is as large as possible, and buy in year i and sell in year j . 9.1 ( 2 points ): Suppose n = 4 and p (1) = 3 , p (2) = 1 , p (3) = 4 , p (4) = 5 . What years will you select in this example? i = 2 j = 4 Toward an Algorithm. Consider the following outline for a divide-and-conquer algorithm to solve the problem. Assume that n is a power of 2, and, for simplicity, your algorithm will return only the maximum value of p ( j ) p ( i ), and not the years themselves. Divide. Let L = { 1 , ..., n/ 2 } and R = { n/ 2 + 1 , ..., n } . Conquer — Recursively find δ L , the biggest increase p ( j ) p ( i ) for i, j L — Recursively find δ R , the biggest increase p ( j ) p ( i ) for i, j R Combine. Use δ L , δ R , and some additional computation to compute and return δ , the biggest increase p ( j ) p ( i ) for i, j ∈ { 1 , . . . , n } . 9.2 ( 2 points ): Assume the combine step is implemented to take O ( n ) time, and let T ( n ) be the running time of this algorithm on an input of size n . Write a recurrence for T ( n ) . Solution: T ( n ) = 2 T ( n/ 2) + O ( n ). Variations such as T ( n ) 2 T ( n/ 2) + cn are also fine. 9.3 ( 2 points ): Solve your recurrence to give a simple expression for T ( n ) . Solution: O ( n log n ) 9.4 ( 3 points ): Describe how to implement the combine step in O ( n ) time. Solution: Find the biggest increase p ( j ) p ( i ) where i L and j M . Do this by letting p ( i ) be the minimum price for all years i L , and letting p ( j ) be the maximum price for all years j R . Call the resulting value δ M . Return the maximum of δ L , δ R , δ M . 10