Please fill out this quiz, and press the "Submit" button at the end. Don't collaborate with anyone on quiz exercise solutions.
Please answer all questions.
SID: [No spaces and no dashes.]
Login ID :
1. If L is a list, a subsequence of L is a subset of the elements of L, in the same order that they appear in L. (The elements don't have to be contiguous; they can be any subset of L.) A subsequence is increasing if each number is larger than the one before it. In this question, the goal is to find the longest increasing subsequence of L that starts with the first element of L.
For instance, if we are given L=[2,3,1,7,4,9], one increasing subsequence is [2,3,7,9]; another is [2,3,9]; another is [2,3,4]; another is [2,9]; and so on. The longest one is [2,3,7,9]. [2,3,4,9] also has the same length, so it would be equally good. So, if we were given L, the desired answer would be either [2,3,7,9] or [2,3,4,9] (either one is fine).
Consider the following greedy algorithm for solving this problem. We include the first element of L in our subsequence. Then, we find the first element of L after that one that is larger than it, and include it in the subsequence. We repeatedly find the first number from L that comes after the last one selected and that is larger than the last one selected, and select it for inclusion in the subsequence. Repeat this until L is exhausted.
If you prefer to read pseudocode:
f(x, L): 1. If L = [], then return []. 2. If head(L) > x, then return head(L)::f(head(L), tail(L)). 3. Otherwise, return f(x, tail(L)). Greedy(L): 1. Return head(L)::f(head(L), tail(L)).
Question: Is Greedy(L) guaranteed to find the longest increasing subsequence of L that starts with L's first element? If yes, explain why briefly. If no, give a counterexample.