Docsity

Algorithms Homework: Change Making, Price-Collecting Path, and Maximum Contiguous Sublist, Assignments of Computer Science

Toggle sidebar
bg1
CS1510 Design and Analysis of Algorithms, Fall 2006
Homework 4. Assigned on Wed Sep 27, 2006. Due in class on Wed Oct 4, 2006.
Note, I will discuss the LIS problem in class rather than reassigning it.
1. Consider the following change making with limited number of coins problem. The input
consists of a positive integer x and a multiset S = { d1, d2, …, dn } of n positive integers.
(Note, a multiset is a set except that it can have more than 1 copy of each member.
Example, { 1, 1, 1, 2, 2 } is a multiset with 5 elements. {1,2} is a multiset with 2 elements)
The goal of the problem is to find a multiset T, if it exists, such that (a) T is a subset of S, (b)
the elements of T sum to x, and (c) the cardinality of T is as small as possible. If there is no
such multiset, output “No”. Describe a dynamic programming algorithm that solves this
problem. Argue that your algorithm is correct.
2. Consider the following price-collecting path problem. The consists of a 2-dimensional grid
graph G=(V,E) with width x and length y, that is, the vertex set V and the edge set E are
defined by
V = { (i,j) | 0 < i < x and 0 < j < y } and
E = { {(i,j),(k,l)} | (i,j) and (k,l) are in V and either (i=k and j+1=l) or (i+1=k and j=l) }.
Each vertex (i,j) in V has a weight wi,j. The weight of a path is defined to be the sum of the
weights of the vertices on the path including the end vertices. The goal is to find a shortest
path from vertex (0,0) to vertex (x,y) with the largest weight. Note, any shortest path from
(0,0) to (x,y) in G has length x+y, that is, it contains x+y edges and x+y+1 vertices. Describe
a dynamic programming algorithm to find the largest weight of any shortest path from (0,0)
to (x,y). Argue that your algorithm is correct.
Hint: What are the possible nodes you need to go through to reach node (i,j)?
3. Consider the maximum contiguous sublist problem. The input to this problem consists of a
list S = s1, s2, …, sn of n numbers. Note that the numbers could be negative. A list X is a
(contiguous) sublist of list S if X = si, …, sj for some pair of indices i and j. Note that an
empty list is a sublist of S. For example, (8,6,1) is a sublist (3,8,6,1,5,7), but (8,6,5) is not.
The value of a list is the sum of its elements. If the list is empty, the value is 0. The goal of
the problem is to find a sublist of S with the maximum value. Describe an algorithm for this
problem. Argue that your algorithm is correct.
1
(0,0)
(4,3)
(3,3)
(4,2)
(1,0)
(0,1)
edge {(4,2),(4,3)}
(3,2)
edge {(3,2),(4,2)}
(0,0)
(4,3)
(3,3)
(4,2)
(1,0)
(0,1)
edge {(4,2),(4,3)}
(3,2)
edge {(3,2),(4,2)}

Partial preview of the text

Download Algorithms Homework: Change Making, Price-Collecting Path, and Maximum Contiguous Sublist and more Assignments Computer Science in PDF only on Docsity!

CS1510 Design and Analysis of Algorithms, Fall 2006 Homework 4. Assigned on Wed Sep 27, 2006. Due in class on Wed Oct 4, 2006. Note, I will discuss the LIS problem in class rather than reassigning it.

  1. Consider the following change making with limited number of coins problem. The input consists of a positive integer x and a multiset S = { d 1 , d 2 , …, dn } of n positive integers. (Note, a multiset is a set except that it can have more than 1 copy of each member. Example, { 1, 1, 1, 2, 2 } is a multiset with 5 elements. {1,2} is a multiset with 2 elements) The goal of the problem is to find a multiset T, if it exists, such that (a) T is a subset of S, (b) the elements of T sum to x , and (c) the cardinality of T is as small as possible. If there is no such multiset, output “No”. Describe a dynamic programming algorithm that solves this problem. Argue that your algorithm is correct.
  2. Consider the following price-collecting path problem. The consists of a 2-dimensional grid graph G=(V,E) with width x and length y , that is, the vertex set V and the edge set E are defined by V = { ( i,j ) | 0 < i < x and 0 < j < y } and E = { {( i , j ),( k , l )} | ( i , j ) and ( k , l ) are in V and either ( i = k and j +1= l ) or ( i +1= k and j = l ) }. Each vertex ( i , j ) in V has a weight w i , j. The weight of a path is defined to be the sum of the weights of the vertices on the path including the end vertices. The goal is to find a shortest path from vertex (0,0) to vertex (x,y) with the largest weight. Note, any shortest path from (0,0) to ( x , y ) in G has length x + y , that is, it contains x + y edges and x + y +1 vertices. Describe a dynamic programming algorithm to find the largest weight of any shortest path from (0,0) to ( x , y ). Argue that your algorithm is correct. Hint: What are the possible nodes you need to go through to reach node ( i , j )?
  3. Consider the maximum contiguous sublist problem. The input to this problem consists of a list S = s 1 , s 2 , …, sn of n numbers. Note that the numbers could be negative. A list X is a (contiguous) sublist of list S if X = s i, …, sj for some pair of indices i and j. Note that an empty list is a sublist of S. For example, (8,6,1) is a sublist (3,8,6,1,5,7), but (8,6,5) is not. The value of a list is the sum of its elements. If the list is empty, the value is 0. The goal of the problem is to find a sublist of S with the maximum value. Describe an algorithm for this problem. Argue that your algorithm is correct. 1 (0,0) (3,3) (4,3) (4,2) (1,0) (0,1) edge {(4,2),(4,3)} (3,2) edge {(3,2),(4,2)} (0,0) (3,3) (4,3) (4,2) (1,0) (0,1) edge {(4,2),(4,3)} (3,2) edge {(3,2),(4,2)}