/*! 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鈥檛 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鈥则辞鈥刵鈥勨赌勨赌Di,0=Truefori=1鈥则辞鈥刵鈥勨赌勨赌刦orj=1鈥则辞鈥刅

鈥勨赌勨赌勨勨赌勨赌刬fxi1j鈥勨赌勨赌勨勨赌勨赌勨勨赌勨赌Di,j=Di1,jxi1翱搁鈥Di1,j鈥勨赌勨赌勨勨赌勨赌別lse鈥勨赌勨赌勨勨赌勨赌勨勨赌勨赌Di,j=Di1,j谤别迟耻谤苍鈥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鈥则辞鈥刵鈥勨赌勨赌Di,0=Truefori=1鈥则辞鈥刵鈥勨赌勨赌刦orj=1鈥则辞鈥刅

鈥勨赌勨赌勨勨赌勨赌刬fxi1j鈥勨赌勨赌勨勨赌勨赌勨勨赌勨赌Di,j=Di1,jxi1翱搁鈥Di1,j鈥勨赌勨赌勨勨赌勨赌別lse鈥勨赌勨赌勨勨赌勨赌勨勨赌勨赌Di,j=Di1,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

You are going on a long trip. You start on the road at mile post 0. Along the way there aren hotels, at mile posts a1<a2<...<an , where eachai is measured from the starting point. The only places you are allowed to stop are at these hotels, but you can choose which of the hotels you stop at. You must stop at the final hotel (at distance an), which is your destination. You鈥檇 ideally like to travel miles a day, but this may not be possible (depending on the spacing of the hotels). If you travel x miles during a day, the penalty for that day is (200x)2. You want to plan your trip so as to minimize the total penalty- that is, the sum, over all travel days, of the daily penalties.Give an efficient algorithm that determines the optimal sequence of hotels at which to stop

You are given a string of n characters s[1...n], which you believe to be a corrupted text document in which all punctuation has vanished (so that it looks something like 鈥渋twasthebestoftimes...鈥). You wish to reconstruct the document using a dictionary, which is available in the form of a Boolean function dict(.): for any string w,

dict(w)={trueifwisavalidwordfalseotherwise

Give a dynamic programming algorithm that determines whether the string s[.]can be reconstituted as a sequence of valid words. The running time should be at mostO(n2) , assuming calls to dict take unit time.

In the event that the string is valid, make your algorithm output the corresponding sequence of words.

A certain string-processing language offers a primitive operation which splits a string into two pieces. Since this operation involves copying the original string, it takes n units of time for a string of length n, regardless of the location of the cut. Suppose, now, that you want to break a string into many pieces. The order in which the breaks are made can affect the total running time. For example, if you want to cut a 20-character string at positions 3 and 10, then making the first cut at position 3 incurs a total cost of 20+17=37, while doing position first has a better cost of 20+17=37.

Give a dynamic programming algorithm that, given the locations of m cuts in a string of length , finds the minimum cost of breaking the string into m +1 pieces.

A contiguous subsequence of a list Sis a subsequence made up of consecutive elements of S. For instance, if Sis 5,15,30,10,5,40,10

then15,30,10 is a contiguous subsequence but5,15,40 is not. Give a linear-time algorithm for the following task:Input: A list of numbers a1,a2,...,an.

Output: The contiguous subsequence of maximum sum (a subsequence of length zero has sum zero).For the preceding example, the answer would be 10,5,40,10, with a sum of 55. (Hint: For each j{1,2,...,n}, consider contiguous subsequences ending exactly at position j.)

Given two strings x=x1x2xnand y=y1y2ym, we wish to find the length of their longest common subsequence, that is, the largest k for which there are indices i1<i2<<ikand j1<j2<<jkwith xi1xi2xik=yj1yj2yjk. Show how to do this in time 0(mn).

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.