$ perl graph.pl 3
5 6 9
$ perl graph.pl 4
1 2 3 4
####
$ perl graph.pl 3
would give:
1 2 3
1 2 4
1 3 4
2 3 4
5 6 9
####
_BEGIN__
#Credit Greg Bacon
#and suggestion by C.Clarkson
my @vertices = 1 .. 9;
my @edges = (
[1,2], [1,3], [1,4], [1,5],
[2,3], [2,4],
[3,4],
[5,6], [5,7], [5,9],
[6,9],
[7,8],
[8,9],
);
$k = shift || 3;
#Construct a string as follows:
$string = (join ',' => @V) . ';'
. (join ',' => map "$_->[0]-$_->[1]", @E);
#Then construct a regular expression as follows:
$regex = '^ .*\b '
. join(' , .*\b ' => ('(\d+)') x $k)
. '\b .* ;'
. "\n";
for ($i = 1; $i < $k; $i++) {
for ($j = $i+1; $j <= $k; $j++) {
$regex .= '(?= .* \b ' . "\\$i-\\$j" . ' \b)' . "\n";
}
}
#Our graph contains a k-clique if and only if
if ($string =~ /$regex/x) {
print join(" ", map $$_, 1..$k), "\n";
}
__END__