0% found this document useful (0 votes)
10 views5 pages

Imp Question

Uploaded by

suyashsingh709
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Imp Question

Uploaded by

suyashsingh709
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Question: Explain KMP matcher and also implement it by an algorithm, where

P=a,a,b,a,b,b,a and T=b,a,b,a,a,b,a,b,b,

Answer : The Knuth-Morris-Pratt (KMP) algorithm is an efficient string matching algorithm


that searches for occurrences of a pattern PPP in a text TTT. It improves on the naive approach
by avoiding unnecessary re-examination of characters that have already been matched. The
algorithm uses a partial match table (or "prefix table") to determine the next position to start
matching from, rather than starting over at the next character in the text.

Key Concepts of KMP:

1. Prefix Table (also known as the "LPS" Array - Longest Prefix Suffix Array):
o The prefix table helps track the longest prefix of the pattern PPP that is also a
suffix up to each position in PPP.
o This table allows us to avoid unnecessary re-evaluations by knowing where we
can safely restart matching when a mismatch occurs.
2. KMP Matching Process:
o Compute the prefix table for the pattern PPP.
o Start matching PPP with the text TTT. Whenever a mismatch occurs, use the
prefix table to shift the pattern efficiently without re-checking the previously
matched characters.

Steps of the KMP Algorithm:

1. Preprocessing (Build the prefix table):


o For the pattern PPP, create an array lps (Longest Prefix Suffix).
o For each character P[i]P[i]P[i], determine the longest proper prefix which is also a
suffix.
2. Matching Process:
o Iterate through the text TTT while matching the pattern PPP using the lps array
to avoid redundant comparisons.

Pattern PPP: a, a, b, a, b, b, a

Text TTT: b, a, b, a, a, b, a, b, b

KMP Algorithm Implementation:


python
Copy code
def compute_lps(pattern):
# This function computes the LPS (Longest Prefix Suffix) array
m = len(pattern)
lps = [0] * m # LPS array initialized with 0's
length = 0 # length of the previous longest prefix suffix
i = 1 # starting from the second character
while i < m:
if pattern[i] == pattern[length]:
length += 1
lps[i] = length
i += 1
else:
if length != 0:
length = lps[length - 1] # try the previous potential prefix
length
else:
lps[i] = 0
i += 1
return lps

def kmp_search(text, pattern):


n = len(text)
m = len(pattern)

# Compute LPS array for the pattern


lps = compute_lps(pattern)

i = 0 # index for text


j = 0 # index for pattern
matches = []

while i < n:
if pattern[j] == text[i]:
i += 1
j += 1

if j == m: # Pattern found
matches.append(i - j)
j = lps[j - 1] # Use the LPS array to skip redundant comparisons
elif i < n and pattern[j] != text[i]:
if j != 0:
j = lps[j - 1]
else:
i += 1
return matches

# Example usage
text = ['b', 'a', 'b', 'a', 'a', 'b', 'a', 'b', 'b']
pattern = ['a', 'a', 'b', 'a', 'b', 'b', 'a']

matches = kmp_search(text, pattern)


print("Pattern found at indices:", matches)

Explanation of the Code:

1. compute_lps: This function calculates the Longest Prefix Suffix (LPS) array for the
given pattern. This array is used during the matching process to skip unnecessary
comparisons.
2. kmp_search: This function performs the actual search by iterating through the text and
using the LPS array to efficiently find all occurrences of the pattern.
Example Walkthrough:

Given the pattern P = ['a', 'a', 'b', 'a', 'b', 'b', 'a'] and text T = ['b', 'a',
'b', 'a', 'a', 'b', 'a', 'b', 'b']:

1. The LPS array for the pattern is computed as:


o LPS = [0, 1, 0, 1, 2, 3, 1]
2. The algorithm starts comparing the pattern against the text:
o Mismatch occurs at various points, but the algorithm uses the LPS array to skip
unnecessary comparisons.
3. The final result is the list of indices where the pattern matches the text:
o In this case, it finds a match starting at index 3.

Output:
less
Copy code
Pattern found at indices: [3]

The pattern P starts at index 3 in the text T. The KMP algorithm ensures that we don't re-check
the same characters unnecessarily, providing a more efficient search than the brute force
approach.

Question: Explain P, NP, NP Hard and NP Complete Classes with example.

Answer: The terms P, NP, NP-Hard, and NP-Complete are fundamental concepts in
computational complexity theory, which is a branch of computer science that studies the
resources required to solve problems. These terms help categorize problems based on their
computational difficulty and how they can be solved. Below is an explanation of each class with
examples.

1. Class P (Polynomial Time)

 Definition: A problem belongs to P if it can be solved by an algorithm that runs in


polynomial time. In simpler terms, the problem can be solved in a time that grows at
most as a polynomial function of the size of the input.
 Time Complexity: If the size of the input is nnn, the algorithm's running time is
O(nk)O(n^k)O(nk), where kkk is a constant.
 Example:
o Sorting an array: The problem of sorting an array of nnn numbers can be solved
in O(nlog⁡n)O(n \log n)O(nlogn) time using algorithms like Merge Sort or
Quick Sort.
o Finding the greatest common divisor (GCD): The Euclidean algorithm to
compute the GCD of two numbers has a time complexity of
O(log⁡min⁡(a,b))O(\log \min(a, b))O(logmin(a,b)).
2. Class NP (Non-deterministic Polynomial Time)

 Definition: A problem belongs to NP if a proposed solution can be verified in


polynomial time, even if finding the solution might take longer. Essentially, if you are
given a candidate solution, you can check whether it is correct in polynomial time.
 Time Complexity: The verification of a solution takes polynomial time, but the time to
find the solution is not guaranteed to be polynomial.
 Example:
o Subset Sum Problem: Given a set of integers, does there exist a subset whose
sum is equal to a given target number? While finding the subset may take
exponential time (since there are 2n2^n2n subsets), if you are given a subset, you
can easily verify whether its sum is correct in polynomial time.

3. Class NP-Hard

 Definition: A problem is NP-Hard if it is at least as hard as the hardest problems in NP.


That is, every problem in NP can be reduced to an NP-Hard problem in polynomial time.
However, NP-Hard problems are not necessarily in NP—they might not even have a
solution that can be verified in polynomial time.
 Time Complexity: NP-Hard problems are generally not solvable in polynomial time
(unless P = NP).
 Example:
o Travelling Salesman Problem (TSP): Given a set of cities and distances
between them, the goal is to find the shortest possible route that visits each city
once and returns to the starting city. Although verifying a given solution (a
specific route) can be done in polynomial time, finding the optimal route is NP-
Hard because it is difficult to compute.
o Halting Problem: Given a program and its input, does the program eventually
halt? This is an NP-Hard problem because it is at least as difficult as any problem
in NP, but it cannot be verified in polynomial time.

4. Class NP-Complete

 Definition: A problem is NP-Complete if it is both in NP and NP-Hard. In other words,


it is a problem in NP that is as hard as any other problem in NP. If any NP-Complete
problem can be solved in polynomial time, then all problems in NP can be solved in
polynomial time (i.e., P = NP).
 Time Complexity: NP-Complete problems are the most challenging problems in NP, and
no polynomial-time solution is known for any NP-Complete problem.
 Example:
o 3-SAT Problem: Given a Boolean formula in conjunctive normal form (CNF),
where each clause has exactly three literals, is there a way to assign truth values to
the variables such that the formula evaluates to true? This is an NP-Complete
problem.
o Knapsack Problem: Given a set of items, each with a weight and value, and a
knapsack with a weight limit, what is the maximum value you can carry without
exceeding the weight limit? The 0/1 Knapsack problem is NP-Complete.

Relationships Between the Classes:

1. P ⊆ NP: Every problem that can be solved in polynomial time (P) can also have its
solution verified in polynomial time (NP), so P is a subset of NP.
2. NP ⊆ NP-Hard: All problems in NP can be reduced to NP-Hard problems, but NP-Hard
problems may not belong to NP since they may not have a solution that can be verified in
polynomial time.
3. NP-Complete Problems: These problems are both in NP and NP-Hard. If you can solve
any NP-Complete problem in polynomial time, then all problems in NP can be solved in
polynomial time, implying P = NP.

Summary Table:

Class Definition Example


P Problems solvable in polynomial time. Sorting, GCD
Problems whose solutions can be verified in Subset Sum, Sudoku
NP
polynomial time. Verification
Problems that are at least as hard as the hardest Travelling Salesman Problem,
NP-Hard
problems in NP (may not be in NP). Halting Problem
NP-
Problems that are both in NP and NP-Hard. 3-SAT, Knapsack Problem
Complete

Open Question: P = NP?

The question of whether P = NP is one of the most important unsolved questions in computer
science. It asks whether every problem whose solution can be verified in polynomial time (NP)
can also be solved in polynomial time (P). If it turns out that P = NP, then all NP-Complete
problems could be solved efficiently (in polynomial time), which would have profound
implications in fields such as cryptography, optimization, and machine learning. However, no
proof has been found either way, and it remains an open problem.

You might also like