Notes:

Problem : Devise an efficient algorithm that, given an sequence of integers $a_1, a_2, \ldots, a_n$, the length of the sequence ($n$), and a nonnegative $k$, determines if there is a run of at least $k$ consecutive equal values in the sequence. For example, if the sequence is $1, 3, 3, 1, 1, 2, 2, 2, 37$ and $k = 3$ then the result should be true because there are 3 2's in a row, but if $k = 4$ then the result should be false. Use an invariant to prove that your algorithm is correct.

Problem :

diagrams of yard perimeters
For the version of the problem with the house, if $n = 7$, $d$ = $[0.0, 1.0, 2.1, 3.1, 4.3, 5.2, 5.8, 7.2, 8.4]$, and $L = 3$, then the optimal number of segments is 4, for example $[0.0, 1.0, 3.1, 5.8, 8.4)]$ (corresponding to segments indicated by consecutive sides of the same boldness). Without the house, if $n = 9$, $q = 3$, $k = 1$, $d = [0.0, 1.0, 2.1, 3.1, 4.3, 5.2, 5.8, 7.2, 8.6, 8.9]$, and $L=3$, then the optimal number of segments is 4 again, for example $[1.0, 3.1, 5.8, 8.6, 9.9]$.

Problem : Consider the problem of scheduling overnight stops for a hiker on a long-distance trek. Each overnight stop must be in a cabin; the distances from the start of the trek to each cabin is given in an array called loc (so the difference between two elements in the array gives the distance between the corresponding cabins). The hiker can travel up to max_d units each day. And with some extra effort, the hiker can travel a little more. But the extra effort takes a toll on the hiker: the total extra effort over the entire trek must not exceed max_e; resting by travelling less than max_d does not affect this.

For example, if the locations of the cabins are [0.0, 1.0, 1.1, 2.0, 2.2, 3.0, 3.5] with max_d = 1.0 and max_e=0.5, then the hiker can complete the trek in 3 days by expending 0.1 extra effort to reach the cabin at 1.1 on the first day, expending another 0.1 extra effort to reach the cabin at 2.2 on the second day, and then expending the remaining 0.3 extra effort to get to the destination, exhausted, on the third day.

The following algorithm schedules the hiker's stops greedily by finding the furthest cabin the hiker can travel to on each day by possibly expending all the remaining extra effort.

Prove that this algorithm returns a shortest possible list of stopping points, or find an example of input parameters that are consistent with the preconditions for which the output is not optimal, explaining what the output is for those inputs and what an optimal schedule is.

FUNCTION schedule_travel(loc, max_d, max_e)
# Preconditions:  
# loc is the list of distances from the starting point of each potential
# stopping points; the first element will be 0.0 (for the distance from
# the starting point to itself), and no two consecutive elements will have
# a difference of more than max_d
#
# max_d is a positive number giving the maximum distance that can be
# travelled in a day without extra effort
#
# max_e is a nonnegative number giving the maximum allowed sum of extra effort

# start at beginning
start <- 0                    # start at beginning
stop_points <- []             # list of stopping points is initially empty
remaining_extra <- max_e      # have all our extra effort left to use
	 
# loop until starting point for current day is the destination
while start < len(loc) - 1:
    stop <- start + 1         # first potential stop to try is one just after start

    # advance stopping point for current day until at destination or the
    # next stopping point is invalid, even with extra effort	       
    while stop < len(loc) - 1 and loc[stop + 1] - loc[start] <= max_d + remaining_extra:
        stop <- stop + 1

    # account for extra effort if travelled more than max_d on current day
    if loc[stop] - loc[start] > max_d:
        remaining_extra <- remaining_extra - (loc[stop] - loc[start] - max_d)

    start <- stop             # starting point for next day is stoping point for current day 
    stop_points.append(stop)  # add stop to end of list of stopping points

return stop_points

Problem :