UPDATE: I expected this part { %$seen } to be quite expensive .... does it even make sense?
Well, it does no harm :)
Um. Do you mean:
Does the syntax make sense?
It just makes a copy (anonymous reference) of the seen hash(ref).
Or; why make a copy?
I make a copy because I want any changes made to it at deeper levels of recursion, to be unwound as the recursion unwinds.
It's purpose is obviously to prevent the algorithm from going in circles when it re-encounters a node it has already visited. But when we visit a node a deeper level we don't to permanently prevent it from visiting that node again if it reaches it via different path earlier in the recursion but later in the iteration.
Hm. Not sure that description works?
Essentially, the hash(ref) $seen and array(ref) $path contain the same information, but arrays are no good for lookup, and hashes are unordered which is no good for paths. Hence using both.
There is a potential optimisation there. I could just build the hash from the array at each level instead of passing it through. In fact, I'll try that later.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
And the culprit was ... usage of self-documenting variable names.
Simply avoiding the array-copy my @path=@_ makes a factor 4.5 performance boost...
Fascinating!!! What a pity Perl doesn't support aliasing out of the box!
{
my %seen;
sub track {
my $last=$_[-1];
$seen{$last}=1;
for my $next (@{$graph{$last}}) {
next if $seen{$next};
if ($next eq $stop) {
#print join ("->",@_,$stop),"\n";
$anzahl++;
print "$anzahl at ",time-$time0,"\n" if $anzahl%10000==0;
} else {
track(@_,$next);
}
}
delete $seen{$last};
}
}