1

Implementation of Exhaustive Search Algorithm for Set Packing

 1 month ago
source link: https://www.geeksforgeeks.org/implementation-of-exhaustive-search-algorithm-for-set-packing/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Implementation of Exhaustive Search Algorithm for Set Packing

Last Updated : 16 Jan, 2023

Exhaustive Search Algorithm:

Exhaustive Search is a brute-force algorithm that systematically enumerates all possible solutions to a problem and checks each one to see if it is a valid solution. This algorithm is typically used for problems that have a small and well-defined search space, where it is feasible to check all possible solutions.

Examples:

Input: Items[] =  {1, 2, 3, 4, 5, 6};
List of sets = {1, 2, 3}, {4, 5}, {5, 6}, {1, 4}
Output: Maximum number of sets that can be packed: 3

Input: Items[] =  {1, 2};
List of sets = {1}, {4, }, {5}, {1}
Output: Maximum number of sets that can be packed: 1

Approach: 

Loop through all the sets  and check if the current item is in the current set If the item is in the set, increment the number of sets that can be packed and Update the maximum number of sets that can be packed

Here is a step-by-step description of how the algorithm works in this code:

  • The program defines a maxPackedSets() function that takes a set of items and a list of sets as input.
  • The function initializes the maximum number of sets that can be packed to 0.
  • The function loops through all the sets in the list of sets. For each set, it initializes the number of sets that can be packed to 0.
  • The function then loops through all the items in the set of items. For each item, it checks if the item is in the current set. If the item is in the set, the number of sets that can be packed is incremented and the item is removed from the set of items so that it is not counted again.
  • The function then updates the maximum number of sets that can be packed by taking the maximum of the current maximum and the number of sets that can be packed for the current set.
  • The function repeats steps 3-5 for all the sets in the list of sets.
  • Once all the sets have been processed, the function returns the maximum number of sets that can be packed as the result.
  • The main() function of the program creates a set of items and a list of sets and then calls the maxPackedSets() function to find the maximum number of sets that can be packed into the given set of items.
  • The result of the maxPackedSets() function is printed to the console, indicating the maximum number of sets that can be packed.

Thus, the code uses the Exhaustive Search algorithm to systematically enumerate and check all the sets in the list of sets to find the maximum number of sets that can be packed into the given set of items. This is a simple and straightforward approach to solving the Set Packing problem, but it can be slow and computationally expensive for problems with a large search space.

Here is a Java program that implements the Exhaustive Search algorithm for the Set Packing problem:

  • Python3
  • Javascript
#include <bits/stdc++.h>
using namespace std;
int maxPackedSets(vector<int>& items,
vector<set<int> >& sets)
{
// Initialize the maximum number of sets that can be
// packed to 0
int maxSets = 0;
// Loop through all the sets
for (auto set : sets) {
// Initialize the number of sets that can be packed
// to 0
int numSets = 0;
// Loop through all the items
for (auto item : items) {
// Check if the current item is in the current
// set
if (set.count(item)) {
// If the item is in the set, increment
// the number of sets that can be packed
numSets += 1;
// Remove the item from the set of items,
// so that it is not counted again
items.erase(remove(items.begin(),
items.end(), item),
items.end());
}
}
// Update the maximum number of sets that can be
// packed
maxSets = max(maxSets, numSets+1);
}
return maxSets;
}
int main()
{
// Set of items
vector<int> items = { 1, 2, 3, 4, 5, 6 };
// List of sets
vector<set<int> > sets
= { { 1, 2, 3 }, { 4, 5 }, { 5, 6 }, { 1, 4 } };
// Find the maximum number of sets that
// can be packed into the given set of items
int maxSets
= maxPackedSets(items, sets);
// Print the result
cout << "Maximum number of sets that can be packed: "
<< maxSets << endl;
return 0;
}
// This code is contributed by poojaagarwal2.
Output
Maximum number of sets that can be packed: 3

Characteristics of Exhaustive Search Algorithm:

  • The Exhaustive Search algorithm is a simple and straightforward approach to solving problems. 
  • However, it can be slow and computationally expensive, especially for problems with a large search space. 
  • It is also not guaranteed to find the optimal solution to a problem, as it only checks solutions that are explicitly enumerated by the algorithm. 
  • Despite these limitations, Exhaustive Search can be a useful technique for solving certain types of problems.

Complexity Analysis: 

Time Complexity: The complexity of the maxPackedSets function is O(nm) where n is the length of the items array and m is the number of sets in the sets list. This is because the function loops through all the items (n) and all the sets (m) in two separate loops.

  • The complexity of the contains function is O(n) because it loops through all the elements in the array (n).
  • The complexity of the remove function is O(n) because it loops through all the elements in the array (n) and adds them to a new list if they are not the value to be removed.
  • Overall, the complexity of the entire program is O(n*m)

Auxiliary Space: O(n*m)
The space complexity of the maxPackedSets function is O(nm) because it creates a new list for the result of the remove function for each item in the items array and each set in the sets list.

  • The space complexity of the contains function is O(1) because it only uses a fixed number of variables regardless of the size of the input.
  • The space complexity of the remove function is O(n) because it creates a new list with a size equal to the number of elements in the input array that are not the value to be removed.
  • Overall, the space complexity of the entire program is O(n*m)

"The DSA course helped me a lot in clearing the interview rounds. It was really very helpful in setting a strong foundation for my problem-solving skills. Really a great investment, the passion Sandeep sir has towards DSA/teaching is what made the huge difference." - Gaurav | Placed at Amazon

Before you move on to the world of development, master the fundamentals of DSA on which every advanced algorithm is built upon. Choose your preferred language and start learning today: 

DSA In JAVA/C++
DSA In Python
DSA In JavaScript
Trusted by Millions, Taught by One- Join the best DSA Course Today!

Recommended Problems
Frequently asked DSA Problems
Like Article
Suggest improvement
Share your thoughts in the comments

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK