 P is for Practical PerlMonks

### comment on

 Need Help??
choroba,
The short circuiting approach is slower than the previous one

I suspect this will heavily depend on the test data which neversaint hasn't supplied. In a nutshell, I am recording path information on the basis that it will pay off down the road. If there is no short circuit possibilities then the overhead of tracking will not be paid for and it becomes slower.

This is why I said I was unhappy with it. I am going to let this sit in my feeble brain for a while and if I can't come up with a better approach I will post a new thread for other monks to weigh in on.

Update: The following is the basically the same algorithm with some optimizations. I would be interested in your test data and/or your benchmark.

```#!/usr/bin/perl
use strict;
use warnings;

my %graph = (
R => [qw/L J Z/], L => [qw/R J X/], J => [qw/R L X Z/], Z => [qw/R
+ J X/], X => [qw/L J Z F D/],
F => [qw/X D/],   D => [qw/Q F X/], Q => [qw/B U D/],   B => [qw/Q
+ P M/], P => [qw/B U/],
U => [qw/Q P S/], M => [qw/B S/],   S => [qw/U M/],
);

my \$n;
my %node_to_int = map {\$_ => \$n++} keys %graph;

my \$routes = find_paths('D', 'M', \%graph);
print "\$_\n" for @\$routes;

sub find_paths {
my (\$beg, \$end, \$graph) = @_;
my (@work, @solution, %done);
for (@{\$graph->{\$beg}}) {
if (\$_ eq \$end) {
push @solution, "\$beg->\$end";
next;
}
my \$seen = '';
vec(\$seen, \$node_to_int{\$_}, 1) = 1;
vec(\$seen, \$node_to_int{\$beg}, 1) = 1;
push @work, ["\$beg->\$_", \$_, \$seen];
}
while (@work) {
my \$item = pop @work;
my (\$path, \$curr, \$seen) = @\$item;
my \$ok;
NODE:
for my \$node (@{\$graph->{\$curr}}) {
my \$bit = \$node_to_int{\$node};
next if vec(\$seen, \$bit, 1);
if (\$done{\$node}) {
for (keys %{\$done{\$node}}) {
next NODE if \$_ eq (\$_ & \$seen);
}
}
\$ok = 1;
if (\$node eq \$end) {
push @solution, "\$path->\$end";
next;
}
my \$new_seen = \$seen;
vec(\$new_seen, \$bit, 1) = 1;
push @work, ["\$path->\$node", \$node, \$new_seen];
}
if (! \$ok) {
my @order = reverse split /->/, \$path;
pop @order;
for (@order) {
vec(\$seen, \$node_to_int{\$_}, 1) = 0;
\$done{\$_}{\$seen} = undef;
}
}
}
return \@solution;
}

Cheers - L~R

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2020-04-01 17:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
The most amusing oxymoron is:

Results (18 votes). Check out past polls.

Notices?