/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} 18 E Consider the following variation... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

Consider the following variation on the change-making problem (Exercise 6.17): you are given denominations x1, x2,...,xn, and you want to make change for a value v, but you are allowed to use each denomination at most once. For instance, if the denominations are 1,5,10,20,then you can make change for 16=1+15and for 31=1+10+20but not for 40(because you can’t use 20 twice).

Input: Positive integers; x1, x2,...,xnanother integer v.

Output: Can you make change for v, using each denominationxi at most once?Show how to solve this problem in time O(nV).

Short Answer

Expert verified

The problem can also be solved by dynamic programming.

Di,0=Truefori=0â€Ôò´Ç n â¶Ä„â¶Ä„Di,0=Truefori=1â€Ôò´Ç n â¶Ä„â¶Ä„forj=1â€Ôò´Ç V

 â¶Ä„â¶Ä„ â¶Ä„â¶Ä„ifxi−1≤j â¶Ä„â¶Ä„ â¶Ä„â¶Ä„ â¶Ä„â¶Ä„Di,j=Di−1,j−xi−1°¿¸é Di−1,j â¶Ä„â¶Ä„ â¶Ä„â¶Ä„else â¶Ä„â¶Ä„ â¶Ä„â¶Ä„ â¶Ä„â¶Ä„Di,j=Di−1,j°ù±ð³Ù³Ü°ù²Ô Dn,V

Step by step solution

01

Defining the recurrence relation

Let the sub problem be Dn,V.

A sub-problemDv−xi is made at each step, from unused denomination.

The possible cases are:

  • Dn−1,V−xn=TRUEimplies that coin of denomination xn is used to make value V, so V−xncan be made using n−1number of coins. So, Dn,Vwill also be true.
  • Dn−1,V=TRUE implies that a coin with denomination xnis not used to make value V and V can be made fromn−1 coin, soDn,V is true.
  • If both above discussed cases are not true that means,Dn,V then V cannot be obtained from n coins.

Based on above conditions, the recurrence relation is as follows:

Dn,V=1;EDn−1,V−xnORDn−1,V0;otherwise

02

Determine an algorithm

The algorithm is given as follows:

Consider an array of coins

Di,0=Truefori=0â€Ôò´Ç n â¶Ä„â¶Ä„Di,0=Truefori=1â€Ôò´Ç n â¶Ä„â¶Ä„forj=1â€Ôò´Ç V

 â¶Ä„â¶Ä„ â¶Ä„â¶Ä„ifxi−1≤j â¶Ä„â¶Ä„ â¶Ä„â¶Ä„ â¶Ä„â¶Ä„Di,j=Di−1,j−xi−1°¿¸é Di−1,j â¶Ä„â¶Ä„ â¶Ä„â¶Ä„else â¶Ä„â¶Ä„ â¶Ä„â¶Ä„ â¶Ä„â¶Ä„Di,j=Di−1,j°ù±ð³Ù³Ü°ù²Ô Dn,V

03

Analyse the time complexity of an algorithm

The first loop runs for n times. There two nested loops takes nV times. Since, n<<nV

Thus, the runtime of the algorithm is OnV.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91Ó°ÊÓ!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Here is yet another variation on the change-making problem (Exercise 6.17). Given an unlimited supply of coins of denominations x1,x2,x3.........xnwe wish to make change for a value v using at most k coins; that is, we wish to find a set of≤kcoins whose total value is v. This might not be possible: for instance, if the denominations are 5 and 10 and k=6, then we can make change for 55 but not for 65. Give an efficient dynamic-programming algorithm for the following problem. Input: ; x1,x2,x3.........xn;k;v.Question: Is it possible to make change for v using at most k coins, of denominations x1,x2,x3.........xn?

Consider the following 3-PARTITION problem. Given integersa1,...,an, we want to determine whether it is possible to partition of {1,...,n} into three disjoint subsets I,J,Ksuch that

∑aii∈I=∑ajj∈J=∑akk∈k=13∑aii∈1 .

For example, for input(1,2,3,4,4,5,8) the answer is yes, because there is the partition(1,8),(4,5),(2,3,4). On the other hand, for input(2,2,3,5) the answer is no. Devise and analyze a dynamic programming algorithm3-PARTITION for that runs in time polynomial in n and in Σiai.

You are given a convex polygon P on n vertices in the plane (specified by their x and y coordinates). A triangulation of P is a collection of n-3diagonals of such that no two diagonals intersect (except possibly at their endpoints). Notice that a triangulation splits the polygon’s interior into n-2 disjoint triangles. The cost of a triangulation is the sum of the lengths of the diagonals in it. Give an efficient algorithm for finding a triangulation of minimum cost. (Hint: Label the vertices of P by 1,....,n, starting from an arbitrary vertex and walking clockwise. For 1≤i<j≤n, let the subproblem A(i,j)denote the minimum cost triangulation of the polygon spanned by vertices i,i+1,...,j.).

Yuckdonald’s is considering opening a series of restaurant along Quaint Valley Highway(QVH). The n possible locations are along a straight line, and the distances of these locations from the start of QVH are, in miles and in increasing order,m1,m22,....,mn.. The constraints are as follows:

At each location, Yuckdonald may open at most one restaurant. The expected profit from opening a restaurant at location i is given aspi, wherepi>0andi=1,2,…,n.

Any two restaurants should be at least k miles apart, where k is a positive integer.

Give an efficient algorithm to compute the maximum expected total profit subject to the given constraints.

A mission-critical production system has n stages that have to be performed sequentially; stage i is performed by machine Mi. Each machine Mi has a probability riof functioning reliably and a probability 1-riof failing (and the failures are independent). Therefore, if we implement each stage with a single machine, the probability that the whole system works is r1·r2···rn. To improve this probability we add redundancy, by having mi copies of the machine Mi that performs stage i. The probability that all mi copies fail simultaneously is only (1-ri)mi,so the probability that stage i is completed correctly is 1 − (1-ri)mi, and the probability that the whole system works isΠni=1(1-1-rimi).Each machine has a cost ci, and there is a total budget to buy machines. (Assume that B and ciare positive integers.) Given the probabilities r1·r2···rn, the costsc1,...,cn, and the budget find the redundanciesm1,...,mn that are within the available budget and that maximize the probability that the system works correctly.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.