Reputation: 5207
I'm stuck with algorithmic problem for several days and any help is appreciated here.
Let's say that I have some modules in my app which can call other modules (as submodules). Something like blocks in Matlab Simulink. Also modules could call each one forming a loop.
For the sake of simplicity I've made a schema how that could look like.
In admin panel administrator can activate or deactivate some of these modules. For activation it is quite easy. Only one chain must be checked. But for deactivation it is tricky. We cannot just deactivate it because it might be used by some other module(s). That's why all possible cases must be checked. Also all submodules of that one must be deactivated (and checked if used by other modules).
For example, let's say that I want to deactivate X (and it's submodules A-B-C-E)
X is inside D:
D - X
and inside:
Z - N
which calls
D - X
Also X calls the whole chain which should be deactivated:
X - A - B - C - E
So once again, this whole chain (X-A-B-C-E) has to be deactivated but we must check if X-A-B-C-E is used somewhere else first.
I work in C++ but any pseudocode would be helpful.
What I've tried so far is setting first X-A-B-C-E in a Map.. Then going for all chains and cancel the ones in my initial Map. The ones which are left are safe to deactivate. But it doesn't work.
Also tried to parse this as Linked lists... But also there I don't know where to start and how to go through all possible combinations.
Like I said, it's the tricky one and any tip is helpful.
Upvotes: 3
Views: 69
Reputation: 58868
This would be the straightforward brute-force approach:
But note that two lists might not be the best data structure here. It is probably more efficient to have a boolean flag in each module indicating whether you've looked at it. I'm just illustrating the general idea, ignoring such details.
If this looks like a breadth-first search, that's because it is.
Upvotes: 1