# Types of Algorithm Laws

Edited by TheGuyLoveNY, Jen Moreau

**5 Parts:**Algorithm Laws Knuth-Morris-Pratt Algorithm: This article is part of a series on 'Design and Algorithm Analysis'. Read the full series here: Referencing this Article Comments

## Algorithm Laws

### Rabin Karp Algorithm:

Rabin-Karp Algorithm is a searching algorithm used specifically to search strings. Rabin-Karp is also known as Karp-Rabin algorithm. This algorithm is a string searching algorithm which was created by Richard M. Karp and Michael O. Rabin, Since it was created by these two scientists, It was named after the creator. Hence, Rabin-Karp algorithm. This Algorithm uses Hash data structure to find the string pattern.

**Performance:**

- Average case : O(n + m)
- Best Case : O(n + m)
- Worst case : O(m)

Where,
The length of the text is **n**.
The pattern of the test is **p**.
Combined length of these texts is**m**.

**For example:**

Text = abcdef Pattern = abc

h(abc) = x, Formula. Where, a = 1, b = 2, c = 3, d = 4, e = 5, f = 6

- h(abc) = 1+2 x 31 + 3 x 32

= 1 + 6 + 27 = 34 Therefore , h(abc) = x = 34

- h(bcd) = 2 + 3 x 31+ 4 x 32

= 2 + 9 + 36 = 47 Therefore, h(bcd) = 47

- h(cde) = 3 + 4 x 31 + 5 x 32

= 3 + 12 + 45 x = 60

- h(def) = 4 + 5 x 31+ 6 x 32

= 4 + 15 + 54 x = 73

**Example 2:**

Text = abdadaab Patter = ab

h(a,b) = x = 7

Where, a = 1, b = 2, d = 3, a = 4, d = 5, a = 6, a = 7, b = 8

h(a,b) = x = 7.

- h(ab) = 1 + 2 x 31

= 1+6 = 7

- h(bd) = 2 + 4 x 31

= 2+12 = 14

- h(da) = 4 + 31

= 7.

- h(ad) = 1 + 4 x 31

= 7

- h(aa) = 1+3

= 4

- h(ab) = 1 + 6

= 7 (same).

Since, h(ab) = 7, The pattern ab is found in the text.

## Knuth-Morris-Pratt Algorithm:

The Knuth-Morris-Pratt Algorithm also called KMP algorithm, is a string search algorithm. KMP algorithm searches a substring in a given string. Similar to a Rabin-Karp algorithm, This algorithm searches for a pattern in a given string. As the name implies, This algorithm was invented by three computer scientists namely Donald **Knuth**, Vaughan **Pratt**, James H. **Morris** in the year 1970 but was officially published in 1977.

**Example: Search the pattern in the given string using KMP Algorithm.**

Text : "abcxdabxabcdabcdabcy" Search pattern : "abcdabcy"

Here, We cannot directly compare the pattern with the text. As KMP algorithm says we compare half of the pattern with prefix and suffix.

Start with first pattern (Prefix) and then with the last pattern(Suffix).

Now, We do the same for other halves.

Hence the pattern is finally matched :

## This article is part of a series on 'Design and Algorithm Analysis'. Read the full series here:

1) Design and Algorithm Analysis Overview

4) Algorithm Graph in Data Structure

5) Types and Styles of Algorithm Searchs

6) **Algorithm_Laws**

7) Design and Algorythm of Dynamic Programming

## Referencing this Article

If you need to reference this article in your work, you can copy-paste the following depending on your required format:

__APA (American Psychological Association)__

Types of Algorithm Laws. (2017). In *ScienceAid*. Retrieved Jul 9, 2020, from https://scienceaid.net/Algorithm_Laws

** MLA (Modern Language Association)**
"Types of Algorithm Laws."

*ScienceAid*, scienceaid.net/Algorithm_Laws Accessed 9 Jul 2020.

** Chicago / Turabian**
ScienceAid.net. "Types of Algorithm Laws." Accessed Jul 9, 2020. https://scienceaid.net/Algorithm_Laws.

If you have problems with any of the steps in this article, please ask a question for more help, or post in the comments section below.

## Comments

## Article Info

Categories : Design and Algorithm Analysis

Recent edits by: TheGuyLoveNY