Design & Analysis of Algorithms KCS502
Design & Analysis of Algorithms KCS502
Unit: V
1
11/30/2025
Contents
• Algebraic Computation
• Fast Fourier Transform
• String Matching
• Theory of NP-completeness
• Approximation algorithms
• Randomized algorithms.
11/30/2025 2
Course Objective
11/30/2025 3
Unit 5-Objective
11/30/2025 4
Course Outcome
At the end of the semester, the student will be able:
Description Bloom’s Taxonomy
CO1 To have knowledge of basic principles of algorithm design and Knowledge, analysis
Analysis, asymptotic notations and growth of functions for time And design
and space complexity analysis and applying the same in
different sorting algorithms
CO2 To apply different problem-solving approaches for advanced Knowledge, analysis
data structures And apply
CO3 To apply divide and conquer method for solving merge sort, Knowledge, analysis and
quick sort, matrix multiplication and Greedy Algorithm for Apply
solving different Graph Problem.
CO4 To analyze and apply different optimization techniques like Knowledge, Analysis And
dynamic programming, backtracking and Branch & Bound to Apply
solve the complex problems
CO5 To understand the advanced concepts like NP Completeness and Knowledge, Analysis and
Fast Fourier Transform, to analyze and apply String Matching, Apply
Approximation and Randomized Algorithms to solve the
complex problems
11/30/2025 5
Programme Outcome
11/30/2025 6
CO-PO and PSO Mapping
CO.K PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
RCS-502.1 3 3 3 3 2 - - - 2 2 - 3
RCS-502.2 3 3 3 3 2 2 - 1 1 1 - 3
RCS-502.3 3 3 2 3 3 2 - 2 1 1 2 3
RCS-502.4 3 3 3 3 2 2 - 2 2 1 3 3
RCS-502.5 2 2 2 2 2 2 - 2 1 1 1 2
Average 2.8 2.8 2.6 2.8 2.2 1.6 - 1.8 1.4 1.2 1.2 2.8
11/30/2025 7
CO-PO and PSO Mapping
RCS-501.1 3 1 3 1
RCS-501.2 3 1 3 1
RCS-501.3 3 1 3 1
RCS-501.4 3 1 3 1
RCS-501.5 3 1 3 1
Average 3 1 3 1
11/30/2025 8
Algebraic Computation(CO5)
Objective
11/30/2025 9
Prerequisite and Recap
• Prerequisite
• Mathematical concepts of Limits and differentiation
• Recap
• Techniques of solving real world problems like dynamic,
backtracking and branch & bound.
11/30/2025 10
Algebraic Computation(CO5)
11/30/2025 11
Algebraic Computation(CO5)
Algebraic Structures
• Basic requirements
– precise representation of algebraic structures
11/30/2025 12
Algebraic Computation(CO5)
• Can be represented by
– Representation of integers
– Representation of polynomials
– Representation of expressions
11/30/2025 13
, Algebraic Computation(CO5)
• Fourier analysis converts a signal from its original domain (often time
or space) to a representation in the frequency domain and vice versa.
11/30/2025 14
, Algebraic Computation(CO5)
Fast Fourier transformation
• The difference in speed can be enormous, especially for long data sets
where N may be in the thousands or millions.
11/30/2025 15
Algebraic Computation(CO5)
DFT(a0,a1,…,an−1) =(y0,y1,…,yn−1)
=(A(wn,0),A(wn,1),…,A(wn,n−1)
=(A(w0n),A(w1n),…,A(wn−1n))
11/30/2025 17
Algebraic Computation(CO5)
Inverse DFT
InverseDFT(y0,y1,…,yn−1) = (a0,a1,…,an−1)
11/30/2025 18
String Matching(CO5)
Objective
11/30/2025 19
Prerequisite and Recap
Prerequisite
• Algorithms
• Finite automata
Recap
• Algebraic Computation
11/30/2025 20
String Matching (CO5)
• The item of P and T are character drawn from some finite alphabet
such as {0, 1} or {A, B .....Z, a, b..... z}.
11/30/2025 21
String Matching (CO5)
11/30/2025 22
String Matching(CO5)
• Using these descriptions, we can say given any string T [1......n], the
substrings are
T [i.....j] = T [i] T [i +1] T [i+2]......T [j] for some 0≤i ≤ j≤n-1.
• Note: If i>j, then T [i.....j] is equal to the empty string or null, which
has length zero.
11/30/2025 23
String Matching(CO5)
• The Rabin-Karp-Algorithm
• Finite Automata
11/30/2025 24
String Matching(CO5)
The naïve 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 value of s.
11/30/2025 25
String Matching(CO5)
NAIVE-STRING-MATCHER (T, P)
• 1. n ← length [T]
• 2. m ← length [P]
• 3. for s ← 0 to n -m
• 4. do if P [1.....m] = T [s + 1....s + m]
• 5. then print "Pattern occurs with shift" s
Analysis: This for loop from 3 to 5 executes for n-m + 1(we need at
least m characters at the end) times and in iteration we are doing m
comparisons. So the total complexity is O (n-m+1)
11/30/2025 26
String Matching(CO5)
• Example:
Suppose T = 1011101110 , P = 111 . Find all the Valid Shift
Solution:
•
11/30/2025 27
String Matching(CO5)
11/30/2025 28
String Matching Algorithm (CO5)
11/30/2025 29
String Matching(CO5)
• If the hash values are unequal, the algorithm will determine the
hash value for next M-character sequence.
• If the hash values are equal, the algorithm will analyze the pattern
and the M-character sequence.
• In this way, there is only one comparison per text subsequence, and
character matching is only required when the hash values match.
11/30/2025 30
String Matching(CO5)
Rabin Karp Matcher(T, P, d, q)
1. nlength[T]
2. mlength[P]
3. hdm-1 mod q
4. p0
5. t0 0
6. For i1 to m (preprocessing)
7. do p(dp +P[i])modq
8. t0 (d t0 +T[i])modq
9. For s0 to n-m (matching)
10. do if p = ts
11. then if P[1…..m] = T[s +1,……..s+m]
12. then “Pattern occurs with shift” s
[Link] s< n-m
14. then ts+1 (d(ts – T[s+1]h) + T[s+m+1]) mod q
11/30/2025 31
String Matching(CO5)
Rabin- Karp- Algorithm
Complexity: The running time of RABIN-KARP-MATCHER in the worst
case scenario O ((n-m+1) m but it has a good average case running time.
If the expected number of strong shifts is small O (1) and prime q is
chosen to be quite large, then the Rabin-Karp algorithm can be
expected to run in time O (n+m) plus the time to require to process
spurious hits.
T = 31415926535.......
P = 26
Here [Link] =11 so Q = 11
And P mod Q = 26 mod 11 = 4
Now find the exact match of P mod Q...
11/30/2025 32
String Matching(CO5)
Solution:
11/30/2025 33
String Matching(CO5)
11/30/2025 34
String Matching(CO5)
11/30/2025 35
String Matching(CO5)
11/30/2025 36
String Matching(CO5)
Rabin- Karp- Algorithm
• The string-matching automaton is a very useful tool which is used in
string matching algorithm.
• It examines every character in the text exactly once and reports all
the valid shifts in O (n) time.
11/30/2025 37
String Matching(CO5)
String Matching With Finite Automata
• The finite automaton starts in state q0 and reads the characters of its
input string one at a time.
• If the automaton is in state q and reads input character a, it moves
from state q to state δ (q, a).
• Whenever its current state q is a member of A, the machine M has
accepted the string read so far. An input that is not allowed
is rejected.
11/30/2025 38
String Matching(CO5)
11/30/2025 39
String Matching(CO5)
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.
11/30/2025 40
String Matching(CO5)
The Prefix Function (Π)
Following pseudo code compute the prefix function, Π:
Compute-Prefix-function(P)
1. nlength[P]
2. Π[1]0
3. K0
4. for q2 to m
5. do while k > 0 and P[k+1] ≠ P[q]
6. do k Π[k]
7. if P[k+1] = P[q]
8. then k k+1
9. Π[q]k
[Link] Π
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).
11/30/2025 41
String Matching(CO5)
Solution:
Initially: m = length [p] = 7 Π [1] = 0 k = 0
11/30/2025 42
String Matching(CO5)
11/30/2025 43
String Matching(CO5)
11/30/2025 44
String Matching(CO5)
11/30/2025 45
String Matching(CO5)
11/30/2025 46
String Matching(CO5)
Boyer and Moore Algorithm
Bad Character Heuristics
11/30/2025 47
String Matching(CO5)
11/30/2025 48
NPCompleteness(CO5)
Objective
11/30/2025 49
Prerequisite and Recap
Prerequisite
• Different Problems like graph colouring
• Travelling Salesman Problem
Recap
• String Matching Algorithm
• Algorithms for solving real world problems
11/30/2025 50
NPCompleteness(CO5)
11/30/2025 51
NPCompleteness(CO5)
11/30/2025 52
NPCompleteness(CO5)
• NP-Complete Problems
Following are some NP-Complete problems, for which no polynomial
time algorithm is known.
• Determining whether a graph has a Hamiltonian cycle
• Determining whether a Boolean formula is satisfiable, etc.
• NP-Hard Problems
The following problems are NP-Hard
• The circuit-satisfiability problem
• Set Cover
• Vertex Cover
• Travelling Salesman Problem
11/30/2025 53
NPCompleteness(CO5)
Circuit Satisfiability
According to given decision-based NP problem, we can design the
CIRCUIT and verify a given mentioned output also within the P time.
The CIRCUIT is provided below:-
11/30/2025 54
NPCompleteness(CO5)
Set Cover Problem
11/30/2025 55
NPCompleteness(CO5)
Example:
U = {1,2,3,4,5}, S = {S1,S2,S3}
S1 = {4,1,3}, Cost(S1) = 5
S2 = {2,5}, Cost(S2) = 10
S3 = {1,4,3,2}, Cost(S3) = 3
Output: Minimum cost of set cover is 13 and set cover is {S2, S3}
There are two possible set covers {S1, S2} with cost 15 and {S2, S3} with
cost 13
11/30/2025 56
NPCompleteness(CO5)
Vertex Cover problem
11/30/2025 57
NPCompleteness(CO5)
Example:
The set of edges of the given graph is −
{(1,6),(1,2),(1,4),(2,3),(2,4),(6,7),(4,7),(7,8),(3,8),(3,5),(8,5)}
11/30/2025 58
NPCompleteness(CO5)
11/30/2025 59
NPCompleteness(CO5)
11/30/2025 60
NPCompleteness(CO5)
Travelling Salesman Problem
• The salesman has to visit each one of the cities starting from a
certain one and returning to the same city.
11/30/2025 61
NPCompleteness(CO5)
Proof
• In TSP, we find a tour and check that the tour contains each vertex
once.
• To prove this, one way is to show that Hamiltonian cycle ≤p TSP (as
we know that the Hamiltonian cycle problem is NPcomplete).
Assume G = (V, E) to be an instance of Hamiltonian cycle.
Hence, an instance of TSP is constructed. We create the complete
graph G' = (V, E'), where
E′={(i,j):i,j ∈ V and i≠j
E′={(i,j):i,j ∈V and i≠j
Thus, the cost function is defined as follows −
t(i,j)= 0 if (i,j) ∈ E
=1 otherwise
11/30/2025 63
NPCompleteness(CO5)
• Conversely, we assume that G' has a tour h' of cost at most 0. The
cost of edges in E' are 0 and 1 by definition. Hence, each edge
must have a cost of 0 as the cost of h' is 0. We therefore conclude
that h' contains only edges in E.
11/30/2025 64
NPCompleteness(CO5)
11/30/2025 65
NPCompleteness(CO5)
11/30/2025 66
Weekly Assignment
11/30/2025 67
Old Question Papers(2019-2020)
11/30/2025 68
Old Question Papers(2019-2020)
11/30/2025 69
Old Question Papers(2019-2020)
11/30/2025 70
Old Question Papers(2019-2020)
11/30/2025 71
Old Question Papers(2019-2020)
11/30/2025 72
Old Question Papers(2018-2019)
11/30/2025 73
Old Question Papers(2018-2019)
11/30/2025 74
Old Question Papers(2018-2019)
11/30/2025 75
Old Question Papers(2018-2019)
11/30/2025 76
Old Question Papers(2018-2019)
11/30/2025 77
Old Question Papers(2017-2018)
11/30/2025 78
Old Question Papers(2017-2018)
11/30/2025 79
Old Question Papers(2017-2018)
11/30/2025 80
Old Question Papers(2017-2018)
11/30/2025 81
Old Question Papers(2017-2018)
11/30/2025 82
Expected Questions for University Exam
11/30/2025 83
Summary
11/30/2025 84
References
11/30/2025 85
Unit 5
Thank You
11/30/2025 86