Professional Documents
Culture Documents
Unit-4 Algorithms Desing & Analysis
Unit-4 Algorithms Desing & Analysis
of Technology, Gorakhpur
Anu Raj
Department of Information Technology & Computer Applications
MMM University of Technology Gorakhpur-273010
Email: anu.raj10@yahoo.com
16-06-2021 Side 1
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
UNIT – IV
Selected Topics: String Matching, Text processing- Justification of text, Sorting
Network, Theory of NP-completeness, Approximation algorithms and
Randomized algorithms, Matrix Operations, Polynomials and FFT, Number
Theoretic Algorithms
16-06-2021 Side 2
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
String Matching
• String Matching Algorithm is also called "String Searching
Algorithm." This is a vital class of string algorithm is
declared as "this is the method to find a place where one is
several strings are found within the larger string”.
16-06-2021 Side 3
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Example
A B C A B A A C A B TEXT
SHIFT=3
A B A A PATTERN
16-06-2021 Side 4
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
16-06-2021 Side 5
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
16-06-2021 Side 6
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Example
SUPPOSE,
T=1011101110 P=111
FIND ALL VALID SHIFT……
T=Text 1 0 1 1 1 0 1 1 1 0
S= 0
P=Patter n 1 1 1
16-06-2021 Side 7
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
1 0 1 1 1 0 1 1 1 0
S=1
1 1 1
16-06-2021 Side 8
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
1 0 1 1 1 0 1 1 1 0
S=2
1 1 1
16-06-2021 Side 9
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
1 0 1 1 1 0 1 1 1 0
S=4
1 1 1
16-06-2021 Side 10
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
1 0 1 1 1 0 1 1 1 0
S=5
1 1 1
16-06-2021 Side 11
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
1 0 1 1 1 0 1 1 1 0
S=6
1 1 1
16-06-2021 Side 12
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
1 0 1 1 1 0 1 1 1 0
S=7
1 1 1
16-06-2021 Side 13
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
16-06-2021 Side 14
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Algorithm
RABIN-KARP-MATCHER(T,P,d,q)
1) n = T.length
2) m = P.length
3) h = d^(m-1) mod q
4) p=0
5) t =0
6) for i =1 to m
7) p = (dp + P[i]) mod q //pre-processing
8) t = (d t + T[i]) mod q
9) for s = 0 to n – m
10) if p == t //matching
if P[1…m] == T[s+1…. s+m]
printf “ Pattern occurs with shift ” s if
s< n-m
t+1 = (d(t- T[s+1]h)+ T[s+m+1]) mod q
16-06-2021 Side 15
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
EXAMPLE
16-06-2021 Side 16
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
S=0
3 1 4 1 5 9 2 6 5 3 5
S=1
3 1 4 1 5 9 2 6 5 3 5
S=3
3 1 4 1 5 9 2 6 5 3 5
S=4
3 1 4 1 5 9 2 6 5 3 5
S=5
3 1 4 1 5 9 2 6 5 3 5
16-06-2021 Side 18
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
S=6
3 1 4 1 5 9 2 6 5 3 5
3 1 4 1 5 9 2 6 5 3 5
• S=8
3 1 4 1 5 9 2 6 5 3 5
16-06-2021 Side 19
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
S=9
3 1 4 1 5 9 2 6 5 3 5
16-06-2021 Side 20
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Sorting Network
16-06-2021 Side 21
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Comparator
• A comparator is a device with two inputs, x and
y, and outputs, x’ and y’, that performs the
following function:
x’= min (x,y)
y’= max (x,y)
x x’ 7 3
y COMPARATOR y’ 3 7
16-06-2021 Side 22
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Sorting Network
16-06-2021 Side 23
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Sorting Network
16-06-2021 Side 24
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
16-06-2021 Side 25
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
The zero-oneprinciple
16-06-2021 Side 27
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Theory of NP-Completeness
❖ Polynomial Time(P)
16-06-2021 Side 28
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
16-06-2021 Side 29
29
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
16-06-2021 Side 30
30
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Problem Reduction
• Problem A reduces to problem B (AB)
• iff A can be solved by using any algorithm which
solves B.
• If AB, B is more difficult (B is at least as hard as A)
Decision problems
16-06-2021 Side 32
32
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
16-06-2021 Side 33
33
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
NP-Completeness
16-06-2021 Side 34
34
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
NP-Completeness
NP-Complete
NP
P
16-06-2021 Side 35
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
NPC Problems
Definition:
❑ A clique in a graph is a set of vertices such that any pair of
vertices are joined by an edge.
16-06-2021 Side 36
36
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Clique Problem
• Clique
• Graph G = (V, E), a subset S of the vertices is a clique if
there is an edge between every pair of vertices in S
1 2
3 4 5
6 7
16-06-2021 Side 37
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
NPC Problems
Definition:
• A vertex cover of G=(V, E) is V’V such that every edge in E is
incident to some vV’.
16-06-2021 Side 38
38
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
• Vertex Cover
• Graph G = (V, E), a subset S of the vertices is a vertex
cover if every edge in E has at least one endpoint in S
1 2
3 4 5
6 7
16-06-2021 Side 39
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
AN APPROXIMATE ALGORITHM
• An Approximate Algorithm is a way of approach NP-
COMPLETENESS for the optimization problem. This technique does
not guarantee the best solution. The goal of an approximation
algorithm is to come as close as possible to the optimum value in a
reasonable amount of time which is at the most polynomial time. Such
algorithms are called approximation algorithm or heuristic algorithm.
• For the traveling salesperson problem, the optimization problem is to
find the shortest cycle, and the approximation problem is to find a
short cycle.
• For the vertex cover problem, the optimization problem is to find the
vertex cover with fewest vertices, and the approximation problem is
to find the vertex cover with few vertices.
16-06-2021 Side 40
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
RANDOMISED ALGORITHM
16-06-2021 Side 41
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
RANDOMISED ALGORITHM
RANDOMISED ALGORITHM
16-06-2021 Side 43
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
MATRIX OPERATIONS
• Strassen’s Matrix multiplication can be performed only on square
matrices where n is a power of 2. Order of both of the matrices are n × n.
• Divide X, Y and Z into four (n/2)×(n/2) matrices as represented below −
• Z=[IKJL]Z=[IJKL] X=[ACBD]X=[ABCD] and Y=[EGFH]Y=[EFGH]
• Using Strassen’s Algorithm compute the following −
• M1:=(A+C)×(E+F)M1:=(A+C)×(E+F)
• M2:=(B+D)×(G+H)M2:=(B+D)×(G+H)
• M3:=(A−D)×(E+H)M3:=(A−D)×(E+H)
• M4:=A×(F−H)M4:=A×(F−H)
• M5:=(C+D)×(E)M5:=(C+D)×(E)
• M6:=(A+B)×(H)M6:=(A+B)×(H)
• M7:=D×(G−E)M7:=D×(G−E)
16-06-2021 Side 44
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Strassen’s Matrix
• Then,
• I:=M2+M3−M6−M7I:=M2+M3−M6−M7
• J:=M4+M6J:=M4+M6
• K:=M5+M7K:=M5+M7
• L:=M1−M3−M4−M5L:=M1−M3−M4−M5
• Analysis
• T(n)={c7xT(n2)+dxn2ifn=1otherwiseT(n)={cifn=17xT(n2)+dxn2otherwis
e where c and d are constants
• Using this recurrence relation, we get T(n)=O(nlog7)T(n)=O(nlog7)
• Hence, the complexity of Strassen’s matrix multiplication algorithm
is O(nlog7)O(nlog7).
16-06-2021 Side 45
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
• Polynomials-
– Algorithms to add, multiply and evaluate polynomials
– Coefficient and point-value representation
• Fourier Transform
– Discrete Fourier Transform (DFT) and inverse DFT to translate between
polynomial representations
– “A Short Digression on Complex Roots of Unity”
– Fast Fourier Transform (FFT) is a divide-and-conquer algorithm based on
properties of complex roots of unity
16-06-2021 Side 46
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
POLYNOMIAL
16-06-2021 Side 47
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
POLYNOMIAL
COMPLEX ROOT
16-06-2021 Side 49
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
FFT
16-06-2021 Side 51
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
FFT
• Fast Fourier Transform (FFT) • The problem of evaluating 𝐴(𝑥) at 𝜔𝑛 0
, 𝜔𝑛 1 , … , 𝜔𝑛 𝑛−1 reduces to 1. evaluating the degree-bound 𝑛 2
polynomials 𝐴 0 (𝑥) and 𝐴 1 (𝑥) at the points 𝜔𝑛 0 2 , 𝜔𝑛 1 2 , … , 𝜔𝑛
𝑛−1 2 2. combining the results by 𝐴 𝑥 = 𝐴 0 𝑥 2 + 𝑥𝐴 1 (𝑥 2 ) • Why
bother? – The list 𝜔𝑛 0 2 , 𝜔𝑛 1 2 , … , 𝜔𝑛 𝑛−1 2 does not contain 𝑛
distinct values, but 𝑛 2 complex 𝑛 2 th roots of unity – Polynomials 𝐴 0
and 𝐴 1 are recursively evaluated at the 𝑛 2 complex 𝑛 2 th roots of unity
– Subproblems have exactly the same form as the original problem, but
are half the size 28
• Example • 𝐴 𝑥 = 𝑎0 + 𝑎1𝑥 + 𝑎2𝑥 2 + 𝑎3𝑥 3 of degree-bound 4 – 𝐴 𝜔4 0
= 𝐴 1 = 𝑎0 + 𝑎1 + 𝑎2 + 𝑎3 – 𝐴 𝜔4 1 = 𝐴 𝑖 = 𝑎𝑜 + 𝑎1𝑖 − 𝑎2 − 𝑎3𝑖 – 𝐴 𝜔4
2 = 𝐴 −1 = 𝑎0 − 𝑎1 + 𝑎2 − 𝑎3 – 𝐴 𝜔4 3 = 𝐴 −𝑖 = 𝑎0 − 𝑎1𝑖 + 𝑎2 + 𝑎3𝑖 •
Using 𝐴 𝑥 = 𝐴 0 𝑥 2 + 𝑥𝐴 1 𝑥 2 – 𝐴 𝑥 = 𝑎0 + 𝑎2𝑥 2 + 𝑥 𝑎1 + 𝑎3𝑥 2 – 𝐴
𝜔4 0 = 𝐴 1 = 𝑎0 + 𝑎2 + 1(𝑎1 + 𝑎3) – 𝐴 𝜔4 1 = 𝐴 𝑖 = 𝑎0 − 𝑎2 + 𝑖(𝑎1 −
𝑎3) – 𝐴 𝜔4 2 = 𝐴 −1 = 𝑎0 + 𝑎2 − 1 𝑎1 + 𝑎3 – 𝐴 𝜔4 3 = 𝐴 −𝑖 = 𝑎0 − 𝑎2 −
𝑖(𝑎1 − 𝑎3)
16-06-2021 Side 52
Number Theoretic Algorithms
Division
Divide -38 by 7:
• Write: -38 = -6*7 + 4, so q = -6, r = 4
Greatest Common Divisor of two numbers is the largest number that divides
both of them.
GCD(A,B) is as follows:
If A = 0 then GCD(A,B)=B,
since the GCD(0,B)=B, Euclidean Algorithm: Example
stop. If B = 0 gcd( 299,221) =?
then GCD(A,B)=A, 299 = 1 ⋅ 221 + 78
since the GCD(A,0)=A, and 221 = 2 ⋅ 78 + 65
stop.
78 = 1 ⋅ 65 + 13
Write A in quotient remainder form (A = B⋅Q + R)
Find GCD(B,R) using the Euclidean Algorithm 65 = 5 ⋅ 13 + 0
since GCD(A,B) = GCD(B,R)
Euclidean Algorithm
• Compute gcd(27,33).
First, we divide the bigger one by the smaller one:
33=1×27+6
27=4×6+3
6=2×3+0
Since 6 is a perfect multiple of 3, gcd(6,3)=3, and we have
found that gcd(33,27)=3
Euler’s Phi-Function
⚫ap-11 = 1 (mod p)
where p is prime and gcd(a,p)=1
⚫ also known as Fermat’s Little Theorem
⚫ also ap = a (mod p)
Solution
We follow the four steps.
1. M = 3 × 5 × 7 = 105
2. M1 = 105 / 3 = 35, M2 = 105 / 5 = 21, M3 = 105 / 7 = 15
3. The inverses are
M1 M1 −1 = 1(mod 5)
3 5 M −1 (mod 3) =1
1
2 M1 −1 (mod 3) =1
M1 −1 =2
Similarly, M2−1 = 1, M3 −1 = 1
4. x = (2 × 35 × 2 + 3 × 21 × 1Vijay+Katta2 × 15 × 1) mod 105 = 23 mod19105
09/26/08
Continued
Example: Chinese remainder theorem
Example
The solution to this set of equations is given in the next section; for
the moment, note that the answer to this set of equations is x = 23.
This value satisfies all equations: 23 ≡ 2 (mod 3), 23 ≡ 3 (mod 5),
and 23 ≡ 2 (mod 7).
22
Public-Key Cryptography
Bob Alice
23
Setting up an RSA Cryptosystem
24
RSA Key Setup
• Select two large primes p and q at random.
• Compute n = pq. Note: (n ) = ( p − 1)( q − 1).
• Select an encryption key e satisfying 1 e (n ) and
gcd( e, ( n )) = 1. (i.e., e Z* ( n ) , e 1.)
• Compute the descryption key: d = e −1 mod (n ).
ed 1 mod ( n ).
d is the inverse of e mod (n ).
• Public key: PU = ( n, e). Private key: PR = ( n, d ).
• Important: p, q, and ( n ) must be kept secret.
RSA Encryption and Decryption
• Suppose Bob is to send a secret message m to Alice.
• To encrypt, Bob will
obtain Alice's public key PU Alice = {e, n}.
encrypt m as c = m e mod n.
Note: m Z n*.
• To decrypt the ciphertext c, Alice will compute
m = c d mod n, using her private key PRAlice = {d , n}.
• What key will Alice use to encrypt her reply to Bob?
RSA Example: Key Setup
• Select two primes: 𝑝 = 17, 𝑞 = 11.
• Compute the modulus 𝑛 = 𝑝𝑞 = 187.
• Compute 𝜑(𝑛) = (𝑝 − 1)(𝑞 − 1) = 160.
• Select 𝑒 between 0 and 160 such that gcd( 𝑒, 160) = 1.
Let 𝑒 = 7.
• Compute 𝑑 = 𝑒 −1 mod 𝜑(𝑛) = 7−1 mod 1 60 = 23
(using extended Euclid′s algorithm).
• Public key: 𝑃𝑈 = (𝑒, 𝑛) = (7, 187).
• Private key: 𝑃𝑅 = (𝑑, 𝑛) = (23, 187).
RSA Example: Encryption & Decryption
• Suppose m = 88.
• Encryption: c = m e mod n = 887 mod187 = 11.
• Decryption: m = c d mod n = 1123 mod187 = 88.
• When computing 1123 mod187, we do not first
compute 1123 and then reduce it modulo 187.
• Rather, when conmputing 1123 , reduce the intermediate
results modulo 187 whenever they get bigger than 187.