School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,096 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,035 people online right now. Registration is fast and FREE... Join Now!




Knuth-Morris-Pratt algorithm

 

Knuth-Morris-Pratt algorithm, proof of correctness

devraj0109

18 Sep, 2009 - 04:37 AM
Post #1

New D.I.C Head
*

Joined: 22 Jul, 2009
Posts: 12

Hello everyone,
I am wondering if anyone familiar with Knuth Morris Pratt algorithm for string matching could help me understand the proof of correctness (online links would be helpful too). I understand the algorithm but I just couldn't understand why it will not miss any sequences while shifting and how to prove it semi formally. For quick refresher here is link to algorithm -
http://www.cbcb.umd.edu/confcour/CMSC423-m...hing_basics.pdf

Thank You.

User is offlineProfile CardPM
+Quote Post


Neumann

RE: Knuth-Morris-Pratt Algorithm

9 Oct, 2009 - 11:18 AM
Post #2

I can judge a book by its cover
Group Icon

Joined: 8 Jul, 2009
Posts: 686



Thanked: 93 times
Dream Kudos: 225
My Contributions
The whole problem occurs when we get a mismatch. Otherwise the algorithm is just a regular sequential search. So, lets assume you started the search at index a and it failed at some indexb. Now, we have 2 choices:

1. Restart the search from index b + 1.
2. Restart the search from index k, where k is a < k <= b.

I assume it's obvious why these are the only 2 choices we have - if the word started somewhere at index i < a it would've been found already.

Now, how do we make a choice between those two? Well, if between a and b there is no letter that matches the first letter of our word, then there is no way in hell that we will get a match at that range. So we choose the first choice. Otherwise, if there is at least one such letter, we restart the search at its first occurrence.

I think it's pretty simple. Check out Wikipedia's article about it. Maybe it will be a bit clearer.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 11:52AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month