monkfan has asked for the wisdom of the Perl Monks concerning the following question:
As you can see the code below only return the "last" k-clique found. My question is how can I modify my code below so it can store and then returns all the "k-clique" found? That is:$ perl graph.pl 3 5 6 9 $ perl graph.pl 4 1 2 3 4
Charles suggested that I use loop to step through the vertices or regex for the second part of the string.$ 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__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Finding number of 'k'-clique in a graph
by blokhead (Monsignor) on Sep 27, 2004 at 16:26 UTC | |
by monkfan (Curate) on Sep 28, 2004 at 03:01 UTC | |
by monkfan (Curate) on Sep 28, 2004 at 03:48 UTC | |
|
Re: Finding number of 'k'-clique in a graph
by Roy Johnson (Monsignor) on Sep 27, 2004 at 16:23 UTC |