in reply to Finding number of 'k'-clique in a graph
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:
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: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"; }
To this:if ($string =~ /$regex/x) { print join(" ", map $$_, 1..$k), "\n"; }
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:$regex .= '(?{ print join(" ", map $$_, 1..$k), "\\n" })(?!)'; $string =~ /$regex/x;
OK, this is still fun, provided it stays out of production code ;)5 6 9 2 3 4 1 3 4 1 2 4 1 2 3
blokhead
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Finding number of 'k'-clique in a graph
by monkfan (Curate) on Sep 28, 2004 at 03:01 UTC | |
by monkfan (Curate) on Sep 28, 2004 at 03:48 UTC |