backpack dynamic programming java

copied from stack overflowI found this really interesting and easy to understandAs rrenaud (and Wikipedia) say, top-down is memoization, and bottom-up is dynamic programming.Memoization and dynamic programming are all about ordering your computations in a way that you avoid recalculating duplicate work. The only difference between the two is that memoization is a lazy, let-it-be approach: whenever you calculate something, cache it. Dynamic programming is the same as memoization, but has one additional feature: it is more active because it involves picking, ahead of time, exactly in which order you will do your computations (technically, having an algorithm that allows you to determine what to calculate based on what you've already calculated, since what you calculate might depend on the values you calculate).The reason it's called top-down and bottom-up is, you can think of the entire computation tree.Memoization often starts at the top, defines itself recursively (assuming the work is already done), and caches results so duplicate sub-trees are not recomputed.example: If you are calculating the Fibonacci sequence fib(100), you would just call this, and it would call fib(100)=fib(99)+fib(98), which would call fib(99)=fib(98)+fib(97), ...etc..., which would call fib(2)=fib(1)+fib(0)=1+0=1.

Then it would finally resolve fib(3)=fib(2)+fib(1), but it doesn't need to recalculate fib(2), because we cached it.With dynamic programming, you work from the leaves back to the root at the top, picking an order.example: If doing fibonacci, you choose to calculate the numbers in this order: fib(2),fib(3),fib(4)... you then cache the values. You can also think of it as filling up a table (another form of caching).This "top-down or bottom-up" view is most obvious if your computation is recursive, but recursion is not necessary for either. In particular: 1) a function can be memoized (cached) even though it is part of an iterative algorithm, and 2) though DP is very iterative, it can be defined recursively in terms of its history. The only thing that matters is you have a computational goal, and you can make computational choices which have a cost defined in terms of other computational choices (or are fixed), and where computing something can unlock new computational choices. In general one ignores side-effects, but you could probably fit them in the memoization/DP model.

With both techniques, you would typically start by writing a recurrence.
backpack yang dipakai artis koreaLet's say we have the problem where given an MxN matrix V of integers, we want to find the largest sum path from the upper left cell to the lower right cell when we are allowed to only move down and right in the matrix.
backpack gestolenDefine F (x,y) as the best path from (x,y) to the destination, and define the coordinate system as having x increase from left to right and having y increase from top to bottom (these coordinate system choices are arbitrary).
backpack l'inouiWith those definitions in place, we write F (x,y) = V[x][y] + max (F (x+1,y), F (x,y+1)).
backpack dubizzle

That is, the best path from (x,y) is the value at (x,y) plus the better (the max) of the two choices we have for the next move.
incase city compact backpack saleThen we'd write some base cases, bounds checks, etc., but the above is the core recurrence.
litesphere laptop backpackFirst we ask if we should use dynamic programming at all here.
swiss army ibex laptop backpackWe draw a little bit of the recursion tree (tree of calls that will be made) and see that starting from F (0,0), cases like F (1,1) will get evaluated multiple times. Therefore, there would be a benefit to caching previously-evaluated cases, which is what dynamic programming is all about.One way of caching is that you do the recursion directly, but before beginning to evaluate any recursive case, check the cache (a hash table or array) to see if the answer for the current combination of arguments to the recursive function is already there, and if so, just use it.

If the answer is not there, evaluate the function, but before returning the answer, store it in the cache for later use. This method is called top-down dynamic programming, or recursion with memoization.Bottom-up dynamic programming is the same core idea of caching previously-evaluated cases, but you optimize the recursion out completely by arranging the evaluation of the cases in such an order that any time you would have needed to call the function recursively, the answer you need is already in the cache, so you just get it from the cache. In our example, let's say we evaluate F for all in-bounds values of x, y starting from the maximum x and maximum y and then going from right to left, bottom to top. Then, whenever we evaluate F(x, y), F(x+1, y) and F(x, y+1) will both already be in the cache (assuming they're in bounds, if they're not, that's handled as a special case).A note on terminology: some people insist that only bottom-up dynamic programming is "dynamic programming" and what I call "top-down dynamic programming" should properly be referred to as only "recursion with memoization".

My usage is consistent with common usage. I personally think calling both of these "dynamic programming" is more enlightening since it highlights that the ideas are related.1.) Top-down: 2.) Bottom-up: you start building the big solutionyou start with the small solutions and build up.F(n,k) = max(F(n-1,k), F(n-1, k-1))You are a renowned thief who has recently switched from stealing precious metals to stealing cakes because of the insane profit margins. You end up hitting the jackpot, breaking into the world's largest privately owned stock of cakes—the vault of the Queen of England. While Queen Elizabeth has a limited number of types of cake, she has an unlimited supply of each type. Each type of cake has a weight and a value, stored in objects of a CakeType class: You brought a duffel bag that can hold limited weight, and you want to make off with the most valuable haul possible. Write a function maxDuffelBagValue that takes of cake type s and a weight capacity, and returns the maximum monetary value the duffel bag can hold.

Weights and values may be any non-negative integer. Yes, it's weird to think about cakes that weigh nothing or duffel bags that can't hold anything. But we're not just super mastermind criminals—we're also meticulous about keeping our algorithms flexible and comprehensive. Does your function work if the duffel bag's weight capacity is 0 kg? Does your function work if any of the cakes weigh 0 kg? Think about a cake whose weight and value are both 0! We can do this in time and space, where n is the number of types of cakes and k is the duffel bag's capacity! You must log in with one click to view the rest.Log in with Github | Log in with Google | Log in with Facebook We'll never post on your wall or message your friends. Once you're logged in, you'll get free full access to this and 4 other questions. time, and space, where n is number of types of cake and k is the capacity of the duffel bag. We loop through each cake (n cakes) for every capacity (k capacities), so our runtime is , and maintaining the array of k+1 capacities gives us the space.

Because of dynamic programming, you have successfully stolen the Queen's cakes and made it big. Keep in mind: in some cases, it might not be worth using our optimal dynamic programming solution. It's a pretty slow algorithm—without any context (not knowing how many cake types we have, what our weight capacity is, or just how they compare) it's easy to see potentially being as bad as if n is close to k. If we cared about time, like if there was an alarm in the vault and we had to move quickly, it might be worth using a faster algorithm that gives us a good answer, even if it's not always the optimal answer. Some of our first ideas in the breakdown were to look at cake values or value/weight ratios. Those algorithms would probably be faster, taking time (we'd have to start by sorting the input). Sometimes an efficient, good answer might be more practical than an inefficient, optimal answer. We know the max value we can carry, but which cakes should we take, and how many?