 Perl-Sensitive Sunglasses PerlMonks

### Re^6: Finding All Paths From a Graph From a Given Source and End Node

by neversaint (Deacon)
 on Nov 01, 2010 at 04:59 UTC ( #868690=note: print w/replies, xml ) Need Help??

Dear Limbic,
I tried the following graph and predertimined start and position in code below. But why It gives empty result? Please refer to the visualization of the graph here (http://graph.gafol.net/cESwUMeNd) .
```
my %graph2 = (
],
],
]
);

sub find_paths {
my (\$beg, \$end, \$graph) = @_;

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

my (@work, @solution);
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;
for my \$node (@{\$graph->{\$curr}}) {
my \$bit = \$node_to_int{\$node};
next if vec(\$seen, \$bit, 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];
}
}

# Return
print Dumper \@solution;
return \@solution;
}

---
neversaint and everlastingly indebted.......
• Comment on Re^6: Finding All Paths From a Graph From a Given Source and End Node

Replies are listed 'Best First'.
Re^7: Finding All Paths From a Graph From a Given Source and End Node
by Limbic~Region (Chancellor) on Nov 01, 2010 at 14:56 UTC
neversaint,
The problem was that the %node_to_int lookup assumed all possible nodes would also be keys in graph. One way to fix it would be to have nodes that don't connect anywhere as node => [] (empty array ref). I chose to provide a solution below that doesn't require the user to type more than is necessary when building the graph.

Cheers - L~R

Create A New User
Node Status?
node history
Node Type: note [id://868690]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2020-02-18 00:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What numbers are you going to focus on primarily in 2020?

Results (74 votes). Check out past polls.

Notices?