Free Essay

Knuth Morris Pratt Algorithm

In:

Submitted By navdeep49
Words 2243
Pages 9
PKnuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm
Kranthi Kumar Mandumula

December 18, 2011

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

outline

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

Definition History Components of KMP Algorithm Example Run-Time Analysis Advantages and Disadvantages References

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Definition:

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

Best known for linear time for exact matching. Compares from left to right. Shifts more than one position. Preprocessing approach of Pattern to avoid trivial comparisions. Avoids recomputing matches.

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

History:

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

This algorithm was conceived by Donald Knuth and Vaughan Pratt and independently by James H.Morris in 1977.

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

History:

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

Knuth, Morris and Pratt discovered first linear time string-matching algorithm by analysis of the naive algorithm. It keeps the information that naive approach wasted gathered during the scan of the text. By avoiding this waste of information, it achieves a running time of O(m + n). The implementation of Knuth-Morris-Pratt algorithm is efficient because it minimizes the total number of comparisons of the pattern against the input string.

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Components of KMP:

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

The prefix-function : It preprocesses the pattern to find matches of prefixes of the pattern with the pattern itself. It is defined as the size of the largest prefix of P[0..j − 1] that is also a suffix of P[1..j]. It also indicates how much of the last comparison can be reused if it fails. It enables avoiding backtracking on the string ‘S’.

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

m ← length[p] a[1] ← 0 k ←0 for q ← 2 to m do while k > 0 and p[k + 1] k ← a[k ] end while if p[k + 1] = p[q] then k ←k +1 end if a[q] ← k end for return Here a =

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

p[q] do

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Computation of Prefix-function with example: Knuth-Morris-Pratt
Algorithm Kranthi Kumar Mandumula

Let us consider an example of how to compute for the pattern ‘p’. Pattern a b a b a c a

I n i t i a l l y : m = length [ p]= 7 [1]= 0 k=0 where m, [1], and k are the length of the pattern, prefix function and initial potential value respectively.

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Knuth-Morris-Pratt Algorithm

Step 1 : q = 2 , k = 0 [2]= 0 q p 1 a 0 2 b 0 3 a 4 b 5 a 6 c 7 a

Kranthi Kumar Mandumula

Step 2 : q = 3 , k = 0 [3]= 1 q p 1 a 0 2 b 0 3 a 1 4 b 5 a 6 c 7 a

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Knuth-Morris-Pratt Algorithm

Step 3 : q = 4 , k = 1 [4]= 2 q p 1 a 0 2 b 0 3 a 1 4 b 2 5 a 6 c 7 a

Kranthi Kumar Mandumula

Step 4 : q = 5 , k = 2 [5]= 3 q p 1 a 0 2 b 0 3 a 1 4 b 2 5 a 3 6 c 7 a

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Knuth-Morris-Pratt Algorithm

Step 5 : q = 6 , k = 3 [6]= 1 q p 1 a 0 2 b 0 3 a 1 4 b 2 5 a 3 6 c 1 7 a

Kranthi Kumar Mandumula

Step 6 : q = 7 , k = 1 [7]= 1 q p 1 a 0 2 b 0 3 a 1 4 b 2 5 a 3 6 c 1 7 a 1

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

A f t e r i t e r a t i n g 6 times , t h e p r e f i x f u n c t i o n computations i s complete : q p 1 a 0 2 b 0 3 A 1 4 b 2 5 a 3 6 c 1 7 a 1

The running time of the prefix function is O(m).

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Algorithm

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

Step 1 : I n i t i a l i z e t h e i n p u t v a r i a b l e s : n = Length o f t h e Text . m = Length o f t h e P a t t e r n . = Prefix −f u n c t i o n of pattern ( p ) . q = Number o f c h a r a c t e r s matched . Step 2 : D e f i n e t h e v a r i a b l e : q=0 , t h e b e g i n n i n g o f t h e match . Step 3 : Compare t h e f i r s t c h a r a c t e r o f t h e p a t t e r n w i t h f i r s t c h a r a c t e r o f text . I f match i s n o t found , s u b s t i t u t e t h e v a l u e o f [ q ] to q . I f match i s found , then i n c r e m e n t t h e v a l u e o f q by 1 . Step 4 : Check whether a l l t h e p a t t e r n elements are matched w i t h t h e t e x t elements . I f not , r e p e a t t h e search process . I f yes , p r i n t t h e number o f s h i f t s taken by t h e p a t t e r n . Step 5 : l o o k f o r t h e n e x t match .

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

n ← length[S] m ← length[p] a ← Compute Prefix function q←0 for i ← 1 to n do while q > 0 and p[q + 1] S[i] do q ← a[q] if p[q + 1] = S[i] then q ←q+1 end if if q == m then q ← a[q] end if end while end for Here a =
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

Example of KMP algorithm:

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

Now let us consider an example so that the algorithm can be clearly understood. String b a c b a b a b a b a c a a b

Pattern a b a b a c a Let us execute the KMP algorithm to find whether ‘p’ occurs in ‘S’.

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

I n i t i a l l y : n = size of S = 15; m = s i z e o f p=7 Step 1 : i = 1 , q = 0 comparing p [ 1 ] w i t h S [ 1 ]

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

String b a c b a b a b a b a c a a b

Pattern a b a b a c a
P[1] does not match with S[1]. ‘p’ will be shifted one position to the right. Step 2 : i = 2 , q = 0 comparing p [ 1 ] w i t h S [ 2 ]

String b a c b a b a b a b a c a a b

Pattern a b a b a c a

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Knuth-Morris-Pratt Algorithm Step 3 : i = 3 , q = 1 comparing p [ 2 ] w i t h S [ 3 ] p [ 2 ] does n o t match w i t h S [ 3 ] Kranthi Kumar Mandumula

String b a c b a b a b a b a c a a b

Pattern a b a b a c a
B a c k t r a c k i n g on p , comparing p [ 1 ] and S [ 3 ] Step 4 : i = 4 , q = 0 comparing p [ 1 ] w i t h S [ 4 ] p [ 1 ] does n o t match w i t h S [ 4 ]

String b a c b a b a b a b a c a a b

Pattern a b a b a c a

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Knuth-Morris-Pratt Algorithm Step 5 : i = 5 , q = 0 comparing p [ 1 ] w i t h S [ 5 ] Kranthi Kumar Mandumula

String b a c b a b a b a b a c a a b

Pattern a b a b a c a
Step 6 : i = 6 , q = 1 comparing p [ 2 ] w i t h S [ 6 ]

p [ 2 ] matches w i t h S [ 6 ]

String b a c b a b a b a b a c a a b

Pattern a b a b a c a

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Knuth-Morris-Pratt Algorithm Step 7 : i = 7 , q = 2 comparing p [ 3 ] w i t h S [ 7 ] p [ 3 ] matches w i t h S [ 7 ] Kranthi Kumar Mandumula

String b a c b a b a b a b a c a a b

Pattern a b a b a c a
Step 8 : i = 8 , q = 3 comparing p [ 4 ] w i t h S [ 8 ]

p [ 4 ] matches w i t h S [ 8 ]

String b a c b a b a b a b a c a a b

Pattern a b a b a c a

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Step 9 : i = 9 , q = 4 comparing p [ 5 ] w i t h S [ 9 ]

Knuth-Morris-Pratt Algorithm p [ 5 ] matches w i t h S [ 9 ] Kranthi Kumar Mandumula

String b a c b a b a b a b a c a a b

Pattern a b a b a c a
Step 1 0 : i = 10 , q = 5 comparing p [ 6 ] w i t h S [ 1 0 ]

p [ 6 ] doesn ’ t matches w i t h S [ 1 0 ]

String b a c b a b a b a b a c a a b

Pattern a b a b a c a
B a c k t r a c k i n g on p , comparing p [ 4 ] w i t h S [ 1 0 ] because a f t e r mismatch q = [5] = 3

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Knuth-Morris-Pratt Algorithm Step 1 1 : i = 11 , q = 4 comparing p [ 5 ] w i t h S [ 1 1 ] Kranthi Kumar Mandumula

String b a c b a b a b a b a c a a b

Pattern a b a b a c a
Step 1 2 : i = 12 , q = 5 comparing p [ 6 ] w i t h S [ 1 2 ]

p [ 6 ] matches w i t h S [ 1 2 ]

String b a c b a b a b a b a c a a b

Pattern a b a b a c a

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Step 1 3 : i = 13 , q = 6 comparing p [ 7 ] w i t h S [ 1 3 ]

p [ 7 ] matches w i t h S [ 1 3 ]

String b a c b a b a b a b a c a a b

Pattern a b a b a c a

pattern ‘p’ has been found to completely occur in string ‘S’. The total number of shifts that took place for the match to be found are: i − m = 13-7 = 6 shifts.

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Run-Time analysis:

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

O(m) - It is to compute the prefix function values. O(n) - It is to compare the pattern to the text. Total of O(n + m) run time.

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Advantages and Disadvantages:

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

Advantages: The running time of the KMP algorithm is optimal (O(m + n)), which is very fast. The algorithm never needs to move backwards in the input text T. It makes the algorithm good for processing very large files.

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Advantages and Disadvantages:

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

Disadvantages: Doesn’t work so well as the size of the alphabets increases. By which more chances of mismatch occurs.

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula

Graham A.Stephen, “String Searching Algorithms”, year = 1994. Donald Knuth, James H. Morris, Jr, Vaughan Pratt, “Fast pattern matching in strings”, year = 1977. Thomas H.Cormen; Charles E.Leiserson., Introduction to algorithms second edition , “The Knuth-Morris-Pratt Algorithm”, year = 2001.

Kranthi Kumar Mandumula

Knuth-Morris-Pratt Algorithm

Similar Documents

Free Essay

Nit-Silchar B.Tech Syllabus

...NATIONAL INSTITUTE OF TECHNOLOGY SILCHAR Bachelor of Technology Programmes amï´>r¶ JH$s g§ñWmZ, m¡Úmo{ à VO o pñ Vw dZ m dY r V ‘ ñ Syllabi and Regulations for Undergraduate PROGRAMME OF STUDY (wef 2012 entry batch) Ma {gb Course Structure for B.Tech (4years, 8 Semester Course) Civil Engineering ( to be applicable from 2012 entry batch onwards) Course No CH-1101 /PH-1101 EE-1101 MA-1101 CE-1101 HS-1101 CH-1111 /PH-1111 ME-1111 Course Name Semester-1 Chemistry/Physics Basic Electrical Engineering Mathematics-I Engineering Graphics Communication Skills Chemistry/Physics Laboratory Workshop Physical Training-I NCC/NSO/NSS L 3 3 3 1 3 0 0 0 0 13 T 1 0 1 0 0 0 0 0 0 2 1 1 1 1 0 0 0 0 4 1 1 0 0 0 0 0 0 2 0 0 0 0 P 0 0 0 3 0 2 3 2 2 8 0 0 0 0 0 2 2 2 2 0 0 0 0 0 2 2 2 6 0 0 8 2 C 8 6 8 5 6 2 3 0 0 38 8 8 8 8 6 2 0 0 40 8 8 6 6 6 2 2 2 40 6 6 8 2 Course No EC-1101 CS-1101 MA-1102 ME-1101 PH-1101/ CH-1101 CS-1111 EE-1111 PH-1111/ CH-1111 Course Name Semester-2 Basic Electronics Introduction to Computing Mathematics-II Engineering Mechanics Physics/Chemistry Computing Laboratory Electrical Science Laboratory Physics/Chemistry Laboratory Physical Training –II NCC/NSO/NSS Semester-4 Structural Analysis-I Hydraulics Environmental Engg-I Structural Design-I Managerial Economics Engg. Geology Laboratory Hydraulics Laboratory Physical Training-IV NCC/NSO/NSS Semester-6 Structural Design-II Structural Analysis-III Foundation Engineering Transportation Engineering-II Hydrology &Flood...

Words: 126345 - Pages: 506

Free Essay

Advanced Algorithms

...Approximation Algorithms Springer Berlin Heidelberg NewYork Barcelona Hong Kong London Milan Paris Singapore Tokyo To my parents Preface Although this may seem a paradox, all exact science is dominated by the idea of approximation. Bertrand Russell (1872–1970) Most natural optimization problems, including those arising in important application areas, are NP-hard. Therefore, under the widely believed conjecture that P = NP, their exact solution is prohibitively time consuming. Charting the landscape of approximability of these problems, via polynomial time algorithms, therefore becomes a compelling subject of scientific inquiry in computer science and mathematics. This book presents the theory of approximation algorithms as it stands today. It is reasonable to expect the picture to change with time. The book is divided into three parts. In Part I we cover a combinatorial algorithms for a number of important problems, using a wide variety of algorithm design techniques. The latter may give Part I a non-cohesive appearance. However, this is to be expected – nature is very rich, and we cannot expect a few tricks to help solve the diverse collection of NP-hard problems. Indeed, in this part, we have purposely refrained from tightly categorizing algorithmic techniques so as not to trivialize matters. Instead, we have attempted to capture, as accurately as possible, the individual character of each problem, and point out connections between problems and algorithms for solving them...

Words: 140657 - Pages: 563

Free Essay

التشفير وأمنية المعلومات

...الفصل الأول التعريف بتقنيات التشفير وأمنية المعلومات 1-1:المقدمة ( Introduction ): إن أمنية المعلومات ناتجة من الحاجة إلى تناقل المعلومات الخاصة لكل من العبارات العسكرية والدبلوماسيـة. هذه الحاجة هي قديمة بقدم الحضارة نفسها. الأسبان القدماء مثلا, شفروا عباراتهم العسكرية. أما بالنسبة للصين, فانه يكفي فقط كتابة العبارات بلغتهم المعروفة والتي تعبر لغة خاصة, وذلك لان القليل من الناس يستطيعون قراءة الحروف الصينية. كانت قنوات الاتصال في السابق بسيطة جدا وكانت ترتب بأسلوب يعتمد في تامين السري على استخدام مراسلين موثوقين. تعتمد الأمنية لمثل هذا التنظيم على كل من موضع الثقة للمراسل وقابليته في أن يبقى محتفظا بالمواقف أو المواقع التي فيها يمكن أن تتعرض العبارات للانتهاك. بسبب اكتشاف أنظمة الحاسبات واستخدام شبكات الحاسبة الواسعة بين الدول, فان القرن العشرين قد غير بصورة ملحوظة مدى مفاهيم الحماية. في الحاسبات المبكرة ( الأولى), فان الأمنية الفيزيائية ومعها سياسة الاختيار الملائم للكادر العامل في الحاسبة كان كافيا لتامين الأمنية. لكن هذا أصبح غير كاف وغير مرن بعد اكتشاف أنظمة حاسبات المشاركة الزمنية (Time-Sharing) والتي تتألف من عدة محطات طرفية موزعة في مساحة جغرافية واسعة. من الجدير بالذكر أن امن وسلامة اتصال,ت الالكترونية في بدء ظهورها لم يكن هاما لان معظم المعلومات المخزونة فيها لم تكن ذات حساسية كبيرة, بعكس ماهي عليه اليوم, إذ كلما ازدادت وارتفعت قيمة المعلومات المخزونة في الحاسبات الالكترونية كلما ازدادت الرغبة لدى بعض الأفراد لمحاولة الوصول إليها من اجل التخريب أو من اجل الكسب غير المشروع بواسطة بيعها إلى الجهات الراغبة بذلك, لذا فقد أصبح امن هذه المعلومات على درجة...

Words: 35136 - Pages: 141

Free Essay

Test2

...62118 0/nm 1/n1 2/nm 3/nm 4/nm 5/nm 6/nm 7/nm 8/nm 9/nm 1990s 0th/pt 1st/p 1th/tc 2nd/p 2th/tc 3rd/p 3th/tc 4th/pt 5th/pt 6th/pt 7th/pt 8th/pt 9th/pt 0s/pt a A AA AAA Aachen/M aardvark/SM Aaren/M Aarhus/M Aarika/M Aaron/M AB aback abacus/SM abaft Abagael/M Abagail/M abalone/SM abandoner/M abandon/LGDRS abandonment/SM abase/LGDSR abasement/S abaser/M abashed/UY abashment/MS abash/SDLG abate/DSRLG abated/U abatement/MS abater/M abattoir/SM Abba/M Abbe/M abbé/S abbess/SM Abbey/M abbey/MS Abbie/M Abbi/M Abbot/M abbot/MS Abbott/M abbr abbrev abbreviated/UA abbreviates/A abbreviate/XDSNG abbreviating/A abbreviation/M Abbye/M Abby/M ABC/M Abdel/M abdicate/NGDSX abdication/M abdomen/SM abdominal/YS abduct/DGS abduction/SM abductor/SM Abdul/M ab/DY abeam Abelard/M Abel/M Abelson/M Abe/M Aberdeen/M Abernathy/M aberrant/YS aberrational aberration/SM abet/S abetted abetting abettor/SM Abeu/M abeyance/MS abeyant Abey/M abhorred abhorrence/MS abhorrent/Y abhorrer/M abhorring abhor/S abidance/MS abide/JGSR abider/M abiding/Y Abidjan/M Abie/M Abigael/M Abigail/M Abigale/M Abilene/M ability/IMES abjection/MS abjectness/SM abject/SGPDY abjuration/SM abjuratory abjurer/M abjure/ZGSRD ablate/VGNSDX ablation/M ablative/SY ablaze abler/E ables/E ablest able/U abloom ablution/MS Ab/M ABM/S abnegate/NGSDX abnegation/M Abner/M abnormality/SM abnormal/SY aboard ...

Words: 113589 - Pages: 455