in reply to Re: Finding All Paths From a Graph From a Given Source and End Node
in thread Finding All Paths From a Graph From a Given Source and End Node

> for large graphs you should consider using a hash instead of passing an array to speed up the test.

e.g. like this

{ my %seen; sub track { my @path=@_; my $last=$path[-1]; $seen{$last}=1; for my $next ( @{$graph{$last}} ) { next if $seen{$next}; if ($next eq $stop) { print join ("->",@path,$stop),"\n"; } else { track(@path,$next); } } delete $seen{$last}; } }

Cheers Rolf

  • Comment on Re^2: Finding All Paths From a Graph From a Given Source and End Node
  • Download Code