while (@work) { my $item = pop @work; # Create lexicals to save a few dereferencing cycles my ($cur_depth, $route, $last_node) = @{$item}[DEPTH, PATH, LAST]; # Update our high water mark if appropriate ($max, $path) = ($cur_depth, $route) if $cur_depth > $max; # How many nodes are greater my $left = $greater{$last_node}[CNT]; # Finish if end of the path # Or if impossible to exceed current max depth next if ! $left || $cur_depth + $left <= $max; push @work, map ["$route:$_", $cur_depth + 1, $_], @{$greater{$last_node}[NODE]}; }