I have a set of elements that have various dependencies on each other. These dependencies might be strict, eg. a depends on b and c; or some elements might have alternatives, eg. s depends on t or u. There are no circular dependencies.
I’m trying to do two things with the dependency information:
- Determine whether a given set of elements has all dependencies resolved
- List all possible completely resolved sets of elements
(In fact 2 is trivial given 1, since I can just generate all permutations and check them, where resources allow. There are probably better algorithms for that though.)
Are there algorithms for this that accommodate elements with alternative dependencies? I’ve found plenty that only account for strict dependencies, but I don’t know enough terminology to narrow my search.
Let’s assume your elements are listed in L. Then, for each element e in L you validate that all direct dependencies are resolved. To validate each element e, you could walk through the list D of e‘s dependencies and ensure that all elements d in D are found in L. To address the case of alternatives, now think of each d as a list of alternative dependencies on it’s own.
To list all possible sets that satisfy your dependencies, you can use a permutation index to iterate through all
alt_ds in all possible different orders. You can generate these indices beforehand, because you know the number of alternatives for each element of L. You could check outndgridin MATLAB used as in the following code, assuming there is element a with three dependencies and b with two:for a reference on how to use such precomputed indices, independently of the language you want to implement this in.