Dsap l05 PDF
Dsap l05 PDF
Heikki Peura
[email protected]
Homework 2
I Start early!
I Please do not import numpy or other libraries
I In the last question, test cases may include up to
twenty letters corresponding to any values from
1-9999
I Timeout 30 seconds
I But most test cases will be small
2 / 18
Previously
Graphs:
I Breadth-first search in unweighted graphs
I Extra material on weighted graphs and web scraping
on the Hub
3 / 18
Designing algorithms is tricky!
4 / 18
Designing algorithms is tricky!
4 / 18
Designing algorithms is tricky!
4 / 18
Greedy algorithms
5 / 18
Greedy algorithms
5 / 18
Knapsack problem
Input:
I Set of n items, with values vi and sizes wi (integer)
I Capacity W
6 / 18
Knapsack problem
Input:
I Set of n items, with values vi and sizes wi (integer)
I Capacity W
I subject to i∈S wi ≤ W
P
6 / 18
Knapsack problem applications
7 / 18
Knapsack problem applications
7 / 18
Knapsack problem applications
7 / 18
Two-item example
Items to pack
I Shirt: value 5, weight 5
I Bottle: value 10, weight 5
8 / 18
Two-item example
Items to pack
I Shirt: value 5, weight 5
I Bottle: value 10, weight 5
Subsets:
I {},{Shirt},{Bottle},{Shirt,Bottle}
8 / 18
Two-item example
Items to pack
I Shirt: value 5, weight 5
I Bottle: value 10, weight 5
Subsets:
I {},{Shirt},{Bottle},{Shirt,Bottle}
8 / 18
Go through all possibilities?
Input:
I Set of n items, with values vi and sizes wi (integer)
I Capacity W
I subject to i∈S wi ≤ W
P
9 / 18
Go through all possibilities?
Input:
I Set of n items, with values vi and sizes wi (integer)
I Capacity W
I subject to i∈S wi ≤ W
P
9 / 18
Greedy approaches for knapsack problem
Input:
I Set of n items, with values vi and sizes wi
I Capacity W
I subject to i∈S wi ≤ W
P
10 / 18
Greedy approaches for knapsack problem
Input:
I Set of n items, with values vi and sizes wi
I Capacity W
I subject to i∈S wi ≤ W
P
10 / 18
Greedy approaches for knapsack problem
Input:
I Set of n items, with values vi and sizes wi
I Capacity W
I subject to i∈S wi ≤ W
P
10 / 18
Greedy approaches for knapsack problem
Input:
I Set of n items, with values vi and sizes wi
I Capacity W
I subject to i∈S wi ≤ W
P
10 / 18
Greedy algorithms for knapsack
Greedy approaches?
I Highest-value item first?
11 / 18
Greedy algorithms for knapsack
Greedy approaches?
I Highest-value item first?
Example: capacity W = 20, three items with values v = [10, 10, 11], weights
w = [10, 10, 11].
I Picking highest value first is bad...
11 / 18
Greedy algorithms for knapsack
Greedy approaches?
I Highest-value item first?
I Lowest-size item first?
Example: capacity W = 20, three items with values v = [10, 10, 11], weights
w = [10, 10, 11].
I Picking highest value first is bad...
11 / 18
Greedy algorithms for knapsack
Greedy approaches?
I Highest-value item first?
I Lowest-size item first?
Example: capacity W = 20, three items with values v = [10, 10, 11], weights
w = [10, 10, 11].
I Picking highest value first is bad...
Example: capacity W = 20, three items with values v = [10, 10, 50], weights
w = [10, 10, 11].
I Picking lowest weight first is bad...
11 / 18
Greedy algorithms for knapsack
Greedy approaches?
I Highest-value item first?
I Lowest-size item first?
Example: capacity W = 20, three items with values v = [10, 10, 11], weights
w = [10, 10, 11].
I Picking highest value first is bad...
Example: capacity W = 20, three items with values v = [10, 10, 50], weights
w = [10, 10, 11].
I Picking lowest weight first is bad...
11 / 18
Greedy algorithm for knapsack
Greedy algorithm:
I Sort items in decreasing bang-for-buck vi /wi
I Pick items until capacity full
12 / 18
Greedy algorithm for knapsack
Greedy algorithm:
I Sort items in decreasing bang-for-buck vi /wi
I Pick items until capacity full
Running time?
I Sorting? — O(n log n)
I Picking items? — O(n)
12 / 18
Greedy algorithm for knapsack
Greedy algorithm:
I Sort items in decreasing bang-for-buck vi /wi
I Pick items until capacity full
Running time?
I Sorting? — O(n log n)
I Picking items? — O(n)
I Total O(n log n)
12 / 18
Is the algorithm correct?
13 / 18
Is the algorithm correct?
13 / 18
Is the algorithm correct?
13 / 18
Many important problems are intractable
Tractable problem = Solvable in polynomial time O(nk ) for some k
14 / 18
Many important problems are intractable
Tractable problem = Solvable in polynomial time O(nk ) for some k
14 / 18
Many important problems are intractable
Tractable problem = Solvable in polynomial time O(nk ) for some k
14 / 18
Many important problems are intractable
Tractable problem = Solvable in polynomial time O(nk ) for some k
14 / 18
Many important problems are intractable
Tractable problem = Solvable in polynomial time O(nk ) for some k
14 / 18
Many important problems are intractable
Tractable problem = Solvable in polynomial time O(nk ) for some k
14 / 18
Many important problems are intractable
Tractable problem = Solvable in polynomial time O(nk ) for some k
15 / 18
My problem is intractable!
15 / 18
My problem is intractable!
15 / 18
Greedy knapsack heuristic
Greedy algorithm:
I Sort items in decreasing bang-for-buck vi /wi
I For each item, pick the item until reach total W
16 / 18
Greedy knapsack heuristic
Greedy algorithm:
I Sort items in decreasing bang-for-buck vi /wi
I For each item, pick the item until reach total W
16 / 18
Greedy knapsack heuristic
Greedy algorithm:
I Sort items in decreasing bang-for-buck vi /wi
I For each item, pick the item until reach total W
16 / 18
Better greedy knapsack heuristic
Modified algorithm: pick either the greedy solution or the most
valuable item
17 / 18
Better greedy knapsack heuristic
Modified algorithm: pick either the greedy solution or the most
valuable item
I Sort items in decreasing vi /wi
I For each item, pick the item until reach total W
I Return either greedy solution or the most valuable item,
whichever is better
17 / 18
Better greedy knapsack heuristic
Modified algorithm: pick either the greedy solution or the most
valuable item
I Sort items in decreasing vi /wi
I For each item, pick the item until reach total W
I Return either greedy solution or the most valuable item,
whichever is better
17 / 18
Better greedy knapsack heuristic
Modified algorithm: pick either the greedy solution or the most
valuable item
I Sort items in decreasing vi /wi
I For each item, pick the item until reach total W
I Return either greedy solution or the most valuable item,
whichever is better
17 / 18
Review
18 / 18
Workshop