class: center, title-slide
## CSCI-UA 480: APS ## Algorithmic Problem Solving
## Dynamic Programming .author[ Instructor: Joanna Klukowska
] .license[ Copyright 2020 Joanna Klukowska. Unless noted otherwise all content is released under a
[Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/).
Background image by Stewart Weiss
] --- layout:true template: default name: section class: inverse, middle, center --- layout:true template: default name: challenge class: challenge --- layout:true template: default name: poll class: inverse, full-height, center, middle --- layout:true template: default name: breakout class: breakout --- layout:true template:default name:slide class: slide .bottom-left[© Joanna Klukowska. CC-BY-SA.] --- ## Knapsack __Task__: Given $n$ items, each with its own value $V_i$ and its own weight/size $W_i$, and a maximum knapsack size $S$, find the maximum value of the items that can be placed into the knapsack. (Each item can be either taken or left - we cannot take part of an item.) -- __Example__ n = 4 V = {100, 70, 50, 10} W = {10, 4, 6, 12} S = 12 Possible options: - item 0, total weight 10, total value 100 - items 1 and 2, total weight 10, total value 120 - item 3, total weight 12, total value 10 --- ## Knapsack - Algorithm `max_val ( item_number, remaining_weight)` - function that returns the maximum value of items given that we have the `remaining_weight` available and the current item we are considering is `item_number` __simple cases__ / __base_cases__ `max_val ( id, 0 ) = 0 // we cannot take any more items` `max_val ( n, rem_W ) = 0 // went past the item with the highest id` __reucrrence ralation__ ``` if W[id] < remW max_val(id, remW) = max_val (id+1, remW ) //don't take the item since it is too heavy else max_val(id, remW) = max ( max_val (id+1, remW - W[id]), //take the item max_val (id+1, remW ) //do NOT take the item ) ``` --- ## Cutting Sticks __Task:__ Given a stick of length 1 <= l <= 1000, and 1<=n <= 50 cuts to be made to the stick (coordinates of the cuts are values in the range [0 .. l]) figure out the most _affordable_ way of cutting the stick. The cost of a cut is determined by the length of the stick to be cut. -- __Example __ l = 100 n = 3 The cut coordinates are A = {25, 50, 75} Possible sequences of cuts: - left to right at positions 25, 50, 75, cost = 100 + 75 + 50 = 225 - right to left at positions 75, 50, 25, cost = 100 + 75 + 50 = 225 - first cut in the middle, then each half-stick in the middle again at positions 50, 25, 75, cost = 100 + 50 + 50 = 200 --- ## Cutting Sticks __Task:__ Given a stick of length 1 <= l <= 1000, and 1<=n <= 50 cuts to be made to the stick (coordinates of the cuts are values in the range [0 .. l]) figure out the most _affordable_ way of cutting the stick. The cost of a cut is determined by the length of the stick to be cut. -- - `A = {0, a_1, a_2, ..., a_n, l}` - an array of cuts that we need - `cut( a_i, a_j )` - the smallest cost of cutting the segment from coordinate `a_i` to `a_j` along __all__ the points `c` in between `a_i` and `a_j`, `a_i < c < a_j` - the cost of making a single cut between coordinates `a_i` and `a_j` is `(a_j - a_i)` recurrence relation: $cut(a_i, a_j)=$ $ \quad \quad = min_{k = i+1}^{j-1} ( cut(a_i, a_k ) + cut (a_k, a_j) + a_j - a_i )$ $cut(a_i, a_j)=0$, when $a_i$ and $a_j$ are adjacent in `A` (i.e., $j=i+1$), since there is no cut needed --- ## Ryan And Larry __Task__ Given a number N, how many ways can K non-negative numbers less than N add up to N? -- .left-column2[ Example 1 : N = 20, K = 2 Answer: 21 0 + 20
1 + 19
2 + 18
3 + 17
...
17 + 3
18 + 2
19 + 1
20 + 0
] -- .right-column2[ Example 2: N = 5, K = 3 Answer: 21 0 + 0 + 5
0 + 1 + 4
0 + 2 + 3
0 + 3 + 2
0 + 4 + 1
0 + 5 + 0
1 + 0 + 4
2 + 0 + 3
3 + 0 + 2
4 + 0 + 1
5 + 0 + 0
... ] --- ## Ryan And Larry __Task__ Given a number N, how many ways can K non-negative numbers less than N add up to N? -- - `ways(N, K)` - number of ways of adding K values that add up to N - when `K=1`, the answer is always 1 regardless of N - assume that the first number we pick for the sum is X, then there are `ways(N-X, K-1)` ways to pick the remaining numbers - in the above, X can be any number between 0 and N recurrence: $ ways(n,K) = \sum_{X=0}^{N} ways(N-X, K-1 ) $ $ ways(N,1) = 1 $ ---