Skip to content

Create minimum_partition.py #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions dynamic_programming/minimum_partition.py
Original file line number Diff line number Diff line change
@@ -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