knapsack-problem

Knapsack - brute force algorithm

拥有回忆 提交于 2019-12-04 16:07:44
问题 I have found this code to solve Knapsack Problem using brute force mechanism (this is mostly for learning, so no need to point out dynamic is more efficient). I got the code to work, and understand most of it. MOST. Here's the question: I've noticed those two conditionals, that I have no idea how they work and why they are in the code - I know they are vital, since any changes I've made caused the algorithm to produce wrong results: // if bit not included then skip if (((i >> j) & 1) != 1)

Subset-sum problem in PHP with MySQL

血红的双手。 提交于 2019-12-04 16:03:56
following Problem: I have a MySQL database with songs in it. The database has the following structure: id INT(11)(PRIMARY) title VARCHAR(255) album VARCHAR(255) track INT(11) duration INT(11) The user should be able to enter a specific time into a php form and the php function should give him a list of all possible combinations of songs which add up to the given time ±X min. So if the user wants to listen to 1 hour of music ±5 minutes he would enter 60 minutes and 5 minutes of threshold into the form and would recieve all possible song sets which add up to a total of 55 to 65 minutes. It

How to Determine Cheapest Commute Ticket Combination

孤者浪人 提交于 2019-12-04 13:50:18
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 during 30 consecutive calendar days. A 365 day ticket at cost z for unlimited rides during 365

0-1 Knapsack algorithm

百般思念 提交于 2019-12-04 11:51:06
问题 Is the following 0-1 Knapsack problem solvable: 'float' positive values and 'float' weights (can be positive or negative) 'float' capacity of the knapsack > 0 I have on average < 10 items, so I'm thinking of using a brute force implementation. However, I was wondering if there is a better way of doing it. 回答1: This is a relatively simple binary program. I'd suggest brute force with pruning. If at any time you exceed the allowable weight, you don't need to try combinations of additional items,

Algorithm for solving this distributing beads puzzle?

落花浮王杯 提交于 2019-12-04 11:16:53
问题 Lets say you have a circle (like below) with N spots, and you have N beads distributed in the slots. Here's an example: Each bead can be moved clockwise for X slots, which costs X^2 dollars. Your goal is to end up with one bead in each slot. What is the minimum amount of money you have to spend to achieve this task? More interesting variation of this problem: Algorithm for distributing beads puzzle (2)? 回答1: In this answer I assume beads can only be moved once. Otherwise it would be evident

Knapsack solution with Backtraking in c++

笑着哭i 提交于 2019-12-04 06:17:58
问题 Im having troubles trying to resolve the Knapsack problem using backtraking. For example, for the following values, the Knapsack function will return 14 as the solution, but the correct result should be 7. int n = 3, weights[] = {2, 3, 1}, values[] = {6, 15, 7}; int solution = 0, max = 2; void Knapsack (int i, int max, int value, int *solution) { int k; for (k = i; k < n; k++) { if (weights[k] <= max) { Knapsack (k, max - weights[k], value + values[k], solution); if (value+ values[k] >

Knapsack with minimum cost

跟風遠走 提交于 2019-12-03 21:59:21
I've got several types of coins, each have weight (wi) and cost (ci). So I've got to make a knapsack with weight==W and (!) minimum cost of coins in it. I can`t make recurrence relation to use DP. Just formulate the usual recurrence relation... Designate the minimum cost achievable with total weight k as Min_cost(k). Bootstrap the memoization with: Min_cost(0) = cost of empty set = 0 Then solve for increasing values of k using: Min_cost(i+1) = min [Min_cost(i) + min [ci, for all items with wi = 1], Min_cost(i-1) + min [ci, for all items with wi = 2], Min_cost(i-2) + min [ci, for all items with

Optimisation/knapsack algorithm with multiple contraints in JavaScript

拟墨画扇 提交于 2019-12-03 21:53:45
I'm looking for a solution to the knapsack problem where multiple constraints are in place. Say our knapsack has a maximum weight of 30kg and we have a set of 100 objects, each with a weight and each with a benefit. These objects could look like this: { name: 'water bottle', weight: 2, benefit: 5 }, { name: 'potatoes', weight: 10, benefit: 6 } Finding the combination of objects with the highest benefit within the maximum weight is simple enough. Here is a nodeJS plugin showing how it can be done... https://gist.github.com/danwoods/7496329 Where I'm struggling is when the objects have more

Which is the best method between Genetic Algorithm and Dynamic Programming to solve classic 0-1 knapsack? [closed]

▼魔方 西西 提交于 2019-12-03 17:04:06
Let's say I have problem like this: Knapsack capacity = 20 million Number of item = 500 Weight of each item is random number between 100 to 20 million Profit of each item is random number between 1 to 10 So which is the best method for my problem? GA or Dynamic Programming? Please give me a simple explanation, as I'm newbie in this.. Dynamic Programming (DP): Exact algorithm - Finds global optimal solution Long running time Uses a lot of memory Very simple to implement Genetic Algorithm (GA): Estimation - Doesn't necessarily find the global optimal solution Short running time Memory usage

Why solving Knapsack problem is not considered as linear programming?

纵饮孤独 提交于 2019-12-03 11:35:24
Why isn't the knapsack problem included under the category of linear programming algorithms in spite of the fact that the Knapsack problem statement seems similar to the problems in linear programming ? Knapsack can be written as an integer linear programming program. Unlike normal linear programming, this problem requires that variables in the solution are integers. Linear programming is known to be solvable in polynomial time, while integer linear programming is NP-complete. Exercise for the reader: show that 3SAT can be reduced to integer linear programming. Trivia: there are approximation