Download as pdf or txt
Download as pdf or txt
You are on page 1of 80

Madan Mohan Malaviya Univ.

of Technology, Gorakhpur

Algorithms Design and Analysis


(MCA-129)

[MCA- IVth Sem, Session: 2020-21]

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”.

• String matching algorithms have greatly influenced


computer science and play an essential role in various real-
world problems.

• It helps in performing time-efficient tasks in multiple


domains. These algorithms are useful in the case of
searching a string within another string.

16-06-2021 Side 3
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Example

String Matching Problem

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

String Matching Algorithms


There are many types of String Matching Algorithms like:-
1)The Naive string-matching algorithm
2)The Rabin-Karp algorithm
3)String matching with finite automata
4)The Knuth-Morris-Pratt algorithm

16-06-2021 Side 5
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

The Naive Algorithm


The naive algorithm finds all valid shifts using a loop that
checks the condition P[1….m]=T[s+1…. s+m] for each of
the n- m+1
possible values of s.(P=pattern , T=text/string , s=shift)
NAIVE-STRING-MATCHER(T,P)
1)n = T.length
2)m = P.length
3)for s=0 to n-m
4) if P[1…m]==T[s+1….s+m]
5)
Printf “Pattern occurs with shift s”

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

So, S=2 is a valid shift…

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

So, S=6 is a valid shift…

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

The Rabin-karp Algorithm

• Rabin and Karp proposed a string matching algorithm that


performs well in practice and that also generalizes to other
algorithms for related problems, such as two-dimensional
pattern matching.

• Like the Naive Algorithm, Rabin-Karp algorithm also slides


the pattern one by one. But unlike the Naive algorithm, Rabin
Karp algorithm matches the hash value of the pattern with the
hash value of current substring of text, and if the hash values
match then only it starts matching individual characters.

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

Pattern P=26, how many spurious hits does the Rabin


Karp matcher in the text T=3 1 4 1 5 9 2 6 5 3 5…
• T=31415926535
P=26
Here T.length=11 so Q=11 and P mod Q = 26 mod
11
=4
Now find the exact match of P mod Q…

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

3 1 mod 1 1 = 9 not equal to 4

S=1
3 1 4 1 5 9 2 6 5 3 5

1 4 mod 1 1 = 3 not equal to


4
S=2
3 1 4 1 5 9 2 6 5 3 5

4 1 mod 1 1 = 8 not equal to


16-06-2021
4 Side 17
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

S=3
3 1 4 1 5 9 2 6 5 3 5

1 5 mod 1 1 = 4 equal to 4 SPURIOUS HIT

S=4
3 1 4 1 5 9 2 6 5 3 5

5 9 mod 1 1 = 4 equal to 4 SPURIOUS HIT

S=5
3 1 4 1 5 9 2 6 5 3 5

9 2 mod 1 1 = 4 equal to 4 SPURIOUS HIT

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

2 6 mod 1 1 = 4 EXACT MATCH


• S=7

3 1 4 1 5 9 2 6 5 3 5

• 6 5 mod 1 1 = 10 not equal to 4

• S=8
3 1 4 1 5 9 2 6 5 3 5

• 5 3 mod 1 1 = 9 not equal to 4

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

3 5 mod 1 1 = 2 not equal to 4

Pattern occurs with shift 6

16-06-2021 Side 20
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Sorting Network

• Sorting networks are comparison networks that always sort


their inputs.
• A sorting network consists of two types of items: comparators
and wires.
• The wires are thought of as running from left to right, carrying
values (one per wire) that traverse the network all at the same
time.
• Each comparator connects two wires.

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

• We shallspeak of the inputsequence<a1,a2,…,an>and


the output sequence <b1,b2,…,bn>, referring to the values
on the input and output wires.

• Asorting network is a comparisonnetwork for which


the output sequence is monotonically increasing (that is
b1 b2 … bn) for every input sequence.

16-06-2021 Side 23
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Sorting Network

• Fig: A Sorting network based on Insertion Sort

16-06-2021 Side 24
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Bitonic Sorting Network

A sequence that monotonically increases and then monotonically decreases,


or else monotonically decreases and then monotonically increases is called a
bitonic sequence. For example: the sequence (2, 5, 6, 9, 3, 1) and (8, 7, 5, 2, 4,
6) are both bitonic.
Half-Cleaner: A bitonic sorter is containing several stages, each of which is
called a half-cleaner. Each half-cleaner is a comparison network of depth 1 in
which input line i is compared with line 1+ for i = 1, 2..... .

16-06-2021 Side 25
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Bitonic Sorting Network


• When a bitonic sequence of 0's and 1's is practiced as input to a
half-cleaner, the half-cleaner produces an output sequence in
which smaller values are in the top half, larger values are in the
bottom half, and both halves are bitonic, and at least one of the
halves is clean.
• Bitonic Sorter: By recursively connecting half-cleaners, we can
build a bitonic sorter, which is a network that sorts bitonic
sequences.
• The first stage of BITONIC-SORTER [n] consists of HALF-
CLEANER [n], which produces two bitonic sequences of half the
size such that every element in the top half is at least as small as
each element in the bottom half.
• Thus, we can complete the sort by utilizing two copies of
BITONIC-SORTER [n/2] to sort the two halves recursively.
16-06-2021 Side 26
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

The zero-oneprinciple

• Thezero-one principlesays thatifa sorting network


works correctly when each input is drawn
from the set {0,1}, then it works correctly on
arbitrary input numbers.

• Once we constructed asorting network and proved


that it can sort all zero-one sequences, we
shall appeal to the zero-one principle to show
that it properly sorts sequences of arbitrary
values.

16-06-2021 Side 27
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Theory of NP-Completeness
❖ Polynomial Time(P)

• Class of problems that can be solved in polynomial time


• Corresponds with problems that can be solved efficiently in
practice

❖Non Deterministic Polynomial Time (NP)


• Problems solvable in non-deterministic polynomial time . . .
• Problems where “yes” instances have polynomial time
checkable certificates

16-06-2021 Side 28
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

• P: the class of problems which can be solved by a


deterministic polynomial algorithm.
• NP : the class of decision problem which can be solved by a
non-deterministic polynomial algorithm.
• NP-hard: the class of problems to which every NP problem
reduces.
• NP-complete (NPC): the class of problems which are NP-
hard and belong to NP.

16-06-2021 Side 29
29
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Relationship Between NP and P


• It is not known whether P=NP or whether P is a proper
subset of NP
• It is believed NP is much larger than P
• But no problem in NP has been proved as not in P
• No known deterministic algorithms that are polynomials
bounded for many problems in NP
• So, “does P = NP?” is still an open question!

16-06-2021 Side 30
30
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Problem Reduction
• Problem A reduces to problem B (AB)
• iff A can be solved by using any algorithm which
solves B.
• If AB, B is more difficult (B is at least as hard as A)

instance transformation instance of B


of A T(tr1)
T(A) T(B) solver of B
answer transformation
of A T(tr2) answer of B
• Note: T(tr1) + T(tr2) < T(B)
• T(A)  T(tr1) + T(tr2) + T(B)  O(T(B))
16-06-2021 Side 31
31
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Decision problems

• The solution is simply “Yes” or “No”.


• Optimization problem : more difficult
Decision problem
• E.g. the traveling salesperson problem
• Optimization version:
Find the shortest tour
• Decision version:
Is there a tour whose total length is less than or equal to a
constant C ?

16-06-2021 Side 32
32
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

NPC and NP-hard

• A problem A is NP-hard if every NP problem reduces to


A.
• A problem A is NP-complete (NPC) if A∈NP and every
NP problem reduces to A.
• Or we can say a problem A is NPC if A∈NP and A is
NP-hard.

16-06-2021 Side 33
33
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

NP-Completeness

• “NP-complete problems”: the hardest problems in NP


• Interesting property
• If any one NP-complete problem can be solved in
polynomial time, then every problem in NP can also be
solved similarly.
• Many believe P≠NP

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

• CLIQUE(k): Does G=(V,E) contain a clique of size k?

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

• Vertex Cover(k): Given a graph G=(V, E) and an integer k, does


G have a vertex cover with k vertices?

Definition:
• A vertex cover of G=(V, E) is V’V such that every edge in E is
incident to some vV’.

16-06-2021 Side 38
38
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Vertex Cover Problem

• 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

• In a randomized algorithm, the output which comes after providing


the input in the device will depend on the output of the randomizer.

16-06-2021 Side 41
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

RANDOMISED ALGORITHM

Randomized algorithm categorized into two parts:


• 1. Las Vegas Algorithms: In the las Vegas algorithm either we get the correct
output or either we get nothing but what it means let understand this with the
help of an example.
• Suppose you want to search information regarding " Randomization algorithm
in DAA" for this, you will search on any search engine and finding the correct
information regarding this is quite a time consuming and depend on the two
cases either you got your query on the first page of the search engine or in the
second case you spend a large amount of the time in surfing the internet for
correct information regarding this if you can't find the information regarding
your query it might be possible that you stopped further searching and, in this
case, your solution was not found or if you found your query you get correct
output and the time complexity depends on the amount of the time you get your
search query result. For the worst case, you got your query on the last page of
the search engine.
16-06-2021 • Side 42
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

RANDOMISED ALGORITHM

• 2. Monte Carlo algorithm: For the same Input different output


produced

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

POLYNOMIAL’S & FFT

• 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

• A polynomial in the variable 𝑥 is a representation of a function 𝐴 𝑥 =


𝑎𝑛−1𝑥 𝑛−1 + ⋯ + 𝑎2𝑥 2 + 𝑎1𝑥 + 𝑎0 as a formal sum 𝐴 𝑥 = 𝑎𝑗𝑥 𝑛−1 𝑗 𝑗=0 .
• We call the values 𝑎0, 𝑎1, … , 𝑎𝑛−1 the coefficients of the polynomial
• 𝐴 𝑥 is said to have degree 𝑘 if its highest nonzero coefficient is 𝑎𝑘.
• Any integer strictly greater than the degree of a polynomial is a degree-
bound of that polynomial
• Examples
• 𝐴 𝑥 = 𝑥 3 − 2𝑥 − 1 – 𝐴(𝑥) has degree 3 – 𝐴(𝑥) has degree-bounds 4, 5, 6,
… or all values > degree – 𝐴(𝑥) has coefficients (−1, −2, 0, 1)
• 𝐵 𝑥 = 𝑥 3 + 𝑥 2 + 1 – 𝐵(𝑥) has degree 3 – 𝐵(𝑥) has degree bounds 4, 5, 6,
… or all values > degree – 𝐵(𝑥) has coefficients (1, 0, 1, 1

16-06-2021 Side 47
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

POLYNOMIAL

• Adding two polynomials represented by the coefficient vectors 𝑎 = (𝑎0, 𝑎1, … ,


𝑎𝑛−1) and 𝑏 = (𝑏0, 𝑏1, … , 𝑏𝑛−1) takes time Θ(𝑛).
• Sum is the coefficient vector 𝑐 = (𝑐0, 𝑐1, … , 𝑐𝑛−1), where 𝑐𝑗 = 𝑎𝑗 + 𝑏𝑗 for 𝑗 =
0,1, … , 𝑛 − 1. • Example 𝐴 𝑥 = 6𝑥 3 + 7𝑥 2 − 10𝑥 + 9 (9, −10, 7, 6) 𝐵 𝑥 = − 2𝑥 3
+ 4𝑥 − 5 (−5, 4, 0, −2) 𝐶(𝑥) = 4𝑥 3 + 7𝑥 2 − 6𝑥 + 4 (4, −6,7,4)
• Multiplying Polynomials • For polynomial multiplication, if 𝐴(𝑥) and 𝐵(𝑥) are
polynomials of degree-bound n, we say their product 𝐶(𝑥) is a polynomial of
degree-bound 2𝑛 − 1.
• Example 6𝑥 3 + 7𝑥 2 − 10𝑥 + 9 − 2𝑥 3 + 4𝑥 − 5 −30𝑥 3 −35𝑥 2 + 50𝑥 − 45 24𝑥
4 +28𝑥 3 −40𝑥 2 + 36𝑥 −12𝑥 6 −14𝑥 5 + 20𝑥 4 −18𝑥 3 −12𝑥 6 −14𝑥 5 +44𝑥 4
−20𝑥 3 −75𝑥 2 + 86𝑥 − 45
• Multiplying Polynomials • Multiplication of two degree-bound n polynomials
𝐴(𝑥) and 𝐵(𝑥) takes time Θ 𝑛 2 , since each coefficient in vector 𝑎 must be
multiplied by each coefficient in vector 𝑏. • Another way to express the product
C(x) is 𝑐𝑗 𝑥 2𝑛−1 𝑗 𝑗=0 , where 𝑐𝑗 = 𝑎𝑘𝑏𝑗−𝑘 𝑗 𝑘=0 . • The resulting coefficient
vector 𝑐 = (𝑐0, 𝑐1, … 𝑐2𝑛−1) is also called the convolution of the input vectors 𝑎
and 𝑏, denoted as 𝑐 = 𝑎⨂𝑏.
16-06-2021 Side 48
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

COMPLEX ROOT

• Complex Roots of Unity →


A complex 𝒏th root of unity (1) is a complex number 𝜔 such that 𝜔 𝑛 = 1.
• There are exactly 𝑛 complex 𝑛 th root of unity 𝑒 2𝜋𝑖𝑘 𝑛 for 𝑘 = 0, 1, … ,
𝑛 − 1 where 𝑒 𝑖𝑢 = cos 𝑢 + 𝑖 sin(𝑢). Here 𝑢 represents an angle in
radians. • Using 𝑒 2𝜋𝑖𝑘 𝑛 = cos 2𝜋𝑘 𝑛 + 𝑖 sin( 2𝜋𝑘 𝑛), we can check that
it is a root 𝑒 2𝜋𝑖𝑘 𝑛 𝑛 = 𝑒 2𝜋𝑖𝑘 = cos(2𝜋𝑘) 1 +𝑖 sin(2𝜋𝑘) 0 = 1
Examples :-
• The complex 4th roots of unity are 1, −1, 𝑖, −𝑖 where −1 = 𝑖.
• The complex 8th roots of unity are all of the above, plus four more 1 2
+ 𝑖 2 , 1 2 − 𝑖 2 , − 1 2 + 𝑖 2 , and − 1 2 − 𝑖 2
• For example 1 2 + 𝑖 2 2 = 1 2 + 2𝑖 2 + 𝑖 2 2 = 𝑖 2

16-06-2021 Side 49
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

FFT

• Fourier Transform -Fourier Transforms originate from signal processing


– Transform signal from time domain to frequency domain
– Input signal is a function mapping time to amplitude
– Output is a weighted sum of phase-shifted sinusoids of varying frequencies
• Fast Multiplication of Polynomials ->Using complex roots of unity
– Evaluation by taking the Discrete Fourier Transform (DFT) of a coefficient vector
– Interpolation by taking the “inverse DFT” of point-value pairs, yielding a coefficient
vector
– Fast Fourier Transform (FFT) can perform DFT and inverse DFT in time Θ(𝑛 log 𝑛)
• Algorithm
1. Add 𝑛 higher-order zero coefficients to 𝐴(𝑥) and 𝐵(𝑥)
2. Evaluate 𝐴(𝑥) and 𝐵(𝑥) using FFT for 2𝑛 points
3. Pointwise multiplication of point-value forms
4. Interpolate 𝐶(𝑥) using FFT to compute inverse DFT
16-06-2021 Side 50
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

DFT & FFT

• Discrete Fourier Transform (DFT) →


• Evaluate a polynomial 𝐴(𝑥) of degree-bound 𝑛 at the 𝑛 complex 𝑛 th roots of unity, 𝜔𝑛 0 ,
𝜔𝑛 1 , 𝜔𝑛 2 , … , 𝜔𝑛 𝑛−1 . – assume that 𝑛 is a power of 2 – assume 𝐴 is given in coefficient
form 𝑎 = (𝑎0, 𝑎1, … , 𝑎𝑛−1)
• We define the results 𝑦𝑘, for 𝑘 = 0, 1, … , 𝑛 − 1, by 𝑦𝑘 = 𝐴 𝜔𝑛 𝑘 = 𝑎𝑗𝜔𝑛 𝑛−1 𝑘𝑗 𝑗=0 .
• The vector 𝑦 = (𝑦0, 𝑦1, … , 𝑦𝑛−1) is the Discrete Fourier Transform (DFT) of the coefficient
vector 𝑎 = 𝑎0, 𝑎1, … , 𝑎𝑛−1 , denoted as 𝑦 = DFT𝑛(𝑎).
• Fast Fourier Transform (FFT) →
• Fast Fourier Transform (FFT) takes advantage of the special properties of the complex roots
of unity to compute DFT𝑛(a) in time Θ(𝑛 log 𝑛).
• Divide-and-conquer strategy – define two new polynomials of degree-bound 𝑛 2, using even-
index and odd-index coefficients of 𝐴(𝑥) separately – 𝐴 0 𝑥 = 𝑎0 + 𝑎2𝑥 + 𝑎4𝑥 2 + ⋯ + 𝑎𝑛−2𝑥
𝑛 2−1 – 𝐴 1 𝑥 = 𝑎1 + 𝑎3𝑥 + 𝑎5𝑥 2 + ⋯ + 𝑎𝑛−1𝑥 𝑛 2−1 – 𝐴 𝑥 = 𝐴 0 𝑥 2 + 𝑥𝐴 1 (𝑥 2 )

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

➢ A nonzero integer b is a divisor of an integer a if there is an


integer q such that a = bq.
➢ If b is a divisor of a, we write b | a, read “b divides a”.
➢ We also say that a is a multiple of b.
➢ A prime number is an integer greater than 1 whose only positive
divisors are 1 and itself.
➢ A positive integer with divisors other than itself and 1 is
composite number.
Example-
8|24 because 24 = 8*3
• 8 is a divisor of 24.
• 24 is a multiple of 8.
• 24 is not prime.
• 24 is composite.
Theorem- (Division Algorithm):
• Let a and b be integers with b > 0. There exist unique integers q and r
with the property that
a = b.q + r, where 0 ≤ r < b
Divide 38 by 7:
• Write: 38 = 5*7 + 3, so q = 5, r = 3

Divide -38 by 7:
• Write: -38 = -6*7 + 4, so q = -6, r = 4

• Divide -235 by 8 ???


Euclidean Algorithm

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

Euler’s phi-function,  (n), which is sometimes called the


Euler’s totient function plays a very important role in
cryptography. The function finds number of integer that are
smaller than n and relativly prime to n.

09/26/08 Vijay Katta 12


Fermat's Theorem

⚫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)

⚫ useful in public key and primality testing

09/26/08 Vijay Katta 14


CHINESE REMAINDER THEOREM
Chinese remainder theorem

The Chinese remainder theorem (CRT) is used to solve a


set of congruent equations with one variable but
different moduli, which are relatively prime, as shown below:

09/26/08 Vijay Katta 17


Solution To Chinese Remainder Theorem

1. Find M = m1 × m2 × … × mk. This is the common modulus.


2. Find M1 = M/m1, M2 = M/m2, …, Mk = M/mk.
3. Find the multiplicative inverse of M1, M2, …, Mk using the
corresponding moduli (m1, m2, …, m k). Call the inverses M1−1, M2 −1,
…, Mk −1.
4. The solution to the simultaneous equations is
Example: Chinese remainder theorem

Example Find the solution to the simultaneous equations:


Find the solution to the simultaneous equations:

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 following is an example of a set of equations with different


moduli:

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).

09/26/08 Vijay Katta 20


Example: Chinese remainder theorem
Suppose
x  1 mod 3
x  6 mod 7
x  8 mod 10
By the Chinese remainer theorem, the solution is:
x  1  70  (70−1 mod 3) + 6  30  (30 −1 mod 7) + 8  21  (21−1 mod10)
 1  70  (1−1 mod 3) + 6  30  (2 −1 mod 7) + 8  21  (1−1 mod10)
 1  70  1 + 6  30  4 + 8  21  1 mod 210
 958 mod 210
 118 mod 210
Public-Key Cryptography

Also known as asymmetric-key cryptography.


Each user has a pair of keys: a public key and a
private key.
• The public key is used for encryption.
• The key is known to the public.
• The private key is used for decryption.
• The key is only known to the owner.

22
Public-Key Cryptography

Bob Alice

23
Setting up an RSA Cryptosystem

• A user wishing to set up an RSA(Rivest–Shamir–


Adleman )cryptosystem will:
• Choose a pair of public/private keys: (PU, PR).
• Publish the public (encryption) key.
• Keep secret the private (decryption) key.

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.

You might also like