partition-problem

Divide list into two equal parts algorithm

≯℡__Kan透↙ 提交于 2020-06-27 04:07:10
问题 Related questions: Algorithm to Divide a list of numbers into 2 equal sum lists divide list in two parts that their sum closest to each other Let's assume I have a list, which contains exactly 2k elements. Now, I'm willing to split it into two parts, where each part has a length of k while trying to make the sum of the parts as equal as possible. Quick example: [3, 4, 4, 1, 2, 1] might be splitted to [1, 4, 3] and [1, 2, 4] and the sum difference will be 1 Now - if the parts can have

divide list in two parts that their sum closest to each other

孤人 提交于 2019-12-17 18:41:56
问题 This is a hard algorithms problem that : Divide the list in 2 parts (sum) that their sum closest to (most) each other list length is 1 <= n <= 100 and their(numbers) weights 1<=w<=250 given in the question. For example : 23 65 134 32 95 123 34 1.sum = 256 2.sum = 250 1.list = 1 2 3 7 2.list = 4 5 6 I have an algorithm but it didn't work for all inputs. init. lists list1 = [], list2 = [] Sort elements (given list) [23 32 34 65 95 123 134] pop last one (max one) insert to the list which differs

Variant of Knapsack

随声附和 提交于 2019-12-14 02:53:22
问题 I'm working on a program to solve a variant of the 0/1 Knapsack problem. The original problem is described here: https://en.wikipedia.org/wiki/Knapsack_problem. In case the link goes missing in the future, I will give you a summary of the 0/1 Knapsack problem (if you are familiar with it, jump this paragraph): Let's say we have n items, each with weight wi and value vi . We want to put items in a bag, that supports a maximum weight W , so that the total value inside the bag is the maximum

Need idea for solving this algorithm puzzle

青春壹個敷衍的年華 提交于 2019-12-07 09:53:42
问题 I've came across some similar problems to this one in the past, and I still haven't got good idea how to solve this problem. Problem goes like this: You are given an positive integer array with size n <= 1000 and k <= n which is the number of contiguous subarrays that you will have to split your array into. You have to output minimum m, where m = max{s[1],..., s[k]}, and s[i] is the sum of the i-th subarray. All integers in the array are between 1 and 100. Example : Input: Output: 5 3 >> n =

Subset sum problem where each number can be added or subtracted

跟風遠走 提交于 2019-12-06 15:52:03
问题 Given a set A containing n positive integers, how can I find the smallest integer >= 0 that can be obtained using all the elements in the set. Each element can be can be either added or subtracted to the total. Few examples to make this clear. A = [ 2, 1, 3] Result = 0 (2 + 1 - 3) A = [1, 2, 0] Result = 1 (-1 + 2 + 0) A = [1, 2, 1, 7, 6] Result = 1 (1 + 2 - 1 - 7 + 6) 回答1: You can solve it by using Boolean Integer Programming. There are several algorithms (e.g. Gomory or branch and bound) and

Need idea for solving this algorithm puzzle

风格不统一 提交于 2019-12-05 17:51:28
I've came across some similar problems to this one in the past, and I still haven't got good idea how to solve this problem. Problem goes like this: You are given an positive integer array with size n <= 1000 and k <= n which is the number of contiguous subarrays that you will have to split your array into. You have to output minimum m, where m = max{s[1],..., s[k]}, and s[i] is the sum of the i-th subarray. All integers in the array are between 1 and 100. Example : Input: Output: 5 3 >> n = 5 k = 3 3 2 1 1 2 3 Splitting array into 2+1 | 1+2 | 3 will minimize the m. My brute force idea was to

Subset sum problem where each number can be added or subtracted

不问归期 提交于 2019-12-04 20:42:51
Given a set A containing n positive integers, how can I find the smallest integer >= 0 that can be obtained using all the elements in the set. Each element can be can be either added or subtracted to the total. Few examples to make this clear. A = [ 2, 1, 3] Result = 0 (2 + 1 - 3) A = [1, 2, 0] Result = 1 (-1 + 2 + 0) A = [1, 2, 1, 7, 6] Result = 1 (1 + 2 - 1 - 7 + 6) You can solve it by using Boolean Integer Programming. There are several algorithms (e.g. Gomory or branch and bound) and free libraries (e.g. LP-Solve) available. Calculate the sum of the list and call it s. Double the numbers

Better results in set partition than by differencing

霸气de小男生 提交于 2019-12-03 06:38:18
问题 Partition problem is known to be NP-hard. Depending on the particular instance of the problem we can try dynamic programming or some heuristics like differencing (also known as Karmarkar-Karp algorithm). The latter seems to be very useful for the instances with big numbers (what makes dynamic programming intractable), however not always perfect. What is an efficient way to find a better solution (random, tabu search, other approximations)? PS: The question has some story behind it. There is a

Better results in set partition than by differencing

心已入冬 提交于 2019-12-02 21:10:17
Partition problem is known to be NP-hard. Depending on the particular instance of the problem we can try dynamic programming or some heuristics like differencing (also known as Karmarkar-Karp algorithm). The latter seems to be very useful for the instances with big numbers (what makes dynamic programming intractable), however not always perfect. What is an efficient way to find a better solution (random, tabu search, other approximations)? PS: The question has some story behind it. There is a challenge Johnny Goes Shopping available at SPOJ since July 2004. Till now, the challenge has been

Creating a partition of a set in Scheme

会有一股神秘感。 提交于 2019-12-02 19:33:07
问题 I'm pretty new to scheme overall and I'm having some issues with figuring out an assignment for school. So please no full answers, just looking for a little insight or a nudge in the right direction so I can figure this out on my own. The problem is as follows: Given a list of numbers, determine whether two subsets can be made from those numbers of equivalent sum and # of items. So for example, if the given set is (1 1) then my program should return #t, otherwise #f. Here is what I have