A friend asked me a question today about an assignment problem. I found a quite straightforward solution, but I feel that it can be made simpler and faster. Your help would be a
templatetypedef gave a very nice proof of why the problem is NP-hard and has no (known) polynomial time solution.
However as with many NP-hard problems, that does not mean you cannot solve it efficiently in practice or that you cannot improve upon a brute force solution.
In particular, you should look into constraint satisfaction problems. They are a more general class of problems that describe precisely what you are trying to solve: Given a set of constraints, what are the values that satisfy all the constraints?
The book AIMA has a very nice chapter on how to solve these types of problems. I suggest you read up on that as there is a lot of good information there and it's very accessible as the book was designed for the undergraduate level. I'll give you some of the big ideas here:
The major questions are:
Here are two heuristics for this:
For the MRV heuristic, break ties by choosing the student that has the most constraints on the other students. The idea behind these heuristics is that you want to pick search paths that are most likely to succeed (LCV). But given a particular search path, you want to fail as early as possible to reduce the amount of time spent on that path (MRV).
Also, instead of naive recursion with basic forward checking, you should use a more advanced search algorithm like AC-3 that looks farther ahead.
Seeing as constraint satisfaction problems are so common in many software engineering applications, there are already a ton of open libraries out there that solve can solve them efficiently. Of course, this is given that the problem you're trying to solve is for a real-world application and not a homework assignment of sorts.