
%g = (a => ['b'],
      b => ['a', 'c', 'd'],
      c => ['b', 'd'],
      d => ['b', 'c', 'e'],
      e => ['d', 'c', 'f'],
      f => ['e'],
     );

sub mincycle {
    my @todo = [shift];
    my %visited;

    while (@todo) {
        my ($cur, $prev, $path) = @{shift @todo};
        if ($path->{$cur}) { # Found cycle?
            return grep $path->{$_} >= $path->{$cur}, keys %$path;
        }
        $path = {%$path, $cur => scalar(keys %$path)};
        my @n = @{$g{$cur}};   # Get neighbors of current vertex
        @n = grep {$_ ne $prev} @n if $prev;   # Don't revisit previous vertex
        push @todo, map [$_, $cur, $path], @n; # Extend paths
    }
    return;
}

for my $start (keys %g) {
    my @p = mincycle($start);
    # my @p = sort { $p->{$a} <=> $p->{$b} } keys %$p;
    print "Starting from $start: @p\n";
}
