Skip to content

Instantly share code, notes, and snippets.

@kemurphy
Created June 13, 2012 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemurphy/8b5d7722d2da8dce12f0 to your computer and use it in GitHub Desktop.
Save kemurphy/8b5d7722d2da8dce12f0 to your computer and use it in GitHub Desktop.
hic sunt leones
sub findMinCycles {
# hic sunt leones
# sorry for the terse code, I promise it works, no need to touch it :)
my ($self, $v, $g, $i, $s) = @_;
if (exists $i->{$v}) {
my @c = @{$s}[$i->{$v}..$#$s];
my $k = (sort { @{$g->{$c[$b]}}<=> @{$g->{$c[$a]}} } (0 .. $#c))[0];
push @c, $v;
local $" = ' -> ';
$self->error("Dependency cycle detected: @c");
$self->verbose("DEBUG: removing $c[$k] -> $c[$k+1]");
my $e = $g->{$c[$k]};
my $w = $c[$k+1];
for my $j (0 .. $#$e) {
if ($w eq $e->[$j]) {
splice @$e, $j, 1;
last;
}
}
} elsif (defined $g->{$v}) {
push @$s, $v;
$i->{$v} = $#$s;
my @e = @{$g->{$v}};
foreach my $w (@e) {
$self->findMinCycles($w, $g, $i, $s);
}
delete $i->{$v};
pop @$s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment