I understand exactly what you're talking about - the CPAN client does something like this when installing modules and dependencies are discovered.
I did something similar when coding a simulation where mobile nodes could talk to each other either directly or by relaying through adjacent nodes if the endpoints were too far way. Essentially, if node1 could talk to node2 but not directly to node3 *AND* node2 could talk to node3, then node1 could in fact talk to node3 (by relaying through node2). If that sounds to you (like it does to me) similar to your problem, have a look at Union/Intersection of Multiple Arrays where JavaFan gave me the idea of "transitive closure" that solved my problem.