0% found this document useful (0 votes)
8 views94 pages

Unit 5 Daa

Uploaded by

samay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views94 pages

Unit 5 Daa

Uploaded by

samay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 94

Noida Institute of Engineering and Technology, Greater Noida

String Matching, Approximation Algorithms and Theory of NP Completeness

Unit: 5

Design & Analysis of Algorithms


ACSE0501
Anshul Varshney
B.Tech 5th Sem Assistant Professor
CS

Anshul Varshney DAA


12/19/2024 Unit V
Content

• Selected Topics
• String Matching
• Theory of NP-completeness
• Approximation algorithms
• Randomized algorithms.

19/12/2024 Anshul Varshney DAA 2


Unit V
Course Objective

• Upon completion of this course, students will be able to do the following:


• Analyze the asymptotic performance of algorithms.
• Write rigorous correctness proofs for algorithms.
• Demonstrate a familiarity with major algorithms and data structures.
• Apply important algorithmic design paradigms and methods of analysis.
• Synthesize efficient algorithms in common engineering design situations.

19/12/2024 Anshul Varshney DAA 3


Unit V
Brief Introduction About Me

• Name : Anshul Varshney

• Designation :Assistant Professor CS Department


• NIET Greater Noida

• Qualification:

• B.C.A. A.C.E.T. Aligarh.


• M.C.A. Dr. A.P.J. Abdul Kalam Technical University, Lucknow 2016.

19/12/2024 Anshul Varshney DAA 4


Unit V
Evaluation
Scheme

Anshul Varshney DAA


Unit V 12/19/2024 5
Syllabus

Anshul Varshney DAA


Unit V 12/19/2024 6
Branch wise Application

• First, we will start with the internet which is very much important for our daily life, and we cannot
even imagine our life without the internet, and it is the outcome of clever and creative algorithms.
Numerous sites on the internet can operate and falsify this huge number of data only with the help
of these algorithms.

• The everyday electronic commerce activities are massively subject to our data, for example, credit
or debit card numbers, passwords, OTPs, and many more. The center technologies used
incorporate public-key cryptocurrency and digital signatures which depend on mathematical
algorithms.

• Even an application that doesn't need algorithm content at the application level depends vigorously
on the algorithm as the application relies upon hardware, GUI, networking, or object direction and
all of these create a substantial use of algorithms.

• There are some other vital use cases where the algorithm has been used such as if we watch any
video on YouTube then next time, we will get related-type advice as recommended videos for us.

19/12/2024 Anshul Varshney DAA 7


Unit V
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 Analysis, asymptotic Knowledge, analysis
notations and growth of functions for time and space complexity analysis and And design
applying the same in different sorting algorithms

CO2 To apply different problem-solving approaches for advanced data structures Knowledge, analysis
And apply
CO3 To apply divide and conquer method for solving merge sort, quick sort, matrix Knowledge, analysis and Apply
multiplication and Greedy Algorithm for solving different Graph Problem.

CO4 To analyze and apply different optimization techniques like dynamic programming, Knowledge, Analysis And Apply
backtracking and Branch & Bound to solve the complex problems

CO5 To understand the advanced concepts like NP Completeness and Fast Fourier Knowledge, Analysis and Apply
Transform, to analyze and apply String Matching, Approximation and Randomized
Algorithms to solve the complex problems

19/12/2024 Anshul Varshney DAA 8


Unit V
Program Outcome

• At the end of the semester, the student will be able:

POs Engineering Graduates will be able to


PO1 Engineering Knowledge
PO2 Problem Analysis
PO3 Design & Development of solutions
PO4 Conduct Investigation of complex problems
PO5 Modern Tool Usage
PO6 The Engineer and Society
PO7 Environment and sustainability
PO8 Ethics
PO9 Individual & Team work
PO10 Communication
PO11 Project management and Finance
PO12 Life Long Learning

19/12/2024 Anshul Varshney DAA 9


Unit V
Program Outcome

• At the end of the semester, the student will be able:

Engineering Apply the knowledge of mathematics, science, engineering


knowledge
fundamentals, and an engineering specialization to the solution of
complex engineering problems.
Problem analysis : Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first
principles of mathematics, natural sciences, and engineering
sciences.
Design solutions for complex engineering problems and design
Design/developme
nt of solutions
system components or processes that meet the specified needs with
appropriate consideration for the public health and safety, and the
cultural, societal, and environmental considerations.
Conduct Use research-based knowledge and research methods including
investigations of
complex problems
design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.
Modern tool usage Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to
complex
Anshul Varshney
engineering activities with DAAan understanding of the
19/12/2024 10
limitations. Unit V
Program Outcome

• At the end of the semester, the student will be able:

The engineer and Apply reasoning informed by the contextual knowledge to assess
society
societal, health, safety, legal and cultural issues and the
consequent responsibilities relevant to the professional engineering
practice.
Environment and Understand the impact of the professional engineering solutions in
sustainability
societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development.
Ethics Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.

Individual and Function effectively as an individual, and as a member or leader in


teamwork
diverse teams, and in multidisciplinary settings.
Communication : Communicate effectively on complex engineering activities with
the engineering community and with society at large, such as, being
able to comprehend and write effective reports and design
documentation, make effective presentations, and give and receive
clear.
Anshul Varshney DAA
19/12/2024 11
Unit V
Program Outcome

• At the end of the semester, the student will be able:

Project management Demonstrate knowledge and understanding of the engineering and


and finance
management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in
multidisciplinary environments.
Life-long learning Recognize the need for, and have the preparation and ability to
engage in independent and lifelong learning in the broadest context
of technological change.

19/12/2024 Anshul Varshney DAA 12


Unit V
CO-PO and PSO Mapping

Design and Analysis of Algorithm ACSE0501

CO.K PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12

ACSE0501.1 3 3 3 3 2 - - - 2 2 - 3

ACSE0501.2 3 3 3 3 2 2 - 1 1 1 - 3

ACSE0501.3 3 3 2 3 3 2 - 2 1 1 2 3

ACSE0501.4 3 3 3 3 2 2 - 2 2 1 3 3

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

19/12/2024 Anshul Varshney DAA 13


Unit V
Program Educational Objectives(PEOs)

PEO1: To have an excellent scientific and engineering breadth so as to comprehend,


analyze, design and provide sustainable solutions for real-life problems using state-of-
the-art technologies.
PEO2:To have a successful career in industries, to pursue higher studies or to support
entrepreneurial endeavors and to face global challenges.
EO3:To have an effective communication skills, professional attitude, ethical values and
a desire to learn specific knowledge in emerging trends, technologies for research,
innovation and product development and contribution to society.
PEO4: To have life-long learning for up-skilling and re-skilling for successful
professional career as engineer, scientist, entrepreneur and bureaucrat for betterment of
society

19/12/2024 Anshul Varshney DAA 14


Unit V
End Semester Question Paper Template
B TECH
(SEM-V) THEORY EXAMINATION 20__-20__
Design & Analysis of Algorithms
ACSE0501
Time: 3 Hours Total Marks:
100
Note: 1. Attempt all Sections. If require any missing data; then choose
suitably.
SECTION A
1.Attempt all questions in brief.
2 x 10 = 20

12/19/2024 Anshul Varshney DAA 15


Unit V
End Semester Question Paper Template

SECTION B
2. Attempt any three of the following: 3 x 10 = 30

SECTION C
3. Attempt any one part of the following: 1 x 10 = 10
Q. No. Question Marks CO

1 10

2 10

12/19/2024 Anshul Varshney DAA 16


Unit V
End Semester Question Paper Template
4. Attempt any one part of the following: 1 x 10 = 10

5. Attempt any one part of the following: 1 x 10 = 10

6. Attempt any one part of the following: 1 x 10 = 10

12/19/2024 Anshul Varshney DAA 17


Unit V
Prerequisite and Recap

• Prerequisite
• Basic concept of c programming language.
• Concept of stack, queue and link list.

• Recap
• Flow Chart
• Algorithm

12/19/2024 Anshul Varshney DAA 18


Unit V
Unit Objective

• This objective this unit is to make students understand about

• Different classes of problems , P Class, NP class, NPC.

• Different string-matching algorithms.

• The Approximation algorithm and Randomized algorithm

12/19/2024 Anshul Varshney DAA 19


Unit V
Topic Objective

This objective this topic is to make students understand about


• Computational Mathematics.

• Algebraic Structures

• Representation

12/19/2024 Anshul Varshney DAA 20


Unit V
String Matching(CO5)
Objective

This objective this topic is to make students understand about


• Concepts of String Matching
• Naïve String Matching
• Rabin Karp Algorithm
• Knuth Morris Pratt
• String Matching with automata
• Boyer Moore Algorithm

12/19/2024 Anshul Varshney DAA 21


Unit V
String Matching(CO5)
Objective

This objective this topic is to make students understand about


• Concepts of String Matching
• Naïve String Matching
• Rabin Karp Algorithm
• Knuth Morris Pratt
• String Matching with automata
• Boyer Moore Algorithm

12/19/2024 Anshul Varshney DAA 22


Unit V
Prerequisite and Recap

Prerequisite
• Algorithms
• Finite automata

Recap
• Algebraic Computation

12/19/2024 Anshul Varshney DAA 23


Unit V
String Matching (CO5)

• 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. String
matching is also used in the Database schema, Network systems.
• Let us look at a few string-matching algorithms before proceeding to their applications in real
world. String Matching Algorithms can broadly be classified into two types of algorithms –
• Given a text array, T [1.....n], of an integer character and a pattern array, P [1......m], of m
characters. The problems are to find r s, called valid shift where 0 ≤ s < n-m and
T [s+1......s+m] = P [1......m].
• In other words, to find even if P in T, i.e., where P is a substring of T.
• The item of P and T are character drawn from some finite alphabet such as {0, 1} or {A,
B .....Z, a, b..... z}.

12/19/2024 Anshul Varshney DAA 24


Unit V
String Matching (CO5)

• Given a string T [1......n], the substrings are represented as T [i......j] for some 0≤i ≤ j≤n-1,
the string formed by the characters in T from index i to index j, inclusive. This process that
a string is a substring of itself (take i = 0 and j =m).
• The proper substring of string T [1......n] is T [1......j] for some 0<i ≤ j≤n-1. That is, we
must have either i>0 or j < m-1.
• 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.
And proper 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.

12/19/2024 Anshul Varshney DAA 25


Unit V
String Matching (CO5)

Algorithms used for String Matching:


There are different types of method is used to finding the string

• The Naive String-Matching Algorithm

• The Rabin-Karp-Algorithm

• Finite Automata

• The Knuth-Morris-Pratt Algorithm

• The Boyer-Moore Algorithm


12/19/2024 Anshul Varshney DAA 26
Unit V
String Matching (CO5)

Naïve String-Matching Algorithm

The naïve approach tests all the possible placement of Pattern P [1.......m] relative to text T
[1......n]. We try shift s = 0, 1.......n-m, successively and for each shift s. Compare T
[s+1.......s+m] to P [1......m].

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

12/19/2024 Anshul Varshney DAA 27


Unit V
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)

12/19/2024 Anshul Varshney DAA 28


Unit V
String Matching (CO5)

• Suppose T = 1011101110 P = 111 Find all the Valid Shift


• Solution:

12/19/2024 Anshul Varshney DAA 29


Unit V
String Matching (CO5)

12/19/2024 Anshul Varshney DAA 30


Unit V
String Matching (CO5)

12/19/2024 Anshul Varshney DAA 31


Unit V
String Matching (CO5)

Rabin- Karp- Algorithm

• The Rabin-Karp string matching algorithm calculates a hash value for the pattern, as well as for
each M-character subsequences of text to be compared.

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

12/19/2024 Anshul Varshney DAA 32


Unit V
String Matching (CO5)

Rabin Karp Matcher(T, P, d, q)

9. For s0 to n-m (matching)


1. nlength[T] 10. do if p = ts
2. mlength[P] 11. then if P[1…..m] = T[s +1,……..s+m]
3. hdm-1 mod q 12. then “Pattern occurs with shift” s
4. p0 13. If s< n-m
5. t0 0 14. then ts+1 (d(ts – T[s+1]h) + T[s+m+1]) mod q
6. For i1 to m (preprocessing)
7. do p(dp +P[i])modq
8. t0 (d t0 +T[i])modq

12/19/2024 Anshul Varshney DAA 33


Unit V
String Matching (CO5)

Rabin- Karp- Algorithm

• For string matching, working module q = 11, how many spurious hits does the Rabin-Karp
matcher encounters in Text T = 31415926535.......

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

12/19/2024 Anshul Varshney DAA 34


Unit V
String Matching (CO5)

Rabin- Karp-
Algorithm

•Solution:

12/19/2024 Anshul Varshney DAA 35


Unit V
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.
Anshul Varshney
12/19/2024 DAA Unit V 36
String Matching (CO5)

String Matching With Finite Automata


A finite automaton M is a 5-tuple (Q, q0,A,∑δ), where
• Q is a finite set of states,
• q0 ∈ Q is the start state,
• A ⊆ Q is a notable set of accepting states,
• ∑ is a finite input alphabet,
• δ is a function from Q x ∑ into Q called the transition function of M.

• 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.
Anshul Varshney DAA
12/19/2024 37
Unit V
String Matching (CO5)

Knuth-Morris and Pratt Algorithm

• We have discussed the Naive pattern-searching algorithm in the previous. The worst-case
complexity of the Naive algorithm is O(m(n-m+1)). The time complexity of the KMP
algorithm is O(n+m) in the worst case.

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

12/19/2024 Anshul Varshney DAA 38


Unit V
String Matching (CO5)

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.

12/19/2024 Anshul Varshney DAA 39


Unit V
String Matching (CO5)

The Prefix Function (Π)


Following pseudo code compute the prefix function, Π:
Compute-Prefix-function(P)
1. nlength[P] //'p' pattern to be matched
2. Π[1]0
3. K0
4. for q2 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
10.Return Π

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
12/19/2024 constant time. Hence the running time ofUnit
Anshul Varshney
computing
V
DAA
prefix function is O (m).
40
String Matching (CO5)

Knuth-Morris and Pratt Algorithm


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

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

12/19/2024 Anshul Varshney DAA 41


Unit V
String Matching (CO5)

Knuth- Morris and Pratt(KMP) algorithm

12/19/2024 Anshul Varshney DAA 42


Unit V
String Matching (CO5)

Knuth- Morris and Pratt(KMP) algorithm

12/19/2024 Anshul Varshney DAA 43


Unit V
String Matching (CO5)

Boyer and Moore Algorithm


• Robert Boyer and J Strother Moore established it in 1977. The B-M String search algorithm
is a particularly efficient algorithm and has served as a standard benchmark for string search
algorithm ever since.

• The B-M algorithm takes a 'backward' approach: the pattern string (P) is aligned with the
start of the text string (T), and then compares the characters of a pattern from right to left,
beginning with rightmost character.

• If a character is compared that is not within the pattern, no match can be found by analysing
any further aspects at this position so the pattern can be changed entirely past the
mismatching character.

12/19/2024 Anshul Varshney DAA 44


Unit V
String Matching (CO5)

Boyer and Moore Algorithm


• For deciding the possible shifts, B-M algorithm uses two preprocessing strategies
simultaneously. Whenever a mismatch occurs, the algorithm calculates a variation using
both approaches and selects the more significant shift thus, if make use of the most effective
strategy for each case.

• The two strategies are called heuristics of B - M as they are used to reduce the search. They
are
• Bad Character Heuristics
• Good Suffix Heuristics

12/19/2024 Anshul Varshney DAA 45


Unit V
String Matching (CO5)

Boyer and Moore Algorithm

Bad Character Heuristics


This Heuristics has two implications:

• Suppose there is a character in a text in which does not occur in a pattern at all. When a
mismatch happens at this character (called as bad character), the whole pattern can be
changed, begin matching form substring next to this 'bad character.’

• On the other hand, it might be that a bad character is present in the pattern, in this case,
align the nature of the pattern with a bad character in the text.

• Thus, in any case shift may be higher than one.


12/19/2024 Anshul Varshney DAA 46
Unit V
String Matching (CO5)

Good Suffix Heuristics:


• A good suffix is a suffix that has matched successfully.
• After a mismatch which has a negative shift in bad character heuristics, look if a substring of pattern
matched till bad character has a good suffix in it, if it is so then we have an onward jump equal to the
length of suffix found.
• This objective this topic is to make students understand about
• Different Class of problems
• P Class
• NP Class
• NPC
• Hard Problems
• Approximation and Randomized algorithm
12/19/2024 Anshul Varshney DAA 47
Unit V
Prerequisite and Recap

Prerequisite
• Different Problems like graph colouring
• Travelling Salesman Problem

Recap
• String Matching Algorithm
• Algorithms for solving real world problems

12/19/2024 Anshul Varshney DAA 48


Unit V
NP Completeness(CO5)

• A problem is in the class NPC if it is in NP and is as hard as any problem in NP. A problem
is NP-hard if all problems in NP are polynomial time reducible to it, even though it may not
be in NP itself.

• If a polynomial time algorithm exists for any of these problems, all problems in NP would
be polynomial time solvable. These problems are called NP-complete. The phenomenon
of NP-completeness is important for both theoretical and practical reasons.
12/19/2024 Anshul Varshney DAA 49
Unit V
NP Completeness(CO5)

A language B is NP-complete if it satisfies two conditions


B is in NP
Every A in NP is polynomial time reducible to B.

If a language satisfies the second property, but not necessarily the first one, the language B is
known as NP-Hard. Informally, a search problem B is NP-Hard if there exists some NP-
Complete problem A that Turing reduces to B.

The problem in NP-Hard cannot be solved in polynomial time, until P = NP. If a problem is
proved to be NPC, there is no need to waste time on trying to find an efficient algorithm for it.
Instead, we can focus on design approximation algorithm.

12/19/2024 Anshul Varshney DAA 50


Unit V
NP Completeness(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
12/19/2024 Anshul Varshney DAA 51
Unit V
NP Completeness(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:-

Although we can design a circuit and verified the mentioned output within Polynomial time
but remember we can never predict the number of gates which produces the high output
against the set of inputs/high inputs within a polynomial time. So we verified the production
and conversion had been done within polynomial time. So it is NPC.

12/19/2024 Anshul Varshney DAA 52


Unit V
NP Completeness(CO5)

• Set Cover Problem

• Given a universe U of n elements, a collection of subsets of U say S = {S 1, S2…,Sm} where


every subset Si has an associated cost.

• Find a minimum cost subcollection of S that covers all elements of U.

• There is no polynomial time solution available for this problem as the problem is a known
NP-Hard problem

12/19/2024 Anshul Varshney DAA 53


Unit V
NP Completeness(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

12/19/2024 Anshul Varshney DAA 54


Unit V
NP Completeness(CO5)

Vertex Cover problem

The minimum vertex cover problem is the optimization problem of finding a smallest vertex
cover in a given graph. The vertex cover problem is an NP-complete problem.
A vertex-cover of an undirected graph G = (V, E) is a subset of vertices V' ⊆ V such that if
edge (u, v) is an edge of G, then either u in V or v in V' or both.

APPROX-VERTEX_COVER (G: Graph) c ← { } E' ← E[G]


1. while E' is not empty do
2. Let (u, v) be an arbitrary edge of E ' c ← c U {u, v}
3. Remove from E' every edge incident on either u or v
4. return c

12/19/2024 Anshul Varshney DAA 55


Unit V
NP Completeness(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)}

12/19/2024 Anshul Varshney DAA 56


Unit V
NP Completeness(CO5)

• Now, we start by selecting an arbitrary


edge (1,6). We eliminate all the edges, In the next step, we have chosen another edge
which are either incident to vertex 1 or (2,3) at random
6 and we add edge (1,6) to cover.

12/19/2024 Anshul Varshney DAA 57


Unit V
NP Completeness(CO5)

• Now we select another edge (4,7). We select another edge (8,5).

Hence, the vertex cover of this graph is {1,2,4,5}.


Analysis
It is easy to see that the running time of this algorithm is O(V + E), using adjacency list
to represent E'.
12/19/2024 Anshul Varshney DAA 58
Unit V
NP Completeness(CO5)

Travelling Salesman Problem

• The traveling salesman problem consists of a salesman and a set of cities.

• The salesman has to visit each one of the cities starting from a certain one and
returning to the same city.

• The challenge of the problem is that the traveling salesman wants to minimize the total
length of the trip

• Suppose the cities are x1 x2..... xn where cost cij denotes the cost of travelling from
city xi to xj. The travelling salesperson problem is to find a route starting and ending at
x1 that will take in all cities with the minimum cost.
12/19/2024 Anshul Varshney DAA 59
Unit V
NP Completeness(CO5)

Proof

1. To prove TSP is NP-Complete, first we have to prove that TSP belongs to NP.

• In TSP, we find a tour and check that the tour contains each vertex once.

• Then the total cost of the edges of the tour is calculated.

• Finally, we check if the cost is minimum.

• This can be completed in polynomial time.

• Thus, TSP belongs to NP.


12/19/2024 Anshul Varshney DAA 60
Unit V
NP Completeness(CO5)

2. Secondly, we have to prove that TSP is NP-hard.


• To prove this, one way is to show that Hamiltonian cycle ≤p TSP (as we know that the
Hamiltonian cycle problem is NP-complete).
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
Anshul Varshney
=1 otherwise DAA
12/19/2024 61
Unit V
NP Completeness(CO5)

• Now, suppose that a Hamiltonian cycle h exists in G. It is clear that the cost of each
edge in h is 0 in G' as each edge belongs to E. Therefore, h has a cost of 0 in G'. Thus,
if graph G has a Hamiltonian cycle, then graph G' has a tour of 0 cost.

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

• We have thus proven that G has a Hamiltonian cycle, if and only if G' has a tour of cost
at most 0. TSP is NP-complete.

12/19/2024 Anshul Varshney DAA 62


Unit V
NP Completeness(CO5)

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

A simple example of an approximation algorithm is one for the Minimum Vertex Cover
problem, where the goal is to choose the smallest set of vertices such that every edge in
the input graph contains at least one chosen vertex.

12/19/2024 Anshul Varshney DAA 63


Unit V
NP Completeness(CO5)

An algorithm that uses random numbers to decide what to do next anywhere in its logic is
called Randomized Algorithm.. For example, in Randomized Quick Sort, we use
random number to pick the next pivot (or we randomly shuffle the array). And in
Karger's algorithm, we randomly pick an edge..

For example:
Select a random number from stream, with O(1) space.
Birthday Paradox
Linearity of Expectation
Random Number generator in arbitrary probability distribution fashion

12/19/2024 Anshul Varshney DAA 64


Unit V
Faculty Video Links, YouTube & NPTEL Video Links and
Online Courses Details
Self Made Video Link:

Youtube/other Video Links


https://www.youtube.com/watch?v=e2cF8a5aAhE
https://www.youtube.com/watch?v=jFW7fwa0Zm8
https://www.youtube.com/watch?v=MEz1J9wY2iM

12/19/2024 Anshul Varshney DAA 65


Unit V
Daily Quiz

• What does NP stand for in complexity classes theory?


• The hardest of NP problems are ____________.
• Travelling salesman problem belongs to which of the class?
• A problem which is both _______ and _________ is said to be NP complete.
• In terms of NTIME, NP problems are the set of decision problems which can be solved
using a non-deterministic machine in _______ time.
• Problems that can be solved in polynomial time are known as?
• The sum and composition of two polynomials are always polynomials.(True/False).
• .... is the class of decision problems that can be solved by non-deterministic polynomial
algorithms?

12/19/2024 Anshul Varshney DAA 66


Unit V
Weekly Assignment
1.Write Rabin-Karp algorithm. For string matching working modulo q =11, how many
spurious hits does the Rabin-Karp matcher encounter in the text T = 3141592653589793
when looking for pattern P = 26? [CO5]

2.Define NP, NP hard and NP complete. Give example of each. [CO5]

3. Explain Approximation and Randomized algorithms. [CO5]

4. Explain Rabin Karp algorithm. For the text 2359023141526739921 and for the pattern
31415 and working modulo q=13 how many valid match and spurious hits does the Rabin
matcher encounter. [CO5]

5. Write short notes on [CO5]


• Algebraic Computation
• FFT
12/19/2024 Anshul Varshney DAA 67
Unit V
MCQ s

1. Which of the following can be used to define NP complexity class?


a) Verifier
b) Polynomial time
c) Both (a) and (b)
d) None of the mentioned

2. Which of the following are not in NP?


a) All problems in P
b) Boolean Satisfiability problems
c) Integer factorization problem
d) None of the mentioned

12/19/2024 Anshul Varshney DAA 68


Unit V
MCQ s

3. Which of the following contains NP?


a) PSPACE
b) EXPSPACE
c) Both (a) and (b)
d) None of the mentioned

4. State true or false?


Statement: If a problem X is in NP and a polynomial time algorithm for X could also
be used to solve problem Y in polynomial time, then Y is also in NP.
a) true
b) false

12/19/2024 Anshul Varshney DAA 69


Unit V
MCQ s

5. Which of the following is incorrect for the given phrase :’solvable by non-
deterministic algorithms in polynomial time’
a) NP Problems
b) During control flow, non-deterministic algorithm may have more than one choice
c) If the choices that nondeterministic algorithm makes are correct, the amount of
time it takes is bounded by polynomial time.
d) None of the mentioned

6. Which of the following can be used to define NP complexity class?


a) Verifier
b) Polynomial time
c) Both (a) and (b)
d) None of the mentioned

12/19/2024 Anshul Varshney DAA 70


Unit V
MCQ s

7. Which of the following does not belong to the closure properties of NP class?
a) Union
b) Concatenation
c) Reversal
d) Complement

8. The worst-case efficiency of solving a problem in polynomial time is?


a) O(p(n))
b) O(p( n log n))
c) O(p(n2))
d) O(p(m log n))

12/19/2024 Anshul Varshney DAA 71


Unit V
MCQ s

9. Halting problem is an example for?


a) decidable problem
b) undecidable problem
c) complete problem
d) trackable problem

10. A non-deterministic algorithm is said to be non-deterministic polynomial if the time-


efficiency of its verification stage is polynomial.
a) true
b) false

12/19/2024 Anshul Varshney DAA 72


Unit V
Glossary Question

Q.1 ________worst-case efficiency of solving a problem in polynomial time.


a) O(p(n)) b) O(p( n log n))
c) O(p(n2)) d) O(p(m log n))

Q.2 Problems that can be solved in polynomial time are known as____________.
a) intractable b) tractable
c) decision d) complete

Q.3 _________ is the class of decision problems that can be solved by non-deterministic
polynomial algorithms.
a) NP b) P
c) Hard d) Complete

12/19/2024 Anshul Varshney DAA 73


Unit V
Glossary Question

Q.4 Problems that cannot be solved by any algorithm are called _________.
a) tractable problems b) intractable problems
c) undecidable problems d) decidable problems

Q.5 Halting problem is an example for___________.


a) decidable problem b) undecidable problem
c) complete problem d) trackable problem.

Q.6 __________conditions have to be met if an NP- complete problem is polynomially


reducible?
a) 1 b) 2
c) 3 d) 4

12/19/2024 Anshul Varshney DAA 74


Unit V
Glossary Question

Q.7 A CNF-satisfiability problem belong to______


a) NP class b) P class
c) NP complete d) NP hard

Q.8 The choice of polynomial class has led to the development of an extensive theory called
________
a) computational complexity b) time complexity
c) problem complexity d) decision complexity

Q.9 A randomized algorithm uses random bits as input in order to achieve a _____________
good performance over all possible choice of random bits.
a) worst case b) best case
c) average case d) none of the mentioned

12/19/2024 Anshul Varshney DAA 75


Unit V
Old Question Papers(2022-2023)

12/19/2024 Anshul Varshney DAA 76


Unit V
Old Question Papers(2022-2023)

12/19/2024 Anshul Varshney DAA 77


Unit V
Old Question Papers(2022-2023)

12/19/2024 Anshul Varshney DAA 78


Unit V
Old Question Papers(2022-2023)

12/19/2024 Anshul Varshney DAA 79


Unit V
Old Question Papers(2022-2023)

12/19/2024 Anshul Varshney DAA 80


Unit V
Old Question Papers(2022-2023)

12/19/2024 Anshul Varshney DAA 81


Unit V
Old Question Papers(2022-2023)

12/19/2024 Anshul Varshney DAA 82


Unit V
Old Question Papers(2022-2023)

12/19/2024 Anshul Varshney DAA 83


Unit V
Old Question Papers(2023-2024)

12/19/2024 Anshul Varshney DAA 84


Unit V
Old Question Papers(2023-2024)

12/19/2024 Anshul Varshney DAA 85


Unit V
Old Question Papers(2023-2024)

12/19/2024 Anshul Varshney DAA 86


Unit V
Old Question Papers(2023-2024)

12/19/2024 Anshul Varshney DAA 87


Unit V
Old Question Papers(2023-2024)

12/19/2024 Anshul Varshney DAA 88


Unit V
Old Question Papers(2023-2024)

12/19/2024 Anshul Varshney DAA 89


Unit V
Old Question Papers(2023-2024)

12/19/2024 Anshul Varshney DAA 90


Unit V
Old Question Papers(2023-2024)

12/19/2024 Anshul Varshney DAA 91


Unit V
Expected Questions for University Exam

1. What are the different applications of DFT and FFT? [CO5]


2. Discuss the string-matching algorithms along with an example. [CO5]
3. How is approximation algorithm different from randomized algorithm? [CO5]
4. Explain Naive string-matching algorithm. [CO5]
5. Discuss the problem classes, NP, P, NPC along with their relationship. [CO5]
6. How does the Rabin-Karp algorithm handle hash collisions? [CO5]
7. How is the transition table constructed for the finite automaton matcher? [CO5]
8. Explain the KMP algorithm and how it improves upon the naïve string-matching
approach. [CO5]
9. Describe the Boyer-Moore string matching algorithm. [CO5]
10. Explain the concept of polynomial-time reductions and their role in proving NP-
completeness. [CO5]

12/19/2024 Anshul Varshney DAA 92


Unit V
Recap Of Unit

This unit gives the insights of different classes of problems , P Class, NP class, NPC. The
different string-matching algorithms have been discussed. The Approximation algorithm
and Randomized algorithm have also been explained.

12/19/2024 Anshul Varshney DAA 93


Unit V
Unit 5

Thank You

Anshul Varshney
12/19/2024 DAA Unit V 94

You might also like