(a)
To discuss the greedy
(a)
Explanation of Solution
To prove that the changing problem of coin has an optimal solution, take ' n ' cent and suppose, for these ' n ' cent changes, there is an optimal solution. The solution takes a coin with value ' c ' and uses ' k ' coins. If the solution of ' n-c ' cent changes problem lies within the solution of ' n ' cent problem solution, that means ' k-1 ' coins are available for ' n-c ' cents. So, take the solution for the ' n-c ' cent changing problem and make sure this should be lesser then ' k- 1 ' coins then figure out the solution for ' n ' cent problem.
While dealing with ' n ' cent problem and try to get an optimal solution by taking minimum no. of coins. Always provide the biggest denomination coin until the rest amount became zero. So, if there is the biggest denomination present at first then by applying greedy methodology changes in quarter, dime, nickel and pennies are:
Q = ⌊ n /25 ⌋ quarter that gives nQ = n mod 25 cent for change.
D = ⌊ nQ /10 ⌋ dimes that gives nD = nQ mod 10 cent for change.
K = ⌊ nD /10 ⌋ dimes that gives nK = nD mod 5 cent for change.
P = nK pennies
By seeing the greedy solution, there are few possibilities like:
- If n = 0; then zero coins in optimal solution.
- If n > 0; then get the biggest denomination with value = ' n ' , suppose it’s ' c ' .
- Now, take a coin and apply process in recursive manner for ' n-c ' cent.
To get the optimal solution through greedy algorithm, one must hold the coin ' c ' with value greater than ' n ' .
There are four cases needs to be considered here:
- If (1= n < 5) then c = 1, this must hold the greedy choice and contains pennies.
- If (5 = n = 10) then c = 5, by supposition, in this it must hold pennies and change 5 pennies to one nickel because this solution is not holding any nickel.
- If (10 = n = 25) then c = 10, by supposition, it must hold a nickel and pennies but not a dime. So, add a dime in the solution.
- If (25 = n ) then c = 25, by supposition, it holds a dime, pennies, and nickel but not quarter. If it has three dimes change it with a quarter and nickel and if has at least two dime then few subset of nickel, dime and pennies add nearly 25 cents and change these coins with a quarter to get a solution.
Here, for showing that it must contains an optimal solution with greedy choices and these choices will further helpful to get the solution of sub problem. Therefore, this greedy approach provides an optimal outcome for the given problem.
For an algorithm that selected a coin and recurses sub problem one by one, the running complexity will be θ ( k ), where ' k ' is the coins used for optimal solution. Now ( k = n), so, running time will be O ( n ) and there is only four type of coins. So, it requires O (1) constant time.
(b)
To show that for given denomination ( c0, c1, c2, ........., ck ), greedy algorithm always gives an optimal solution where ( c > 1) and (1 = k ).
(b)
Explanation of Solution
Take denomination c0, c1, c2, ........., ck and changes amount ' n ' . By taking this, it is clear that
Only few coins are required in changing coin to give an optimal solution. Apply greedy algorithm, take biggest denomination coin first means take a denomination ci (not ck ), where ( k>i ).
- To make sure that solution should be optimal take any denomination ( ci ) no. of coin that must be lesser then ' c ' .
- Take coins of ' ci ' denomination having ( x0, x1, ...., xk ) optimal solution and prove that for every ( k >i ), denomination ( c >xi ).
- And for few j , ( c = xi ) and change denomination coins cj with cj+1coin .
- Therefore, xj reduced by c, xj +1 increased by 1 and total no. of coins reduced by c -1.
This is contradicting about ( x0, x1,....., xk ) being a solution. So, that optimal solution should have denomination ' c ' greater than xi for any ' ci ' denomination.
Thus the only solution of the given problem is xk = n/ck ; for ( k>i ), xi = n mod ci +1/ ci . Therefore this is proved that greedy algorithm with ( c0, c1, c2, ........., ck ) denomination always gives an optimal solution.
(c)
To prove that greedy algorithm does not provides the optimal solution all the time.
(c)
Explanation of Solution
Take the denomination set as {1, 3, 4} and the value of ' n =6’.
Then greedy solution will give two one cent coin {1, 1} and a four-cent coin {4} like {1, 1, 4} but two coins as {3, 3} is optimal solution.
So, for the set {1, 3, 4}, optimal solution will not present by using greedy algorithm.
In the other example where denomination set contains value {1, 10, 25}, for ' n = 30’; greedy algorithm provide a quarter and five pennies {25, 1, 1, 1, 1, 1} but the optimal solution should be three dime like {10, 10, 10}.
Thus, greedy algorithm will not provide an optimal solution always.
(d)
To show that the time complexity of the algorithm is O ( nk ), where ' k ' is the total no. of coins.
(d)
Explanation of Solution
While applying the dynamic programming, take the demonstration ' d ' define as d1 , d2 , d3 ,...., dk and to make changes for ' i ' cent, no. of coins required min( c [ i ]). So, the minimum coins required can be calculated as:
c [ i ] = 1 + min{ c [ i-dj ]}, if ( i > 0) and (1 = j = k ),
c [ i ] = 0, if ( i = 0)
This formula should be implemented as,
MakeChange ( d,n )
- For ( i ← 1) to n
- Do c [ i ] ← 8
- For ( j ← 1) to k
- Do if ( dj = i )
- Then if c [ i ] > c [ i-dj ] + 1
- Then c [ i ] ← c [ i - dj ] + 1
- denom[ i ] ← dj
- Return demon and ' c '
By seeing the algorithmic procedure of MakeChange( d,n ), it is showing that it requires two loops up to ' n ' times as outer loop and ' k ' times as inner loop.
Thus, the complexity of the implementation will be O ( nk ).
Want to see more full solutions like this?
Chapter 16 Solutions
Introduction to Algorithms
- Question: Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsinx Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.arrow_forward23:12 Chegg content://org.teleg + 5G 5G 80% New question A feed of 60 mol% methanol in water at 1 atm is to be separated by dislation into a liquid distilate containing 98 mol% methanol and a bottom containing 96 mol% water. Enthalpy and equilibrium data for the mixture at 1 atm are given in Table Q2 below. Ask an expert (a) Devise a procedure, using the enthalpy-concentration diagram, to determine the minimum number of equilibrium trays for the condition of total reflux and the required separation. Show individual equilibrium trays using the the lines. Comment on why the value is Independent of the food condition. Recent My stuff Mol% MeOH, Saturated vapour Table Q2 Methanol-water vapour liquid equilibrium and enthalpy data for 1 atm Enthalpy above C˚C Equilibrium dala Mol% MeOH in Saturated liquid TC kJ mol T. "Chk kot) Liquid T, "C 0.0 100.0 48.195 100.0 7.536 0.0 0.0 100.0 5.0 90.9 47,730 928 7,141 2.0 13.4 96.4 Perks 10.0 97.7 47,311 87.7 8,862 4.0 23.0 93.5 16.0 96.2 46,892 84.4…arrow_forwardYou are working with a database table that contains customer data. The table includes columns about customer location such as city, state, and country. You want to retrieve the first 3 letters of each country name. You decide to use the SUBSTR function to retrieve the first 3 letters of each country name, and use the AS command to store the result in a new column called new_country. You write the SQL query below. Add a statement to your SQL query that will retrieve the first 3 letters of each country name and store the result in a new column as new_country.arrow_forward
- We are considering the RSA encryption scheme. The involved numbers are small, so the communication is insecure. Alice's public key (n,public_key) is (247,7). A code breaker manages to factories 247 = 13 x 19 Determine Alice's secret key. To solve the problem, you need not use the extended Euclid algorithm, but you may assume that her private key is one of the following numbers 31,35,55,59,77,89.arrow_forwardConsider the following Turing Machine (TM). Does the TM halt if it begins on the empty tape? If it halts, after how many steps? Does the TM halt if it begins on a tape that contains a single letter A followed by blanks? Justify your answer.arrow_forwardPllleasassseee ssiiirrrr soolveee thissssss questionnnnnnnarrow_forward
- 4. def modify_data(x, my_list): X = X + 1 my_list.append(x) print(f"Inside the function: x = {x}, my_list = {my_list}") num = 5 numbers = [1, 2, 3] modify_data(num, numbers) print(f"Outside the function: num = {num}, my_list = {numbers}") Classe Classe that lin Thus, A pro is ref inter Ever dict The The output: Inside the function:? Outside the function:?arrow_forwardpython Tasks 5 • Task 1: Building a Library Management system. Write a Book class and a function to filter books by publication year. • Task 2: Create a Person class with name and age attributes, and calculate the average age of a list of people Task 3: Building a Movie Collection system. Each movie has a title, a genre, and a rating. Write a function to filter movies based on a minimum rating. ⚫ Task 4: Find Young Animals. Create an Animal class with name, species, and age attributes, and track the animals' ages to know which ones are still young. • Task 5(homework): In a store's inventory system, you want to apply discounts to products and filter those with prices above a specified amount. 27/04/1446arrow_forwardOf the five primary components of an information system (hardware, software, data, people, process), which do you think is the most important to the success of a business organization? Part A - Define each primary component of the information system. Part B - Include your perspective on why your selection is most important. Part C - Provide an example from your personal experience to support your answer.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningOperations Research : Applications and AlgorithmsComputer ScienceISBN:9780534380588Author:Wayne L. WinstonPublisher:Brooks Cole