Anda di halaman 1dari 3

08/08/13

string search - Boyer Moore Algorithm Understanding and Example? - Stack Overflow

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Tell me more

Boyer Moore Algorithm Understanding and Example?

I am facing issues in understanding Boyer Moore String Search algorithm. I am following the following document. Link I am not able to work out my way as to exactly what is the real meaning of delta1 and delta2 here, and how are they applying this to find string search algorithm. Language looked little vague.. Kindly if anybody out there can help me out in understanding this, it would be really helpful. Or, if you know of any other link or document available that is easy to understand, then please share. Thanks in advance.
algorithm string-search

edited Jun 1 '11 at 21:54 PengOne 28.3k 6 54 94

asked Jun 1 '11 at 21:16 AGeek 1,485 5 30 53

Is the Wikipedia article any help? Oli Charlesworth Jun 1 '11 at 21:19 No, wikipidea is also not helpful. I tried reading it.. but it is of no help... Please if anyone can help, its urgent... AGeek Jun 1 '11 at 21:38 @AGeek: Well, which stage of the example on the Wikipedia page do you not follow? Oli Charlesworth Jun 1 '11 at 21:43 @Oli - thanks. I understood the first para... I am not able to understand First table and second table creation, and how are they helping to search the existence of pattern.. :) AGeek Jun 1 '11 at 21:52 Cannn anybodyyy helpp.. Please...... AGeek Jun 1 '11 at 22:07 show 2 more comments

4 Answers
First piece of advice, take a deep breath. You're clearly stressed, and when you're stressed the first thing that happens is that large chunks of your brain shut down. This makes understanding hard, which increases stress, and you've got a problem. A 5 minute timeout to improve your headspace may seem impossible to take, but can be surprisingly helpful. Now that said, the algorithm is based on a simple principle. Suppose that I'm trying to match a substring of length m. I'm going to first look at character m. If that character is not in my string, I know that the substring I want can't start in characters 1 ,2 ,. . . , m. If that character is in my string, I'll assume that it is at the last place in my string that it can be. I'll then jump back and start trying to match my string from that possible starting place. This piece of information is my first table. Once I start matching from the beginning of the substring, when I find a mismatch, I can't just start from scratch. I could be partially through a match starting at a different point. For instance if I'm trying to match a n a n d in a n a n a n d successfully match, a n a n, realize that the following a is not a d, but I've just matched a n, and so I should jump back to trying to match my third character in my substring. This, "If I fail after matching x characters, I could be on the y'th character of a match" information is stored in the second table. Note that when I fail to match the second table knows how far along in a match I might be based on what I just matched. The first table knows how far back I might be based on the character that I just saw which I failed to match. You want to use the more pessimistic of those two pieces of information. With this in mind the algorithm works like this:

stackoverflow.com/questions/6207819/boyer-moore-algorithm-understanding-and-example

1/3

08/08/13

string search - Boyer Moore Algorithm Understanding and Example? - Stack Overflow
s t a r ta tb e g i n n i n go fs t r i n g s t a r ta tb e g i n n i n go fm a t c h w h i l en o ta tt h ee n do ft h es t r i n g : i fm a t c h _ p o s i t i o ni s0 : J u m pa h e a dmc h a r a c t e r s L o o ka tc h a r a c t e r ,j u m pb a c kb a s e do nt a b l e1 I fm a t c ht h ef i r s tc h a r a c t e r : a d v a n c em a t c hp o s i t i o n a d v a n c es t r i n gp o s i t i o n e l s ei fIm a t c h : i fIr e a c h e dt h ee n do ft h em a t c h : F O U N DM A T C H-r e t u r n e l s e : a d v a n c es t r i n gp o s i t i o na n dm a t c hp o s i t i o n . e l s e : p o s 1=t a b l e 1 [c h a r a c t e rIf a i l e dt om a t c h] p o s 2=t a b l e 2 [h o wf a ri n t ot h em a t c hIa m] i fp o s 1<p o s 2 : j u m pb a c kp o s 1i ns t r i n g s e tm a t c hp o s i t i o na tb e g i n n i n g e l s e : s e tm a t c hp o s i t i o nt op o s 2 F A I L E DT OM A T C H
answered Jun 1 '11 at 23:36 btilly 13.4k 1 13 25

This looked an awesome text and explanation.. but due to more text m lil' confused... I am trying to understand it now... Anyway Thanks. I will let you knw when i am done... AGeek Jun 5 '11 at 11:54 can you help me on wiki program... creation of first table... what is he actually trying to do..... AGeek Jun 11 '11 at 7:13 en.wikipedia.org/wiki/Boyer_moore >>> the first table creation,,,, i read the code also, but cudn't get the idea as to how to create the first table AGeek Jun 11 '11 at 7:13

I think it is asked about the creation of tables (jump tables), which is not shown by your program. Downvote for you :( It is obvious that in case of a mismatch the jump should be the max of table1 and table2. But creation of the tables is important. kingsmasher1 Jan 8 '12 at 8:16

add comment

The insight behind Boyer-Moore is that if you start searching for a pattern in a string starting with the last character in the pattern, you can jump your search forward multiple characters when you hit a mismatch. Let's say our pattern p is the sequence of characters p 1, p 2, ..., p n and we are searching a string s, currently with p aligned so that p n is at index i in s. E.g.: s=W H I C HF I N A L L YH A L T S . A TT H A TP O I N T . . . p=A TT H A T i= ^ The B-M paper makes the following observations: (1) if we try matching a character that is not in p then we can jump forward n characters: 'F' is not in p, hence we advance n characters: s=W H I C HF I N A L L YH A L T S . A TT H A TP O I N T . . . p= A TT H A T i= ^ (2) if we try matching a character whose last position is k from the end of p then we can jump forward k characters: ' 's last position in p is 4 from the end, hence we advance 4 characters: s=W H I C HF I N A L L YH A L T S . A TT H A TP O I N T . . . p= A TT H A T i= ^ Now we scan backwards from i until we either succeed or we hit a mismatch. (3a) if the mismatch stackoverflow.com/questions/6207819/boyer-moore-algorithm-understanding-and-example

2/3

08/08/13

string search - Boyer Moore Algorithm Understanding and Example? - Stack Overflow
Now we scan backwards from i until we either succeed or we hit a mismatch. (3a) if the mismatch occurs k characters from the start of p and the mismatched character is not in p, then we can advance (at least) k characters. 'L' is not in p and the mismatch occurred against p 6, hence we can advance (at least) 6 characters: s=W H I C HF I N A L L YH A L T S . A TT H A TP O I N T . . . p= A TT H A T i= ^ However, we can actually do better than this. (3b) since we know that at the old i we'd already matched some characters (1 in this case). If the matched characters don't match the start of p, then we can actually jump forward a little more (this extra distance is called 'delta2' in the paper): s=W H I C HF I N A L L YH A L T S . A TT H A TP O I N T . . . p= A TT H A T i= ^ At this point, observation (2) applies again, giving s=W H I C HF I N A L L YH A L T S . A TT H A TP O I N T . . . p= A TT H A T i= ^ and bingo! We're done.
edited Nov 7 '12 at 18:39 Framester 2,518 2 25 73 answered Jun 2 '11 at 2:25 Rafe 2,746 1 7 10

As clearly demonstrated in this answer, an elaborate example is by far the best and simplest way to convey the gist of any reasonably sophisticated algorithm. user698585 Apr 5 '12 at 8:46

add comment

What about the web site of the co-inventor of this algorithm -- does this help? http://www.cs.utexas.edu/users/moore/best-ideas/string-searching/index.html cheers!
answered Jun 1 '11 at 22:41 Matthias 713 3 14 add comment

I have found this link, this explains the algorithm in the most basic way. Hope this helps. http://www.blackbeltcoder.com/Articles/algorithms/fast-text-search-with-boyer-moore
answered Mar 11 '12 at 20:11 dharam 863 1 7 28 add comment

Not the answer you're looking for? Browse other questions tagged algorithm
string-search or ask your own question.

stackoverflow.com/questions/6207819/boyer-moore-algorithm-understanding-and-example

3/3

Anda mungkin juga menyukai