I'm not entirely sure what "extend the hash" means, but I think you might be well served by looking at the Graph::Base distribution.
Building the all-pairs shortest-path network might seem like overkill, but if you can find a way to cache that and only rebuild it when the network changes, you should then be able to repeatedly pull out whichever subset you're interested in.
use Graph::Directed; my $network = Graph::Directed->new(); # Build a directed network of partners and members foreach my $partner ( keys %partners ) { foreach my $item ( @{ $partners{$partner} ) { # Add each pairing as a network edge with weight 1 $network->add_edge( $partner, $item ); $network->set_attribute('weight', $partner, $item, 1); } } # Now build a shortest-path network my $friends = $network->APSP_Floyd_Warshall(); # Now review all of the links in the network my $required_level = 2; my ( @all_levels ) = $friends->edges(); while ( scalar @all_levels ) { my ( $from, $to ) = splice(@all_levels, 0, 2); my $degrees = $friends->get_attribute('weight', $from, $to); next if ( $degrees > $required_level ); push @{ $allmembers{$from} }, $to; }
At a minimum, take a look at the implementation of APSP_Floyd_Warshall, which seems to be related to what you're looking for.
I'm hardly a graph-theory expert, so the above might not really be what the OP was asking for... corrections would be welcome. I just knew that there must be an existing implementation for this and searched CPAN for Graph.
In reply to Re: creating a hash of arrays/hashes
by simonm
in thread creating a hash of arrays/hashes
by jonnyfolk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |