Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Basic Algorithms, Summer 2023

CSCI-UA.0310-001

Homework 3

Due Saturday, June 17 (11:59 p.m.)

Instructions

• Answer each question on a separate page.

• Submit your homework as a single pdf file to Gradescope. It can be either a scanned copy of your written solutions or photos joined into a pdf; your solution should be readable. You are also encouraged to typeset the solutions in TEX or MS Word. Gradescope will ask you to mark where each answer is - please make sure to do this - points will be deducted otherwise.

•  For each question where you are required to design an algorithm, you need to clearly explain the algorithm and prove the running time complexity as asked in the question.

• You are encouraged to discuss with your peers/consult supplementary material if neces- sary, but the final written solutions must be your own.

–  For each solution that you discussed or looked at extra materials for hints, you are required to mention it in your solution. This will not incur any penalty, but if noticed otherwise by the graders it may bright into question your academic integrity and there will be more serious repercussions. Writing down solutions or answers from some source without citing the source or without understanding how what you have written is correct is cheating and will be treated as such.

Question 0: List all your collaborators and sources: (–∞ points if left blank)

You must enter the names of your collaborators or other sources as a response to Question 0. Do NOT leave this blank; if you worked on the homework entirely on your own, please write “None”here. Even though collaborations in groups of up to 3 people are encouraged, you are  required to write your own solution.

Question 1: (6+4=10 points)

Recall that a sorting algorithm is stable if objects with equal keys appear in the same order in the sorted output as in the unsorted input. For example, consider a list of Student Objects, with two fields, Student.name and Student.age. If sorting according to age, given a list of Student objects, a stable sort will order the list in increasing order of the student ages. If two students have the same age, they will appear in the same order as in the unsorted list.

1. State and prove an appropriate loop invariant for the Merge subroutine (as seen in class) needed to prove the stability of MergeSort.

2.  Using the result of part 1, prove that MergeSort (as seen in class) is stable for any input size n ∈ N by induction on n.

Question 2: (3+7=10 points)

Consider the following recurrence:

T(1) = 0                                                                   (1)

T(2) = 1                                                                   (2)

T(n) = T(⌈n/3⌉) + T(⌈2n/3⌉) + 1   if n > 2                     (3)

You will use the substitution method (i.e., induction) to find a Θ bound for T(n).

1. Warmup. For this part, ignore the base cases and also do not worry about the ceilings in (3).

To get a better feel for the recurrence and have a guess for a solution, try plugging in n, n, and n2 into (3). Compare the left and right hand sides of (3) for each of these substitutions, and determine whether the left hand side or right hand side of (3) is greater for each.

2.  Use your work in part (a) to help you determine a guess for p > 0 such that T(n) = Θ (np). Formally prove your result using the substitution method. For full credit, you’ll need to show both upper and lower bound. (Hint: consider cnp + d when proving the bounds, with your value of p, and determine appropriate values of c and d.)

Question 3: (5 points)

Consider the following inductive step in an attempted proof by induction showing n2 = O (n). Suppose for the strong inductive hypothesis that n2 = O (n) for all n ≤ k . Then

(k + 1)2 = k2 + 2k + 1

= O(k) + 2k + 1 by the inductive hypothesis

= O (k)

Explain precisely why this is incorrect.

Question 4: (3+4+3=10 points)

Solve each of the following recurrence relations using the Master Theorem. State and justify

which case of the Master Theorem is applied. If Master Theorem cannot be applied, justify why it cannot be applied.

1. T(n) = 4T(n/2) + n2

2. T(n) = 4T(n/2) + n2 log(n)

3. T(n) = 4T(n/2) + n3