Algorithm for reaching the population with minimum labels

My data is people and tags with a many-to-many relationship.

I need an algorithm that will find the minimum set of tags, so that the union of the groups of people marked with each tag in the solution will cover the entire population.

Any ideas?

+3
source share
3 answers

This is an NP-hard problem. It takes a long time to make sure that you really have the minimum number of tags required.

A fairly quick and easy solution that I would use is

while there are users left in the pool:
    find the tag that represents the most users
    add that tag to the list
    remove all the users that that tag represents from the pool

If you want, you can then loop through and make sure there aren't any 
unnecessary tags //but that probably won't help much

There are, of course, several ways in which tags could be tagged so that this is not the best way and does not find the optimal solution. However, I'm sure it should be pretty close.

+4

NP- .

: , . = , , , . , , , , "" -.

Oposite ( ) , + .

+1

A many-to-many relationship between people and tags forms a bipartite graph, which, fortunately, is very different from the general case. Therefore, the problem is not NP-complete, but can be solved in polynomial time . This is similar to finding a maximum match, for which Wikipedia provides several alternatives .

+1
source

Source: https://habr.com/ru/post/1758558/


All Articles