Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 888641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:25:58+00:00 2026-05-15T13:25:58+00:00

Minimum Set Cover is a question where you must find the minimum number of

  • 0

Minimum Set Cover is a question where you must find the minimum number of sets needed to cover every element.
For example, imagine that we have a set of X=array(1,2,3,4,5,6) and 5 another set S, where

S[1]=array(1, 4) 
S[2] =array(2, 5) 
S[3] =array(3, 6)
S[4] =array(1, 2, 3) 
S[5] =array(4, 5, 6)

The problem is to find minimum number of sets of S which cover every element of X. So obviously the minimum set cover in our case will be S[4] and S[5] because they cover all the elements.
Does anybody have an idea how to implement this code in PHP. Note, that this is NP-complete so there is no fast algorithm to solve it. Any solution in PHP will be welcomed. And BTW it is not a homework, I need to use this algorithm in my web application in order to generate suggestion list.
Thanks in advance.

Update 1
There are many applications of Set Covering Problem. Some of the interesting ones are:

  1. Construction of Optimal logic circuits
  2. Air-crew Scheduling
  3. Assembly line balancing
  4. Information retrieval
  5. Art Gallery problem
  6. Genome Sequencing
  7. Red-Blue SetCover problem

Update 2
For example, here you can see the working version of the problem I mentioned. Here, even it shows visually the sets. But I need the pure PHP code for that, if somebody has it please be kind to provide us with the working example in PHP. Thanks

Update 3
Finally, I have solved the problem in PHP. My solution based on the algorithm proposed on a very famous book called Introduction to Algorithms, section The set-covering problem. Here how my solution looks like:

$MainSet=array(1, 2, 3, 4, 5, 6, 7);
$SubSet=array(array(1,4), array(2, 5), array(3, 6), array(1, 2, 3), array(4, 5, 6));

$UncoveredElements=$MainSet;
$CoveringSets=array();
while (count($UncoveredElements)>0){
    $S=SubSetS($UncoveredElements, $SubSet);
    if (is_array($S)){
        $UncoveredElements=array_diff($UncoveredElements, $S);
        $CoveringSets[]=$S;
    }else
        break; //finish work
}
echo "Sets that cover MainSet are:";
var_dump($CoveringSets);

//Subset S is chosen that covers as many uncovered elements as possible
function SubSetS($UncoveredElements, $SubSetArr){
    $max=0; $s=array();
    foreach($SubSetArr as $SubSet){
        $intersectArr=array_intersect($UncoveredElements, $SubSet);
        $weight=count($intersectArr);
        if ($weight>$max){
            $max=$weight;
            $s=$SubSet;
        }
    }
    if ($max>0)
        return $s;
    else
        return 0;
}

Any comments and ideas about my solution? For me, it solves my problem, that is what I wanted. But if you suggest any optimization, correction to the code, please fill free.
BTW, thanks a lot for participants of the question for their valuable responses.

Final Update
This Greedy approach for a Set Covering problem does not always guarantee an optimal solution. Consider the following example:
Given: Elements of Main Set = {1, 2, 3, 4, 5, 6}
Now, consider 4 subsets as follows: Subset 1 = {1, 2}, Subset 2 = {3, 4}, Subset 3 = {5, 6}, Subset 4 = {1, 3, 5}.
The Greedy algorithm for the above collection of Subsets gives the Minimum Set Cover as:
Minimum Set Cover contains the Subsets= { 1, 2, 3, 4}.
Thus, though the minimum collection of subsets that cover all the elements of the Main set are the first three subsets, we get the solution containing all the 4 Subsets.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:25:59+00:00Added an answer on May 15, 2026 at 1:25 pm

    Bakhtiyor, what you say you need for this problem is a little bit contradictory. You said first that you want a minimum set cover, and pointed out yourself that the problem is NP-hard. The algorithm that you posted is the greedy set cover algorithm. This algorithm finds a pretty small set cover, but not necessarily the minimum set cover. Two people posted an algorithm that searches more thoroughly and does find a minimum set cover, and you said in one comment that you don’t need an optimal solution.

    Do you need an optimal solution or not? Because, finding the minimum set cover is a very different algorithm problem from finding a fairly small set cover. An algorithm for the former has to be written very carefully so that doesn’t take ages for a large input. (By I large input I mean, say, 40 subsets.) An algorithm for the latter is easy and you did a good job with your own code. The one thing that I would change is that before your your loop statement “foreach($SubSetArr as $SubSet)”, I would randomize the list of subsets. That gives the algorithm some chance of being optimal for many inputs. (But, there are examples where the minimum set cover does not include the largest subset, so for some inputs this particular greedy algorithm will have no chance of finding the minimum, regardless of order.)

    If you really do want the minimum set cover, and not just a pretty good set cover, then you should state the largest input that you want the code to solve completely. That is a crucial detail that affects how fancy the algorithm needs to be for your task.


    To respond to what some other people have said: First, it is certainly not necessary to search over all collections of subsets if you want the optimal solution. Second, you shouldn’t be looking for “the” algorithm for the problem. The problem has many algorithms, some faster than others, some more complicated than others.

    Here is one way that you can start with an algorithm that searches over all collections, and accelerate it to make something much better. I won’t supply code since I don’t think that the questioner wants such a fancy solution, but I can describe the ideas. You can arrange the search as a branched search: Either the best set cover contains the largest subset A or it does not. (It works well to start with the largest set.) If it does, you can remove the elements of A from the list of elements, remove the elements of A from the elements of the other subsets, and solve the remaining subset cover problem. If it does not, you can still remove A from the list of subsets and solve the remaining subset cover problem.

    So far, this is just a way to arrange a search over everything. But, there are several important ways to take shortcuts in this recursive algorithm. As you find solutions, you can keep a record of the best one that you have found so far. This sets a threshold that you have to beat. At each stage you can total the sizes of the largest threshold-1 subsets remaining, and see if they have enough elements to cover the remaining elements. If not, you can give up; you won’t beat the current threshold.

    In addition, if in this recursive algorithm any element is only covered by one subset, you can throw in that subset for sure whether or not it is the largest one. (In fact, it’s a good idea to add this step even to the greedy algorithm that Bakhtiyor coded.)

    These two accelerations can each prune away huge branches from the search tree, and they work even better in combination.

    There is also a clever data structure for this type of problem due to Don Knuth called “Dancing Links”. He proposed it for the exact cover problem, which is a little bit different from this one, but you can adapt it to minimum subset cover and all of the ideas above. Dancing links is a mesh of intersecting linked lists that allows you check subsets in and out of the recursive search without having to copy the entire input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the minimum set of HTTP verbs that a server should allow for
What are the basic minimum set of Code Analysis rules that you would recommend
I can set the minimum version required (for example XP SP3) in Inno-Setup by
How do you set up NAnt with only the bare minimum binaries? I have
What is the minimum set of primitives required such that a language is Turing
Find minimum window width in string x that contains all characters of another string
Possible Duplicate: Algorithm to find minimum number of weighings required to find defective ball
Given a directed graph, I need to find the minimum set of vertices from
This is probably a question that has been asked before, but I couldn't find
Is it possible to set the minimum number of products required for a category

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.