Change Making Problem using Brute Force
The Change Making Problem is a classic optimization problem where the goal is to make change for a specific amount using the fewest number of coins from a given set of denominations. When approaching this problem with a brute force method, it involves examining all possible combinations of coins that add up to the given amount and selecting the combination with the minimum number of coins.
Brute Force Solution for Change Making Problem
Problem Setup:
Given a set of coin denominations (e.g., `[1, 5, 10]`) and a total amount (e.g., `12`), find the minimum number of coins that make up that amount.
Brute Force Approach:
1. Generate All Combinations:
Produce all combinations of coins that sum up to the target
amount. This usually requires recursion, where you explore each possible way to
reach the amount using the given denominations.
2. Select Minimum Coin Combination:
For each combination that sums to the target amount, count
the number of coins used. Keep track of the combination that uses the fewest
coins.
3. Return Result:
The combination with the minimum number of coins is the solution.
Pseudocode Example:
function
minCoins(coins, numberOfCoins, amount):
if
amount == 0:
return 0 // No coins needed for zero amount
minCoinsRequired = INFINITY
//
Try every coin that has a value smaller than amount
for
i from 0 to numberOfCoins:
if coins[i] <= amount:
subResult = minCoins(coins, numberOfCoins, amount coins[i])
// Check if the subResult needs fewer coins
if subResult != INFINITY and subResult + 1 <
minCoinsRequired:
minCoinsRequired = subResult + 1
return minCoinsRequired
// Example call
minCoins([1, 5,
10], 3, 12)
Time Complexity:
The brute force approach is highly inefficient for this problem. Its time complexity is approximately O(m^n), where m is the amount and n is the number of different denominations. The reason for this high complexity is the repetitive calculation of the same subproblems.
Important Considerations:
Inefficiency and Redundancy:
The brute force
solution involves a lot of redundant computations, as it doesn't store results
of subproblems for future reference.
Dynamic Programming as an Alternative:
A more efficient approach is to use dynamic programming, which solves each subproblem only once and stores its result to avoid redundant calculations.
In summary, while the brute
force method can theoretically solve the Change Making problem by exploring
every possible combination of coins, it is not practical for larger amounts or
a greater number of denominations due to its exponential time complexity. For
more efficient solutions, methods like dynamic programming are highly
recommended.
Detailed Time Complexity analysis
Recurrence Relation
Let ( C(i, a) ) represent the minimum number of coins needed to make change for amount ( a ) using the first ( i ) coin denominations.
Explanation
1. Base
Cases:
- If ( a = 0 ), no coins are needed, so (
C(i, 0) = 0 ) for any ( i ).
- If ( i = 0 ) and ( a > 0 ), it's impossible to make change (as no denominations are available), so ( C(0, a) = infinity ) for ( a > 0 ).
2. Recursive
Cases:
- If the value of the ( i )-th coin (denoted
as `coins[i]`) is greater than the amount ( a ), we cannot use this coin, so
the problem remains the same as ( C(i-1, a) ).
- Otherwise, we have two choices:
- Exclude the ( i )-th coin and solve
for the rest of the coins, which is ( C(i-1, a) ).
- Include the ( i )-th coin, reduce the
amount by the value of this coin, and solve the subproblem with the new amount (
a - text{coins}[i] ), which adds 1 coin to the solution of ( C(i, a - text{coins}[i])
).
- The minimum of these two choices gives us the minimum number of coins.
Time Complexity Implication
The
recurrence relation highlights the overlapping subproblems and optimal
substructure properties of the problem, making it a good candidate for
optimization through Dynamic Programming. However, in its basic recursive form
(as presented in the `minCoins` function), the algorithm has an exponential
time complexity due to the computation of the same subproblems multiple times.
Comments
Post a Comment