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

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 to n   Di,0=Truefori=1 to n   forj=1 to V

      ifxi1j         Di,j=Di1,jxi1OR Di1,j      else         Di,j=Di1,jreturn Dn,V

Step by step solution

01

Defining the recurrence relation

Let the sub problem be Dn,V.

A sub-problemDvxi is made at each step, from unused denomination.

The possible cases are:

  • Dn1,Vxn=TRUEimplies that coin of denomination xn is used to make value V, so Vxncan be made using n1number of coins. So, Dn,Vwill also be true.
  • Dn1,V=TRUE implies that a coin with denomination xnis not used to make value V and V can be made fromn1 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;EDn1,VxnORDn1,V0;otherwise

02

Determine an algorithm

The algorithm is given as follows:

Consider an array of coins

Di,0=Truefori=0 to n   Di,0=Truefori=1 to n   forj=1 to V

      ifxi1j         Di,j=Di1,jxi1OR Di1,j      else         Di,j=Di1,jreturn 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 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

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 1i<jn, let the subproblem A(i,j)denote the minimum cost triangulation of the polygon spanned by vertices i,i+1,...,j.).

Exon chaining.Each gene corresponds to a subregion of the overall genome (the DNA sequence); however, part of this region might be “junk DNA.” Frequently, a gene consists of several pieces called exons, which are separated by junk fragments called introns. This complicates the process of identifying genes in a newly sequenced genome.

Suppose we have a new DNA sequence and we want to check whether a certain gene (a string) is present in it. Because we cannot hope that the gene will be a contiguous subsequence, we look for partial matches—fragments of the DNA that are also present in the gene (actually, even these partial matches will be approximate, not perfect). We then attempt to assemble these fragments.

Let x 1Kndenote the DNA sequence. Each partial match can be represented by a triple li,ri,wi, where xliKriis the fragment and is a weight

representing the strength of the match (it might be a local alignment score or some other statistical quantity). Many of these potential matches could be false, so the goal is to find a subset of the triples that are consistent (nonoverlapping) and have a maximum total weight.

Show how to do this efficiently.

Pebbling a checkerboard. We are given a checkerboard which has 4 rows and ncolumns, and has an integer written in each square. We are also given a set of 2n pebbles, and we want to place some or all of these on the checkerboard (each pebble can be placed on exactly one square) so as to maximize the sum of the integers in the squares that are covered by pebbles. There is one constraint: for a placement of pebbles to be legal, no two of them can be on horizontally or vertically adjacent squares (diagonal adjacency is fine).

  1. Determine the number of legal patterns that can occur in any column (in isolation, ignoring the pebbles in adjacent columns) and describe these patterns.

Call two patterns compatible if they can be placed on adjacent columns to form a legal placement. Let us consider subproblems consisting of the first columns 1kn. Each subproblem can be assigned a type, which is the pattern occurring in the last column.

  1. Using the notions of compatibility and type, give an O(n)-time dynamic programming algorithm for computing an optimal placement.

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.

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?

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