First of all, are you doing this for fun (showing that regex matching is NP-hard) or for real (your boss really wants you to find all k-cliques)? Since you've found Greg Bacon's code, you probably realize that the k-clique problem is NP-complete.

Solving hard problems with silly regexes is a lot of fun (I've done it myself a few times) but inflexible and hard to maintain. Sure, it might be a little faster since it's in C, but either way it'll be exponential (in this case O(n^k)). As for building a regex to return all the k-cliques, I don't think it's likely. You'd probably have to start from scratch with a new regex and idea. This regex stops as soon as it finds something that matches all the constraints.

If you are really serious about solving k-clique queries, I'd recommend a more "traditional" algorithm (no regexes). Just iterate through all of the n-choose-k subsets of points (see Iterating over combinations for the combinations sub). Some sort of hash of edges (instead of a list) would be useful so you can do quick lookups. Here's the basic pseudocode:

my $iter = combinations( $k => \@vertices ); SUBSET: while ( my @subset = $iter->() ) { my $pairs = combinations( 2 => \@subset ); while ( my @pair = $pairs->() ) { next SUBSET unless @pair is an edge in the graph; } print "@subset is a $k-clique\n"; }
Update: Actually, as Roy Johnson suggested above, augmenting Greg Bacon's original regex to print all k-cliques wasn't too hard. Just change this:
if ($string =~ /$regex/x) { print join(" ", map $$_, 1..$k), "\n"; }
To this:
$regex .= '(?{ print join(" ", map $$_, 1..$k), "\\n" })(?!)'; $string =~ /$regex/x;
You may have to add use re 'eval' for this to work.. Now, when the regex engine has found k vertices which satisfy the expression so far, the (?{code}) section prints them out, but the (?!) causes the overall expression to fail. So the engine backtracks and keeps trying to select more vertices that match. With your example, I got the output:
5 6 9 2 3 4 1 3 4 1 2 4 1 2 3
OK, this is still fun, provided it stays out of production code ;)

blokhead


In reply to Re: Finding number of 'k'-clique in a graph by blokhead
in thread Finding number of 'k'-clique in a graph by monkfan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.