Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Knuth Morris Pratt 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(); } } }

image text in transcribed

image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions