Jump to content

Tarjan: Difference between revisions

225 bytes added ,  4 years ago
m
→‎{{header|Perl}}: fixed Perl code
m (→‎{{header|Perl}}: fixed Perl code)
Line 273:
=={{header|Perl}}==
{{trans|Perl 6}}
<lang perl>use feature 'state'5.016;
use feature 'state';
use List::Util qw(min);
use experimental qw(lexical_subs);
 
sub tarjan {
ourmy (%k) = @_;
ourmy (%onstack, %index, %lowlink, @stack, @connected);
our @connected = ();
 
my sub strong_connect {
my ($vertex, $i) = @_;
state $index{$vertex} = 0$i;
$indexlowlink{$vertex} = $i =+ $index1;
$lowlinkonstack{$vertex} = $index++1;
push @stack, $vertex;
for my $onstackconnection (@{$k{$vertex}}) = 1;{
for my $connection if (@{not defined $kindex{$vertex}connection}) {
if (not $index{ __SUB__->($connection}), {$i + 1);
$lowlink{$vertex} strong_connect= min($lowlink{$connection}, $lowlink{$vertex});
}
$lowlink{$vertex} = min($lowlink{$connection},$lowlink{$vertex});
} elsif ($onstack{$connection}) {
$lowlink{$vertex} = min($lowlinkindex{$connection}, $lowlink{$vertex});
}
}
if ($lowlink{$vertex} eq $index{$vertex}) {
Line 307 ⟶ 308:
 
for (sort keys %k) {
strong_connect($_, 0) unless $index{$_};
}
@connected;
}
 
my %test1 = (
0 => [1],
1 => [2],
2 => [0],
3 => [1, 2, 4],
4 => [3, 5],
5 => [2, 6],
6 => [5],
7 => [4, 6, 7]
);
);
 
my %test2 = (
'Andy' => ['Bart'],
'Bart' => ['Carl'],
'Carl' => ['Andy'],
'Dave' => [qw<Bart Carl Earl>],
'Earl' => [qw<Dave Fred>],
'Fred' => [qw<Carl Gary>],
'Gary' => ['Fred'],
'Hank' => [qw<Earl Gary Hank>]
);
);
 
print "Strongly connected components:\n";
2,747

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.