Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: pattern finding algorithm

by lima1 (Curate)
on Aug 01, 2007 at 07:42 UTC ( [id://629989]=note: print w/replies, xml ) Need Help??


in reply to Re: pattern finding algorithm
in thread pattern finding algorithm

I haven't tried to understand every line, but where exactly is the performance gain compared to the naive algorithm? Instead of comparing m positions you compare 2^(m-1) patterns?
for my $pat (1 .. 2**$m-1) { ... for my $i (0 .. $n-2) { for my $j ( $i+1 .. $n-1 ) { ... } } }

Replies are listed 'Best First'.
Re^3: pattern finding algorithm
by dogz007 (Scribe) on Aug 01, 2007 at 13:59 UTC
    Let me see if I can explain, limal. There are n sequences, each of length m. A pattern may consist of a match (1) or no-match (0) in each position of the sequence, such as 100101001. Think of it as a match "template". There are 2^m-1 patterns (disregarding the case of all zeros). To find all the matches, use each match template to compare every sequence to every other sequence, for a total of (2^m-1)*sum(1..n-1) comparisons.

    Now let's see how many it would take to find all of them using the "naive" algorithm. The algorithm used by GrandFather makes only sum(1..n-1) comparisons. But consider a hypothetical set of 6 sequences. At the end of the naive algorithm, sequence 5 might match 6 in some way. However, this match may also occur in earlier sequences. So to find these would require that this specific match be checked against all the other sequences. Therefore, to find all possible matches would require (n-1)*sum(1..n-1) comparisons.

    Depending on the sizes of m and n, either method may be faster. For the case at hand (m=9,n=2000), the naive algorithm requires 1999*sum(1..1999)=3,996,001,000 comparisons, while my pattern template algorithm only requires (2^9-1)*sum(1..1999)=1,021,489,000 comparisons. This makes my algorithm faster (meaning fewer comparisons) by a factor of (n-1)/(2^m-1)=3.9119 . As an added bonus, it also finds all of the submatches, although kdt2006 has expressed that they are not needed.
      Maybe I just need some coffee, but GrandFathers algorithm generates the list of all maximum patterns including the sequence ids. In your example, the hash would contain something like
      $matches{$pattern} = [ [1,3], ... [ 5, 6 ], ];
      after sum(1..n-1) comparsions. Isn't that what kdt2006 asked for?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-24 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found