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
๎
.