Question

There are n trading posts numbered 1 to n as you travel downstream. At any trading...

There are n trading posts numbered 1 to n as you travel downstream. At any trading post i you can rent a canoe to be returned at any of the downstream trading posts j, where j >= i. You are given an array R[i, j] defining the costs of a canoe which is picked up at post i and dropped off at post j, for 1 ≤ i ≤ j ≤ n. Assume that R[i,i] = 0 and that you can’t take a canoe upriver. Your problem is to determine a sequence of rentals which start at post 1 and end at post n, and that has the minimum total cost.

a) Describe verbally and give pseudo code for a DP algorithm called CanoeCost to compute the cost of the cheapest sequence of canoe rentals from trading post 1 to n. Give the recursive formula you used to fill in the table or array.

b) Using your results from part a) how can you determine the sequence of trading posts from which canoes were rented? Give a verbal description and pseudo code for an algorithm called PrintSequence to retrieve the sequence.

c) What is the running time of your algorithms?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We have to reach from 1 to n and we have to rent a canoe to reach there. Consider the following statement:

Minimum cost for reaching from 1 to n will be the sum of the minimum cost of reaching from 1 to i and minimum cost of reaching from i to n.

the above statement holds the answer to the question.

What we have to do is find the value of i that minimizes this sum.

Here we define our solution recursively:

Now, we can store the results in the matrix C of size nxn, where C[i,j] represents minimum canoe cost from i to j, initially initialised to 0. Here is the pseudocode:

CanoeCost(x, y)

A = zeroes(n,n);

for i from 1 to n

for j from 1 to n-i

A[j, j+i] = minj<k<j+i{A[j,k] + A[k,j+i]}

end

end

return A[1,n]

b) to get the canoe used, we just need to store the value of k for which we obtained minimum expression in algorithm and then sorting them in ascending order.

Here is the pseudocode:

PrintSequence(x, y)

A = zeroes(n,n);

list = {}

for i from 1 to n

for j from 1 to n-i

A[j, j+i] = minj<k<j+i{A[j,k] + A[k,j+i]}

add k to list

end

end

Sort(list)

print(list)

c) Running time is same for both the algorithms. Both have two loops and in the innermost loop, we calculate minimum which takes O(n)time, so, overall complexity is O(n3)

Add a comment
Know the answer?
Add Answer to:
There are n trading posts numbered 1 to n as you travel downstream. At any trading...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • There are n trading posts numbered 1 to n as you travel downstream. At any trading...

    There are n trading posts numbered 1 to n as you travel downstream. At any trading post i you can rent a canoe to be returned at any of the downstream trading posts j>i. You are given a cost array R(i,j) giving the cost of these rentals for all 1≤i<j≤n. We can assume that R(i,i)=0, and that you can't go upriver (so perhaps R(i,j)= ∞ if i>j). For example, one cost array with n=4 might be the following. The problem...

  • There are n trading posts along a river. At any of the posts you can rent...

    There are n trading posts along a river. At any of the posts you can rent a canoe to be returned at any other post downstream. (It is next to impossible to paddle against the current.) For each possible departure point i and each possible arrival point j the cost of a rental from i to j is known. However, it can happen that the cost of renting from i to j is higher than the total cost of a...

  • You are canoeing down a river and there are n renting posts along the way. Before...

    You are canoeing down a river and there are n renting posts along the way. Before starting your journey, you are given, for each 1 lessthanorequalto i lessthanorequalto j lessthanorequalto n, the fee f(i, j) for renting a canoe from post i to post j. These fees are arbitrary. For example it is possible that f(1, 3)= 10 and f(1, 4) = 5. You begin at trading post 1 and must end at trading post n (using rented canoes). Your...

  • When asked to describe an algorithm you are expected to give a clear pseudo-code description of...

    When asked to describe an algorithm you are expected to give a clear pseudo-code description of the algorithm 1. (10 pts) Here is a new sorting algorithm NewSort Suppose the original call made is NewSort(A,0,n-1) where A is an array integers. == void NewSort(int A[], int i, int j){ \\ sorts the subarray Aſi..j] if (j i+1) \\when there are only 2 elements if (A[i] > A[j]) swap(A,i,j) \\swaps A[i] and A[j] else { int k = (j-i+1)/3; NewSort(A,i,j-k); \\...

  • Given an array A[1..n] of positive integers and given a number x, find if any two...

    Given an array A[1..n] of positive integers and given a number x, find if any two numbers in this array sum upto x. That is, are there i,j such that A[i]+A[j] = x? Give an O(nlogn) algorithm for this. Suppose now that A was already sorted, can you obtain O(n) algorithm?

  • 2. Assume you have an input array A with entries numbered 1 through n. One intuitive...

    2. Assume you have an input array A with entries numbered 1 through n. One intuitive way to randomize A is to generate a set of n random values and then sort the array based on these arbitrary mumbers. (a) (10 pts) Write pseudocode for this PERMUTE-BY-SorTInG method (b) (10 pts) Do best-, average, and worst-case analyses for this method. (c) Extra credit (15 pts): The algorithm RANDOMIZE-In-PLACE(A) A. length for i 1 to n 1 n= 2 swap Ali...

  • 6. Consider the following basic problem. You're given an array A consisting of n integers A[1],...

    6. Consider the following basic problem. You're given an array A consisting of n integers A[1], A[2], , Aln]. You'd like to output a two-dimensional n-by-n array B in which B[i, j] (for i <j) contains the sum of array entries Ali] through Aj]-that is, the sum A[i] Ai 1]+ .. +Alj]. (The value of array entry B[i. Λ is left unspecified whenever i >j, so it doesn't matter what is output for these values.) Here's a simple algorithm to...

  • Suppose you have an array S indexed from 1 to n which contains n numbers, not...

    Suppose you have an array S indexed from 1 to n which contains n numbers, not in any particular order, and you wish to count how many times a given number x occurs in S. Consider the recursive algorithm below for this which finds the number of occurrences of x in the index range i...j in S. Of course, solving the problem would involve an initial call to the algorithm for the range 1.n: int CountOccur (int i,j) { int...

  • In this project, you will work on the algorithm (discussed in Module 1) to determine the...

    In this project, you will work on the algorithm (discussed in Module 1) to determine the length of the longest sub sequence of consecutive integers in an array You will implement the algorithm using Hash tables. You are provided with sample code (in C++ and Java) representing the linked list-based implementation of Hash tables (as an array of Linked Lists). You could go through the code to understand the implementation of a Hash table and the functions that can be...

  • (1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an...

    (1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an integer between 1 and n. (2) Suppose Algorithm-1 does f(n) = n**2 + 4n steps in the worst case, and Algorithm-2 does g(n) = 29n + 3 steps in the worst case, for inputs of size n. For what input sizes is Algorithm-1 faster than Algorithm-2 (in the worst case)? (3) Prove or disprove: SUM{i**2} [where i changes from i=1 to i=n] ϵ tetha(n**2)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT