From c41d10abeb4906b2fe4d03dffd926d1413b91cd0 Mon Sep 17 00:00:00 2001 From: Jesse Adams Date: Sun, 20 Sep 2020 16:23:40 -0500 Subject: [PATCH 1/2] Created SubsetSum.java --- DynamicProgramming/SubsetSum.java | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 DynamicProgramming/SubsetSum.java diff --git a/DynamicProgramming/SubsetSum.java b/DynamicProgramming/SubsetSum.java new file mode 100644 index 00000000..80596346 --- /dev/null +++ b/DynamicProgramming/SubsetSum.java @@ -0,0 +1,57 @@ +package DynamicProgramming; + +public class SubsetSum { + + /* + This algorithm will determine if a set of integers contains + a subset that sum to a given integer. The algorithm will + return true if such a subset exists and false otherwise. + This is a dynamic programming implementation. + */ + + private static boolean subsetSum(int arr[], int n, int sum){ + boolean isSum[][] = new boolean[n + 2][sum + 1]; + + isSum[n + 1][0] = true; + for (int i = 1; i <= sum; i++) { + isSum[n + 1][i] = false; + } + + for (int i = n; i > 0; i--) { + isSum[i][0] = true; + for (int j = 1; j <= arr[i - 1] - 1; j++) { + if (j <= sum) { + isSum[i][j] = isSum[i + 1][j]; + } + } + for (int j = arr[i - 1]; j <= sum; j++) { + isSum[i][j] = (isSum[i + 1][j] || isSum[i + 1][j - arr[i - 1]]); + } + } + + return isSum[1][sum]; + } + + /* + This is a driver method to run the algorithm with four + test values: the first two should evaluate true, and the + last two should evaluate false. + */ + public static void main(String args[]) { + int arr[] = new int[]{50, 4, 10, 15, 34}; + int n = arr.length; + // 4 + 10 + 15 + 34 = 64 + int sum1 = 64; + // 50 + 15 + 34 = 99 + int sum2 = 99; + // No subset of the given array will give a sum + // of 5 or 66 + int sum3 = 5; + int sum4 = 66; + + System.out.println(subsetSum(arr, n, sum1)); + System.out.println(subsetSum(arr, n, sum2)); + System.out.println(subsetSum(arr, n, sum3)); + System.out.println(subsetSum(arr, n, sum4)); + } +} \ No newline at end of file From e5a3e4b479134d9e33ec3a3cdcee9d0def7e8ada Mon Sep 17 00:00:00 2001 From: shellhub Date: Tue, 22 Sep 2020 10:09:04 +0800 Subject: [PATCH 2/2] reformat code --- DynamicProgramming/SubsetSum.java | 49 ++++++++++++------------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/DynamicProgramming/SubsetSum.java b/DynamicProgramming/SubsetSum.java index 80596346..18a35724 100644 --- a/DynamicProgramming/SubsetSum.java +++ b/DynamicProgramming/SubsetSum.java @@ -2,15 +2,27 @@ package DynamicProgramming; public class SubsetSum { - /* - This algorithm will determine if a set of integers contains - a subset that sum to a given integer. The algorithm will - return true if such a subset exists and false otherwise. - This is a dynamic programming implementation. + /** + * Driver Code */ + public static void main(String[] args) { + int[] arr = new int[]{50, 4, 10, 15, 34}; + assert subsetSum(arr, 64); /* 4 + 10 + 15 + 34 = 64 */ + assert subsetSum(arr, 99); /* 50 + 15 + 34 = 99 */ + assert !subsetSum(arr, 5); + assert !subsetSum(arr, 66); + } - private static boolean subsetSum(int arr[], int n, int sum){ - boolean isSum[][] = new boolean[n + 2][sum + 1]; + /** + * Test if a set of integers contains a subset that sum to a given integer. + * + * @param arr the array contains integers. + * @param sum target sum of subset. + * @return {@code true} if subset exists, otherwise {@code false}. + */ + private static boolean subsetSum(int[] arr, int sum) { + int n = arr.length; + boolean[][] isSum = new boolean[n + 2][sum + 1]; isSum[n + 1][0] = true; for (int i = 1; i <= sum; i++) { @@ -31,27 +43,4 @@ public class SubsetSum { return isSum[1][sum]; } - - /* - This is a driver method to run the algorithm with four - test values: the first two should evaluate true, and the - last two should evaluate false. - */ - public static void main(String args[]) { - int arr[] = new int[]{50, 4, 10, 15, 34}; - int n = arr.length; - // 4 + 10 + 15 + 34 = 64 - int sum1 = 64; - // 50 + 15 + 34 = 99 - int sum2 = 99; - // No subset of the given array will give a sum - // of 5 or 66 - int sum3 = 5; - int sum4 = 66; - - System.out.println(subsetSum(arr, n, sum1)); - System.out.println(subsetSum(arr, n, sum2)); - System.out.println(subsetSum(arr, n, sum3)); - System.out.println(subsetSum(arr, n, sum4)); - } } \ No newline at end of file