Knapsack when weight and values are positive? - algorithm

What is the most accurated algorithm to Knapsack when weight and values are positive?

not sure what language your using but Wikipedia has a great page on information and algorithms to solve it. If you want more sample code to understand how to do it, check this site out: http://rosettacode.org/wiki/Knapsack_problem/Unbounded/Python_dynamic_programming (this is all in python but there's more languages).
Basically it depends on what your doing but the most common way to solve this is dynamic programming.

Related

Genetic programming for symbolic equation solving

I'm trying to perform a genetic programming system which solves an equation (basically first and second degree polynom equations) symboliclly.
It means that for a*x+b=0 it must give me a tree representation of -b/a. What i mean by symbolically is that i'm not giving numbers for 'a' and 'b' ... basically what Generic Programming are made for.
I'm actually stuck in finding the fitness function. For a given gene (potential solution), how can i predict how far is this one from the right solution.
My doubt is that there's no way to do such a thing unless i convert the problem into a numerical one.
I've been searching in the net, but all i found was related to genetic algorithms and so numerical resolution for such problem.
a link exposing such topic is found here: Generic Programming but it's not explaining a detailed approach about the problem.
Any ideas ?
Thanks in advance.
I've finally found a solution.
I've found a powerfull library which is designed for Genetic Algorithms for java developpers, and have a module specialized in Genetic Programming (GP).
Library's link: Jenetics
My implementation of the solution is available on my github at this link: Symbolic Equation Solver
It's basically a Maven based project, which makes use of Java 11 and the above library to solve Linear and Quadratic equations and gives symbolic (and not numeric) result.

Precalculate Result of A*

Currently learning about the A* search algorithm and using it to find the quickest solution to the N-Puzzle. For some random seed of the initial starting state, the puzzle may be unsolvable which would result in extremely long wait times until the algorithm has search the entire search-space and determined there is not solution to the give start state.
I was wondering if there is a method of precalculating whether the A* algorithm will fail to avoid such a scenario. I've read a bit about how it is possible but can't find a direct answer as to a method in which to do it.
Any guidance or options are appreciated.
I think A* does not offer you a mechanism to know whether or not a problem is solvable. Specifically for N-Puzzle, I think this could help you to check if it can be solved or not:
http://www.geeksforgeeks.org/check-instance-8-puzzle-solvable/
It seems that if you are in a state where you have an odd amount inversion, you know for sure the problem for that permutation is infeasible.
For the N-puzzle specifically, there are only two possible parities, so you just need to check which parity the current puzzle is.
There is an in-depth explanation on how to do this on the math stackexchange
For general A* problems, no, there is no way to pre-compute if the graph is solvable.

How to parallel Knapsack problems?

Knapsack problems is a very famous problem. Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.
This problem can be solved with dynamic programming and can be found on every tutorial book of algorithm. But how can I write a parallel version?
That is a very interesting question, the best way to obtain a (good) answer is to use google scholar on such a question. The following link is probably the most recent paper on the subject.

Solving puzzle with tree data structure

I am currently following an algorithms class and we have to solve a sudoku.
I have already a working solution with naive backtracking but I'm much more interest in solving this puzzle problem with a tree data structure.
My problem is that I don't quite understand how it works. Is anyone can explain to me the basic of puzzle solving with tree?
I don't seek optimization. I looking for explanation on algorithms like the Genetic algorithm or something similar. My purpose only to learn at this point. I have hard time to take what I read in scientific articles and translate it on real implementation.
I Hope, I've made my question more clear.
Thank you very much!
EDIT: I edit the post to be more precise.
I don't know how to solve Sudoku "with a tree", but you're trying to mark as many cells as possible before trying to guess and using backtrace. Therefore check out this question.
Like in most search problems, also in Sudoku you may associate a tree with the problem. In this case nodes are partial assignments to the variables and branches correspond to extensions of the assignment in the parent node. However it is not practical (or even feasible) to store these trees in memory, you can think of them as a conceptual tool. Backtracking actually navigates such a tree using variable ordering to avoid exploring hopeless branches. A better (faster) solution can be obtained by constraint propagation techniques since you know that all rows, columns, and 3x3 cells must contain different values. A simple algorithm for that is AC3 and it is covered in most introductory AI books including Russell & Norvig 2010.

Create a sum of 1000, 2000, etc. from set of numers

Ok, so here's the problem:
I need to find any number of intem groups from 50-100 item set that add up to 1000, 2000, ..., 10000.
Input: list of integers
Integer can be on one list only.
Any ideas on algorithm?
Googling for "Knapsack problem" should get you quite a few hits (though they're not likely to be very encouraging -- this is quite a well known NP-complete problem).
Edit: if you want to get technical, what you're describing seems to really be the subset sum problem -- which is a special case of the knapsack problem. Of course, that's assuming I'm understanding your description correctly, which I'll admit may be open to some question.
You might find Algorithm 3.94 in The Handbook of Applied Cryptography helpful.
I'm not 100% on what you are asking, but I've used backtracking searches for something like this before. This is a brute force algorithm that is the slowest possible solution, but it will work. The wiki article on Backtracking Search may help you. Basically, you can use a recursive algorithm to examine every possible combination.
This is the knapsack problem. Are there any constraints on the integers you can choose from? Are they divisible? Are they all less than some given value? There may be ways to solve the problem in polynomial time given such constraints - Google will provide you with answers.

Resources