in reply to Re: Finding All Paths From a Graph From a Given Source and End Node
in thread Finding All Paths From a Graph From a Given Source and End Node
Dear BrowserUK,
I have successfully generalize your code above so that it just take %graph, $start,$end as input
and return final array. Thanks so much. BTW, what's the time complexity of your subroutine?
sub findPathsAll { my ($graph,$start,$end) = @_; my $findPaths_sub; $findPaths_sub = sub { my( $seen, $start, $end ) = @_; return [[$end]] if $start eq $end; $seen->{ $start } = 1; my @paths; for my $node ( @{ $graph->{ $start } } ) { my %seen = %{$seen}; next if exists $seen{ $node }; push @paths, [ $start, @$_ ] for @{ $findPaths_sub->( \%seen, $node, $end ) }; } return \@paths; }; my @all; push @all,[@$_] for @{ $findPaths_sub->( {}, $start, $end ) + }; return @all; }
---
neversaint and everlastingly indebted.......
neversaint and everlastingly indebted.......
|
---|
In Section
Seekers of Perl Wisdom