I have a code that works well here:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %graph =(
F => ['B','C','E'],
A => ['B','C'],
D => ['B'],
C => ['A','E','F'],
E => ['C','F'],
B => ['A','E','F']
);
sub findPaths {
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( \%seen, $node, $end ) };
}
return \@paths;
}
my $start = "B";
my $end = "E";
print "@$_\n" for @{ findPaths( {}, $start, $end ) };
What I want to do is to generate a more general subroutine
so that it just take \%graph, $start,$end as input and return final array.
I tried to do it this way but it doesn’t compile.
sub findPathsAll {
my ($graph,$start,$end) = @_;
my $findPaths_sub;
$findPaths_sub {
my( $seen) = @_;
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;
}
What’s the right way to do it?
I can’t figure out what you want
findPathsAllto return, so this may not be quite what you want. Anyway, here’s a translation offindPathsinto a lexical recursive coderef:Some notes:
You declare a coderef like this:
Note the assignment operator and the trailing semicolon. If you want the coderef to be recursive, you must have already declared
$var. (If you saymy $var = sub { ... };, the new variable doesn’t exist until after the sub is created, so it can’t refer to$var.)You call it like this:
(There are other syntaxes that work, but I think this is the preferred one.)
There was a subtle memory leak in the first version I posted. Perl uses a reference-count garbage collector, which means it can’t delete self-referential data structures. Since the coderef in
$findPaths_subcaptured a reference to itself, it would never be cleaned up (until program exit). I now use theweakenfunction from Scalar::Util (as mentioned by singingfish in his blog entry) to avoid that.$strongRefis used only to keep the coderef from being garbage collected before we’re done with it.