in reply to Solving a deep recursion problem
my %seen; my @nodes = ([$start, 0]); # $start is the starting node while (@nodes) { my ($node, $dist) = @{shift(@nodes)}; my @adj = ...nodes adjacent to $node...; for (@adj) { next if $seen{$_}; $seen{$_} = 1; if ($dist < $MAX) { push(@nodes, [$_, $dist+1]); ...add $_ to list of interesting nodes... } } }
This replaces the recursion with iteration.
|
|---|