Вы находитесь на странице: 1из 2

CSE 101 Fall 2016

Homework 7
Due: Friday, March 10, 2017 at 11:59pm

Instructions

Homework should be done in groups of one to four people. You are free to change group
members at any time throughout the quarter. Problems should be solved together, not divided
up between partners. A single representative of your group should submit your work
through Gradescope. Submissions must be received by 11:59pm on the due date, and there
are no exceptions to this rule.

Homework solutions should be neatly written or typed and turned in through Gradescope
by 11:59pm on the due date. No late homeworks will be accepted for any reason. You will be
able to look at your scanned work before submitting it. Please ensure that your submission is
legible (neatly written and not too faint) or your homework may not be graded.

Students should consult their textbook, class notes, lecture slides, instructors, TAs, and tutors
when they need help with homework. Students should not look for answers to homework
problems in other texts or sources, including the internet. Only post about graded homework
questions on Piazza if you suspect a typo in the assignment, or if you dont understand what
the question is asking you to do. Other questions are best addressed in office hours.

Your assignments in this class will be evaluated not only on the correctness of your answers,
but on your ability to present your ideas clearly and logically. You should always explain how
you arrived at your conclusions, using mathematically sound reasoning. Whether you use
formal proof techniques or write a more informal argument for why something is true, your
answers should always be well-supported. Your goal should be to convince the reader that
your results and methods are sound.

For questions that require pseudocode, you can follow the same format as the textbook, or you
can write pseudocode in your own style, as long as you specify what your notation means. For
example, are you using = to mean assignment or to check equality? You are welcome to use
any algorithm from class as a subroutine in your pseudocode. For example, if you want to sort
list A using InsertionSort, you can call InsertionSort(A) instead of writing out the pseudocode
for InsertionSort.

1
1. A contiguous subsequence of a list S is a subsequence made up of consecutive elements of S. For
instance, if S is
5, 15, 30, 10, 5, 40, 10,
then 15, 30, 10 is a contiguous subsequence but 5, 15, 40 is not.
(a) How many contiguous subsequences are there for a list of length n?
(b) Give a linear-time dynamic programming algorithm for the following task:
Input: A list of numbers, a1 , . . . , an
Output: The contiguous subsequence of maximum sum (a subsequence of length zero has
sum zero.)
For the preceding example, the answer would be 10, 5, 40, 10, with a sum of 55.

(Hint: For each j 2 {1, 2, . . . , n}, consider contiguous subsequences eding exactly at position j.

2. Recall that an inversion in a list of distinct integers (a1 , . . . , an ) is a pair ai , aj such that i < j and
ai > aj . The problem here is to find the value of the biggest dierence among inversion pairs.
Example: for the sequence [11, 7, 1, 17, 4, 3, 6, 2, 13, 20, 15, 9], the greatest dierence among all inversion
pairs is 15 coming from the pair (17, 2)
Design a linear time dynamic programming algorithm to solve this problem.

3. An ordered graph G is a directed graph with vertices V = (v1 , . . . , vn ) with the following property. All
edges must go from a vertex of lower index to a vertex of higher index and every vertex except for vn
has at least one outgoing edge.
Design a dynamic programming algorithm that takes as input an ordered graph G and outputs the
length of the longest path from v1 to vn .

4. Given a list of n positive integers, design a O(n) dynamic programming algorithm that finds the sum
of the subsequence with the maximum sum such that no two elements in the subsequence are next to
each other.
Example: for the list [7, 4, 5, 8, 9, 8, 1] then the answer will be 23 coming from [7, 4, 5, 8, 9, 8, 1], 7+8+8 =
23.

Вам также может понравиться