x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 2315

# Set Cover problem - algorithm

Hi,
Can someone please share a program in java which does the following.
if given the following sets as inputs,
a={1,2,3,8,9,10}
b={1,2,3,4,5}
c={4,5,7}
d={5,6,7}
e={6,7,8,9,10}

and U={1,2,3,4,5,6,7,8,9,10}
the program will find all the combinations of the set and find out the minimum number of sets that together has all the elements of U.
In the above example, the minimum number is 2. set b and e together covers all of U.
So basically, it is a set covering problem. In the Set Covering problem, we are given a universe U, such that |U|=n, and sets S1,……,Sk are subsets of U. A set cover is a collection C of some of the sets from S1,……,Sk whose union is the entire universe U. Additionally, we must minimize the cost of the sets.

0
sarah2248
2 Solutions

Commented:
Since the set cover problem is NP-Complete you can only use a greedy algorithm for small universes or the algorithm will never finish. But since your example only has 5 sets, it's not too bad.

It can be very simple if you use recursion. I'm guessing this is for an assignment, so I'm not writing the code for you, but I'll help you get started and help you if you get stuck.

Here's how I would approach it. Loop through your sets using a for loop that does this:
Take the current set S
Does S cover the universe?
If so return 1 and the name of the set.
If not, remove the sets in S from the universe and loop through the other sets (only the ones after the current one) and find the minimum that covers the remaining portion of the universe (by calling the same function this code is in). Return 1+the minimum and the set that covered plus this set.

This will give the minimum set cover in a fairly efficient time. To speed it up some, keep track of how many sets have been picked so far and the current minimum so you can just quit if the current set of sets is bigger or equal to the current minimum.

If the above is confusing, try to figure it out and let us know where you are stuck.
0

Commented:
My solution would be as follows:

Every possible set will be 5C1 + 5C2 + 5C3 etc...

So first iterate over all sets in the 5C1 case (i.e. set A then B then C) and ask if those sets cover U if so return 1

If it doesn't generate new sets A U B, A U C etc. (in the 5C2 group) and see if those cover return 2

pseudo code is as follows:

global set A,B,C,D,E, U

checkCoverage(Set X)
{
for each element in U is that element in X
if yes return true
return false
}

getminimumnumber(int group)
{
for i = A to D do
if checkcoverage(i,U)
return 1
start = B
for i = A to D do
{
for j= start to E do
{
X = i U j
if checkcoverage(X,U)
}      return 2
start = start++;

proceed similarly for the 5C3, 5C4 and 5C5 combinations
}

A more elegant way to go about it is to make all possible combinations of sets up front and then iterate through the list. Obviously this problem is NP complete as the above expert mentioned and thus the algorithm is completely useless for large numbers of sets.

0

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.