From 688a9ab3bcc8312d88014ed93177c0b9d75f88cc Mon Sep 17 00:00:00 2001 From: dhruvsaini Date: Tue, 3 Jan 2017 16:42:31 +0530 Subject: [PATCH] Create minimum_partition.py Partition a set into two subsets such that the difference of subset sums is minimum --- dynamic_programming/minimum_partition.py | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 dynamic_programming/minimum_partition.py diff --git a/dynamic_programming/minimum_partition.py b/dynamic_programming/minimum_partition.py new file mode 100644 index 000000000..25b7621e1 --- /dev/null +++ b/dynamic_programming/minimum_partition.py @@ -0,0 +1,28 @@ +""" +Partition a set into two subsets such that the difference of subset sums is minimum +""" +def findMin(arr): + n = len(arr) + s = sum(arr) + + dp = [[False for x in range(s+1)]for y in range(n+1)] + + for i in range(1, n+1): + dp[i][0] = True + + for i in range(1, s+1): + dp[0][i] = False + + for i in range(1, n+1): + for j in range(1, s+1): + dp[i][j]= dp[i][j-1] + + if (arr[i-1] <= j): + dp[i][j] = dp[i][j] or dp[i-1][j-arr[i-1]] + + for j in range(s/2, -1, -1): + if dp[n][j] == True: + diff = s-2*j + break; + + return diff