Sub topic 10
Coin Change and Graph Representations
Assume you know Big-O notation, arrays, loops, and basic graph terms such as vertex and edge. This chapter uses dynamic programming (DP), which stores answers to smaller subproblems so they can be reused.
Coin change: counting ways and minimum coins
Suppose the coin denominations are [1, 2, 5] and the target amount is 5. Coin change has two common versions:
- Counting ways: How many combinations produce the amount? The order of coins does not matter, so
1 + 2 + 2and2 + 1 + 2count as one way. - Minimum coins: What is the smallest number of coins needed?
Counting combinations
Let ways[a] be the number of combinations that make amount a. The base case is:
ways[0] = 1There is one way to make zero: choose no coins.
Process each coin before increasing the amount:
ways[a] += ways[a - coin]This order prevents counting different arrangements as different combinations.
Minimum number of coins
Let best[a] be the fewest coins needed to make amount a. The recurrence is:
best[a] = min(best[a], best[a - coin] + 1)Set unreachable amounts to infinity and use best[0] = 0. The amount loop may run upward because a coin can be used repeatedly.
def coin_change(coins, amount):
ways = [0] * (amount + 1)
ways[0] = 1
best = [float("inf")] * (amount + 1)
best[0] = 0
for coin in coins:
for current in range(coin, amount + 1):
ways[current] += ways[current - coin]
best[current] = min(best[current],
best[current - coin] + 1)
minimum = best[amount]
if minimum == float("inf"):
minimum = None
return ways[amount], minimum
print(coin_change([1, 2, 5], 5)) # (4, 1)The four combinations are 5, 2 + 2 + 1, 2 + 1 + 1 + 1, and five 1 coins. The minimum is one coin.
The time complexity is O(nA), where n is the number of denominations and A is the target amount. The space complexity is O(A). A greedy choice, such as always taking the largest coin, does not work for every denomination set. For example, with coins [1, 3, 4] and amount 6, greedy uses three coins (4 + 1 + 1), while the optimum uses two (3 + 3).
Representing graphs: adjacency list versus matrix
An adjacency list stores, for each vertex, the vertices directly connected to it. An adjacency matrix stores a two-dimensional table where entry M[u][v] records whether an edge exists, or stores its weight.
| Operation | Adjacency list | Adjacency matrix |
|---|---|---|
| Space | O(V + E) | O(V²) |
| Test whether edge (u, v) exists | O(degree(u)) | O(1) |
| Iterate over neighbors of u | O(degree(u)) | O(V) |
| Insert an edge | O(1) | O(1) |
Lists are preferable for sparse graphs, where E is much smaller than V². Matrices are useful for dense graphs or algorithms such as Floyd-Warshall that inspect every vertex pair. For an undirected graph, store each edge in both endpoint lists or both symmetric matrix entries.
Exercise
With coins [1, 3, 4] and amount 6, what are the minimum number of coins and the number of combinations?
Show answer
The minimum is 2, using 3 + 3. The combinations are 1+1+1+1+1+1, 1+1+1+3, 1+1+4, and 3+3, so there are 4.