Question

In Java, implement a dynamic programming solution to the Set Partition problem. Recall that the Set...

In Java, implement a dynamic programming solution to the Set Partition problem.

Recall that the Set Partition problem, given a set S = {a1, a2, …, an} of positive integers (representing assets) requires partitioning into two subsets S1 and S2 that minimizes the difference in the total values of S1 and S2.

For testing your code,

a) populate an initial set of 100 assets, with random values between 1 and 1000, identify the (minimum) difference between the two partitions, and time your set-partitioning algorithm.

b) populate again an initial set of 100 assets, but with random values between 1 and 100,000, identify the (minimum) difference between the two partitions, and time your set-partitioning algorithm.

Write and submit the code, along with a Report your findings with the two populations, minimum difference between the partitions, and times. In addition, provide in your report the dynamic programming formula and your reasoning for its correctness.

**Make sure to follow ALL instructions please**

Homework Answers

Answer #1
// A Recursive java program to solve 
// minimum set partition problem.
import java.io.*;
import java.util.Random;

class Main {
    // Returns the minimum value of
    // the difference of the two sets.
    static int findMin(int array[], int n) {
        // Calculate total of all elements
        int total = 0;
        for (int i = 0; i < n; i++)
            total += array[i];

        // Create an array to store
        // results of subproblems
        boolean res[][] = new boolean[n + 1][total + 1];

        // Initialize first column as true.
        // 0 total is possible with all elements.
        for (int i = 0; i <= n; i++)
            res[i][0] = true;

        // Initialize top row, except res[0][0],
        // as false. With 0 elements, no other
        // total except 0 is possible
        for (int i = 1; i <= total; i++)
            res[0][i] = false;

        // Fill the partition table
        // in bottom up manner
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= total; j++) {
                // If i'th element is excluded
                res[i][j] = res[i - 1][j];

                // If i'th element is included
                if (array[i - 1] <= j)
                    res[i][j] |= res[i - 1][j - array[i - 1]];
            }
        }

        // Initialize difference of two totals.
        int difference = Integer.MAX_VALUE;

        // Find the largest j such that res[n][j]
        // is true where j loops from total/2 t0 0
        for (int j = total / 2; j >= 0; j--) {
            // Find the
            if (res[n][j] == true) {
                difference = total - 2 * j;
                break;
            }
        }
        return difference;
    }

    // Driver program
    public static void main(String[] args) {
        // create instance of Random class
        Random rand = new Random();
        int ran = 1000; // change the value to change the range of random numbers
        int[] array = new int[100];
        for (int i = 0; i < 100; i++) {
            array[i] = rand.nextInt(ran) + 1;
        }
        int n = array.length;
        long startTime = System.nanoTime();
        System.out.println("The minimum difference between 2 sets of random values between 1 and " + ran + " is "
                + findMin(array, n));
        long endTime = System.nanoTime();
        long duration = (endTime - startTime) / 1000000;
        System.out.println("Time required: " + duration + " milliseconds.");

    }
}

(a) Output when random values between 1 and 1000

The minimum difference between 2 sets of random values between 1 and 1000 is 0
Time required: 58 milliseconds.

(a) Output when random values between 1 and 100000

The minimum difference between 2 sets of random values between 1 and 100000 is 0
Time required: 4217 milliseconds.

Reasoning:

Here we are creating a 2dimensional matrix res[n+1][total+1] where n is the number of elements in the provided set and total is the sum of all these elements. We are here solving in a bottom-up fashion.
We will divide the set into 2 partitions
Let res[n+1][total+1] = {1 if a subset from 1st to i'th has a sum equal to j, 0 otherwise} where i is from 1 to n and j is from 0 to total of all elements.
So, res[n+1][total+1] will be 1 if the sum j is achieved including i'th item or if the sum j is achieved excluding i'th item.

Let sum of all the elements be S.

To find the min difference in the total values of the two set, we will find j so that min{total - j*2 : res[n][j] == 1 } where j changes from 0 to total/2

The idea is, total of S1 is j and it should be closest to sum/2, i.e., 2*j should be closest to the total.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation....
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation. case:    W17400 APIGEE: PEOPLE MANAGEMENT PRACTICES AND THE CHALLENGE OF GROWTH Ranjeet Nambudiri, S. Ramnarayan, and Catherine Xavier wrote this case solely to provide material for class discussion. The authors do not intend to illustrate either effective or ineffective handling of a managerial situation. The authors may have disguised certain names and other identifying information to protect confidentiality. This publication may not be...
Mattel Responds to Ethical Challenges Business Ethics This case was written by Debbie Thorne, John Fraedrich,...
Mattel Responds to Ethical Challenges Business Ethics This case was written by Debbie Thorne, John Fraedrich, O. C. Ferrell, and Jennifer Jackson, with the editorial assistance of Jennifer Sawayda. This case was developed for classroom discussion rather than to illustrate either effective or ineffective handling of an administrative, ethical, or legal discussion by management. All sources used for this case were obtained through publicly available material. Mattel, Inc. is a world leader in the design, manufacture, and marketing of family...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT