String Search. The Boyer Moore algorithm

Use this section to discuss your embedded Flowcode projects.
Post Reply
mnfisher
Valued Contributor
Posts: 2100
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 165 times
Been thanked: 967 times

String Search. The Boyer Moore algorithm

Post by mnfisher »

More Fun with Flowcode :-)

Reading a copy of Niklaus Wirth's 'Algorithms + Data Structures' (from 1980s) - I came across the Boyer Moore algorithm...

A common problem in programming is searching a string (or array of bytes) looking for a match with a pattern. Think genetic sequencing?

The normal string search uses a 'linear' algorithm - and for searching large data arrays with complex strings there are better algorithms.

Here I use the Boyer Moore algorithm. I used an Arduino - and because of the limited RAM I allow data in the range 0..127 (printable ASCII) and strings. In the real world - this would be 0..255 (or possiby not for genetic data?)

The BoyerMoore algorithm works best with longer patterns - and here is a rather contrived example - in a string of 1500 random characters - I copy some instances (here 3 and no check that they don't overlap) of the pattern - then time 1000 searches for (all) occurrences of the pattern in the data string.

Obviously - searching for the same data 1000 times is unrealistic - and I could optimise for this by only doing the setup once (BadChar) - but that might be construed as cheating? However this could be done for searching for the same data in multiple data arrays.

Here it is ~twice as fast as the linear search (3792ms linear and 1739ms BM for 1000 iterations),

Martin
Attachments
BoyerMoore.fcfx
(28.86 KiB) Downloaded 21 times

chipfryer27
Valued Contributor
Posts: 2022
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 444 times
Been thanked: 665 times

Re: String Search. The Boyer Moore algorithm

Post by chipfryer27 »

Hi Martin

Is it raining outside? I ask as you really should get out more <s>

Joking apart, this is a very handy example. Much appreciated.

Regards

Post Reply