I’m trying to build a data tree like the one shown below, and I need an efficient matching algorithm that can do the following.
You can think of this tree as a list of prerequisites for taking courses. For example, course1 has prerequisite 3 and 4, and course 3 has prerequisites 7 and 8. If one wants to take course1, he/she must take both 3 and 4, or all prerequisites for course3 or course4 (so if he/she took 7,8,4, it’s equivalent to having taken 3 and 4).
Now a kid comes and says he wants to take course2, providing that he took course 8,9 and 6 in advance. How can I quickly check if he is eligible?
The only way I can think of right now is to construct a look-up table that contains all combinations of prerequisites, and checks through it to find the match. However, as the tree grows bigger(I’m trying to build one with potentially >10,000 nodes), this method is going to blow up my computer.
Does anyone have any advice for this? Or better yet, is there a well-defined searching algorithm that can handle this type of task already?
Thanks in advance.
Jim

Fig: Some arbitrary data tree
Performance will probably be fine just doing it in a simple way. If the student wants 2, first check whether they satisfy the requirements imposed by the
5prerequisite, then check whether they satisfy the requirements imposed by the6prerequisite. You requirements-checking function is going to involve a recursive call or similar.If this doesn’t work for some reason (cycles in the graph?!), you could go in the other direction to what you’ve described: start from the courses the student has taken, and flood-fill to get a list of all the courses the student qualifies for. Check whether the target is in that list (or rather: stop immediately if you encounter the target while building the list).
The flood fill is somewhat complicated by the fact that you’re not just following arrows: I can qualify for
1in four different ways:(3 or (7 and 8)) and (4 or 9). But the basic idea is the same: keep testing the parents of my reachable nodes to see whether I can add anything to my set, until I can’t any more.Applicable to both methods: I’m not entirely clear what the rules are, whether this “satisfying the prerequisites of 3 instead of taking 3” is transitive or not. Given:
I get that having
(5,6)qualifies me for2, but does(5,6,3)qualify me for1?If so then I think the process is a bit easier, because as a simplifying assumption I can just pretend that if I’ve taken
(5,6)then I’ve also taken4, and the question “am I eligible for 2?” becomes, “have I taken 2?”.It seems to me more sensible if it doesn’t, since I don’t suppose I can really take a whole bunch of elementary classes and then jump straight into post-graduate work 10 levels higher up. Then there’s a functional difference between courses that I’ve taken, vs courses whose prerequisites I’ve taken. That difference needs to be tracked. In the case of the flood-fill, I think that means 2 flags per node to consider rather than 1. In the case of the requirements-checking function, I think it means you need 2 checks (for direct and indirect satisfaction), and no recursion is needed.