a4-soln

.pdf

School

University of British Columbia *

*We aren’t endorsed by this school

Course

320

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

5

Uploaded by DukeSheepPerson1030

Report
CPSC 320 2023S2: Assignment 4 Solutions 3 More Recurrences Consider this recurrence relation: T ( n ) = 1 , for n < n 0 , T ( n/ 2) + T (5 n/ 9) + T (4 n/ 7) + n 2 , for n n 0 , Unfortunately, the Master Theorem doesn’t apply directly to this recurrence, and expanding out the recursion tree looks very painful! However, there are still ways to solve (or at least bound) this recurrence! 1. (1 mark) Consider this nicer recurrence relation: L ( n ) = 1 , for n < n 0 , L ( n/ 2) + L ( n/ 2) + L ( n/ 2) + n 2 , for n n 0 , Fortunately, the Master Theorem does apply to this recurrence! What are the values of a , b , and k , and what does the Master Theorem give as the big- Θ bound for L ( n ) ? SOLUTION: a = 3 , b = 2 , and k = 2 . Using the Worksheet version of the Master Theorem, we find that a = 3 < 4 = 2 2 = b k . Therefore, L ( n ) = Θ( n 2 ) . 2. (3 marks) Prove that L ( n ) T ( n ) for all n > 0 . You may assume that n 0 is whatever is convenient so that your base cases work out. You may also assume that both L ( n ) and T ( n ) are monotonically non-decreasing, e.g., for all m < n , you have L ( m ) L ( n ) and T ( m ) T ( n ) . Hint: This should be a very short strong induction proof. Here and for the rest of the problem you can ignore floors and ceilings, i.e., assume that when you divide n by a constant the result is an integer. SOLUTION: In the base case, n < n 0 , and L ( n ) = 1 = T ( n ) , so L ( n ) T ( n ) . In the inductive case, assume that for all n 0 < n , L ( n 0 ) T ( n 0 ) . We find that: L ( n ) = L ( n/ 2) + L ( n/ 2) + L ( n/ 2) + n 2 L ( n/ 2) + L (5 n/ 9) + L (4 n/ 7) + n 2 by monotonicity T ( n/ 2) + T (5 n/ 9) + T (4 n/ 7) + n 2 by I.H. = T ( n ) . 3. (2 marks) Similarly, define a recurrence relation U ( n ) such that T ( n ) U ( n ) for all n > 0 , and where you can directly solve U ( n ) via the Master Theorem. You should try to make U ( n ) as tight to T ( n ) as possible. SOLUTION: U ( n ) = 1 , for n < n 0 , U (4 n/ 7) + U (4 n/ 7) + U (4 n/ 7) + n 2 , for n n 0 , We relied on the fact that 4 / 7 > 5 / 9 > 1 / 2 . 1
4. (2 marks) Prove that T ( n ) U ( n ) for all n > 0 . (Your proof should be very similar to the proof you did for L ( n ) .) SOLUTION: To prove that T ( n ) U ( n ) , we do another strong induction: In the base case, n < n 0 , and T ( n ) = 1 = U ( n ) , so T ( n ) U ( n ) . In the inductive case, assume that for all n 0 < n , T ( n 0 ) U ( n 0 ) . We find that: T ( n ) = T ( n/ 2) + T (5 n/ 9) + T (4 n/ 7) + n 2 T (4 n/ 7) + T (4 n/ 7) + T (4 n/ 7) + n 2 by monotonicity U (4 n/ 7) + U (4 n/ 7) + U (4 n/ 7) + n 2 by I.H. = U ( n ) . 5. (1 mark) What are the values for a , b , and k for your U ( n ) , and what is the resulting big- Θ bound for U ( n ) that the Master Theorem gives you? (Hint: You’ll probably want a calculator to do some arithmetic with 1 / 2 , 5 / 9 , and 4 / 7 , their reciprocals, and their squares...) SOLUTION: a = 3 , b = 7 / 4 , and k = 2 . Using the Worksheet version of the Master Theorem, we find that a = 3 < 3 . 0625 = (7 / 4) 2 = b k . Therefore, U ( n ) = Θ( n 2 ) . 6. (1 mark) What big- Θ bound can you conclude for T ( n ) ? SOLUTION: Since L ( n ) T ( n ) U ( n ) , and both L ( n ) = Θ( n 2 ) and U ( n ) = Θ( n 2 ) , we know that T ( n ) = Θ( n 2 ) , too. 2
4 Travel Itinerary You are the manager of the Abbotsford branch of a large multinational company, and your company just bought out a small business based in Brampton, Ontario. However, your company has not found a manager for the Brampton branch yet, so they have asked you to oversee both branches until the end of the year. There are n days remaining in the year. Because you will be spending a fair amount of time in Brampton, you have determined that it will be more convenient to stay in hotels in both cities, rather than continuing to rent your apartment in Abbotsford. You cannot stay in either location for more than 7 days because you do not want to be away from either branch for too long. You must determine how to split your time between Abbotsford and Brampton in a way that minimizes the total cost of your hotels and flights. The cost of your flight/hotel depends on the day (weekends/holidays might be more expensive, or there might be promotions on certain days) and the city. You are given four arrays that provide the cost of the hotels and flights for each day. The arrays f A , f B , h A , and h B , each of length n , are defined as follows: f A [ i ] is the cost of flying to Abbotsford on day i f B [ i ] is the cost of flying to Brampton on day i h A [ i ] is the cost of staying in a hotel in Abbotsford on day i h B [ i ] is the cost of staying in a hotel in Brampton on day i You are looking to minimize the cost of accommodations plus flights, subject to the constraint that you cannot spend more than 7 days in one location. You can assume that your flights are always in the morning (so if you are flying between cities on day i , you only have to pay for a hotel in your destination city that day). Let C A [ i ] be the lowest possible value of the objective function for days 1 to i , assuming you are staying in Abbotsford on day i . Similarly, let C B [ i ] be the lowest possible value of the objective function for days 1 to i , assuming you are staying in Brampton on day i . In either case, assume that you are staying in Abbotsford on day 0. The solutions to Tutorial 7 provide recurrences for these values. 1. (4 marks) Using the recurrences from Tutorial 8, write pseudo-code for an iterative dynamic programming algorithm that takes f A , f B , h A and h B as input and populates the arrays C A and C B for every value 1 i n . Note: it is acceptable to leave min() operations in your pseudo-code instead of writing out an inner loop. SOLUTION: 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