0% found this document useful (0 votes)
18 views17 pages

Mid 2 Daa

DAA

Uploaded by

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

Mid 2 Daa

DAA

Uploaded by

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

DAA MID IMPORTANT QUESTIONS

1. NP-Hard and NP-Complete


2. Difference between Exponential time and Polynomial time
3. Discuss about Sum of Subsets
4. Types of Complexity Classes
5. Discuss the relationship between P, NP, NP-Hard and NP-Complete With a neat
diagram
6. Cook’s Theorem
7. Hamiltonian cycle with an Example
8. Explain steps in 8 Queens problem, Draw minimum three outputs in Backtracking
Method
9. Discuss General Method by using Backtracking
10. Explain steps in OBST using dynamic program and solve n=4; (a1, a2, a3, a4) =
(a, b, c, d), p (1,2,3,4) = (3,3,1,1), q (0,1,2,3,4) = (2,3,1,1,1) [Common in all]
Que: NP-Hard and NP-Complete

NP problems are again classified into 2 types.

I) NP Hard
II) NP Complete.

NP-hard class

An NP-hard problem is at least as hard as the hardest problem in NP and it is the class of the
problems such that every problem in NP reduces to NP-hard.

Features:

1. All NP-hard problems are not in NP.


2. It takes a long time to check them. This means if a solution for an NP-hard problem is given
then it takes a long time to check whether it is right or not.
3. A problem A is in NP-hard if, for every problem L in NP, there exists a polynomial-time
reduction from L to A.

Some of the examples of problems in Np-hard are:

1. Halting problem.
2. Qualified Boolean formulas.
3. No Hamiltonian cycle.

For example:

The halting problem is famously NP-hard but not NP-complete. This means it's at least as hard
as any problem in the NP class, but unlike NP-complete problems, it isn't guaranteed to have a
solution verifiable in polynomial time.

Imagine a machine with two gears:

• Left Gear (Halter): This gear represents a hypothetical program that can solve the
halting problem. It takes two inputs: a program (right gear) and its input (text on a strip).
• Right Gear (Verifier): This gear represents a program trying to solve an NP problem
(e.g., Boolean satisfiability). It has a slot for the input related to the NP problem.

NP-complete class

A problem is NP-complete if it is both NP and NP-hard. NP-complete problems are the hardest
problems in NP.
In mathematics, if all statements of a system can be explained, it is said to be complete.
NP complete problems are powerful problems that can solve all problems of class NP and
hence these problems are said to be NP complete.

Features:

1. NP-complete problems are special as any problem in NP class can be transformed or reduced
into NP-complete problems in polynomial time.
2. If one could solve an NP-complete problem in polynomial time, then one could also solve
any NP problem in polynomial time.

Some example problems include:


1. 0/1 Knapsack.
2. Hamiltonian Cycle.
3. Satisfiability.
4. Vertex cover.

For Example:

Vertex Cover (VC) Problem

To prove VC is NP-complete we have to prove the following −

• VC is Non-deterministic Polynomial (NP).


• A NPC problem can be reduced into VC.

To prove VC is NP, find a verifier which is a subset of vertices which is VC and that can be
verified in polynomial time. For a graph of n vertices it can be proved in O(n2). Thus, VC is
NP.

Now consider the “clique” problem which is NPC and reduce it into VC to prove NPC. Clique
of a graph G is a subset of vertices such that these vertices form a complete subgraph in the
given graph G.

The two graphs titles (a) and (b) are given below for the VC problem −
Consider graph (a), here clique is {a,b,c,d}.

Now calculate a graph as shown in (b) which is as follows −

Completed graph of all the vertices in (a) – (a)

For the graph (b), we can say that the vertex cover is {s,t} which covers all the edges of the
(b). This {s,t} = {a,b,c,d,s,t} – {Clique of (a)} Thus conversely, we can say that we can reduce
clique into the VC problem and conversely can find both VC and clique of a given undirected
graph. This means VC is NP-complete reducible.

Hence proved that VC is an NPC.

Que: Difference between Exponential time and Polynomial time

Rank Feature Exponential Time Polynomial Time


Explodes very rapidly with Increases steadily with increasing
1 Growth Rate
increasing input size input size
O(n^k), O(n log n) (k being any
2 Big O Notation O(a^n), O(2^n)
non-negative integer)
Real-world
3 Impractical for large inputs Manageable for large inputs
Performance
Sorting algorithms (Merge Sort,
Brute-force search, Power Set
4 Examples Quick Sort), String matching
Generation
algorithms (Boyer-Moore)
Often infeasible for even
5 Feasibility Often feasible for very large inputs
moderately sized inputs
Small changes in input size can
Impact of Input Execution time increases
6 lead to massive execution time
Size proportionally with input size
differences
Time taken grows like building Time taken grows like a simple
7 Description blocks stacked on top of each mathematical expression involving
other n
Imagine a graph where the y-axis
Imagine a graph where the y-
8 Visualization increases steadily but not as
axis shoots up very quickly
dramatically
Useful for very small input sizes Preferred for practical applications
9 Suitability
or theoretical problems. with potentially large datasets
Exponential time algorithms are Polynomial time algorithms are
NP- unlikely to be NP-complete candidates for being NP-complete
10
Completeness (though some rare exceptions (if also verifiable in polynomial
exist) time)
Que: Discuss about Sum of Subsets

The "sum of subsets" problem is a classic problem in combinatorial optimization. It involves


finding subsets of a given set of numbers whose sum matches a particular target sum.

The problem can be stated as follows:

Given a set of numbers, find all possible subsets whose sum equals a given target sum.

This problem is often solved using techniques such as backtracking, dynamic programming, or
bitmasking.
Que: Types of Complexity Classes

Complexity classes categorize computational problems based on their inherent difficulty and
the amount of computational resources required to solve them. Here are some common
complexity classes:
P Class

The P in the P class stands for Polynomial Time. It is the collection of decision
problems(problems with a “yes” or “no” answer) that can be solved by a deterministic machine
in polynomial time.

Features:

1. The solution to P problems is easy to find.

2. P is often a class of computational problems that are solvable and tractable.

3. Tractable means that the problems can be solved in theory as well as in practice. But the
problems that can be solved in theory but not in practice are known as intractable.

This class contains many natural problems like:

1. Calculating the greatest common divisor.


2. Finding a maximum matching.
3. Decision versions of linear programming.

NP Class

The NP in NP class stands for Non-deterministic Polynomial Time. It is the collection of


decision problems that can be solved by a non-deterministic machine in polynomial time.

Features:

1. The solutions of the NP class are hard to find since they are being solved by a non-
deterministic machine but the solutions are easy to verify.
2. Problems of NP can be verified by a Turing machine in polynomial time.
Example:
This class contains many problems that one would like to be able to solve effectively:
1. Boolean Satisfiability Problem (SAT).
2. Hamiltonian Path Problem.
3. Graph coloring.

NP problems are again classified into 2 types. I) NP Hard II) NP Complete.

[Already discussed above-NP HARD &COMPLETE]


Que: Discuss the relationship between P, NP, NP-Hard and NP-Complete With a neat
diagram

Complexity
Characteristic feature
Class

P Easily solvable in polynomial time.

NP Yes, answers can be checked in polynomial time.

Co-NP No, answers can be checked in polynomial time.

All NP-hard problems are not in NP and it takes a long time to check
NP-hard
them.

NP-complete A problem that is NP and NP-hard is NP-complete.

Theory should write about p,np,np complete and hard

Que: Cook’s Theorem

Cook’s theorem

• In computational complexity theory, the Cook–Levin theorem, also known as Cook’s


theorem, states that the Boolean satisfiability problem is NP-complete.
• That is, it is in NP, and any problem in NP can be reduced in polynomial time by a
deterministic Turing machine to the Boolean satisfiability problem.
• Stephen Arthur Cook and L.A. Levin in 1973 independently proved that the satisfiability
problem(SAT) is NP-complete.
SAT is a significant problem and can be stated as follows:

Given a Boolean expression F having n variables x1,x2,….,xn, and Boolean operators, is it


possible to have an assignment for variables true or false such that binary expression F is true?
This problem is also known as the formula – SAT.

An SAT(formula-SAT or simply SAT) takes a Boolean expression F and checks whether the
given expression(or formula) is satisfiable.

A Boolean expression is said to be satisfactory for some valid assignments of variables if the
evaluation comes to be true.

Some important terminologies of a Boolean expression:

Boolean variable: A variable, say x, that can have only two values, true or false, is called a
boolean variable.

Literal: A literal can be a logical variable, say x, or the negation of it, that is x or x̄; x is called
a positive literal, and x̄ is called the negative literal.

Clause: A sequence of variables(x1,x2,….,xn) that can be separated by a logical OR operator


is called a clause. For example, (x1 V x2 V x3) is a clause of three literals.

Expressions: One can combine all the preceding clauses using a Boolean operator to form an
expression.

CNF form: An expression is in CNF form(conjunctive normal form) if the set of clauses are
separated by an AND (^), operator, while the literals are connected by an OR (v) operator.

The following is an example of an expression in the CNF form:


f = (x1 V x̄2 V x3) ∧ (x1 V x̄3 V x2)
3 – CNF: An expression is said to be in 3-CNF if it is in the conjunctive normal form, and
every clause has exact three literals.
Thus, an SAT is one of the toughest problems, as there is no known algorithm other than the
brute force approach.
A brute force algorithm would be an exponential-time algorithm, as 2n possible assignments
need to be tried to check whether the given Boolean expression is true or not. Stephen Cook
and Leonid Levin proved that the SAT is NP-complete.

3-CNF-SAT(3-SAT)
This problem contains the expression is in a conjunctive normal form and that every clause
should contain exactly three literals.
This problem is also about assigning n assignments of truth values to n variables of the Boolean
expression such that the output of the expression is true.
In simple words, given an expression in 3-CNF, a 3-SAT problem is to check whether the given
expression is satisfiable.
If we consider,
f = (x1 V x̄2 V x3) ∧ (x1 V x2 V x̄3 )

it is the Boolean satisfiability problem of 3 literals.. So every literal can take either 0 or 1 as
input Values.

i.e., x1 = 0 or 1; x2 = 0 or 1; x3 = 0 or 1.. we are having 3 literals and every literal with 2 inputs
. So we can have a total of 2n = 23 = 8 possible outcomes.

For inputs x1 = 0 ; x2 = 0 ; x3 = 0 =>f = (x1 V x̄2 V x3) ∧ (x1 V x2 V x̄3)


=>f = (0 V 𝟎̅ V 0 ) ∧ (0 V 0 V 𝟎̅ ) => 1 ∧ 1 = 1 i.e, True..
x1 = 1 ; x2 = 1 ; x3 = 1 =>f = (x1 V x̄2 V x3) ∧ (x1 V x2 V x̄3)
=>f = (1 V 𝟏̅ V 1 ) ∧ (1 V 1 V 𝟏̅) => 1 ∧ 1 = 1 i.e, True..

Likewise, for all the values , the solution will be true..


The Boolean expression satisfiable for all the valid assignments of variables and the evaluation
comes to be true.Hence, the theorem is proved..
Que: Hamiltonian cycle with an Example

What is Hamiltonian Path?


Hamiltonian Path in a graph G is a path that visits every vertex of G exactly once
and Hamiltonian Path doesn’t have to return to the starting vertex. It’s an open path.
• Similar to the Hamiltonian Cycle problem, finding a Hamiltonian Path in a
general graph is also NP-complete and can be challenging. However, it is often a
more easier problem than finding a Hamiltonian Cycle.
• Hamiltonian Paths have applications in various fields, such as finding optimal
routes in transportation networks, circuit design, and graph theory research.

Hamiltonian Cycle using Backtracking Algorithm:

Create an empty path array and add vertex 0 to it. Add other vertices, starting from the
vertex 1. Before adding a vertex, check for whether it is adjacent to the previously added
vertex and not already added. If we find such a vertex, we add the vertex as part of the
solution. If we do not find a vertex then we return false.
Illustrations:
Let’s find out the Hamiltonian cycle for the following graph:

• Start with the node 0 .


• Apply DFS for finding the Hamiltonian path.
• When base case reach (i.e. total no of node traversed == V (total vertex)):
• Check weather current node is a neighbour of starting node.
• As node 2 and node 0 are not neighbours of each other so return
from it.

Starting from start node 0 calling DFS

• As cycle is not found in path {0, 3, 1, 4, 2}. So, return from node 2, node 4.
• Now, explore another option for node 1 (i.e node 2)
• When it hits the base condition again check for Hamiltonian cycle
• As node 4 is not the neighbour of node 0, again cycle is not found then return.

• Return from node 4, node 2, node 1.

• Now, explore other options for node 3.


Hamiltonian Cycle

• In the Hamiltonian path {0,3,4,2,1,0} we get cycle as node 1 is the neighbour of


node 0.
• So print this cyclic path .
• This is our Hamiltonian cycle.
Que: Explain steps in 8 Queens problem, Draw minimum three outputs in Backtracking
Method
Que: Discuss General Method by using Backtracking

You might also like