Question: Algorithm Help in Visual Studio using C#: I have the below code for a Knuth Morris Pratt Algorithm. I am unable to get the right

Algorithm Help in Visual Studio using C#:

I have the below code for a Knuth Morris Pratt Algorithm. I am unable to get the right results that gets printed to the console. Im having a hard time figuring out what is wrong with the code. I have pasted my results, my source code and how KMP works.

Program.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace KMP { public class Program { string s; string pattern; int[] f = new int[100];

public Program(string name) {

}

public void InputPattern() { Console.WriteLine("Enter the pattern you want to search: "); pattern = (Console.ReadLine()).ToUpper(); //read user input

}

public void RandomGenerator() { Random geneSequence = new Random(); // The random number sequence

for (int i = 0; i

switch (x) { case 1: s = s + 'C'; break;

case 2: s = s + 'T'; break;

case 3: s = s + 'A'; break;

case 4: s = s + 'G'; break; }

}

Console.WriteLine(s); //display results of string }

public void preKMP() { int m = pattern.Length; int k; f[0] = -1; for (int i = 1; i = 0) { if (pattern[k] == pattern[i - 1]) break; else k = f[k]; } f[i] = k + 1;

Console.WriteLine("Pattern found at:{0}", i);

Console.WriteLine(pattern); }

}

public int Search() // s is string sequence, pattern is what is inputted from user {

//kmp algorithm is executed here

int m = pattern.Length; int n = s.Length; int[] f = new int[m];

preKMP(); int i = 0; int k = 0;

while (i

return 0;

}

public void DisplayTime() { var watch = System.Diagnostics.Stopwatch.StartNew(); // the code that you want to measure comes here watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds;

Console.WriteLine("Time processed: {0}ms", watch.ElapsedMilliseconds); }

}

}

ProgramTest.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace KMP { public class ProgramTest { public static void Main(string[] args) { Program myProgram = new Program("Thank you for running the " + "Knuth Morris Pratt Algorithm");

myProgram.InputPattern(); //read in the inputted pattern from user

myProgram.RandomGenerator(); //run the random generator program

myProgram.preKMP(); myProgram.Search();

myProgram.DisplayTime();

Console.Read(); } } }Algorithm Help in Visual Studio using C#: I have the below code

This is code i received from online that can give another perception, but i wan to enter the code and not have the code already in the console.

// C# program for implementation of KMP pattern

// searching algorithm

using System;

class GFG {

void KMPSearch(string pat, string txt)

{

int M = pat.Length;

int N = txt.Length;

// create lps[] that will hold the longest

// prefix suffix values for pattern

int []lps = new int[M];

int j = 0; // index for pat[]

// Preprocess the pattern (calculate lps[]

// array)

computeLPSArray(pat,M,lps);

int i = 0; // index for txt[]

while (i

{

if (pat[j] == txt[i])

{

j++;

i++;

}

if (j == M)

{

Console.Write("Found pattern "+

"at index " + (i-j));

j = lps[j-1];

}

// mismatch after j matches

else if (i

{

// Do not match lps[0..lps[j-1]] characters,

// they will match anyway

if (j != 0)

j = lps[j-1];

else

i = i+1;

}

}

}

void computeLPSArray(string pat, int M, int []lps)

{

// length of the previous longest prefix suffix

int len = 0;

int i = 1;

lps[0] = 0; // lps[0] is always 0

// the loop calculates lps[i] for i = 1 to M-1

while (i

{

if (pat[i] == pat[len])

{

len++;

lps[i] = len;

i++;

}

else // (pat[i] != pat[len])

{

// This is tricky. Consider the example.

// AAACAAAA and i = 7. The idea is similar

// to search step.

if (len != 0)

{

len = lps[len-1];

// Also, note that we do not increment

// i here

}

else // if (len == 0)

{

lps[i] = len;

i++;

}

}

}

}

// Driver program to test above function

public static void Main()

{

string txt = "ABABDABACDABABCABAB";

string pat = "ABABCABAB";

new KMP_String_Matching().KMPSearch(pat,txt);

}

}

CAUsers\abir\Desktop\Wife's FolderKM PKMP\bin,Debug\KMP.exe Enter the pattern you want to search: TATACCCATCCTTCCAATAATCACTAACTACTTCATATCCAATCCATAAAATAACTATTCCCTAAAATTACCCAAATACTCATTCATAACTTCAAATACCACTCTTCCATCCTAATAAAC CTCCACATCATTCATAAATCTATATAOCAATTTTTTAC CCTCCACTACCATAAATTCTTCATCCACATCTCAATACAACATACTATCCCTACTCCACAACTTCATTACCCTCTAACTTTTAATATACATAAACCCATCACAAAAAAAACATATTCACO CACAACCTCTAATCATTCATAACTTTTCTAATATAAAACATCTCCTTCACACATTTTACCTCCTTCCCCATTACAACATCCTTTAAACATCCTACTCCAATCAATCCTTACACCCCATTA TACATCAAATTACTAATTAATCTCTTATACATAATTACCCCTACCCTACCTCTATTCATCTTCTAACCCCATCATACTCCACTTATAATCTAACAACCTCACAATACATTCAACCTCAAG ATACAAAAAAACCACATTCCCCTAATCTCCATTCTTCTATACATAAACCCCTATAAACCTATATCTAATTTATTCCCACAACCACCTACTTCTTTTTCTTCAATATACTAAACCTTTATA AAAAACCCCAAATAACTTCATTTCATCCCAAAAATCACATCACTCATCTTCAATTTTTACCCCCAAATCTCTATATCACACAATACACCCATCTTCACTCCACATACCAACTTACTCTTA ATTAATCCCAACCAACTACCAATTACATTAACAACTTCCATAACCCTAACACTCACCACAAAACAATTTTTTATTCACTATCACCTATCCCTCTAACATCTCCTTTTCAACCTCCTATCO ACCTCACCTAACACTTCTAACATTACCCTTATACTTATCTCCTCACCTATTTACCCTAACACAAATTAACAACCTACTTAATTAACTTACAAAAACCTCCTTAATTCATCACTACTACAT AAATCCCCCTTTCCCTATCTAAATTATAATTCTTTTTTTT Pattern found at:1 Pattern found at:2 Pattern found at:3 Pattern found at:4 Pattern found at:1 Pattern found at:2 Pattern found at:3 Pattern found at:4 CAUsers\abir\Desktop\Wife's FolderKM PKMP\bin,Debug\KMP.exe Enter the pattern you want to search: TATACCCATCCTTCCAATAATCACTAACTACTTCATATCCAATCCATAAAATAACTATTCCCTAAAATTACCCAAATACTCATTCATAACTTCAAATACCACTCTTCCATCCTAATAAAC CTCCACATCATTCATAAATCTATATAOCAATTTTTTAC CCTCCACTACCATAAATTCTTCATCCACATCTCAATACAACATACTATCCCTACTCCACAACTTCATTACCCTCTAACTTTTAATATACATAAACCCATCACAAAAAAAACATATTCACO CACAACCTCTAATCATTCATAACTTTTCTAATATAAAACATCTCCTTCACACATTTTACCTCCTTCCCCATTACAACATCCTTTAAACATCCTACTCCAATCAATCCTTACACCCCATTA TACATCAAATTACTAATTAATCTCTTATACATAATTACCCCTACCCTACCTCTATTCATCTTCTAACCCCATCATACTCCACTTATAATCTAACAACCTCACAATACATTCAACCTCAAG ATACAAAAAAACCACATTCCCCTAATCTCCATTCTTCTATACATAAACCCCTATAAACCTATATCTAATTTATTCCCACAACCACCTACTTCTTTTTCTTCAATATACTAAACCTTTATA AAAAACCCCAAATAACTTCATTTCATCCCAAAAATCACATCACTCATCTTCAATTTTTACCCCCAAATCTCTATATCACACAATACACCCATCTTCACTCCACATACCAACTTACTCTTA ATTAATCCCAACCAACTACCAATTACATTAACAACTTCCATAACCCTAACACTCACCACAAAACAATTTTTTATTCACTATCACCTATCCCTCTAACATCTCCTTTTCAACCTCCTATCO ACCTCACCTAACACTTCTAACATTACCCTTATACTTATCTCCTCACCTATTTACCCTAACACAAATTAACAACCTACTTAATTAACTTACAAAAACCTCCTTAATTCATCACTACTACAT AAATCCCCCTTTCCCTATCTAAATTATAATTCTTTTTTTT Pattern found at:1 Pattern found at:2 Pattern found at:3 Pattern found at:4 Pattern found at:1 Pattern found at:2 Pattern found at:3 Pattern found at:4

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!