knapsack-problem

Dynamic Programming and the 0/1 knapsack

风格不统一 提交于 2019-12-06 10:51:47
问题 I'm having some trouble understanding dynamic programming, even though I've read through so many resources trying to understand. I understand an example given of dynamic programming using the fibonacci algorithm. I understand how if you use the divide and conquer approach to it, you'll end up solving some sub-problems multiple times, and dynamic programming takes care of that by solving those overlapping subproblems but only once (and storing them for future reference). However, I have been

How to Determine Cheapest Commute Ticket Combination

自闭症网瘾萝莉.ら 提交于 2019-12-06 07:01:32
问题 My local train service recently added an option for dialy commute. I am trying to determine the algorithm for finding the cheapest combination of tickets for a given set of round trips on given days. Here is the problem in english. Given a set of days and and rides per day what combination of the following is the cheapest. A round trip ticket at cost w per round trip. A 7 day ticket at cost x for unlimited rides during 7 consecutive calendar days. A 30 day ticket at cost y for unlimited rides

0-1 Knapsack with additional restriction (colored items)?

两盒软妹~` 提交于 2019-12-06 04:53:58
I'm working on this problem mostly out of curiosity in my downtime at work. Imagine the normal 0-1 Knapsack problem, except all the items are either yellow, red, blue, or green, and due to your OCD you must have exactly 2 items of each color in your knapsack. So instead of the normal items each item has 3 properties: Weight, Value, Color. Is this even still a knapsack problem, or is it better define in some other way? I'll use nCk to represent "n choose k" for ease of typing. Since you must have exactly 2 items of each color, the number of feasible solutions is O( nC2 ), which is O( n^2 ).

Detecting gaps in grid of divs

早过忘川 提交于 2019-12-05 14:30:33
EDIT The solution has been found! Here's a blog post about it, and here is the Github repo ! I am working on creating a grid of divs that is composed of multiple sized boxes, these sizes are set height's and widths - but are generated dynamically so each time the page is loaded there is a different grid. My problem - I tried using Masonry, but it winds up leaving gaps, also tried isotope. I am currently floating the elements left which is causing for the breaks in the layout. How it's built - I calculate the screen dimensions and determine the best column count for the page ranging from 1 to 6

Are these 2 knapsack algorithms the same? (Do they always output the same thing)

久未见 提交于 2019-12-05 04:50:01
In my code, assuming C is the capacity, N is the amount of items, w[j] is the weight of item j, and v[j] is the value of item j, does it do the same thing as the 0-1 knapsack algorithm? I've been trying my code on some data sets, and it seems to be the case. The reason I'm wondering this is because the 0-1 knapsack algorithm we've been taught is 2-dimensional, whereas this is 1-dimensional: for (int j = 0; j < N; j++) { if (C-w[j] < 0) continue; for (int i = C-w[j]; i >= 0; --i) { //loop backwards to prevent double counting dp[i + w[j]] = max(dp[i + w[j]], dp[i] + v[j]); //looping fwd is for

Algorithm for distributing beads puzzle (2)?

只谈情不闲聊 提交于 2019-12-05 01:43:27
问题 Let's say you have a circle (shown below) with N slots. Your goal is to end up with a specified number of beads in each slot, and you have an array of size N containing the amount of beads you need in each slot. For example, if the array was {1, 5, 3}, then you would need to end up with 1 bead in slot 1, 5 beads in slot 2, and 3 beads in slot 3. You have an infinite amount of beads. You can "unlock" X slots. Once you unlock a slot, you can start putting beads in that slot. You can move beads

How to calculate the best price [duplicate]

左心房为你撑大大i 提交于 2019-12-04 18:32:01
This question already has answers here : Selecting A combination of minimum cost (5 answers) Closed 4 years ago . I have a interesting problem. I would like to know some good approaches to solve this. I have a small store in which I have 'n' number of products Each product as a non zero price associated with it A product looks like this { productId:productA; productName:ABC; price:20$ } To enhance the customer retention, I would like to bring in combo model to sell. That is, I define m number of combos Each combo looks like this { comboId:1; comboName:2; constituentProducts: {Product1,

Trying to figure out the classic Knapsack recurrence

纵饮孤独 提交于 2019-12-04 17:34:58
I am reading about the Knapsack Problem (unbounded) which is, as I understand a classic in DP. Although I think I understand the solution as I read it, I am not clear how I can translate it to actual code. For example in the following recurrence "formula": M(j) = MAX {M(j-1), MAX i = 1 to n (M(j - Si) + Vi) } for j >=1 I am not sure how I can translate this to code, since it is not clear to me if the inner MAX should be there or it should just be instead: M(j) = MAX {M(j-1), M(j - Si) + Vi } for j >=1 Any help to figure out the formula and to code it? you can code it like this: for w = 0 to W

Knapsack with limit on total items used

不打扰是莪最后的温柔 提交于 2019-12-04 17:12:19
So, I have a knapsack where a number of items that can be placed into the knapsack has a limit, while the amount of weight of the items also has a limit. So given item limit 5, and weight 100: We would find the 5 items (can repeat 5x same item) that best fit weight 100. I have solved both unbounded and bounded(each item has a limit, but the total amount of items used has no limit) in dynamic programming. But I'm a bit confused with how to do this new approach. Would this be a multidimensional knapsack problem, like volume and weight? But instead, we want item's used and weight? Or is it a 0-1

Possible Combination of Knapsack problem and?

瘦欲@ 提交于 2019-12-04 16:19:44
Alright quick overview I have looked into the knapsack problem http://en.wikipedia.org/wiki/Knapsack_problem and i know it is what i need for my project, but the complicated part of my project would be that i need multiple sacks inside a main sack. The large knapsack that holds all the "bags" can only carry x amount of "bags" (lets say 9 for sake of example). Each bag has different values; Weight Cost Size Capacity and so on, all of those values are integer numbers. Lets assume from 0-100. The inner bag will also be assigned a type, and there can only be one of that type within the outer bag,