Ieronim,
I give. While I understand your admittedly fast code - it is extremely hard to follow. The cleanest code I could come up with using a bi-directional BFS is as fast as your transform() before following my recommendation. That is to say, it is about half as fast as your current version:
use constant SRC => 0;
use constant TGT => 1;
sub find_path2 {
my ($src, $tgt, $list) = @_;
my (@src_work, @tgt_work, %path);
for my $dir (SRC, TGT) {
my ($word, $work) = $dir == SRC ? ($src, \@src_work) : ($tgt,
+\@tgt_work);
$path{$word}[$dir] = -1;
for (@{$list->{$word}}) {
push @$work, $_;
$path{$_}[$dir] = $word;
}
}
while (1) {
for my $dir (SRC, TGT) {
my @next;
my $work = $dir == SRC ? \@src_work : \@tgt_work;
for my $word (@$work) {
return build_path(\%path, $word) if $path{$word}[abs($
+dir - 1)];
for (@{$list->{$word}}) {
next if $path{$_}[$dir];
push @next, $_;
$path{$_}[$dir] = $word;
}
}
@$work = @next;
}
return 'Path not found' if ! @src_work && ! @tgt_work;
}
}
sub build_path {
my ($tree, $node) = @_;
my $path = "-$node";
for my $dir (SRC, TGT) {
my $word = $tree->{$node}[$dir];
while ($word ne '-1') {
$path = $dir == SRC ? "-$word$path" : "$path-$word";
$word = $tree->{$word}[$dir];
}
}
return substr($path, 1);
}
Update (2006-07-03): Simply moving
return build_path(\%path, $word) if $path{$word}[abs($dir - 1)]; to the inner most for loop and s/word/_/ brings the performance much closer. See the update in
this node for an explanation why.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.