Matching with constraints

爷,独闯天下 提交于 2019-12-07 17:35:20

问题


For fun, I'm creating a program that generates partners for a Secret Santa gift exchange. However, in this setup, instead of randomly generating pairs, constraints are allowed.

Example: Person A and Person B hate each other, so neither A nor B should be assigned to buy a gift for the other.

Second Example: Person C bought a gift for Person D last year. Person C should not be assigned to buy a gift for Person D, but person D should still be allowed to buy C a gift.

In general, I want to generate a bijective function from a set to itself, but that function needs to be sensitive to constraints. If there are too many constraints such that the problem can't be solved, the routine should return an error or something.

This looks to me like some sort of graph problem, but I don't really know what direction to go in to solve it. How can I solve this problem programmatically? Are there existing algorithms I can use/modify?


回答1:


Essentially your problem is well-known assignment problem:

Consider a bipartite graph where each side has all persons as nodes (yes, each person will appear twice: once at the left side and once at the right one). Then you can add an edge from person A from the left side to the right side's person B if A is allowed to send a gift to B. Then you can apply, for example, Hungarian algorithm to find assignment which maximizes number of allowed gifts.




回答2:


Sounds like constraint Satisfaction problem. There is a graphical representation for the problem, that shows visually how it is solved. I took a course on AI that talked about it. Here is a lecture slides: Lecture. Slide 15 shows the picture. Read through the slides, there are several algorithms that solve CSP, including Variable Elimitation ( slide 44).




来源:https://stackoverflow.com/questions/20205154/matching-with-constraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!