Algorithm Analysis (CSC 301 2015F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Outline] [EBoard] [Reading] [Lab] [Assignment]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Algorist]
Related Courses: [Walker (2014F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker]
Overview
Input: Set of integers (1, 2, 7, 15, 25) + another integer (t) Goal: Find the smallest set of integers whose sum = t. You may repeat any integer
Example: 14
Five primary techniques for doing this kind of problem
A greedy solution
set = { }
For v = largest to smallest
while (v <= t)
t -= v
set += { v }
If t == 0
return set
else
Report that no solution is available
Example
t = 23, values = 25, 15, 7, 2, 1
t = 23, set = { }, v = 25 NO
t = 23, set = { }, v = 15 YES
t = 8, set = { 15 }, v = 15 NO
t = 8, set = { 15 }, v = 7 YES
t = 1, set = { 15, 7 }, v = 7 NO
t = 1, set = { 15, 7 }, v = 2 NO
t = 1, set = { 15, 7 }, v = 1 YES
t = 0, set = { 15, 7, 1 }, v = 1 NO
But what about 21 and 37?
21: 15, 2, 2, 2 (but 7, 7, 7 is better) 37: 25, 7, 2, 2, 1 (but 15, 15, 7 is better)
Hack: If (v + next-smallest) <= t
Counter-Example: 1, 20, 25, 50. Make 80.
Hacking at the greedy algorithm is unlikely to succeed.
Divide-and-conquer is likely to be wrong.
Exhaustive search v1
For i = 1 to t
Make all sets of size i
Determine if any of those sets sums to t.
If so, return that set
Exhaustive search v2
minimize(values, t) => set
if t == 0
return { }
otherwise
tmp = universe; (a really big set)
for each v in values
tmp = minimize(values, t-v)
if (sizeof(tmp)) < sizeof(best)
best = tmp + { v }
Usually used for optimization problems, particularly optimization problems that have a simple but expensive recursive solution.
Two ideas:
minimize(values, t) => set
table = ....;
for (i = 0; i <= t; i++)
table[i] = universe; (a really big set)
for each v in values
if (table[t-v] < sizeof(best))
best = table[t-v] + { v }
Like the stamp problem:
Input is
Goal: Find a subset of the pairs whose total weight <= total weight whose worth is maximized
Harder: