Open In App

Java Program to Implement Heuristic Approach to Solve Set Packing

Last Updated : 05 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The set packing problem is a type of combinatorial optimization problem that involves selecting a subset of sets from a given collection of sets such that no two selected sets share any common elements. The goal is to maximize the number of sets selected.

A heuristic approach is a method for solving a problem that uses a practical, rather than an optimal, solution. Heuristic approaches are often used for problems that have no known optimal solution, or for problems that are too large or complex to be solved optimally in a reasonable amount of time. The heuristic approach in the provided Java program uses a simple algorithm that iterates through all the sets and selects a set if it contains at least one unique element that has not been included in any of the previously chosen sets. This approach is not guaranteed to find the optimal solution, but it will find a solution that is a good approximation.

Algorithm:

The algorithm used in the provided Java program is a simple heuristic approach that iterates through all the sets and selects a set if it contains at least one unique element that has not been included in any of the previously chosen sets. The algorithm can be described as follows:

  • Initialize an ArrayList to store the chosen sets, and a boolean array to keep track of chosen elements.
  • Iterate through all sets using a nested loop.
  • For each set, iterate through all its elements using another nested loop.
  • If an element is 1 and has not been chosen before, mark it as chosen, and add the set to the chosen sets list.
  • After the iterations, print out the chosen sets.

Example:

Java




import java.util.ArrayList;
import java.util.Random;
  
public class SetPacking {
    public static void main(String[] args) {
        int n = 100; // number of sets
        int m = 50; // number of elements
        int[][] sets = new int[n][m]; // sets represented as a 2D array
        Random rand = new Random();
  
        // Initialize sets with random elements
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                sets[i][j] = rand.nextInt(2);
            }
        }
  
        ArrayList<Integer> solution = new ArrayList<>(); // list to store the chosen sets
        boolean[] elements = new boolean[m]; // array to keep track of chosen elements
  
        // Iterate through all sets
        for (int i = 0; i < n; i++) {
            boolean added = false;
            for (int j = 0; j < m; j++) {
                if (sets[i][j] == 1 && !elements[j]) {
                    added = true;
                    elements[j] = true;
                }
            }
            if (added) {
                solution.add(i);
            }
        }
  
        // Print the chosen sets
        for (int i : solution) {
            System.out.print(i + " ");
        }
    }
}


Output

0 1 2 3 4 6 

Explanation:

In the above code a 2D array of size of n x m is created where n is the number of sets and m is the number of elements. Then the 2D array is initialized with random elements. An ArrayList is created to store the chosen sets. After this the boolean array is created to keep track of the chosen element. It then iterates through all the sets and checks if the set contains any element that has not been chosen yet. If the set contains an element that has not been chosen yet, it adds the set to the solution and marks the elements as chosen. It prints the chosen sets.

Note: The output will be different every time the program is run, because of the random initialization of the sets.

Complexity analysis:

The time complexity of the above algorithm is O(n*m), where n is the number of sets and m is the number of elements. The above algorithm is a greedy algorithm. It chooses the sets greedily, i.e., it chooses the sets that contain the maximum number of elements that have not been chosen yet. The space complexity is O(n + m), where n is the number of sets and m is the number of elements. The reason is that the algorithm uses an ArrayList to store the chosen sets and a boolean array to keep track of chosen elements. The above algorithm is not optimal. It may not give the maximum number of sets that can be chosen.

Note: It’s worth noting that this algorithm is not optimal as it doesn’t guarantee the best solution and it’s approximate, but it’s a good option when the data is huge and you need a quick solution.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads