Skip to content

Commit

Permalink
Implemented the Knuth Morris Pattern Matching Algorithm. (#2702)
Browse files Browse the repository at this point in the history
  • Loading branch information
srimani-programmer authored May 30, 2020
1 parent 6ce77da commit 3751213
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions Knuth_Morris_Pratt_Algorithm/KMP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

// Function to perform the LPS (Longest Proper Prefix Suffix)
function ComputeLPS(string $pattern, array &$lps) {

$len = 0;
$i = 1;
$M = strlen($pattern);
$lps[0] = 0;

while ($i < $M) {
if ($pattern[$i] == $pattern[$len]) {
$len++;
$lps[$i] = $len;
$i++;
} else {
if ($len != 0) {
$len = $lps[$len - 1];
} else {
$lps[$i] = 0;
$i++;
}
}
}
}

function KMPStringMatching(string $str, string $pattern): array {

$matches = [];
$M = strlen($pattern);
$N = strlen($str);
$i = $j = 0;
$lps = [];

ComputeLPS($pattern, $lps);

while ($i < $N) {

if ($pattern[$j] == $str[$i]) {
$j++;
$i++;
}

if ($j == $M) {
array_push($matches, $i - $j);
$j = $lps[$j - 1];
} else if ($i < $N && $pattern[$j] != $str[$i]) {
if ($j != 0)
$j = $lps[$j - 1];
else
$i = $i + 1;
}
}

return $matches;
}

$txt = "AABAACAADAABABBBAABAA";
$pattern = "AABA";
// Performing the Pattern Matching Operation.
$matches = KMPStringMatching($txt, $pattern);

if ($matches) {
foreach ($matches as $pos) {
echo "Pattern found at index : " . $pos . "\n";
}
}

/*
Complexity for the Algorithm : O(N + M)
Where O(M) is for Computing LPS
O(N) is for Computing KMP Algorithm.
*/

/*
INPUT:
$txt = "AABAACAADAABABBBAABAA";
$pattern = "AABA";
OUTPUT:
Pattern found at index : 0
Pattern found at index : 9
Pattern found at index : 16
*/
?>

0 comments on commit 3751213

Please sign in to comment.