Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Zeke plans to backpack across the Sawtooth Mountain Range in Idaho. He can carry

ID: 3808443 • Letter: Z

Question

Zeke plans to backpack across the Sawtooth Mountain Range in Idaho. He can carry two liters of water, and he can hike m miles before running out of water. Zeke will start with two full liters of water. His map shows all the places along the trail at which he can refill his water from streams (don’t forget to filter, Zeke!) and the distances between these locations. The goal is to minimize the number of water stops along his route across the mountain range. Give an efficient greedy method by which he can determine which water stops he should make. Prove that your strategy yields an optimal solution, and give its running time. To prove that the problem has optimal substructure, follow the approach on page 416 in the paragraph beginning with “the usual cut-and-paste argument...”.

The usual cut-and-paste argument shows that the optimal solution Ai must also include optimal solutions to the two subproblems for Sik and Ski. If we could find a set Ak, of mutually compatible activities in Ski IA kj then we could use A kj rather than kj in a solution to the subproblem for Su. We A would have constructed a set of A lAkil 1 IA. kl IA 1 Aul mutually compatible activities, which contradicts the assumption that Aij is an optimal solution. A symmetric argument applies to the activities in Sik This way of characterizing optimal substructure suggests that we might solve the activity-selection problem by dynamic programming. If we denote the size of an optimal solution for the set Sij by cli, j], then we would have the recurrence c[i, j] Of course, if we did not know that an optimal solution for the set Su includes activity ak, we would have to examine all activities in Sij to find which one to choose, so that if S 0, cli, j] max. ak ES We could then develop a recursive algorithm and memoize it, or we could work bottom-up and fill in table entries as we go along. But we would be overlooking another important characteristic of the activity-selection problem that we can use to great advantage.

Explanation / Answer

word right here that this can be solved very without difficulty via in reality outputting 2*(k - 1) for a given okay . The cause here, but, is to illustrate the primary concept of recursion instead of fixing the problem.

algorithm 1:   Even(positive integer k)

input: okay , a wonderful integer

Output: k-th even natural quantity (the first even being zero)

set of rules:

if okay = 1, then go back 0;

else go back Even(okay-1) + 2 .

right here the computation of Even(okay) is decreased to that of Even for a smaller input price, this is Even(k-1). Even(okay) subsequently turns into Even(1) that's 0 with the aid of the primary line. as an example, to compute Even(3), set of rules Even(k) is known as with ok = 2. within the computation of Even(2), set of rules Even(okay) is referred to as with k = 1. on account that Even(1) = 0, zero is lower back for the computation of Even(2), or even(2) = Even(1) + 2 = 2 is obtained. This value 2 for Even(2) is now returned to the computation of Even(three), and even(three) = Even(2) + 2 = four is received.

As may be seen with the aid of evaluating this set of rules with the recursive definition of the set of nonnegative even numbers, the primary line of the set of rules corresponds to the premise clause of the definition, and the second line corresponds to the inductive clause.

by manner of contrast, allow us to see how the identical hassle can be solved by way of an iterative set of rules.

algorithm 1-a:   Even(superb integer k)

input: ok, a fine integer

Output: k-th even herbal range (the primary even being 0)

algorithm:

int   i, even;

i := 1;

even := zero;

whilst( i < ok )

          even := even + 2;

          i := i + 1;

return even .

instance 2: algorithm for computing the okay-th energy of two

set of rules 2   Power_of_2(natural number k)

enter: ok , a herbal variety

Output: k-th electricity of 2

set of rules:

if k = zero, then go back 1;

else go back 2*Power_of_2(ok - 1) .

by means of way of assessment, allow us to see how the equal problem can be solved by an iterative set of rules.

algorithm 2-a   Power_of_2(herbal wide variety k)

input: k , a herbal quantity

Output: k-th strength of 2

algorithm:

int   i, power;

i := 0;

strength := 1;

whilst( i < okay )

          strength := electricity * 2;

          i := i + 1;

return electricity .

the next example does no longer have any corresponding recursive definition. It indicates a recursive way of solving a trouble.

instance 3: Recursive set of rules for Sequential search

algorithm three   SeqSearch(L, i, j, x)

enter: L is an array, i and j are wonderful integers, i   j, and x is the important thing to be looked for in L.

Output: If x is in L among indexes i and j, then output its index, else output 0.

set of rules:

if i   j , then

   if L(i) = x, then go back i ;

   else return SeqSearch(L, i+1, j, x)

else return zero.

Recursive algorithms can also be used to check items for club in a set.

instance 4: set of rules for checking out whether or not or not a number of x is a herbal variety

algorithm 4   natural(more than a few x)

enter: a variety of x

Output: "yes" if x is a natural quantity, else "No"

set of rules:

if x < zero,   then go back "No"

else

    if x = 0,   then go back "yes"

    else go back natural( x - 1 )

instance 5: set of rules for checking out whether or not or now not an expression w is a proposition(propositional shape)

set of rules five   Proposition( a string w )

enter: A string w

Output: "sure" if w is a proposition, else "No"

set of rules:

if w is 1(actual), 0(fake), or a propositional variable, then return "sure"

else if w = ~w1, then return Proposition(w1)

   else

     if ( w = w1   w2 or w1 w2 or w1 w2 or w1 w2 ) and

       Proposition(w1) = sure and   Proposition(w2) = yes

     then go back yes

     else go back No

stop