# CS代考 ECS 122A – Algorithm & Analysis Midterm 2 Solution – cscodehelp代写

ECS 122A – Algorithm & Analysis Midterm 2 Solution
Question 2 (5 points each)
1. Divide and conquer 2. Yes
Question 3 (53 Points)

(a) (1point)10×(5−5)=0 (b) (1point)10×(5−3)=20 (c) (1point)10×(5−1)=40
(10 points) Greedy choice:
Do the assignments in order of decreasing difficult di.
(20 points) Greedy choice property: Answer:
• Outputformat:Let[a1,a2,…,an]bethegivennhomeworkassignmentswithdifficulty[d1,d2,…,dn]. Assume without loss of generality that the assignments are sorted in decreasing difficulty, i.e.,
di ≥di+1(1≤i≤n−1).
• B: Since the input assignments are sorted in decresing difficulty, our greedy solution is B = [1, 2, …, n], i.e., we do homework ai on week i.
• OPT: Let OPT = [o1, …, on] be an optimal solution such that we do homework aoi on week i.
• B′: Suppose ok = 1. i.e., we do assignment a1 on week k in the OPT solution. Then let B′ = [1, o2, o3, …, ok−1, o1, ok+1, on]. (Note: By cut-and-paste method, we replace o1 by 1. But if o1 ̸= 1, we will do homework a1 twice. So we need to replace ok by o1.)
To show B′ is still optimal:

Let p(A) be the points we get when doing the assignments in order specified by A. We have
p(B′)−p(OPT)=(d1×(n−1)+do1 ×(n−k))−(do1 ×(n−1)+d1×(n−k)) = d1 ×(k−1)−do1 ×(k−1)
= (k−1)(d1 −do1) ≥0
So the total points we can get from B′ is greater than or equal to the total points we can get from OPT. OPT is optimal so B′ must also be optimal.
4. (20 points) Suboptimality property: Answer:
Given n assignments [a1, …, an] and an optimal solution OPT = [o1, …, on] to the problem, where ∀1≤i≤n.1≤oi ≤n,weneedtoshowthesolutionB=[o1,…,on−1]isoptimalwhengivenn−1 assignments[a1,…,aon −1,aon +1,an].
Suppose B is not optimal. Then there exists an optimal solution B′ = [b1, …, bn−1]. Let p(A) be the points we get when doing the assignments in order specified by A. We have
p(B′)= ∑ dbi ×(n−1−i) (1) 1≤i≤n−1
p(B)= ∑ doi ×(n−1−i) (2) 1≤i≤n−1
p(B′) > p(B) (3) p(B′ +[on]) = ∑ dbi ×(n−i)+don ×(n−n) = ∑ dbi ×(n−i) (4)
1≤i≤n−1 1≤i≤n−1
p(B+[on]) = p(OPT) = ∑ doi ×(n−i)+don ×(n−n) = ∑ doi ×(n−i) (5)
1≤i≤n−1 1≤i≤n−1 Based on formulas (1), (2) and (3),
∑ dbi ×(n−1−i) > ∑ doi ×(n−1−i) 1≤i≤n−1 1≤i≤n−1
Combining with formulas (4) and (5), p(B′ + [on]) > p(OPT). This is a contradiction.
Conclusion: B is optimal.
(Note: p(B′ + [on]) ̸= p(B′) + doi × (n − n), similar for p(B + [on]).)
Question 4 Dynamic Programming (32 points) Answer:
Take item 4
∑ dbi ×(n−i) > ∑ doi ×(n−i) 1≤i≤n−1 1≤i≤n−1
r = r = 0
r = r = 0
r = r = 0
r = max(v4 + r[1 − w4], r) = max(6 + r, 0) = max(6, 0) = 6

r = r = 0
r = max(v2 + r[2 − w2], r) = max(7 + r, 0) = max(7, 0) r = r = 7
r = max(v4 + r[2 − w4], r) = max(6 + r, 7) = max(6, 7) = 7
Take item 2 3.
r = max(v1 + r[3 − w1], r) = max(5 + r, 0) = max(5, 0) r = max(v2 + r[3 − w2], r) = max(7 + r, 5) = max(7, 5) = 7 r = r = 7
r = max(v4 + r[3 − w4], r) = max(6 + r, 7) = max(13, 7) = 13
Take item 4 and 2 4.
r = max(v1 + r[4 − w1], r) = max(5 + r, 0) = max(5, 0) = 5 r = max(v2 + r[4 − w2], r) = max(7 + r, 5) = max(7, 5) = 7 r = max(v2 + r[4 − w2], r) = max(10 + r, 7) = max(10, 5) = 10 r = max(v4 + r[4 − w4], r) = max(6 + r, 10) = max(13, 7) = 13
Take item 4 and 2
RUBRICS: For each of the four questions, 1.5 points for each r[i][j] and 2 points for the items being taken.

Posted in Uncategorized