Docsity

CMPS 214 Problem Set 8: Dynamic Programming, Bipartiteness, and k-Well-Connected Graphs, Exercises of Algorithms and Programming

Toggle sidebar
bg1
CMPS 214 Problem Set 8
Style guide and expectations:
Please see the Homework part of the syllabus for guidance
on what we are looking for in homework solutions. We will grade according to these standards.
You should cite all sources you used outside of the course material.
What we expect:
Make sure to look at the
We are expecting
blocks below each problem
to see what we will be grading for in each problem!
Exercises
We suggest you do these on your own. As with any homework problem, though, you may
ask the course sta for help.
1. DP for recurrences
Consider the recurrence relation dened by
T
(
n
)=2
T
(
n
1) +
T
(
n
2) + 1
;
with
T
(0) =
T
(1) = 0
.
(a) Write a
bottom-up
dynamic programming algorithm that computes
T
(
n
)
. Your
algorithm should run in time
O
(
n
)
.
[
We are expecting:
Pseudocode. No explanation is required.]
Solution:
Algorithm 1
computeTBottomUp(n)
1:
result = [-1] * (n+1)
2:
result[0] = 0
3:
result[1] = 0
4:
for
i
= 2
to
n
do
5:
result[i] = 2*result[i-1] + result[i-2] + 1
6:
end for
7:
return
result[n]
(b) Write a
top-down
dynamic programming algorithm that computes
T
(
n
)
. Your
algorithm should run in time
O
(
n
)
.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CMPS 214 Problem Set 8: Dynamic Programming, Bipartiteness, and k-Well-Connected Graphs and more Exercises Algorithms and Programming in PDF only on Docsity!

CMPS 214 Problem Set 8

Style guide and expectations: Please see the Homework part of the syllabus for guidance on what we are looking for in homework solutions. We will grade according to these standards. You should cite all sources you used outside of the course material.

What we expect: Make sure to look at the We are expecting blocks below each problem to see what we will be grading for in each problem!

Exercises

We suggest you do these on your own. As with any homework problem, though, you may ask the course sta for help.

  1. DP for recurrences Consider the recurrence relation dened by

T (n) = 2T (n 1) + T (n 2) + 1;

with T (0) = T (1) = 0.

(a) Write a bottom-up dynamic programming algorithm that computes T (n). Your algorithm should run in time O(n). [We are expecting: Pseudocode. No explanation is required.] Solution:

Algorithm 1 computeTBottomUp(n) 1: result = [-1] * (n+1) 2: result[0] = 0 3: result[1] = 0 4: for i = 2 to n do 5: result[i] = 2*result[i-1] + result[i-2] + 1 6: end for 7: return result[n]

(b) Write a top-down dynamic programming algorithm that computes T (n). Your algorithm should run in time O(n).

[We are expecting: Pseudocode. No explanation is required.] Solution:

Algorithm 2 computeTTopDown(n, dp) 1: if n == 0 or n == 1 then 2: return 0 3: else if dp[n] != -1 then 4: return dp[n] 5: end if 6: return 2*dp[n-1] + dp[n-2] + 1

The arguments to the initial call of the function above are (n; dp), where dp is a list of length n + 1 initialized with 1 s.

Our friend's algorithm is incorrect and we can show that this is indeed the case with a counter example. Consider the graph in the picture above and assume the algorithm chose to delete vertices in the following order: 1, 2, 3, 4, 5. Notice that when the algorithm starts backtracking after hitting the base case it will place vertex 5 in L, vertex 4 in L, vertex 3 in L, vertex 2 in R, and will get stuck at vertex 1 as it will have no set that it can be placed in and so the algorithm will return false even though the graph is indeed bipartite.

Problems

ˆ Try the problems on your own before asking questions. ˆ Write up your answers yourself, in your own words. You should never share your typed- up solutions.

  1. k-well-connected graphs Let G = (V; E) be an undirected, unweighted graph with n vertices and m edges. For a subset S  V , dene the subgraph induced by S to be the graph G^0 = (S; E^0 ), where E^0  E, and an edge fu; v g 2 E is included in E^0 if and only if u 2 S and v 2 S. For any k < n, say that a graph G is k-well-connected if every vertex has degree at least k. For example, in the graph G below, the subgraph G^0 induced by S = fa; b; c; dg is shown on the right. G^0 is 3 -well-connected, since every vertex in G^0 has degree at least 3. However, G is not 3 -well-connected since vertex e has degree 2.

a

b

c

d

e

f

G = (V; E)

a

b

c

d

G^0 = (S; E^0 ), for S = fa; b; c; dg

(a) Design a greedy algorithm to nd a set S  V of maximum size so that the subgraph G^0 = (S; E^0 ) induced by S is k-well-connected. In the example above, if k = 3, your algorithm should return fa; b; c; dg, and if k = 4 your algorithm should return the empty set. You may assume that your representation of a graph supports the following oper- ations: ˆ degree(v): return the degree of a vertex in time O(1) ˆ remove(v): remove a vertex and all edges connected to that vertex from the graph, in time O(degree(v )). Your algorithm should run in time O(n^2 ).

[We are expecting: Pseudocode AND an English description of what your algo- rithm is doing.]

Solution:

Algorithm 5 Main(G) 1: n = G.length 2: visited = [false] * n 3: deleted = [false] * n 4: for v = 0 to n - 1 do 5: if !visited[v] then 6: DFSModied(G, v, visited, deleted) 7: end if 8: end for 9: kCoreVertices = {} 10: for v = 0 to n - 1 do 11: if !deleted[v] then 12: kCoreVertices.add(v) 13: end if 14: end for 15: return kCoreVertices

(b) Prove by induction that your algorithm is correct. [We are expecting: A formal proof by induction. Be sure to clearly state your inductive hypothesis, base case, inductive step, and conclusion.]

Solution: Inductive hypothesis: After making the t'th greedy vertex deletion, there is an optimal solution that extends the solution that the algorithm has constructed so far. That is, the optimal solution is a subset of the set of vertices that have not been deleted yet. Base case: Before any vertices are deleted, the optimal solution is evidently a subset of the set of vertices that have not been deleted yet. Inductive step: Assuming that the IH is true, that is, the solution is a subset of the vertices that have not been deleted yet, the t+1'th vertex deletion involves a vertex of degree < k, as a vertex is deleted if and only if its degree is found to be < k at some point, so we know that after the t+1'th vertex deletion, the optimal solution that extends the solution that the algorithm has constructed so far is unaltered. Conclusion: At the end of the algorithm, the algorithm will return a set of vertices that have not been deleted yet. No deletion of vertices deletes a vertex in the eventual solution (we proved this already). Also, vertices get checked for deletion after changes in their neighborhoods, so all vertices that should have been deleted got deleted. The set that we end up returning is therefore the set of vertices that we are looking for.

  1. Thanksgiving Turkeys On Thanksgiving day, you arrive on an island with n turkeys. You've already had Thanksgiving dinner so you don't want to eat the turkeys (and maybe you prefer tofurkey anyway), but you do want to wish them all a Happy Thanksgiving. However, the turkeys each have very dierent sleep schedules. Turkey i is awake only in a single closed interval [ai ; bi ]. Your plan is to stand in the center of the island and say loudly Happy Thanksgiving! at certain times t 1 ; : : : ; tm. Any turkey who is awake at one of the times tj will hear the message. It's okay if a turkey hears the message more than once, but you want to be sure that every turkey hears the message at least once. (a) Design a greedy algorithm which takes as input the list of intervals [ai ; bi ] and outputs a list of times t 1 ; : : : ; tm so that m is as small as possible and so that every turkey hears the message at least once. Your algorithm should run in time O(n log(n)). [We are expecting: Pseudocode and an English description of the main idea of your algorithm, as well as a short justication of the running time.] Solution: This can be done using a line sweeping algorithm.

Algorithm 6 ThanksgivingTurkeys(Awake[n][2]) 1: Awake.sort(). Sort by nondecreasing order of sleeping time 2: last_shout_time = - 3: shout_times = [] 4: for i = 0 to n - 1 do 5: if last_shout_time < Awake[i][0] then 6: last_shout_time = Awake[i][1] 7: shout_times.append(Awake[i][1]) 8: end if 9: end for 10: return shout_times

(b) Prove by induction that your algorithm is correct. [We are expecting: A formal proof by induction. Be sure to clearly state your inductive hypothesis, base case, inductive step, and conclusion.] Solution: Inductive hypothesis: After saying Happy Thanksgiving! t times, there is an optimal solution that extends the current solution. Base case: After saying Happy Thanksgiving! 0 times, there is an optimal solution that extends that.

  1. [Optional] Continuous Knapsack

In this exercise we'll look at a continuous variant of the knapsack problem that we saw in class. You have a knapsack with a capacity of Q ounces and there are n items; the dierence between this exercise and the version that we saw in class is that you can take a fractional amount of each item. For example, perhaps one item is 3 : 6 ounces of brightly colored sand; you can choose to take 2 : 5235 ounces of sand for your knapsack if that's how much you want. Each item i has a value per ounce vi > 0 (measured in units of dollars per ounce) and a quantity qi > 0 (measured in ounces). There are qi ounces of item i available to you, and for any real number x 2 [0; qi ], the total value that you derive from x ounces of item i is x  vi. Your goal is to choose an amount xi  0 to take for each item i in order to maximize the value

i xi^ vi^ that you receive while satisfying: (1) you don't overll the knapsack (that is,

i xi^ ^ Q), and (2) you don't take more of an item than is available (that is, 0  xi  qi for all i). Assume that

i qi^ ^ Q, so there always is some way to ll the knapsack. (a) Design a greedy algorithm which takes as input Q along the tuples (i; vi ; qi ) for i = 0; : : : ; n 1 , and outputs tuples (i; xi ) so that (1) and (2) hold and

i xi^ vi^ is as large as possible. Your algorithm should take time O(n log(n)). [We are expecting: ˆ Pseudocode AND an English explanation of what it is doing. ˆ A justication of the running time. ] To avoid confusion in the function below, C represents the capacity of the knap- sack, V and Q are the values and quantities of the n items such that when index at i 2 [n] will give you the value and quantity of item i. I are the numbers 1 ; : : : ; n that represent each of the n items.

Algorithm 7 continuousKnapsack(C, I, V, Q, n) 1: output = [] 2: reverseSort(I, V/Q). sort the list I in reverse sorted order based on the array of ratios: V/Q 3: for i = 0 to n - 1 do 4: if C >= Q[I[i]] then 5: C = C - Q[I[i]] 6: output.append((I[i], Q[I[i]])) 7: else if C < Q[I[i]] then 8: output.append((I[i], C)) 9: C = 0 10: end if 11: end for 12: return output

(b) Fill in the inductive step below to prove that your algorithm is correct. ˆ Inductive hypothesis: After making the t'th greedy choice, there is an opti- mal solution that extends the solution that the algorithm has constructed so far. ˆ Base case: Any optimal solution extends the empty solution, so the inductive hypothesis holds for t = 0. ˆ Inductive step: (you ll in) ˆ Conclusion: At the end of the algorithm, the algorithm returns a set S^ of tuples (i; xi ) so that

i xi^ =^ Q. Thus, there is no solution extending^ S^ other than S^ itself. Thus, the inductive hypothesis implies that S^ is optimal. [We are expecting: A proof of the inductive step: assuming the inductive hy- pothesis holds for t 1 , prove that it holds for t.] Solution: Let C be the capacity of the knapsack, let I = fi 1 ; : : : ; ing represent items, let V = fv 1 ; : : : ; vng represents the values of the items, and let Q = fq 1 ; : : : ; qng after the three lists have been sorted in decreasing order based on the ratio between V and Q. Denote the instance of the knapsack problem as (C; I; V; Q; n). Assume the inductive hypothesis holds for  t 1. This means the last t 1 choices allow for an optimal solution that extend the solution the algorithm has been constructed so far. To make things simpler when looking at the t'th choice, we can redene a new instance of the continuous knapsack problem as follows: subtract the knapsack's capacity by the total sum of capacities of the choices made so far, as well subtracting the capacities and values from the items based on the choices made so far.

  1. [Optional] Fish Stops

Plucky the Pedantic Penguin is walking t miles across Antarctica. He needs to eat along the way, but he can only eat when there's a shing hole for him to catch sh. He can walk at most m miles between meals, and he knows how n shing holes are laid out along his route. Plucky is given an array F so that F [i] gives the distance from the start of his journey to the i'th shing hole. There are n shing holes along the way, including at the beginning and the end: F [0] = 0; F [n 1] = t. For example, the array F = [0; 3 ; 4 ; 6 ; 10 ; 12], with t = 12 corresponds to the setup below:

Plucky wants to stop as few times as possible, given that he can walk at most m miles without eating. (It is okay if he walks exactly m miles between meals). He starts out hungry, so he will always sh at 0 miles; he will also always sh at his destination (at t miles), whether or not he's hungry. In the example above, if m = 4, then Plucky should stop 5 times (including his stops at the beginning and the end), for example at 0 ; 4 ; 6 ; 10 ; 12 miles.

(a) Design a greedy algorithm for Plucky to use. The algorithm should have the following properties:

ˆ Your algorithm should take as input the array F , as well as the parameters m and t. You may assume that F is sorted. ˆ Your algorithm should output a list fishStops which contains a shortest list of places that Plucky could stop for sh. In the example above, the algorithm could output [0; 4 ; 6 ; 10 ; 12]. If Plucky cannot make it to his destination t miles away, then your algorithm should return Stay Home. ˆ Your algorithm should run in time O(n). [We are expecting: Pseudocode AND an English description of what it is doing. You do not need to justify the running time.] Solution:

Algorithm 8 GreedyFishStops(F, n, m) 1: shStops = [] 2: shStops.append(F[0]) 3: miles = m 4: for i = 1 to n - 1 do 5: miles = miles - (F[i] - F[i - 1]) 6: if miles < 0 then 7: if F[i - 1] == shStops[n - 1] then 8: return Stay Home 9: else 10: shStops.append(F[i - 1]) 11: miles = m 12: i = i - 1 13: end if 14: else if miles == 0 then 15: shStops.append(F[i]) 16: miles = m 17: end if 18: end for 19: shStops.append(F[n - 1]) 20: return shStops

The algorithm simply lets Plucky greedily walk as many miles possible before eating at a sh hole, while keeping track of which sh holes were visited in the shHole list. Clearly if the the distance between two consecutive sh holes are are greater than m, then there is no solution to the problem, and the algorithm will return Stay Home. This runs in O(n) since it goes over every element of F at most twice.

(b) Prove by induction that your algorithm is correct. You may assume that there is a way for Plucky to make it t miles (aka, the algorithm won't return Stay Home) if it's easier.

[We are expecting: A formal proof by induction. Be sure to clearly state your inductive hypothesis, base case, inductive step, and conclusion.] Solution: To begin the proof, rst let us dene a few things: let the f ishStops be the solution that is outputted by our algorithm, and let f ishStops^ be the optimal solution. Notice that clearly jf ishStopsj  jf ishStopsj. Now, let g(i; X) be the position of plucky after taking the rst i stops from list X. We will rst prove the following: 8 i 2 [0; jf ishStopsj] : g(i; f ishStops)  g(i; f ishStops). We will proceed via a proof by induction: