Docsity

Homework 5 Greedy Algorithm, Quizzes of Computer science

Toggle sidebar
bg1
1. Use the proposed strategy to formulate a greedy algorithm. The input is a (sorted) list of positions
X = {x๎˜0๎˜,x๎˜1๎˜,...,x๎˜n๎˜}, where x๎˜0 ๎˜= 0 and x๎˜n ๎˜= L, and a maximum distance d. The expected output is a
(sorted) sequence of indices G = {g๎˜0๎˜, g๎˜1๎˜, . . . , g๎˜k๎˜}, with g๎˜0 ๎˜= 0 and g๎˜k ๎˜= n. Present the algorithm
using pseudocode.
Initialize empty array A
a = d
for ๎˜i = 0 ๎˜to ๎˜n:
if ๎˜x[i] > a ๎˜then ๎˜a = g[i]
A.append(a)
end if
end for
2. The greedy algorithm selects the bars given by the indices G = {g๎˜0 ๎˜, g๎˜1 ๎˜, . . . , g๎˜k ๎˜}. Show that G is
a solution to the problem, meaning that the greedy algorithm always finds a way from the first bar
to the last one using only legal moves.
A solution can be mapped as getting from the first bar to last bar using only legal
moves. By contradiction, if we say that G is not optimal, then that means that m <
k, or that we cannot get from the first bar to the last using only legal moves. But,
since greedy stays ahead, the position of i must be less than or equal to the max
number (m) of moves that can occur. But, for d(0), if the candidate has moved 0
distance, then that candidate j would not be included. Therefore this cannot be an
optimal solution, and proves that m<k must be false. Therefore the greedy solution
is a solution that can be used.
3. We will use the ๎˜greedy stays ahead
๎˜
strategy to prove optimality of the greedy solution. Consider
two solutions, with the indices representing the selected bars sorted in increasing order: The
greedy solution G = {g๎˜0๎˜,g๎˜1๎˜,...,g๎˜k๎˜} and an optimal solution O = {o๎˜0๎˜,o๎˜1๎˜,...,o๎˜m๎˜}. Since O is an
optimal solution, we have m โ‰ค k.
a. State the claim that we need to prove for each step of the greedy algorithm. The claim
should express that ๎˜greedy stays ahead
๎˜
.
pf3

Partial preview of the text

Download Homework 5 Greedy Algorithm and more Quizzes Computer science in PDF only on Docsity!

  1. Use the proposed strategy to formulate a greedy algorithm. The input is a (sorted) list of positions X = {x 0 ,x 1 ,...,xn}, where x 0 = 0 and xn = L, and a maximum distance d. The expected output is a (sorted) sequence of indices G = {g 0 , g 1 ,... , gk}, with g 0 = 0 and gk = n. Present the algorithm using pseudocode.

Initialize empty array A

a = d

for i = 0 to n:

if x[i] > a then a = g[i]

A.append(a)

end if

end for

  1. The greedy algorithm selects the bars given by the indices G = {g 0 , g 1 ,... , gk }. Show that G is a solution to the problem, meaning that the greedy algorithm always finds a way from the first bar to the last one using only legal moves.

A solution can be mapped as getting from the first bar to last bar using only legal

moves. By contradiction, if we say that G is not optimal, then that means that m <

k, or that we cannot get from the first bar to the last using only legal moves. But,

since greedy stays ahead, the position of i must be less than or equal to the max

number (m) of moves that can occur. But, for d(0), if the candidate has moved 0

distance, then that candidate j would not be included. Therefore this cannot be an

optimal solution, and proves that m<k must be false. Therefore the greedy solution

is a solution that can be used.

  1. We will use the greedy stays ahead strategy to prove optimality of the greedy solution. Consider two solutions, with the indices representing the selected bars sorted in increasing order: The greedy solution G = {g 0 ,g 1 ,...,gk} and an optimal solution O = {o 0 ,o 1 ,...,om}. Since O is an optimal solution, we have m โ‰ค k. a. State the claim that we need to prove for each step of the greedy algorithm. The claim should express that greedy stays ahead.

We claim that for i โ‰ค m, gi โ‰ฅ oi.

b. Prove the claim for i = 0, the base case.

For the base case, take i = 0. By construction, the greedy algorithm chooses g 0 = d(0), the

candidate who went length of 0 on the monkeybars. The optimal solution must meet the

requirement. In particular, it must allow all candidates to reach the end. This means that

the first length in the optimal solution cannot be any later than the departure length of the

first candidate. We have o 0 โ‰ค d(0) = g 0.

c. Suppose that the claim is true for 0 โ‰ค i โ‰ค m and prove that it is true for i+1.

Assuming that the statement is true for all 0 โ‰ค i โ‰ค m. Among all the candidates that

choose the bars, gi, consider the one who falls the earliest. Call that candidate j.

The greedy algorithm chooses gi+1 = d(j). By the induction hypothesis, gi โ‰ฅ oi, so

there is no time ol for l โ‰ค i in the optimal solution when candidate j can reach the

end of the monkey bars. Since the optimal solution must meet the requirement and

in particular allow all the candidates to reach the end, candidate j must be

permitted to also reach the end. We must therefore have oi+1 โ‰ค d(j). But then oi+1 โ‰ค

d(j) = gi+1 and we have shown that if the statement is true for i < m, it is also true

for i + 1. By induction, the claim is true for i โ‰ค m.

d. Use the claim to prove optimality of the greedy algorithm

We will finally use the greedy stays ahead argument to prove that the greedy

solution is optimal. This means that optimal solution, k = m. Since O is an optimal

solution, we already know that m โ‰ค k, so we want to show that m โ‰ฅ k.

For the sake of contradiction, suppose that G is not optimal, that is m < k. This

means that there is a distance gm+1 scheduled by the greedy algorithm that

corresponds to the length that some candidate j went on the monkey bars. From the

greedy stays ahead claim, we know that for i โ‰ค m, gi โ‰ฅ oi. In particular, for the last

distance in the optimal solution, gm โ‰ฅ om. But then, candidate j cannot have

reached the end at any of the times in O. This contradicts the fact that O is an