0% found this document useful (0 votes)
67 views

UNIT-V String Matching

The document discusses various string matching algorithms like naive, Rabin-Karp, and Knuth-Morris-Pratt algorithms. It explains how each algorithm works and analyzes their time complexities.

Uploaded by

Jaya krishna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

UNIT-V String Matching

The document discusses various string matching algorithms like naive, Rabin-Karp, and Knuth-Morris-Pratt algorithms. It explains how each algorithm works and analyzes their time complexities.

Uploaded by

Jaya krishna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

DESIGN AND ANALYSIS OF ALGORITHM

String Matching Algorithms

➢Introduction
➢Naïve String-Matching Algorithm
➢Rabin-Karp Algorithm
➢Knuth-Morris-Pratt Algorithm
➢Tries
➢Suffix Tries
➢NP-hard and NP-complete problems
➢Cook’s Theorem

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 1
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
Pattern Searching algorithms are used to find a pattern or
substring from another bigger string called text. There are different algorithms. The
main goal to design these types of Algorithms to reduce the time complexity.

Naive Pattern Searching

Naïve pattern searching is the simplest method among other pattern searching
algorithms. It is a straightforward (Brute Force) method. It checks for all character of
the main string to the pattern. This algorithm is helpful for smaller texts. It does not
need any pre-processing phases. We can find substring by checking once for the
string. It also does not occupy extra space to perform the operation.

The time complexity of Naïve Pattern Search method is O(m*n). The m is the size of
pattern and n is the size of the main string.

Naïve or Brute Force method:

• The Brute Force algorithm compares the pattern to the text, one character at a time,
until unmatching characters are found:
TWO ROADS DIVERGED IN A YELLOW WOOD
ROADS
TWO ROADS DIVERGED IN A YELLOW WOOD
ROADS
TWO ROADS DIVERGED IN A YELLOW WOOD
ROADS
TWO ROADS DIVERGED IN A YELLOW WOOD
R OADS
TWO ROADS DIVERGED IN A YELLOW WOOD
ROADS

• The algorithm can be designed to stop on either the first occurrence of the pattern, or
upon reaching the end of the text.

Algorithm:
begin
patlen = pattern length;
textlen = text length;
for i = 0 to (textlen - patlen) do
for j = 0 to patlen do
if(text[i + j] ≠ p[j]) then
break the loop;
if(j = = patlen) then
display the index i;
End.

Example:
tetththeheehthtehtheththehehtht
the

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 2
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
tetththeheehthtehtheththehehtht
the
tetththeheehthtehtheththehehtht
the
tetththeheehthtehtheththehehtht
the
tetththeheehthtehtheththehehtht
the
tetththeheehthtehtheththehehtht
the

Worst case:
• Given a pattern M characters in length, and a text N characters in length...
compares pattern to each substring of text of length M.
For example, M=5.
1) A A A A A A A A A A A A A A A A A A A A A A A A A A A H
AAAAH 5 comparisons made
2) A A A A A A A A A A A A A A A A A A A A A A A A A A A H
AAAAH 5 comparisons made
3) A A A A A A A A A A A A A A A A A A A A A A A A A A A H
AAAAH 5 comparisons made
4) A A A A A A A A A A A A A A A A A A A A A A A A A A A H
AAAAH 5 comparisons made
5) A A A A A A A A A A A A A A A A A A A A A A A A A A A H
AAAAH 5 comparisons made
6) ....
N) A A A A A A A A A A A A A A A A A A A A A A A A A A A H
5 comparisons made AAAAH
• Total number of comparisons: M (N-M+1)
• Worst case time complexity: Ο(MN)

Best case if pattern found:


• Given a pattern M characters in length, and a text N characters in length...
• Finds pattern in first M positions of text.
For example, M=5.
1) AAAAAAAAAAAAAAAAAAAAAAAAAAAH
AAAAA 5 comparisons made
• Total number of comparisons: M
• Best case time complexity: Ο(M)

Best case if pattern not found:


• Given a pattern M characters in length, and a text N characters in length...
• Always mismatch on first character.
For example, M=5.
1) AAAAAAAAAAAAAAAAAAAAAAAAAAAH
OOOOH 1 comparison made

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 3
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
2) AAAAAAAAAAAAAAAAAAAAAAAAAAAH
OOOOH 1 comparison made
3) AAAAAAAAAAAAAAAAAAAAAAAAAAAH
OOOOH 1 comparison made
4) AAAAAAAAAAAAAAAAAAAAAAAAAAAH
OOOOH 1 comparison made
5) AAAAAAAAAAAAAAAAAAAAAAAAAAAH
OOOOH 1 comparison made
6) ...
N) AAAAAAAAAAAAAAAAAAAAAAAAAAAH
1 comparison made OOOOH

• Total number of comparisons: N


• Best case time complexity: Ο(N)

Rabin-Karp Algorithm

1. The Rabin-Karp string searching algorithm calculates a hash value for the
pattern, and for each M-character subsequence of text to be compared.
2. If the hash values are unequal, the algorithm will calculate the hash value for
next M-character sequence.
3. If the hash values are equal, the algorithm will do a Brute Force comparison
between the pattern and the M-character sequence.
4. In this way, there is only one comparison per text subsequence, and Brute
Force is only needed when hash values match.

Let us understand the algorithm with the following steps:


Let the text be:

the pattern to be searched in the above text be:

Let us assign a numerical value(v)/weight for the characters we will be using in the
problem. Here, we have taken first ten alphabets only (i.e., A to J).

‘n’ be the length of the pattern and ‘m’ be the length of the text.

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 4
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
Here, m=10 and n=3. Let d be the number of characters in the input set.
Here, we have taken input set {A, B, C, ..., J}. So, d=10. You can assume any suitable
value for d.

Let us calculate the hash value of the pattern.

hash value for pattern(p) = Σ (v * dm-1) mod 13


= ((3 * 102) + (4 * 101) + (4 * 100)) mod 13
= 344 mod 13 = 6
In the calculation above, choose a prime number (here, 13) in such a way that we can
perform all the calculations with single-precision arithmetic

Calculate the hash value for the text-window of size m.


For the first window ABC,
hash value for text(t) =Σ (v * dm-1) mod 13
= ((1 * 102) + (2 * 101) + (3 * 100)) mod 13
= 123 mod 13 = 6
Compare the hash value of the pattern with the hash value of the text.
If they match then, character-matching is performed.
In the above examples, the hash value of the first window (i.e., t) matches with p.
so, go for character matching between ABC and CDD. Since they do not match so, go
for the next window.

We calculate the hash value of the next window by subtracting the first term and
adding the next term as shown below.
t = ((d * (t - v[character to be removed] * h) + v[character to be added] ) mod 13
= ((10 * (6 - 1 * 9) + 3) mod 13 = 12
Where, h = dm-1 = 103-1 = 100.
t = ((1 * 102) + ((2 * 101) + (3 * 100)) * 10 - (1 * 102) + (3 * 100)) mod 13
= 233 mod 13 = 12
For BCC, t = 12 (≠6). Therefore, go for the next window.
After a few searches, we will get the match for the window CDD in the text

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 5
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
Knuth-Morris-Pratt(KMP) Algorithm:

Knuth-Morris and Pratt introduce a linear time algorithm for the string-matching
problem. A matching time of O (n) is achieved by avoiding comparison with an
element of 'S' that have previously been involved in comparison with some element of
the pattern 'p' to be matched. i.e., backtracking on the string 'S' never occurs

Components of KMP Algorithm:

1. The Prefix Function (Π): The Prefix Function, Π for a pattern encapsulates
knowledge about how the pattern matches against the shift of itself. This information
can be used to avoid a useless shift of the pattern 'p.' In other words, this enables
avoiding backtracking of the string 'S.'

2. The KMP Matcher: With string 'S,' pattern 'p' and prefix function 'Π' as inputs,
find the occurrence of 'p' in 'S' and returns the number of shifts of 'p' after which
occurrences are found.

The Prefix Function (Π)

Following pseudo code compute the prefix function, Π:

COMPUTE- PREFIX- FUNCTION (P)


1. m = length [P]; //'p' pattern to be matched
2. Π [0] = 0, Π [1] = 0;
3. k = 0;
4. for q = 2 to m do
5. {
6. if (P [k + 1] ≠ P [q])
7. {
8. k = 0;
9. Π [q] = k;
10. }
11. else
12. {
13. k = k + 1;
14. Π [q] = k;
15. }
16. }
10. return Π;

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 6
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
Running Time Analysis:

In the above pseudo code for calculating the prefix function, the for loop from step 4
to step 10 runs 'm' times. Step1 to Step3 take constant time. Hence the running time of
computing prefix function is O (m).

Example: Compute Π for the pattern 'p' below:

Solution:
Initially: m = length [p] = 7
Π [1] = 0
k=0

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 7
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms

After iteration 6 times, the prefix function computation is complete:

The KMP Matcher:


The KMP Matcher with the pattern 'p,' the string 'S' and prefix function 'Π' as input,
finds a match of p in S. Following pseudo code compute the matching component of
KMP algorithm:

KMP-MATCHER (T, P)
1. n = length [T];
2. m = length [P];
3. Π = COMPUTE-PREFIX-FUNCTION (P)
4. q = 0 // numbers of characters matched
5. for i = 1 to n do // scan S from left to right
6. {
7. while ((q >= 0) and (P [q + 1] ≠ T [i])) do
8. q = Π [q] // next character does not match
9. If (P [q + 1] = T [i]) then q = q + 1 // next character matches
10. If (q = m) // is all of p matched?
11. then print "Pattern occurs with shift" i – m+1
12. q = Π [q]; // look for the next match
13. }

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 8
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
Running Time Analysis:
The for loop beginning in step 5 runs 'n' times, i.e., as long as the length of the string
'S.' Since step 1 to step 4 take constant times, the running time is dominated by this
for the loop. Thus running time of the matching function is O (n).

Example-1: Given a string 'T' and pattern 'P' as follows:

Let us execute the KMP Algorithm to find whether 'P' occurs in 'T.'

For 'p' the prefix function, ? was computed previously and is as follows:

Solution:
Initially: n = size of T = 15 m = size of P = 7

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 9
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 10
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 11
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms

Pattern 'P' has been found to complexity occur in a string 'T.' The total number of
shifts that took place for the match to be found is i-m = 13 - 7 = 6 shifts

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 12
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
Example-2:

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 13
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
Tries:

1. In computer science, a tries is also called digital tree and sometimes radix tree
or prefix tree.
2. Tries is a tree-based data structure for storing strings in order to support fast
pattern matching.
3. Tries are used for information retrieval.
4. Tries is used to store the character in each node not the key.
5. Path from root to node is associated with key.
6. Tries uses character of a key to guide the search process.
7. All the descendants of the node have a common prefix of the string associated
with that node.

Types:
I. Standard Tries:
The standard tries for a set of strings S is an ordered tree such that:

• Each node but the root is labeled with a character.


• The children of a node are alphabetically ordered
• The paths from the external nodes to the root yield the strings of S.
• Example: Standard tries for the set of strings S = {bear, bell, bid, bull, buy,
sell, stock, stop}

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 14
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
Example: Standard tries for the set of strings S = {bear, bell, bid, bull, buy, sell,
stock, stop}

Root

b s
False False

e t
e i u
False
False

o
a l d l y l
False
False False False

r l l l c p
False False True False True False False False

True k
True True
True False True

True

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 15
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
III. Suffix Tries:

• A suffix trie is a compressed trie for all the suffixes of a text.


• The suffix trie for a text X of size n from an alphabet of size d.
• Suffix tries stores all the n(n−1)/2 suffixes of X in O(n) space.
• Suffix tries supports arbitrary pattern matching and prefixes matching
queries in O(dm) time, where m is the length of the pattern.
• Suffix tries can be constructed in O(dn) time
• Applications:
• Word matching.
• Prefix matching.
Example: Minimize example
Suffixes are :
e
ze
ize
mize
imize
nimize
inimize
minimize

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 16
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms

0 1 2 3 4 5 6 7
M I N I M I Z E

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 17
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms

NP-hard and NP-complete Problems: Basic concepts

The algorithms are categorized into two groups.


1. Polynomial time algorithms
2. Exponential time algorithms

Polynomial time algorithms Exponential time algorithms

Linear search---n 0/1 knapsack problem---2n


Binary search---log(n) traveling salesman problem---2n
Insertion sort---n2 sum of subset----2n
Merge sort---nlog(n) graph coloring---2n
Matrix multiplication—n3 Hamiltonian cycles---2n

➢ Presently we cannot provide a method solve exponential time algorithms to


solve in polynomial time.
➢ We try to show the similarity between exponential time algorithms, because if
we solve one problem then we can solve the remaining problems.
➢ If we unable to find a deterministic polynomial time algorithm for exponential
time algorithm, then we try to find a nondeterministic polynomial time
algorithm for exponential time algorithm.
Nondeterministic Algorithms:
We introduce three functions in nondeterministic algorithms,
1. Choice (): arbitrarily choose one of the elements of set S.
2. Failure (): signals an unsuccessful completion.
3. Success (): signals a successful completion.
Nondeterministic algorithm for searching:
Algorithm Nsearch(A, n, key)
{
j = choice (); ----------- nondeterministic O (1)
if (key = A[j]) then
{
write (j);
Success (); ------------- nondeterministic O (1)
}
write (0);
Failure (); ------------------ nondeterministic O (1)
}

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 18
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
The time complexity of nondeterministic searching algorithm is O(1)
Nondeterministic algorithm for sorting

Algorithm Nsort(A, n)
{
for i = 1 to n do B[i] = 0; // initialize B
for i = 1 to n do
{
j = Choice (1, n);
If (B[j] ≠ 0) then Failure ();
B[j] = A[i] ;
}
for i = 1 to n-1 do //verify order
If(B[i] > B[i + 1]) then Failure();
write (B[1…n]);
Success ();
}

The time complexity of nondeterministic sorting algorithm is O(n)

Class P:
P is the set of all deterministic polynomial time algorithms.
Examples- sorting, searching, MST, Huffman coding etc.

Class NP:
NP is the set of all nondeterministic polynomial time algorithms.

Note: We try exponential time algorithms are convert to nondeterministic


polynomial time algorithms, presently we don’t know how to convert.

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 19
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
Relation between exponential time algorithms:
The base problem is satisfiability problem.

Satisfiability (SAT) problem:


Let x1, x2, ….., xn denote Boolean variables (value is either true or false). A
literal is either a variable or its negation. A formula in the propositional calculus is an
expression that can be constructed by using literals and the operations ‘and’ and ‘or’.

A formula is in conjunctive normal form (CNF) if and only if it is represented as


⋀𝑘𝑖=1 𝐶𝑖 , where the Ci are Clauses, each represented as ⋁ 𝑙𝑖𝑗, lij are literals.

The satisfiability problem is to determine whether a formula is true for some


assignment of truth values to the variables. CNF-satisfiability is the satisfiability
problem for CNF formula.
Example:
Let x1, x2, x3 be Boolean variables.
CNF = C1  C2 = (x1 V 𝑥2 ̃ V x3)  (𝑥1̃ V x2 V 𝑥3 ̃ ).
The satisfiability problem is to find the values of xi ‘s for clauses C1 and C2 are true.
The possible values for x1, x2, x3 are

S. No X1 X2 X3
1 0 0 0
2 0 0 1
3 0 1 0
4 0 1 1
5 1 0 0
6 1 0 1
7 1 1 0
8 1 1 1

For 3 variable, 8 possibilities, so for n variables 2n possibilities.

State space tree: for n = 3

It is easy to obtain a polynomial time nondeterministic algorithm that terminates


successfully if and only if a propositional formula E(x1, x2, …., xn) is satisfiable.
Such an algorithm could proceed by simply choosing (non deterministically) one of

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 20
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
the 2n possible assignment of truth values to (x1, x2, ….., xn) and verify that E(x1, x2,
…., xn) is true for that assignment.

Reduce to:
Let L1 and L2 be two problems. Problem L1 reduces to L2 (also written L1 
L2) if and only if there is a way to solve L1 by a deterministic polynomial time
algorithm using a deterministic algorithm that solves L2 in polynomial time. That is if
we have a polynomial time algorithm for L2, then we can solve L1 in polynomial
time.

Note:  is transitive relation. That is if L1 L2 and L2  L3, then L1  L3)

NP-hard:
A problem L is NP-hard if and only if satisfiability reduces to L (satisfiability
 L).

NP-complete:
A problem L is NP-complete if and only if L is NP-hard and L is in NP.

Example:
Satisfiability problem is NP- hard and satisfiability  NP. So, satisfiability
problem is NP-complete.

0/1 knapsack problem is NP-hard:

Satisfiability problem reduces to 0/1 knapsack problem. So, we find a formula for
satisfiability problem is convert into a formula for 0/1knapsack problem in
polynomial time.
Suppose the 0/1 knapsack problem is n = 3, m = 8, (w1, w2, w3) = (5, 4, 3) and (p1,
p2, p3) = (10, 8, 12).
The solution set X = {x1 = 0/1, x2 = 0/1, x3 = 0/1}
So, the possibilities of xi’s are

S. No X1 X2 X3
1 0 0 0
2 0 0 1
3 0 1 0
4 0 1 1
5 1 0 0
SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY
SRKR ENGINEERING COLLEGE Page 21
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
6 1 0 1
7 1 1 0
8 1 1 1

State space tree

Therefore, satisfiability reduces to 0/1 knapsack problem.


Hence 0/1 knapsack problem is NP-hard.

Clique Decision problem:


Clique:
Let G = (V, E) be a graph. A clique is a complete subgraph of a given graph.
Example:

Clique decision problem:


check whether the given graph having a clique of size k or not.
Clique optimization problem:
find maximum clique in the given graph.
Clique decision problem is NP-hard
We show that satisfiability reduces to clique decision problem.
We find a formula for satisfiability problem that convert into a formula for clique
decision problem in polynomial time.
Let x1, x2, x3 be three variables. The CNF formula for satisfiability problem is

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 22
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
F = C1  C2  C3 = (x1 V x2)  (𝑥1
̃ V 𝑥2̃ )  (x1 V x3)
Consider the graph for clique decision problem having a clique of size 3.

Let G = (V, E) be a graph. Where V = {(a, i) / i  Ci and a  {x1, x2, x3}}and


E = {(<a, i>, <b, j>) / i ≠ j and b ≠ 𝑎̃}
So the graph is

Suppose we take subgraph V = {<𝑥1 ̃ , 2>, <x2, 1>, <x3, 3>} of clique size 3.
The value of x1 = 0, x2 = 1 and x3 = 1
The formula for satisfiability problem F = C1  C2  C3 = (x1 V x2)  (𝑥1 ̃) 
̃ V 𝑥2
(x1 V x3) = 1.
Suppose we take subgraph V = {<x1, 1>, <𝑥2 ̃ , 2>, <x3, 3>} of clique size 3.
The value of x1 = 1, x2 = 0 and x3 = 1
The formula for satisfiability problem F = C1  C2  C3 = (x1 V x2)  (𝑥1 ̃) 
̃ V 𝑥2
(x1 V x3) = 1.
Suppose we take subgraph V = {<x1, 1>, <𝑥2 ̃ , 2>, <x1, 3>} of clique size 3.
The value of x1 = 1, x2 = 0 and x3 = 0
The formula for satisfiability problem F = C1  C2  C3 = (x1 V x2)  (𝑥1 ̃) 
̃ V 𝑥2
(x1 V x3) = 1.
So, satisfiability problem is reduces to clique decision problem.
Hence clique decision problem is NP-hard problem.

Some NP-hard Problems:

1. 0/1 knapsack problem


2. Clique decision problem
3. Node cover decision problem
4. Chromatic number decision problem
5. Directed Hamiltonian cycle problem
6. Traveling salesman decision problem
7. Job sequencing with deadlines problem

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 23
DESIGN AND ANALYSIS OF ALGORITHM
String Matching Algorithms
The following decision problems are NP-complete problems:
1. Node cover
2. Planar node cover
3. Colorability
4. Undirected Hamiltonian cycle
5. Planar undirected Hamiltonian cycle
6. Planar directed Hamiltonian path

Cook’s theorem:
Satisfiability is in P if and only if P = NP.
Proof:
Suppose P = NP.
We already know that satisfiability is in NP = P
So, satisfiability is in P.
Conversely suppose satisfiability is in P.
We show that P = NP. Clearly P  NP.
Now we show that NP  P
To do this, we show how to obtain from any polynomial time nondeterministic
decision algorithm A and input I a formula Q (A, I) such that
Q is satisfiable if and only if A has a successful termination with input I.
If the length of the I is ‘n’ and the time complexity of A is p(n), then the
length of Q is O(p3(n) log n) = O(p4(n)).
The time needed to construct Q is also O(p3(n) log n).
A deterministic algorithm Z to determine the outcome of A on any input I can
be easily obtained.
Algorithm Z simply computes Q and then uses a deterministic algorithm for
satisfiability problem to determine whether Q is satisfiable.
If O(q(m)) is the time needed to determine whether a formula of length m is
satisfiable, then the complexity of Z is O(p3(n) log n + q(p3(n) log n)).
If satisfiability is in P, then q(m) is a polynomial function of m and the
complexity of Z becomes O(r(n)) for some polynomial r().
Hence if satisfiability is in P, then P = NP

SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY


SRKR ENGINEERING COLLEGE Page 24

You might also like