MPCS 55001 Algorithms — Winter 2020
Homework 1. This problem set is due Monday January 13 at 11:59 pm.
It must be submitted electronically to Canvas.
Reading: CLRS chapters 1–3; chapter 4, sections 4.1,
4.3–4.5.
Reading for lecture 2: chapters 5, 7, and 11.
Problem assignment:
-
"DO" exercises are strongly recommended to check your understanding of the
concepts. Do them but do not submit them.
-
Problems labeled "HW" are standard homework problems that you are required
to submit.
"Do" Exercises (do but do not submit):
-
"DO"
Exercise 2.3-5, page 39.
-
Write pseudocode for binary search as a recursive procedure.
-
Write pseudocode for binary search as a iterative procedure.
-
Write a loop invariant for your iterative procedure and prove it correct.
-
"DO"
Exercise 2.3-7, page 39. Write pseudocode for your algorithm.
-
"DO"
Problem 2-1, pages 39–40.
-
"DO"
Problem 2-4, pages 41–42.
-
"DO"
Exercise 3.1-1, page 52.
-
"DO"
Exercise 4.5-1, page 96.
-
"DO"
Problem 30-1, parts a and c, pages 920–921.
Homework Assignment:
DUE Monday January 13 at 11:59 pm
-
Collaboration policy: There is no penalty for acknowledged
collaboration. To acknowledge collaboration, give the names of students with
whom you worked at the beginning of your homework submission and mark
any solution that relies on your collaborators' ideas.
Note: you must write up each problem solution by yourself without
assistance.
DO NOT COPY or rephrase someone else's solution.
The same requirement applies to books and other written sources: you should
acknowledge all sources that contributed to your solution of a homework
problem.
DO NOT COPY or rephrase solutions from written sources.
-
Internet policy: Looking for solutions to homework problems on the
internet, even when acknowledged, is STRONGLY DISCOURAGED. If you find a
solution to a homework problem on the internet, do not not copy it.
Close the website, write up your solution by yourself, and cite
the url of website in your writeup.
-
Copied solutions obtained from a written source, from the internet,
or from another person will receive ZERO credit and will be
flagged to the attention of the instructor.
-
Write out your work. If you just write your answer without showing your
work, you will not receive credit.
-
When describing an algorithm in pseudocode, explain the meaning of
your variables in English. Comment the lines of your pseudocode. Your
pseudocode should be short, clear, and complete to receive full credit.
-
Use the master theorem to solve recurrences if it applies.
Homework Problems:
-
HW
We are given an array A[1.. n] of n real numbers,
n ≥ 3, with the property that A[1] ≥ A[2] and
A[n − 1] ≤ A[n]. We say that an
element A[x] is a local minimum if
A[x − 1] ≥ A[x] and A[x]
≤ A[x + 1].
-
Example: There are 6 local minima in the following array:
We obviously can find a local minimum in O(n) time by scanning
through the array. Give a divide and conquer algorithm that finds a
local minimum in O(lg n) time. Your algorithm should
return the index of a local minimum.
You may use the following test data as an example to help you understand this
problem and write correct pseudocode:
readme
input
output.
-
Describe your algorithm in pseudocode. Comment your code.
No credit for Ω(n) time algorithms. (5 points)
-
Give a worked example or diagram to show more precisely how your algorithm
works. (2 points)
-
Argue that your algorithm is correct. Simply restating the steps of the
algorithm will receive no credit. (1 point)
-
Analyze the asymptotic running time of your algorithm (this includes writing
and solving the associated recurrence). (1 point)
Now suppose we generalize the problem to two dimensions. The input is an
m × n matrix M of real numbers. An element
M[x, y] is a local minimum if no neighbor has a strictly
lower value (equivalently, if all neighbors have equal or higher values).
Give an efficient algorithm to find a local minimum on this input.
Your algorithm should return an ordered pair of indices of a local minimum.
-
Describe your algorithm in pseudocode. Comment your code.
Maximum points for the most efficient algorithms. No credit for
Ω(mn) time algorithms.
(6 points)
-
Give a worked example or diagram to show more precisely how your algorithm
works. (2 points)
-
Argue that your algorithm is correct. Simply restating the steps of your
algorithm will receive no credit. (1 point)
-
What is the asymptotic running time of your algorithm in terms of m
and n? Analyze the running time of your algorithm; justify your
claim for its running time.
(2 points)
-
HW
(a) Given an array A[1.. n] of n real numbers,
give an algorithm to compute the number of "distinct pairs", which are
ordered pairs of indices (i, j) such that
A[i] ≠ A[j], in
O(n lg n) time.
-
Example:
A = [10, 10, 19]
distinct pairs: (1, 3), (2, 3), (3, 1), (3, 2)
count: 4
-
Describe your algorithm in pseudocode. Comment your code.
(5 points)
-
Give a worked example or diagram to show more precisely how your algorithm
works.
(2 points)
-
Argue that your algorithm is correct. Simply restating the steps of the
algorithm will receive no credit. (1 point)
-
Analyze the asymptotic running time of your algorithm. Simply stating the
running time will receive no credit. (1 point)
(b) Given an array A[1.. n] of n real numbers,
give an algorithm to compute the number of "distinct triples", which are
indices
(i, j, k) such that
A[i], A[j], A[k] are distinct, in
O(n2) time.
-
Describe your algorithm in pseudocode. Comment your code.
(6 points)
-
Give a worked example or diagram to show more precisely how your algorithm
works.
(2 points)
-
Argue that your algorithm is correct. Simply restating the steps of the
algorithm will receive no credit. (1 point)
-
Analyze the asymptotic running time of your algorithm. Simply stating the
running time will receive no credit. (1 point)
-
HW
Consider two square matrices X and Y of size
n = 2k, where k is a positive integer.
Let Z denote their product, i.e.,
XY = Z,
where X, Y, and Z are all of size n, i.e.,
n × n matrices, with real-number entries. (See
here
for a refresher on matrices and matrix multiplication. Of particular interest
is the section on "Block matrices and submatrices" on page 9.) Observe that
Z can be computed using a divide-and-conquer approach.
The first step is to divide the two input matrices into quadrants, as follows:
For example, if
then
Note that these quadrants are square matrices of size n/2. Furthermore,
they can be used to compute the quadrants of Z as follows:
-
Write pseudocode for a divide-and-conquer algorithm that takes
X and Y as input and returns their product, Z.
You may use the following subroutines, all of which run in
O(n2) time:
-
Divide-Matrix which takes a square matrix A and returns its
quadrants A1, A2, A3,
A4.
-
Add-Matrices which takes two matrices A and B and
returns their sum A + B.
-
Combine-Matrices which takes quadrants A1,
A2, A3, and A4
returns a square matrix A.
(5 points)
A second, more clever algorithm involves defining 7 new square matrices of
size n/2 as follows:
-
Using the new n/2 × n/2 matrices
Q1, …, Q7 defined above,
show that the following equations for the quadrants of Z match
those given in the first part of the problem. (3 points)
-
Write pseudocode for a divide-and-conquer algorithm that takes
X and Y as input and computes Z using the seven matrices
Q1, …, Q7 given above.
You may use the subroutines Divide-Matrix, Add-Matrices,
and Combine-Matrices.
(5 points)
-
Analyze and compare the asymptotic running times of the two
divide-and-conquer algorithms above.
Count the number of additions and multiplications of
n/2 × n/2
square matrices required for each algorithm.
Write out the recurrences and solve them using the master theorem.
Specify the base cases. Which algorithm is asymptotically faster?
(3 points)
Gerry Brady, Chris Jones, Tushant Mittal, and Andrew Eckart
Monday January 6 20:56:03 CST 2020