Greedy Algorithms
Greedy Algorithms
• GREEDY-ACTIVITY-SELECTOR(s, f)
• 1 n ← length[s]
• 2 A ← {1}
• 3j←1
• 4 for i ← 2 to n
• 5 do if si ≥ fj
• 6 then A ← A U{i}
• 7 j ←i
• 8 return A
0–1 knapsack problem
• The 0–1 knapsack problem is posed as
follows. A thief robbing a store finds n items; the
ith item is worth vi dollars and weighs wi pounds,
where vi and wi are integers.
• He wants to take as valuable a load as possible,
but he can carry at most W pounds in his
knapsack for some integer W.
• Which items should he take? (This is called the
0–1 knapsack problem because each item must
either be taken or left behind; the thief cannot
take a fractional amount of an item or take an
item more than once.)
fractional knapsack problem