Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^5: Fast common substring matching

by Roy Johnson (Monsignor)
on Nov 29, 2005 at 17:08 UTC ( #512710=note: print w/ replies, xml ) Need Help??

in reply to Re^4: Fast common substring matching
in thread Fast common substring matching

Yes, after I came up with my algorithm, I realized what all the output from GrandFather's code meant. I had thought it was just some sort of cryptic progress meter. :-)

The (reasonably) obvious way to get the longest substring for each pair of input strings would be to run my algorithm using each pair of strings as input rather than the whole list of strings. That's probably more work than GF's method, though. I thought about trying it, but something shiny caught my attention...

Update: but now I've done it. It runs on 20 strings of 1000 characters in something under 10 seconds for me. 100 strings of 1000 characters takes about 4 minutes.

use warnings; use strict; use Time::HiRes; if (@ARGV == 0) { print "Finds longest matching substring between each pair of a set + of test\n"; print "strings in the given file. Pairs of lines are expected with + the first\n"; print "of a pair being the string name and the second the test str +ing."; exit (1); } my $minmatch = 10; my $startTime = [Time::HiRes::gettimeofday ()]; my %strings; while (<>) { chomp(my $label = $_); chomp(my $string = <>); # Compute all substrings @{$strings{$label}} = map [substr($string, $_), $label, $_], 0..(len +gth($string) - $minmatch); } print "Loaded. Generating combos...\n"; my @keys = sort keys %strings; my @best_overall_match = (0); for my $ki1 (0..($#keys - 1)) { for my $ki2 (($ki1 + 1)..$#keys) { my @strings = sort {$a->[0] cmp $b->[0]} @{$strings{$keys[$ki1]}}, + @{$strings{$keys[$ki2]}}; # Now walk through the list. The best match for each string will b +e the # previous or next element in the list that is not from the origin +al substring, # so for each entry, just look for the next one. See how many init +ial letters # match and track the best matches my @matchdata = (0); # (length, index1-into-strings, index2-into-s +trings) for my $i1 (0..($#strings - 1)) { my $i2 = $i1 + 1; ++$i2 while $i2 <= $#strings and $strings[$i2][1] eq $strings[$i +1][1]; next if $i2 > $#strings; my ($common) = map length, ($strings[$i1][0] ^ $strings[$i2][0]) + =~ /^(\0*)/; next if $common < $minmatch; if ($common > $matchdata[0]) { @matchdata = ($common, [$i1, $i2]); } elsif ($common == $matchdata[0]) { push @matchdata, [$i1, $i2]; } } next if $matchdata[0] < $minmatch; if ($matchdata[0] > $best_overall_match[0]) { @best_overall_match = ($matchdata[0]); } if ($matchdata[0] >= $best_overall_match[0]) { push @best_overall_match, map { ["$strings[$_->[0]][1]:$strings[$_->[0]][2]", "$strings[$_->[1 +]][1]:$strings[$_->[1]][2]"] } @matchdata[1..$#matchdata]; } print "$keys[$ki1] and $keys[$ki2]: $matchdata[0] chars\n"; for my $i (@matchdata[1..$#matchdata]) { if ($strings[$i->[0]][1] eq $keys[$ki2]) { @{$i}[0,1] = @{$i}[1,0]; } print "... starting at $strings[$i->[0]][2] and $strings[$i->[1] +][2], respectively.\n"; } } } print "Best overall match: $best_overall_match[0] chars\n"; print "$_->[0] and $_->[1]\n" for (@best_overall_match[1..$#best_o +verall_match]) ; print "Completed in " . Time::HiRes::tv_interval ($startTime) . "\n";

Caution: Contents may have been coded under pressure.

Comment on Re^5: Fast common substring matching
Download Code
Replies are listed 'Best First'.
Re^6: Fast common substring matching
by bioMan (Beadle) on Nov 29, 2005 at 22:37 UTC

    I had thought it was just some sort of cryptic progress meter. :-)

    LOL - I know what you mean.

    I'm still going over your original code to see how you did what you did -- trying to learn some perl :-)

    I'll give the new code a try. I also see that the minimum length in your code doesn't have to be a power of 2. This should allow me to analyze a limit boundary that appears to be present in my data. Grandfather's code allowed me to come up with what I feel is a pretty good estimate for the value of the limit, but this should allow a closer examination of the limit.


      Actually as far as I can remember my code doesn't require a power of 2 for the minimum size either. It may have been more important in earlier versions than in the current version.

      Somewhere on my todo list is an item to look at Roy's code, but I've not got down to that item on the list yet. :)

      DWIM is Perl's answer to Gödel

      Thanks for the clarification. For some reason I got it in my head that the minimum length of the substring had to be a power of 2. That idea must have come from someone else's algorithm for the longest common string search.

      Nontheless, your script has been very useful to me.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://512710]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2016-02-08 08:18 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (271 votes), past polls