Set Cover problem - algorithm

Posted on 2010-11-26
Last Modified: 2013-04-26
Can someone please share a program in java which does the following.
if given the following sets as inputs,

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.

Thanks so much in advance.
Question by:sarah2248
LVL 37

Accepted Solution

TommySzalapski earned 250 total points
Comment Utility
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.

Assisted Solution

matstein earned 250 total points
Comment Utility
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.

LVL 100

Expert Comment

Comment Utility
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now