I have a computational problem where I have a set of tags that can be given away exactly once, and a number of items that each have a set of possible choices of tags that they can accept. I need to assign the tags in such a way that each item gets a tag whenever there is a solution to the problem.
To give a specific example, if I had a dictionary like this:
options = {
'a': [1],
'b': [2, 3],
'c': [1, 2]
}
..then the solution would be:
{'a': 1, 'b': 3, 'c': 2}
I will also need to be able to expand the problem to handle multiple constraints, e.g.
[1], ['A', 'B'] # -> (1, A) or (1, B)
[1, 2, 3], ['A', 'C'] # -> (3, C)
[1, 2], ['A', 'C'] # -> (2, A) or (2, B)
I have tried to come up with workable solutions based on a priority queue for the simple case, but that completely falls apart with multiple constraints. I could brute-force a solution using itertools.permutations
, but I'd prefer an efficient implementation. Is there any known algorithm that might be applicable to this problem?