With all the questions “excellent combination” and “Cartesian product” in SO, I’m sure that there is a name and a canonical solution for this, but I don’t turn it over.
Refresh ... Here is a potentially best example: Suppose a club has regular lotteries. Many items are played for each event, and participants buy tickets individually. In the lottery at night, the lottery manager prints batches of name cards, batches A, B, C and so on. When each item is played, he throws one of these pre-assembled parties into the bunker, mixes them and draws a name. After the prize is distributed, the name returns to the party, which he repeats if any other item has the same party of participants. Question: Is there a stateless algorithm that can collect batches of name cards by printing the minimum total number of cards? [If not, Chris Shain HashSet <> example is the most efficient stateful alternative that I know of.]
Source question and examples: Consider the following lists of people, sandwiches, and allergies (stored relationally, these data structures are designed to keep postings short and are not an integral part of a question or solution):
var people = { "Pete", "Barb", "Debbie", "Frank", "Ralph", "Sally" };
var sandwiches = { "Peanut Butter", "Egg Salad", "Tuna Salad", "Oven Roasted Chicken", "Gluten-free Twigs" };
var allergies = {
{ "Pete", null },
{ "Barb", { "Peanut Butter" } },
{ "Debbie", { "Peanut Butter", "Egg Salad", "Tuna Salad" } },
{ "Frank", { "Egg Salad", "Tuna Salad" } },
{ "Ralph", { "Oven Roasted Chicken" } },
{ "Sally", { "Egg Salad", "Tuna Salad" } } };
To find people who can eat this sandwich, I can, of course, thin through sandwiches (external) and people (internal) quite easily and check for allergies.
I want, however, to provide and publish the smallest list of non-allergic sets of people that will cover all sandwiches (people will obviously belong to more than one set), with no more than one set of people for any sandwich and maximize reuse, for example, a set [ Pete, Barb, Debbie, Frank, Sally] will cover both skim cream and popcorn.
, , , . , , (, ). , A, B, C .., , , , , . (, .)
, - , . , .
, ( ) .
. , MySQL GROUP_CONCAT. , , . :
// SandwichPeople = the sandwich list with a concatenated list of
// people who can eat it:
SELECT Sandwich.SandwichName, GROUP_CONCAT(Person.FullName SEPARATOR ', ') as MemberNames
FROM Sandwich JOIN Person on [...not allergic...]
// SandwichRoster = distinct People from SandwichPeople with auto id
INSERT IGNORE INTO SandwichRoster (MemberNames)
SELECT DISTINCT MemberNames from SandwichPeople
// Match sandwiches with rosters:
SELECT SandwichPeople.SandwichName, SandwichRoster.ID
FROM SandwichPeople
JOIN SandwichRoster on SandwichPeople.MemberNames = SandwichRoster.MemberNames