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