Solved

Set Cover problem - algorithm

Posted on 2010-11-26
4
1,837 Views
Last Modified: 2013-04-26
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.

Thanks so much in advance.
0
Comment
Question by:sarah2248
4 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 250 total points
ID: 34219300
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
 
LVL 2

Assisted Solution

by:matstein
matstein earned 250 total points
ID: 35932810
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
 
LVL 100

Expert Comment

by:mlmcc
ID: 36268303
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This algorithm (in C#) will resize any image down to a given size while maintaining the original aspect ratio. The maximum width and max height are both optional but if neither are given, the original image is returned. This example is designed t…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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