Minimal number of groups necessary to cover user/p

2019-03-04 06:02发布

问题:

I have a list of 365 customers. Each of them has a potentially unique list of products they are allowed to order, up to 18 out of 24 total products (at present). I would like to use groups to assign the permissions. How can I determine the minimum number of unique permission sets?

I have a hunch the answer involves relational division, but I'm still very fuzzy on how that works.

To clarify: I am not just interested in which users have exactly the same permissions. I want to find groups I can use, each user potentially a member of multiple groups, that can reproduce the permissions. For instance, group "A" might have permission to 99498; group "B" have permission to 99507, 99508, 99512. All three users in the truncated data below would be members of "A", and only the first two would be members of "B".

CREATE TABLE UsersProducts (
    [User] INTEGER NOT NULL, 
    [Product] INTEGER NOT NULL, 
    PRIMARY KEY ([User],[Product])
)
CREATE TABLE GroupsProducts (
  [Group] INTEGER NOT NULL,
  [Product] INTEGER NOT NULL,
  PRIMARY KEY ([Group],[Product])
)
CREATE TABLE GroupsUsers (
  [Group] INTEGER NOT NULL,
  [User] INTEGER NOT NULL,
  PRIMARY KEY ([Group],[User])
)

-- Given this UsersProducts information, I want 
--the GroupsProducts and GroupsUsers tables filled.

INSERT UsersProducts
        ( [User],[Product] )
VALUES  (11804,99498),
(11804,99506),
(11804,99507),
(11804,99508),
(11804,99512),
(11804,99547),
(11804,99592),
(11804,99594),
(11804,99647),
(11804,99658),
(11804,99660),
(11804,99667),
(11804,99694),
(11804,99700),
(11804,99771),
(11947,99498),
(11947,99506),
(11947,99507),
(11947,99508),
(11947,99512),
(11947,99547),
(11947,99592),
(11947,99594),
(11947,99647),
(11947,99658),
(11947,99660),
(11947,99667),
(11947,99700),
(11947,99720),
(11947,99771),
(12009,99498),
(12009,99506),
(12009,99507),
(12009,99508),
(12009,99512),
(12009,99547),
(12009,99575),
(12009,99592),
(12009,99594),
(12009,99596),
(12009,99647),
(12009,99658),
(12009,99660),
(12009,99667),
(12009,99694),
(12009,99700),
(12009,99720),
(12009,99771),
(12353,99498),
(12353,99512),
(12353,99547),
(12353,99575),
(12353,99592),
(12353,99594),
(12353,99596),
(12353,99647),
(12353,99658),
(12353,99660),
(12353,99667),
(12353,99694)
-- etc. 365 distinct users, 28 distinct products. 4012 pairings.

回答1:

Thanks to Anon and Gordon Linoff, and much googling on my own: this is an example of the set-cover problem, or perhaps even more difficult, and solving this is just not worth it. We will go with the 24 groups solution with some users being a member of 18 groups at a time. This is known to be far from minimal but it will work and that's all we need. Only the input phase requires care.