Question

In: Computer Science

1. Purpose: Apply various algorithm design strategies to solve a problem, practice formulating and analyzing algorithms,...

1. Purpose: Apply various algorithm design strategies to solve a problem, practice formulating and analyzing algorithms, implement an algorithm. In the US, coins are minted with denominations of 50, 25, 10, 5, and 1 cent. An algorithm for making change using the smallest possible number of coins repeatedly returns the biggest coin smaller than the amount to be changed until it is zero. For example, 17 cents will result in the series 10 cents, 5 cents, 1 cent, and 1 cent.

a) (4 points) Give a recursive algorithm that generates a similar series of coins for changing n cents. Don’t use dynamic programming for this problem.

b) (4 points) Write an O(1) (non-recursive!) algorithm to compute the number of returned coins.

c) (1 point) Show that the above greedy algorithm does not always give the minimum number of coins in a country whose denominations are 1, 6, and 10 cents.

d) (6 points) Given a set of arbitrary denominations C =(c1,...,cd), describe an algorithm that uses dynamic programming to compute the minimum number of coins required for making change. You may assume that C contains 1 cent, that all denominations are different, and that the denominations occur in in increasing order.

Solutions

Expert Solution

1.

// Returns true if there is a subset of set[] with sun equal to given sum
int isSubsetSum(int set[], int n, int sum)
{
// Base Cases
if (sum == 0)
return 1;
if (n == 0 && sum != 0)
return 0;

if (set[n-1] > sum) // If last element is greater than sum, then ignore it
return isSubsetSum(set, n-1, sum);


//excluding //including
return 1+ min(isSubsetSum(set, n-1, sum),isSubsetSum(set, n-1, sum-set[n-1]));
}

Time Complexity = O(sum^n), where n is the number of coins given

Space Complexity: O(sum) for the recursion call stack.

2. Using greedy

#include<bits/stdc++.h>

using namespace std;
int main()
{   
int d;
cin>>d;
vector<int>deno;
for(int i=0;i<d;i++){
int e;
cin>>e;

deno.push_back(e);

//NOTE HERE I INSER DATA IN SORTING ORDER. EX.{1,5,10,20,50....}.{1,5,6,9} //if input data is not in sorting order then we have to sort data

}
  
int V;
vector<int> res;
cin >> V;
  
for (int i = d-1 ; V != 0; i--)
{
while (V < deno[i]) i--;
res.push_back(deno[i]);
V -= deno[i];
//cout << "V : " << V << endl;
i++;
}

  for (int i = 0; i < res.size(); i++)
cout << res[i] << " ";
    cout << endl;
  return 0;
}

  • Time Complexity: O(V).
  • Auxiliary Space: O(1) as no additional space is used.

3. in will fail in if i isert input {1,5,6,9}.then output will be {9,1,1} but output should be {5,6}.so greedy does not work here.

Note: you can try with insert input in above programm.

4.

Dynamic programming solve the greedy problem.

#include<bits/stdc++.h>

using namespace std;

int F(int *coins, int n, int sum)
{
int dp[n+1][sum+1];
  

for(int i = 0; i <= n; i++) {
for(int j = 0; j <=sum; j++) {
dp[i][j] = 1000000;
}
}
for(int i=0;i<=n;i++){
dp[i][0]=0;
}
  

for(int i = 1; i <= n; i++) {
for(int j = 1; j <= sum; j++)
{
if(j < coins[i-1]) dp[i][j] = dp[i-1][j];
else
{
  dp[i][j] = min(dp[i-1][j], 1 + dp[i][j-coins[i]]);
  }
}
}

if(dp[n][sum] == 1000000) dp[n][sum] = -1; //Not possible to make sum from these coins
return dp[n][sum];
}

int main() {
   int t;
   cin>>t;
   while(t--)
   {
   int n,sum;
   cin>>sum>>n;
   int *arr = new int[n+1];
   for(int i = 1; i <= n; i++){
   cin>>arr[i]; //insert coins
   }
   sort(arr+1,arr+n+1); //If coins are not in sorted order then we have to sort them.
     cout<<F(arr, n, sum)<<endl;
   }
   return 0;
}

Time complexity of the above solution is O(sum*n). // sum:is make a required sum and n is number of coin


Related Solutions

Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is...
Problem 2. Purpose: practice algorithm design using dynamic programming. 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,A and A,A,A,A (on the other hand, the subsequence A,C,T is not palindromic). Assume you are given a sequence x[1...n] of characters. Denote L(i,j) the length of the longest palindrome in the substring x[i,...,j]. The goal of the Maximum Palindromic Subsequence Problem (MPSP)...
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
Problem 1. The purpose of this problem is to practice the use of logic operations and...
Problem 1. The purpose of this problem is to practice the use of logic operations and quantifiers. For each Statement X below determine if each of the three statementsX1, X2, X3 that follow it satisfy the following: a) Xi implies X; b) X implies Xi; c) if Xi is true then X must be false; d) if X is true then Xi must be false. Statement A. In every house there is a mouse. A1. There is no house without...
The purpose of this assignment is to create a design challenge that highlights various problem-solving techniques....
The purpose of this assignment is to create a design challenge that highlights various problem-solving techniques. Write a paper of approximately 250-500 words utilizing a business writing format to present and frame the design challenge. Reference the example provided in The Field Guide to Human-Centered Design for additional information. Address the following: What problem are you trying to solve? Who will be affected by the problem? To what degree will the group identified be affected? Consider that new technologies are...
Part1: 1.An algorithm is . a) a series of actions that solve a particular problem. b)...
Part1: 1.An algorithm is . a) a series of actions that solve a particular problem. b) an english description of a problem to be solved. c) the process of converting between data types. d) None of the above. 2. Program control is best defined as . a) the degree of control a program has over the computer on which it is executed. b) the line of code that is executing at a given time. c) the order in which a...
What are the different algorithms we may use to solve the 0/1 Knapsack problem? Compare and...
What are the different algorithms we may use to solve the 0/1 Knapsack problem? Compare and contrast the different methods.
1.What is a code of practice and what is its purpose in structural design? 2.List the...
1.What is a code of practice and what is its purpose in structural design? 2.List the principal sources of uncertainty in structural design and discuss how these uncertainties are rationally allowed for in design. 3.The characteristic strengths and design strengths are related via the partial safety factor for materials. The partial safety factor for concrete is higher than for steel reinforcement. Discuss why this should be so 4. Describe in general terms the ways in which a beam and column...
Design and analysis of Algorithms: How do I write a k way merge sort algorithm from...
Design and analysis of Algorithms: How do I write a k way merge sort algorithm from a merge sort algorithm that splits 2 arrays while sorting?
Purpose of the Assignment Identify strategies to reduce the risk for injury to clients in various...
Purpose of the Assignment Identify strategies to reduce the risk for injury to clients in various environments. Course Competencies Select appropriate nursing interventions when providing multidimensional care to clients experiencing alterations in mobility. Strategies for safe effective multidimensional nursing practice when providing care for clients experiencing sensory and perception disorders. Instructions In a one page Word Document, plan interventions with rationale which will promote an environment of safety for the described client below. Consider the client’s medical history and medications....
The purpose of this assignment is to identify and apply OSCM concepts/tools to solve problems in...
The purpose of this assignment is to identify and apply OSCM concepts/tools to solve problems in managing operations and supply chains. Students are expected to find an interesting OSCM problem from the real business world, think about how you can apply the OSCM concepts/tools that you learn in this course to solve the problem, and write up a report based on your analysis. More specifically, each student should: • Find a problem in managing operations and supply chains from the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT