Sol3b - CS 577: Introduction to Algorithms 11/02/06...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 577: Introduction to Algorithms 11/02/06 Homework Solution: 3b Instructor: Shuchi Chawla TA: Siddharth Barman Question 1 1a Consider a boolean array A indexed from 0 to x . A ( v ) = 1 indicates that we can make change for value v using the given denominations a 1 to a n , A ( v ) = 0 otherwise. The recurrence relation for a general v follows from the following observation: We can make change for v if there exists a coin a i such that we can make change for v- a i . Hence A ( v ) = 1 if for any i ∈ { 1 ..n } , A ( v- a i ) = 1. We initialize A (0) = 1 and for each v from a 1 to x check whether there exists i ∈ { 1 ..n } such that A ( v- a i ) (obviously v ≥ a i ). Finally change for x exists iff A ( x ) = 1. The array A is of size x and it takes O ( n ) time to fill in each entry hence the running time for this sieve like algorithm is O ( nx ). 1b The problem is essentially subset sum with the universal set S = { a 1 ,a 2 ,...,a n } . For completeness we sketch the solution below. Consider a two dimensional n × x boolean array A . Here A ( i,v ) = 1 indicates that we can obtain change for v by selecting some coins from the set { a 1 ,..,a i } . The recurrence relation is A ( i,v ) = 1 if A ( i- 1 ,v ) = 1 or A ( i- 1 ,v- a i ) = 1. The initialization is to set A ( l, 0) = 1 for l = 0 to n . Finally A ( n,x ) = 1 implies that we can obtain change for x using at most one coin of each given denomination. We fill up an n × x boolean matrix, each entry of which takes constant time for updating hence the time complexity of the given algorithm is O ( nx ). 1c We extend the solution of 1 a by keeping track of the number of coins required to generate a value v . Formally we have an array A indexed from 0 to x . The entry A ( v ) indicates the minimum number of coins required to generate change of v using a 1 to a n . A ( v ) = ∞ indicates that change for v is not possible. Also at each step we must insure that at most k coins are used. Hence the recurrence relation is to first select L as L = min i ∈ [ n ] { 1 + A ( v- a i ) } if L > k then A...
View Full Document

This note was uploaded on 02/17/2011 for the course CS 577 taught by Professor Joseph during the Spring '08 term at University of Wisconsin Colleges Online.

Page1 / 4

Sol3b - CS 577: Introduction to Algorithms 11/02/06...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online