Unit 4
Unit 4
Time complexity=T(nm)
Pattern Matching-Brute Force
KMP Matching
KMP Algorithm
Time Complexity of KMP
• The Knuth-Morris-Pratt algorithm performs
pattern matching on a text string of length n
and a pattern string of length m in O(n+m)
time.
• Where n – is for searching and m – is for
creating the table
Boyer Moore
Boyer Moore
Time Complexity of Boyer Moore
• The worst-case running time of the BM
algorithm is O(nm)
• Average case time complexity is O(n)
Tries
• A trie (pronounced "try") is a tree-based data
structure for storing strings in order to
support fast pattern matching.
• The main application for tries is in information
retrieval. Indeed, the name "trie" comes from
the word "retrieval.“
• Stores a set of strings. We assign nodes in the
tree to letter.
Standard Trie Example
0 1
2 3
3
C
A+B
0 A+B
1
1 2 1 2
A
B A B
a
0 0 1 1 1 1 1
b 0
1 1 2 2 2 2
X a 0
1 2 2 2 3 3
a 0
1 2 2 2 3 3
b 0
1 2 3 3 3 4
a 0
1 2 3 3 4 4
⚫ Optimization problems
◦ Find a solution with the “best” or “optimum” value
Not
hamiltonian hamiltonian
⚫ Decision problem:
Given a directed graph G = (V, E), is there a cycle that contains
each and every vertex in V only once
Polynomial Algorithm and Exponential
Algorithm
Polynomial Exponential
• Linear Search-O(n) • Nqueens-O(nn)
• Binary Search-O(logn) • SumOfSubset-O(2n)
• Bubblesort-O(n2) • Mcoloring-O(nmn)
• Mergesort-O(nlogn) • HamiltonianCycle-O(nn)
• GreedyKnapsack-O(n) • 0-1 Knapsack-O(2n)
• HuffmanCod-O(nlogn) • SAT
Deterministic and Non Deterministic
Algorithm
• Deterministic Algorithm are traceable.
• Non Deterministic Algorithm are non
traceable.
• Although Exponential Algorithm cannot be
converted into Polynomial Algorithm, they can
be converted to non-deterministic Polynomial
Algorithm.
Example of Non –Deterministic Algorithm
• Non-Deterministic Search Algorithm with a
time complexity of O(1)
Algorithm NPSearch(arr,n,k)
{
i=Choice() // This step has to be figured out with O(1)
If(k=arr[i])
{
Print(“Found”)
}
Else
{
Print(“Not Found”)
}
}
Class P and NP
⚫ Class P consist of deterministic algorithm that are
solvable in polynomial time
⚫ Problems in P are called tractable.
⚫ Examples: O(n2), O(n3), O(1), O(n lg n)
⚫ Class NP
⚫ Problems not in P are intractable or unsolvable
⚫ Example:O(2n), O(nn), O(n!)
• Hamiltonian Path: Given a graph G = (V, E),
determine a path that contains each and every vertex
in V only once
• Traveling Salesman: Find a minimum weight
Hamiltonian Path.
Review: P and NP
• What do we mean when we say a problem is
in P?
– A: A deterministic solution can be found in
polynomial time
• What do we mean when we say a problem is
in NP?
– A: A non deterministic solution can be verified in
polynomial time
• What is the relation between P and NP?
– A: P NP, but no one knows whether P = NP
Is P = NP? P
NP
P
NP
Satisfiability(SAT)
• I/P : Boolean formula
• O/P : Is formula satisfiable?
SAT Є NP
Proof : Assignment to variables
Verifier: Uses these assignments and checks
that the formula evaluates to true (T).
Example: (x1∧y1) ∨ (x2∧y2) ∨ ... ∨ (xn∧yn)
3-SAT Example
Satisfiability
• SAT is considered as the base problem which
has the same exponential time complexity as
others.
• If SAT can be solved in polynomial time then
all other problems related to it can also be
solved in polynomial time.
SAT problem can be related to other
exponential problem
yes
yes
f Problem B no
no
Problem A
Reduction of Satisfiability
• Assume Satisfiability is NP-Hard.
• If Satisfiability(SAT) can be reduced to some other
problem(L), then the problem(L) also becomes NP-
Hard.
SAT𝝰 L
• Conversion has to be done in polynomial time.
• If SAT can be solved in polynomial time then the
problem(L) can also be solved in polynomial time
and vice-versa.
• Transitive Property=> SAT𝝰 L1 , L1𝝰 L2 , Then SAT𝝰 L2
NP-Completeness
P
NP-complete
NP
9
NP-Completeness P
NP-complete
NP
⚫ A problem B is NP-complete if:
(1)B = NP
(2)SAT𝝰 B
⚫ If B satisfies only property (2) we say that B is NP-hard
⚫ Nopolynomial time algorithm has been found for an NP-
Complete problem
⚫ No one has ever proven that “no polynomial time algorithm
can exist for any NP-Complete problem”
Relationship among P,NP,NP-Complete
and NP-Hard
NP
NP-Complete
Joshi Jesal
COOK’s Theorem
• If SAT has an efficient algorithm then so does
other problems in NP.