Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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 ofkcoins 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?

Short Answer

Expert verified

It may or may not be possible to make change for v using at most k coins,of denominations x1,x2,x3.........xn

Step by step solution

01

Dynamic programming approach.

In dynamic programming there are all possibilities and more time as compared to greedy programming. and the Dynamic programming approach always gives the accurate or correct answer. In dynamic programming have to compute only distinct function call because as soon as compute and store in one data structure so that after this reuse afterward if it is needed.

02

Define Recurrence Relation and define its Algorithm.

Let T(v) be the minimum number of coins needed.

We have ‘n’ number of coins of denomination, where we have check for the possibility to make a change for value v by taking at most k coins from given denominations.

The supply of coins of denominations x1,x2,x3.........xnand 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. 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.

To achieve this, we will create dynamic algorithm where first we need to find the least number of coins needed to get value v. After this, comparing it with ‘k’ coins to check if we can make value using k coins only.

So, the recurrence relation will be:

Tv=MINMIN{Tv-x+1;if1in,Tv=;otherwise,

Here x1,x2,x3...........xnwe take an array Change [] with v elements in it. This means the length of array Change [] will be v. We will take the value of each element as ‘infinity’. Here value at index ‘j’ will tell about the number of coins need to make value. So, when we trace index ‘v’, the algorithm will check if the value at index ‘v’ is less than ‘k’. to compute only distinct function call because as soon as compute and store in one data structure so that after this reuse afterward if it is needed.

So, algorithm will return true if the condition is satisfied else, return false.

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 Vaia!

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

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.

Time and space complexity of dynamic programming. Our dynamic programming algorithm for computing the edit distance between strings of length m and n creates a table of size n×mand therefore needs O (mn) time and space. In practice, it will run out of space long before it runs out of time. How can this space requirement be reduced?

  1. Show that if we just want to compute the value of the edit distance (rather than the optimal sequence of edits), then only O(n) space is needed, because only a small portion of the table needs to be maintained at any given time.
  2. Now suppose that we also want the optimal sequence of edits. As we saw earlier, this problem can be recast in terms of a corresponding grid-shaped dag, in which the goal is to find the optimal path from node (0,0) to node (n,m). It will be convenient to work with this formulation, and while we’re talking about convenience, we might as well also assume that is a power of 2.
    Let’s start with a small addition to the edit distance algorithm that will turn out to be very useful. The optimal path in the dag must pass through an intermediate node (k,m2) for some k; show how the algorithm can be modified to also return this value k.
  3. Now consider a recursive scheme:
    Procedure find-path((0,0)(n,m))
    Compute the value kabove
    find-path ((0,0)k,m2)
    find-path k,m2n,m
    concatenate these two paths, with kin the middle.
    Show that this scheme can be made to run inO (mn) time and O(n) space.

A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence

A,C,G,T,G,T,C,A,A,A,A,T,C,G

has many palindromic subsequences, including A,C,G,C,Aand A,A,A,A(on the other hand, the subsequence A,C,Tis not palindromic). Devise an algorithm that takes a sequence X[1...n]and returns the (length of the) longest palindromic subsequence. Its running time should be0(n2).

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

Give an O(nt) algorithm for the following task. Input: A list of n positive integers a1,a2,...,an; a positive integer t. Question: Does some subset of the ai’s add up to t? (You can use each ai at most once.) (Hint: Look at subproblems of the form “does a subset of{a1,a2,...,ai} add up to ?”)

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.

Sign-up for free