Question

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.

to j 1 234 1 037 Cost from i 3

The problem is to find a dynamic programming algorithm that computes the cheapest sequence of rental taking you from post 1 all the way down to post n. In this example, the cheapest way is to rent canoes from post 1 to post 3, and then from post 3 to post 4 for a total cost of 5. The second problem is to find the least cost associated with this sequence. You are to design a dynamic programming algorithm for both the problems.

Describe the table and what does each entry in the table mean?

How will the table be initialized?

In which order the table will be filled?

What is the recurrence?

How will you use the table to find what is cheapest sequence of canoe rental (for the first problem) and the least cost of the canoe rentals (for the second problem)?

Give the asymptotic complexity of the algorithms. Implement the your algorithm by using C

Input: The input consists of n+1 lines: the first line will be a single integer that indicates the number of trade posts. The next n lines will give the rental costs that taking post i to j (where i≤j). For example, the input of the above given example would be:

4
0 2 3 7
0 2 4
0 2
0


Output: Output the minimum cost to travel from post 1 to post n. Output the sequence of canoe renting that achieves the goal. For example, the sample output of the above given example would be:

The minimum cost is 5
The renting sequence is 1->3->4

I have an algorithm to find the minimum cost. How could I go about printing the renting sequence?

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

Solution

Description of the table:

The cost table indicates the cost values from one post to another. The entries in the cost table indicates the cost value from the post i to the post j. For example, in the given table, the cost from the post 1 to post 4 is 7.

Initialization of the table:

The table can initialise using 2-dimensional array. The values of the cost from post i to post j can store in the 2-dimensional array. For example, let the 2-dimensional array is postcost[][].

Then, the value of the cost from the post 1 to post 4 is stored as postCost[1][4] =7.

Filling the table:

The values in the table can be filled by taking the post i to j where i value is less than or equal to j (i<=j).

Recurrence: The recurrence has one or more bases cases and recursive cases. The recursive function is sequence of term in a linear combination of the previous terms.

The minimum cost can be find by using the recursion function and checks every path and by finding the sum of the cost of the post of the minimum path using the recursive functions.

Program screenshot:

# include<stdio.h> //function prototypes int getMiniumcost (int postCost [10] [10]) int findShortestPath (int postCost [10] [10], int source, int destination); //declare the variables int numofTrades; int postCost[10] [10]0 ; //definition of the main int main() //read the number of trade posts. scanf (%d, &nurnOfTrades) ; int i, ji //read the rental costs that taking post i to j (where išj) for (i -0; i < numofTrades; i++) for (j0 j < numofTrades: j++) if (i < j) scanf (%d, &postcost [i] [j]); //call the function getMiniumcost) by passing the postCost array printf (The min. Cost cost is %d\n, getMiniumCost (postcost)); return 0;

Screenshot of the output:

Code to copy:

#include<stdio.h>

//function prototypes

int getMiniumCost(int postCost[10][10]);

int findShortestPath(int postCost[10][10], int source, int destination);

//declare the variables

int numOfTrades;

int postCost[10][10] = { 0 };

//definition of the main

int main()

{

       //read the number of trade posts.

       scanf("%d", &numOfTrades);

       int i, j;

       //read the rental costs that taking post i to j (where i≤j)

       for (i = 0; i < numOfTrades; i++)

       {

              for (j = 0; j < numOfTrades; j++)

              {

                     if (i <= j)

                     {

                           scanf("%d", &postCost[i][j]);

                     }

              }

       }

       //call the function getMiniumCost() by passing the postCost array

       printf("The minimum cost is %d\n", getMiniumCost(postCost));

       return 0;

}

//definition of the function getMiniumCost()

int getMiniumCost(int postCost[10][10])

{

       //call the function findShortestPath()

       return findShortestPath(postCost, 0, numOfTrades - 1);

}

//definition of the function findShortestPath()

int findShortestPath(int postCost[10][10], int source, int destination)

{

       // if source and destination is same

       if (source == destination || source + 1 == destination)

              return postCost[source][destination];

       int min_Cost = postCost[source][destination];

       //iterate for all the nodes

       for (int i = source + 1; i<destination; i++)

       {

              int temp = findShortestPath(postCost, source, i) + findShortestPath(postCost, i, destination);

              if (temp < min_Cost)

              {

                     min_Cost = temp;

              }

       }

       //return the min_Cost cost

       return min_Cost;

}

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

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

  • The problem is to find a solution that computes the cheapest sequence of rentals taking you...

    The problem is to find a solution that computes the cheapest sequence of rentals taking you from post 1 all the way down to post n. In this example, the cheapest sequence is to rent from post 1 to post 3 (cost 3), then from post 3 to post 4 (cost 2), with a total cost of 5 (less than the direct rental from post 1 to post 7, which would cost 7). Find a Brute Force Algorithm for the...

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

  • need help with #1 thank you! EXERCISES Section 1.1 (Note: Solutions to odd-numbered exercises are given...

    need help with #1 thank you! EXERCISES Section 1.1 (Note: Solutions to odd-numbered exercises are given in the back of the book.) 1. Write an algorithm that computes the sum of the first n terms of the series 2. Write an algorithm that finds the smallest element among a, b, and c 3. Write an algorithm that finds the smallest element in the sequence s, s S, of n dis- tinct numbers. The input is the sequence, s, and the...

  • Preferably in python but java is good too Task 1: Minimum Spanning Trees For this warm-up...

    Preferably in python but java is good too Task 1: Minimum Spanning Trees For this warm-up task you are to implement any efficient minimum spanning tree algorithm that takes a sequence of edge-weighted graphs and outputs the minimum cost weight of a spanning tree of each Input Format For this assignment we use adjacency matrices with positive integer weights. Here a zero entry at row i and column J indicates that no edge i] exists in the graph. The first...

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

  • Please solve using Java and comment your code for clarity. Sample 3. Input: 4 1 231...

    Please solve using Java and comment your code for clarity. Sample 3. Input: 4 1 231 Output: 0 This sequence also does not have a majority element (note that the element 1 appears twice and hence is not a majority element). Problem Introduction Majority rule is a decision rule that selects the alternative which has a majority, that is, more than half the votes. Given a sequence of elements (1.02....,On, you would like to check whether it contains an element...

  • 8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za...

    8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za then anstwer-1 return answer 18 and the (a) Assume that this algorithm receives as input the numbersz-32 and corresponding sequence of integers 2 | 3 1 1 4151617| 8| 9 | 10 İ 11 İ 12 | 13 | 14|15 | 16 | 17 |...

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