Daa Part 4

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

POPULAR PUBLICATIONS

TRAC TABL E AND INTRACTABLE PROBLEIVls


~

vr Chapt er at a Glance

• P-NP Class:
Determini stic algorithm: An algorithm having a property that the result of every operatio .
. n IS
uniquely defined is called a deterministic algorithm.
Non- Determini stic Algorithm: An algorithm containing certain operation whose outco~es
are not uniquely defined but are limited to specified set of possibilities, is called a non.
determinis tic algorithm.
The P versus NP problem is to determine whether every language accepted by some
nondeterm inistic algorithm in polynomial time is also accepted by some (deterministic)
algorithm in polynomia l time.
• NP- Complete problem:
The NP-Comp lete problems are an interesting class of problems whose status is unknown.
The properties of NP-Complete problem are:
No polynomia l-time algorithm has been discovered for solving an NP-Compl ete problem
No polynom ial lower bound in respect of time overhead has been· proved for any NP-
Complete problem.

Multiple Choice Type Questio ns

1. Which one is true of the following? [WBUT 2008]


a) All NP hard problems are NP compete
b) All NP complete problems are NP hard
c) Some NP complete problem are NP hard
d) none of these
Answer: (b)
[WBUT 2009]
2. Traveling salesman problem belongs to
a) P class b) NP class c) NP- Hard d) NP-comp lete class
Answer: (d)

3. Consider the following statemen ts: [WBUT 2009]


NP hard problem is a subset of NP complete problem.
Which of the following alternativ es-is true.
a) 1- True, II-False b) Both True
c) Both False d) 1- False, 11- True
Answer: (d)

DAA-106
DESIGN AND ANALYSIS OF ALGORITHMS

. h one is true of the following? [WBUT 2010, 2013]


1
4. W~ ~ 11 NP hard problems are NP complete
a All NP complete problems are NP hard
~: some NP complete problems are NP hard
d) None of these
.Answer: (b)
Travelling Salesman Problem is [WBUT 2012, 2013]
5 b) NP c) NP Complete d) none of these
· a) NP Hard
.Answer: (a)
Which one is true of the following? [WBUT 2014]
6
· a) all NP hard problems are NP complete
b) all NP complete problems are NP hard
c) some.NP complet~ problems are NP hard
d) none of these
.Answer: (c)

Short Answer Type Questions

1. Define classes P, NP and NP complete. [WBUT 2008]


Answer:
Class P and Class NP:
Refer to Question No. 1(a) ofLong Answer Type Questions.
r
Class NP complete:
Refer to Question No. 1(c) ofLong Answer Type Questions.
2. Describe circuit satisfiability problem and prove that circuit-SAT is in NP.
[WBUT 2008]
. 0~
Define circuit satisfiability problem and prove that circuit SAT is in class NP.
[WBUT 2014]
Answer:
Proving that circuit-SAT is in NP, The algorithm V() takes in input the description of a
circuit C and a sequence of n Boolean values x1,x2, ... , Xn and V (C, X1, ••. , Xn) = C(x1, ••• ,
Xn). i.e. V simulates or evaluates the circuit.

Now we have to prove that for every decision problem A in NP, we can find a reduction
from A to CSAT. This is a difficult result to prove, and it is impossible to prove it really
formally without introducing the Turing machine model of computation. We will prove
the result based on the follcwing fact, of which we only give an informal proof.

· DAA-107
POPULAR PUBLICATIONS

S~ppose A is a decision problem that is solvable in p(n) time by some program P, where
n_is the length of the input. Also assume that the input is represented as a sequence of
bits. .
Then, for every fixed n, there is a circuit Cn of size about
O((p(n/). (log p(n))o(IJ)
such that for every input x = (x 1, • •• , Xn) oflength n, we have
A(x) = Cn(x,, ... , Xn)
That is, circuit Cn solves problem A on all the inputs oflength n.
Fu,:therrnore, there exists an efficient algorithm (running in time polynomial in p(n)) that
on mput n and the description of P produces Cn.

3. Describe Clique Decision Problem (CDP). [W_


BUT 2009, 2012]
Answer:
A Clique Decisio11 Problem (CDP) in fin undirected graph G = (V, E) is a subset V' c V
of vertices, each pair of which is connected by an edge in E. In other words, a clique is a
complete subgraph of G. The size of a clique -is the number of vertices it contains. The
clique problem is the optimization problem of finding a ·clique of maximum size in a
graph. As a decision problem, we ask simply whether a clique of a given size k exists in
the graph. The formal definition is
CLIQUE= {(G, k): G is a _graph with a clique o~size k}.

A I algorithm for determining whether a graph G = (V, E) with IVI vertices has a clique of
size k is to list all k-subsets of V , and check each one to see whether it forms' a clique.
t
The running time of this algorithm is n(k2 1Ck)), whicn is polynomial if k is a constant
In, general, however, k could be near IV], in which case the algorithm runs in super-
polynomial time. As one might suspect, an efficient algorithm for the clique problem is
unlikely to exist K

4. What do you mean by non-deterministic algorithms? [WBUT 2009, 2012, 2016]


OR,
What is nondeterministic algorithm? Explain with example. [WBUT 2014]
Answer:
In the theory of computation, a nondeterministic algorithm is an algorithm with one or
more choice points where multiple different continuations are possible, without any
specification of which one will be taken. A particular execution of such an algorithm
picks a choice whenever such a point is reached. Thus, different execution paths of the
algorithm arise when it is applied to the same input / initial state, and·these paths, when
they terminate, generally produce different output/ end in different final states.
It is not random, as in randomized algorithm, or indeterminate. Rather it has the super
computational characteristic of choosing th~ optimal behavior. Conceptually, a
nondeterministic algorithm could run on a deterministic computer with an unlimited
number of parallel processors. Each time more than one _step is possible, new processes
are instantly forked to try all of them. When one successfully finishes, that result is
returned. Thus, the computation is as fast as if it always chooses the right step.
DAA-108
DESIGN AND ANALYSIS OF ALGORITHMS

·s notion is defim~d for theoretic~l_a~alysis. Nondete


nninistic algorithms compute the
'fhi e c1ass of function~ ~. determi~1stic algorithms,
but the complexity may be much
sartl Ever)' nondeterm_imstic algorithm can be turne
less. 'bl with expo nent ial slow down. d into a deterministic algorithm,
p0551 y algorithm has three phases
such an
_ Nondeterm 1.m.st1.c "g~es smg . " l I .
P ,ase: n . this phase arbitrary string, s, is written
starting at_so_m;, pla~e _m ~em ory (s ma~ differ for
each time it is run)
Deterministic verifying phase: In this phase a norm
al algorithm will consider the
- input to the deci sion prob lem & s may retu ~-tru
e/false.
_ Output phase: It verif ies phas e outputs. If it is then outp
ut is true; else no output.
Example: .
Merge Sort,
Spanning Tree

S. ShoW that 25A T is in P but 35A T is NP-comp


lete. [WBUT 201 01
Answer:
2SATis inP .
To show that 2SA T E P, we outl me a polynomial time algo rithm for solv ing 2SA .
The idea is ·to cons truc t a direc ted grap h G(F ) = (V, T.
E) with
- (i) V = {x,_x I, F cont ains the literal x}
(ii)E = {(x, y), (y, x) I (xv y) E F} c (V x V)
We interpret the ( dire cted ) edge s of G as impl
ications. Beca use of the follo wing
equivalence
. X Vy = ( X ~ y) A ( y ~x)
An assignment of F corr espo nds to an assig nme nt
of G. An assig nme nt is satis fyin g iff no
implication mod eled thro ugh an edge is violated. G
is satisfiable iff for no x ther e exis ts a
path from x to x or from x tox in G. In othe r word
s: in orde r ·to be satisfying, x mus t not
imply x, and x mus t not impl y x.

Thus, the follo wing algo rithm solv es 2SA T (wit h


input size n = IFI):
1) Construct the grap h G(F ), whic h has O(n) vertices
and edges. •
.
2) For each vari able x, sear ch a path from ·x to x. Effo
- 2
rt: O(n )
3) ~f no such path s exis t, retu rn 'yes ', othe rwis e retur
n 'no'
This algo rithm has com plex ity O(n2).

3SAT is NP-complete
.
A vers~on of the SAT prob lem in whic h ever y clause has
3 literals is calle d 3SA T. _
Let the give n insta nce of SAT cont ain the collection
C = { ci, c2, .. . , Cm} of clau ses over
u
the variables = {ui, u , ••• , Un}. We cons truct
a collection C~ of 3 ~i~eral clau_ses ov'!r
the Variables
follows:
u o
whi' h
2

c cons1s o
t fthe on'ginal variables plus sets of additional varia bles as
·

DAA-109
POPULAR PUBLICATIONS

Replace each clause c1 e C by a collection of 3 literal clauses _over the va~ables which
appear in ci plus some additional variables which appear only m these 3 hteral clauses.
The exact procedure depends on the number of literals in ci.
Let ci E C be given by {z1, 22, .. . , zd where the z/s are literals over U..
If k = I ci = {zi}, use two additional variables {Yi.t, Yi,2}- Fonn the collection
c; = {{z1, Yi.I, Yi,2}, {z1, Yi.I, Yi,2}, {z1, Yi.I, Yi.2}, {21, Yi.I, Yi.'2}} .
k = 2 ci = {z1, z2}, use one additional variable {Yi,i}.
Fonn the collection c;
= {{z 1, 22, Yu }, {z1, z2; Yi,1}}
k = 3 Cj = {z,, Z2, 23}. No additional variables. c; = Cj.
k > 3 Use the addi tional variables {y~1, Yi,2, • •• , Yi,k-3}. Fonn the collection
c; = {{z1, 22, Yi,i} , {Yi.I, 23, Yi,2}, {Yi,2, Zt, Yi,3}, {Yi,3, Zs, Yi,4}, • · ·, {Yi,k-3, 2k-1, 2k}}
To show that this procedure fonns a polynomial reduction, firstly we observe that the
number of 3 literal clauses in C' is bounded by a polynomial in nm ~hich leads to the
procedure being a polynomial time one. Secondly, whenever C is satisfiable, then the C'
clauses fonned in the cases k $ 3 ·are automatically satisfied (for arbitrary assignment of .
any add!tional variables) while in the case k > 3

(a) If 21 or 22 is true, assign all additional variables the truth value "false". In this case,
the first literal in each relevant clause is true.
(b) If z1r 1 or zk is true, assign all additional variables the truth value "true". In this case,
the third literal in each relevant clause is true.
(c) Otherwise, if z1 is true, assign Yij the value ''tr.i.Je" when 1 $ j $ l - 2 and the value
"false" when l - I $ j $ k - 3. In this case, the third literal in each relevant clause .
preceding the one which includes z1 is true while the first literal in each relevant
clause succeeding the one which includes z1 is true. _ _
Thus all the clauses in C' are satisfied. Conversely, if all the clauses in C' are satisfied
by an truth assignment to U' (which includes the original variables), then C is satisfied
by the same truth assignment restricted to U.
Thus we conclude that C' is satisfied if and only if C is satisfied.

6. Show that 3-colouring problem is NP-complete. [WBUT 2010]


Answer:
(I) 3-Coloring is in NP because for each node a color from ( 1, 2, 3). Check if for each
edge ( u, V ) , the color of u is different from that V of
(2) Reduce 3-SAT to 3-COLORING. .
(3) Given a 3-SAT fonnula ofm clauses on n variables xhx2,.,.,Xn,

We construct a graph Gas follows. We have


(a) a vertex vi for each variable Xi, ·
(b) a vertex vi' for the negation of each variable x-
(c) 5 verticesj 1-j 5 for each clausej, "

DAA-110
DESIGN AND ANALYSIS OF ALGORITI-IMS

d) 3 special vertices: T, F, R _
( uld like T, F, and R to be forced to different colors, so we will add edges between
We · . . nodes, and node that is colored the same color
1e. For th e remammg
emwoto form a tnang
: TIFIR will be ~ailed colored TRUE/FALSE ~D, res~ecti_vely. -
W ould like the edges to enforce the constramts on satisfymg assignments.
c:n:raint: For ~II i, exactl~ one ofvi and vi' is ~olored TRUE and one is colored FALSE.
d es: for each 1,_form a triangle between vi, vi, and R.
~o:straint: For each cl~use j, at least one of the literals in the clause is colored TRUE.
Edges; for each. clause J, say= (xi or not(xj) or xk), we have the following ga~get.
vi -- - Jl .
I\
1 j3---j4
I1 -t ' J
vj'---j2 I T
I ;
vk --------- ---j 5
Claim: If each of Vi, v/, and vk is colored TRUE or FALSE, then gadget is 3-colorable iff
at least one ofvi, v/, .and vk is colored TRUE.
.

Proof: Ifvi, v/, and vk are all colored false, then we are forced to the following colors:
F ---jl
I\
. I j3--- F
II I \
F ---j2 I T
I I
F ----------- R
But then j 1, j 2 ,j 3 all must be· colored different colors and NONE can be colored F, so there
is no legal coloring.
The remainder of the proof considers the 7 possible combinations of coloring vi, v/, and
vk such that at least one is colored TRUE and the rest are colored FALSE, and shows that
a3 coloring exists in each case.
As an example, if vk is colored TRUE but vi and v/ are colored FALSE, we have the
following legal 3-coloring: ·_ · ·
F T · .
I\
I F R
11 I \
F R I T
I ;
T ---- - ---- - - F
The other cases are similar and were presented in class. The construction takes
polynomial time. F_ollows from the above arguments thus 3-COLORING is NP-complete.
7· State satisfiability problem. State Cook's theorem in connection with P and NP
Problem. . [WBUT 201_5]

DAA-111
POPULAR PUBLICATIONS

Answer: .
· · h applicati
1st part: of great_ pract1c~l importance, ~It
SATISFIABILITY, or SAT, is a problem
chip testing and com puter des ign t~ image ~nalys1s and softwons
ranging from :
g. It is also a can onical hard problem . Heres what an instance of SAT lo
engineerin
like: .
(xvyvz)(xvy)(yvz)(zvx)(xvyvz)
normal form (1(CN.F).1 It 1sda collection of auses
cl
This is a Booleanfiormula in conjunctive . . . several
d1s1un~t10n og1ca or, enoted v) of
(the parentheses), each consisting of the e (such as x) or the negation of one
re a literal is either a Boolean vanab]
literals whe
'
.
(such as x ). so th
sfying truth assign men t is an assign ment of false or true to each variable
A sati owin a~
e is true. The S~ T problem is the foll
every clause contains a literal whose valu ing tl'ij~·
e normal fonn, either find a satisfy
given a Boolean formula in conjun_ctiv
assignment or ,else report that none exists.

2nd part: problem SAT is NP-Complete, via


the
k's The ore m giv es a pro of that the
The Coo
NP may be reduced to it.
technique of showing that any problem in problem
e a formal definition of the SAT
Before proving the theorem, we giv Fonn),
formula cp in CNF (Conjuctive Normal
(Satisfiability Problem): Given a Boolean en some Boolean fonnula cp with n
sati sfia ble ? In oth er wor ds, we are giv
is the formula
es x I, x2, ... , xn, cp = CI /\ C2 /\ · · · /\ Ck, where each of the Ci is a
Boolean variabl drawn from the set
form (~ V ti2 V . . . V ti1), with each lu is a literal
clause of the 1
exists some setting
{ Xi, X 2 , •.. ,xn ,Xi ,X2 , ... ,xn }.
We need to decide whether or not the
e of
x x that cau se the form ula cp to be satisfied (take on a Boolean I valu
of the 1 2 , , ... , xn

true).
Cook-Levin Theorem: SAT is NP-complete.
L has a polynomial time verifier V :
Statement: Suppose L is a NP problem, then ,
l. If x EL, 3 witness y, V (x, y) = I
·
2. If x Ii!: L, V witness y, V (x, y) = O
ld a circuit with pol yno mia l size for the verifier V since the verifier runs in
We can bui
tim e (no t: th~t this fact is non trivial; however, it is 'left to the reader to verify
poly~o?1ial circuit has /x/ + /y/
that It ts true). The c1rcu1t contains AND, OR and NOT gates. The /y/ are
rces , whe re /xi of them are hardco ded to the values of the bits in x and the rest
sou_
variables.

number of a graph? [WBUT 20171


8. What do you mean by chromatic

DAA-112
DESIGN AND ANALYSIS OF ALGOR ITHMS

~:s wer: · ·
least number of color needed to color a graph is called its chroma tic number. The
hr:rnatic numbe r, say X of a complete graph G is X(G) = d+ 1, where d is the degree of
~he vertex. So if we consider a graph G is a comple te graph, i.e., .each node is connec
ted

LJ
all other nodes of the graph then total number of colors required to color all the nodes
w dl .
of the graph is + .e.g.~ 0---- 0 ~

01 02 GJ G4
In the above figure each graph like G 1, G2, G3 or 04 requires ( d+ 1) colors to ~olor
all
the nodes, where d is the degree of each node.

9, can we define Euler graph with SAT problem? [MODE L QUEST ION]
Answer: · ·.
In the summe r of 1735 Leonhard Euler, the famous Swi~s mathematician, was walkin
g
across the bridges of the East Prussian town of Konigsberg. After a while, he noticed
in
frustration that, no matter where he started ·his walk from, no matter how cleverl y
he
continued, it was impossible to cross each bridge exactly once. And from this silly
ambition, the field of graph theory was born. Euler identified at once the map of the park
into a graph whose vertices are the four land masses (two islands, two banks) and whose
edges are the seven bridges: Northern bank

Small Big
island island

Southe rn bank
This g:aph has multipl e edges betwee n two v·e rtices. A feature we have not been allowin
0 g
~ • far m our discuss ion, but one that is meanin gful for this particu
ndge must lar problem , since each
be accoun ted for separately. We are looking for a path that goes through each
e~ge exactly once (the path is allowed to repeat vertices). In other words, we are asking
this question:·
:en can a gr_aph be drawn without lifting the pencil from the paper? ·
e a~swer discove red by Euler is simple, elegant, and intuitive: If and only if (a) the
graph 1s connec ted and (b) every vertex, with the possible excepti on of two vertices
(the
~tart and final vertice s of the walk), has even degree. This is why Konigs berg's park-w as
imp s1'bl
os
.
e to travers e: all four vertices have odd degree. .
DAA-113
POPULAR PUBLICATIONS

let us defi ne a search problem called EULER


To put it in terms of our present concerns, . h edge exactly once. It follows fi
PATH· Given a graph find a path that contains eac rorn
· . 'd - .ttl tht'nking that this search problem can be solved -
10
Euler's o6servat1on, an a 1t e more ,
polynomial time.
Long Answer Type Question s

1. a) Define the classes P and NP. [WBUT 200S, 2009, 2016]


Answer:
CJass P: . • d
A • •
Let A be an algorithm to solve a problem X. W~ say that IS eten_mm~tic if, when
presented with an instance of the problem X, it has_ only one cho_ice 1~ each step
throughout its execution. Thus, if A is run again and agam on the same mput mstance, its
output never changes.
The class of decision problem P consists of those decision problems whose solution can
be obtained using a deterministic algorithm that runs in polynomial number of steps, i.e.,
in 0 ( nk) steps, for some nonnegativ e integer k, where n is the input size.

Class NP:
The class NP consists of those problems for which there exists a deterministic algorithm
A, which presents with a claimed solution to an instance of the problem. This is able to
verify its correctness in polynomial time. That is, if the claimed solution leads to a yes
answer, there is a way to verify this solution in polynomial time. For example, in the
hamiltonia n-cycle problem, given a directed graph G = (V, E), a certificate would be a
sequence ( v1, v2, v3, •• •, v,11 ) of 1"11 vertices. It is easy to check in polynomial time that
(vi, Vi+t) E E for i = 1, 2, 3, ... , I"11 - 1 and that (v111, v1) EE as well. Any problem in P is
also in NP, since if a problem is in P then we can solve it in polynomial time without
even being given a certificate. We will formalize this notion later in this chapter, but for
now we can believe that P E NP. The open question is whether or not P is a proper
subset of NP.

b) Discuss what you mean by Polynomial Reductions. [WBUT 2005, 2009, 2016]
Answer:
The root of NP-Co~plete~ess is reducibility. Informally, a problem p can· be reduced to
another problem Q 1f any mstance of P can be "easily rephrased" as an instance of Q, the
solution to. which provides a solution to the instance of P. This rephrasing is · called
transformation. If P reduces to Q, P is "no harder to solve" than Q.

Suppose,
Pl : is an unknown problem (easy/hard?)
P2 : is known to be easy
Ifwe can reduc_e Pl to P2, that is, we can easily solve Pl .usin
P2 as a subroutine then P2 is easy · g

DAA-114
DESIG N AND ANALYSIS OF ALGO RITHM S

example of reduction is that solving linear equations is reducible to


one ofthe . solving
atic equations.
quadr .

e.g.,. n a set of Booleans, is at least one TRUE. .
p· Give . • ·
·. Given a set of integers. _ . _
Q. fiorrnation: (x 1, X2, .•. , Xn) = {y1, Y2, ••• , Yn) where y; - l 1f _ .
Trans X; -TRU E, y; - 0 1f
·
>';:::::FALSE
Discuss diagrammatically the relations among P class, NP
class, NP-hard and
c) mplete
NP-CO . [WBUT 2005, 2009]
OR,
Define classes P, NP, NP hard and NP-complete and also expla
in their relationship
diagrammatically.
(WBU T 20141
OR, .
Define p-class, NP-class, NP-complete class and NP-hard
class of probl ems. What
is the relation between them?
[WBU T 2015, 2016]
Answer: . ·
.• We say that a decision problem Pi is NP-complete if it is NP-h·ard and
■ it is also in the class NP itself. .
■ . in symbols, Pi is NP-complete if Pi is NP-hard and Pi e
NP.
• Highly informally, it means that Pi is one of the hardest problems in
NP.
• So the NP-complete problems form a set of problems that may
or piay not be
intractable but, whether intractable or not, are all, in some sense
, of equiv alent
complexity.
• If anyone ever shows that an NP-complete problem is tractable, then
• Every NP-complete problem is also tractable indeed,
• Every problem in NP is tractable and so P = NP.
• If anyone ever shows that an NP-complete problem is intractable, then
• Every NP-complete problem is also intractable and, of course, P -j:.
NP.
• So there are two possibilities as shown in figure:
.
-- NP P=N P
(-NP-Co_;.._~_te_}

NP-hard
"

• We don't kno~ which the case is·, ·b ut this gives Computer Scientists
a clear line of
attack. It makes sense to focus efforts on the NP-complete problems;
they all stand or
fall together.
·
• So these sound like very significant problems in our th~ory.

DAA-115
POPULA R PU BLICATI ONS

2. Prove that COP (Cliqu e Decision Problem) is NP-Complete.


[WBUT 2006, 2009, 2012]
Answer :
To ~how th nt CLIQUE E N P, for a given graph G = (V, E), we use the set V' c V of
n·rt1ccs in the clique a-. a certifica te for G. Checking whether V' is a clique can be
accompl ished in polynom ial time by checking whether. for each pair u, v E V', the edge
(u. v) belongs to£. •
We next prove th;,it 3-CNF-S ATs, CLI QUE, which shows that the clique probl~rn is NP-
hard. The rt'ductio n algorithm begi ns with an instance of 3-CNF-SAT. Let 'P. =Ci/\ C2 /\
••· A CA. be a Boolean formula in 3-CNF with k clauses. For r = 1,
2, .... k, each clause c
0

ha~ exact ly thr~e distinct literals 11\ I/ , and 1/ We shall construct a graph G such that <p j;
satisfiab le if and only if G has a clique of size k.
The graph G = (V. £) is construc ted as fol lows. For each clause (l,rv l/ v i/ ) in <p, we
place a triple of vertices v 1r, vi' , and v/ into V. We put an edge between tv, 0 vertices v{
1

and v/ if both of the followin g hold:


• vt anJ v/ arc in di ffcrcnt triples, that is, r f. s. and
• th~ir correspo nding literals arc co11siste11t, that is, I{ is not the negation of 1/.
This graph can easily be compute d from <p in polynom ial time. As an example of this
constructi o n, if we have <p = (xi v -·x2 v --._x3) /\ (--..x 1 v X2 v x3) /\ (xi v X2 v X3), then G is ·
. the graph s hown in Fi gure below.

C: =--. x, V .l1 V X3

T he graph G derived from the 3-CNF fonnula <p = C 1 A C2 A C3, where


C 1 = (x , v -,x 2 v -..x3) C 2=(--x, v X2 v X3 ) and C3 = (x, v x2 v x3 ) , in reducing 3-CNF-SAT
to CLIQUE. A satisfyin g assignm ent of the fonnula has x2 = 0, x 3 = 1, and x, may be
either O or I. This assignm ent satisfies C, with -..x2, and it satisfies C2 and C3 with XJ,
correspo nding to the clique with lightly shaded vertices. ·
We must show that this transform ation of <p into G is a reduction. First, suppose that <p
has a -satisfyi ng assignm ent Then each clause C, contains at least one literal I( that is
assigned 1, and each such literal correspo nds to a vertex v,'. Pickirig one such "true"
literal from each clause yields a set JI' of k vertices. We claim that Y' is a clique. For any
two vertices vr,
v/ e V', where r :f: s, both correspo nding literals are mapped to I by the
DAA-116
DESIGN A!\TD ANALYSIS OF ALGORITHMS

a tisfying assignment, and thus the literals cannot be complements. Thus by the
gi·ven sction of G, t he e dge ( v;,r 'Js) belongs to E. ,
strU
con rsely suppose that G has a clique V' of size k. No edges in G coni1ect vertices · in
conve e trlple, and so V' ·contains exactly one vertex per triple. We can assign 1 to each
the
. sarn r V' · I " f · · 1 · ·
11.r such that vi E wit 1out ,ear o assigning to both a hteral and its complement,
G
h_tera contains no edges between inconsistent literals. Each clause is satisfied, and so <p
since
is satisfied.
) What is No~-deter~inistic algorithm? Differentiate between Deterministic and
3· a -Deterministic algorithm. .
No~rite algorithm to sort an array using Deterministic and Non-Deterministic
b) hnique. Compare the two techniques and show that the time complexity of non-
te~ ministic technique is better than Deterministic.
d; ;:scribe p class, NP class, NP hard and NP complete class and describe their
~elationships. [WBUT 2011]
Answer: . ·
a) Refer to Question No. 4 of Short Answer Type Questions..

b) There are many deterministic algorithm for ~orting a.n arr~y,_quicksort is one of them.
The following procedures together fonn the qmck sort algorithm.

QuickSort (A, 1;,, r)


{
if p < r then
{
q ~ Partition (A, p, r);
Quick Sort (A, p, q) _;
Quick Sort (A, q + 1, r);

Algorithm for partition


Partition (A, p~ r)
{

. initialize x - A[p];
i ~ P-1, j - r+l;
while (i < j)
{
While (A[--j] > x)
{
' .
while (A[++i] < x)
{ .

if ( i < j ) · then
Exchange (A[i], A[j]);
}
}
}

DAA-11_7
- ----

AT IO NS
PO PU LA R PU BL IC

r et u rn j;
}

Exchange(A, I, j)
{
p +- A (i ];
A (i ] +- A [j ];
A (j ] +- p;
} vo t key ab ou t w hi ch the array is partitioned.
st key x - A [p] as a pi will
Partition selects the fir left of the pivo t, ot he rwise i_f x, 2: A[ p] , A[ p]
move towards the
If x < A [p], A [p j will
ht of the pivot key x.
m ove towards the rig

Complexity A na ly si s
y.
• B es t ca se e pi vo t is in th e m iddle position of the arra exactly
• In th e best case, th as sum e th at th e tw o sub arrays are each
equations, we 0 constant
• To simplify the al on e. So , we ge t J{ n) = 21{n/2) + en , c>
origin
ha lf the length of the
d n ~ 2 with T( I) = 1. similar analysis leads
to
( independent of n ) an r M er ge so rt, an d a
r to the formula fo
• This is ve ry si m ila
T(n) = en lo g2n + n
which is O(n log 2 n).
equally
ea ch of th e siz es of the left pt,lrtitions ar e
e as su m e that
• A ve ra ge case: W
has probability 1/n. also of T( n- i-1 ), is
lik el y, an d hence each e of T( i), an d he nc e
n, the average valu
• With this assum...ptio + T( n- 1) )/n
(T (0 ) + T (l ) +
rrence relation becomes .
• Naturally, ou r reTcu(l ) + ... + T(n-1))/n + en .
T( n- 1) ) + cn
2
T( n) = 2(T(0) + (l ) + ... +
g bo th si de s by n we find nT(n) = 2(T(0) + T
• Multiplyin by n- 1 gives . .
• Substitution of2(nT(0) + T( I) + ... ·+ T(n-2)) + c(n-1)2 -l) T( n- 1)
(n -l )T (n -1 ) = m th e pr ev io us on e, we ge t nT (n )- (n
st equation fro
• Subtracting+th2ce nla-c ·
-1 ) + 2cn·
= 2T (n -1 ) g co ns ta nt c, we a
_ rri ve at nT(n) = (n + I )T (n
ignorin
• Rearranging anhd ou t by n(n+ I) gives
D iv is io n th roug
• + 2c /(n +l )
T( n) /( n+ l) = T( n- 1) /n /n
/n = T( n- 2) /( n- l) + 2c
• Hence, T( n- 1)/3 )/2 + 2c/3
Similarly T(2) = T (l
· .
• Thus 2c (l/ 3 + ¼ + ... + 1/ (n +l ))
t,
T( n) /( n+ l) = T( l) /2 + + I) + y - '31 2, w he re y is Eu le r's constan
• Th e sum in bracim kets is about log c(n
y 0.577.
which is approx atel n) = O(n log 2 n)
T( n) /(n + I) = O (lo g 2 n) and thus T(
• So ,

DAA-118
DESIGN AND ANALYSIS OF ALGORITHMS

• Worst case: In quick sort techniqLI:e, the worst case condition arises when the
elements of the array are already sorted.
• If the pivot is alway~ the smallest element, then always/= 0
• We ignore the term T(0)=l, so the recurrence relation is T(n) = T(n-1) + en
• So, T(n-l)=T(n-2)+c(n-l) and so on until we get T(2)=T(l)+c(2)
■ Substituting backwards, we get T(n) = T(l) + c(n + ... + 2) = O(n2)
It may be noted that this case happens if we always take the pivot to be the first element
in the array and th~ array is already sorted. .
Let A[i], 1 :5; i :5; n, be an unsorted array of positive integers. The nonde~erministic
·algorithm NSort(A, n) sorts the number into nondecreasing order and then outputs them
in this order. An auxiliary array B[l :n] is used for conbience. Line 4 initializes B to zero
though any value different from all the A[i] will do. In the for loop of lines 5 to 10, each
A[i] is assigned to a position in B. Line 7 nondeterministically determines this position.
Line 8 ascertaines that BO] has not already been used. Thus, the order of the numbers in
B is some permutation of the initial order in A. The for loop of lines 11 and 12 verifies
that B is sorted 'in nondecreasing order. A successful completion is achieved if and only if
the numbers ar eoutput in.nondecreasing order. Since there is always a set of choices at
a
line 7 for such an output order, algorithm NSort is sorting algorithm. Its complexity is
O(n). All deterministic sorting algorithms must have a complecity 0(nlog n).
1 Algorithm NSort(A, n)
2 II Sort~ positive integers
3 {
4 for i: = 1 ton do B[i]: = 0, II Initialize B[].
5 for i: = 1 ton do
6 {
7 j:= Choice(l, n);
8 it B[j] · :;t O then Failure() ;
9 . B[j]: = A[i];
10 }
11 For i = 1 ton -1 do II. Verify order.
12 If B[i]>B[i+l]then Failure();
13 Write ([Bl:n]);
14 Success}
15 }

c) Refer to Question No. 1 ofLong Answer Type Quesiions.


4
· What do you mean by deterministic and non-deterministic algorithm? Write a
non-deterministic algorithm for searching an element from a given li~t of reaL
nAumbers. Also, specify its time complexity. [WBUT 2015]
nswer:
1·st part:
~~;halgorit~ where result is uniquely defined is a termed the detem1inistic algorithms.
th ~lgonthms agree with the way programs are executed on a computer. In a
eoretical framework we can remove this restriction on the outcome of every operation.
DAA-119
POPULAR PUBLICATIO NS

We can ~ll?w algorithms to contain operations whose outcomes are not uniquely defined
but are II m1ted to specified sets of possibilities.

The. machine executing such operations is allowed to choose anyone of these outcome
su b~ect to a termination condition. s
This leads to the concepts of a nondeterministic algorithm.
_T o specify such algorithms, we introduce three new functions:
(a) Choose (s) arbitrarily chooses one of the elements of set 5 .
(b) Failure () signals an unsuccessful completion.
(c) Success () signals a successful completion.
The assignment statement x: = choice (I, n) could result in x being assigned anyone of the
integers in the range (1, n) there is no rule specifying how this choice is to be made. The
fai lure* l and s uccess() signals are used to define a computation of the algorithm. These
statements cannot be used to effect a return. Whenever there is a set of choices that leads
to a successful compl etion. Then one such set of choices is always made and the
algorithm terminates successfull y.
nd
2 part:
A non-d eterministic algorithm for searching an element from a given list of real numbers:
Let A[ 1..n} be an array and x be the target element to search for.
We can write the fo llowi ng nondeterministic search algorithm:
N_SEARC H(A, x)
{
j = choice(! n)
if A[j] = X then
print (j)
succes s
endif
print ( ' 0 ')
f a ilure
}
Time complexity : O(n).

5. Write short notes on the following:


a) Clique decision problem [WBUT 2003, 2015]
b) NP-hard and NP-complete classes · [WBUT 2017]
c) Importance of P, NP classes [MODEL QUESTION]
Answer:
. a) Clique decision problem:
Refer to Question No. 3 of Short Answer Type Questions.

b) NP-hard and NP-complete classes: ·


Refer to Question No. 1 (c) of Long Answer Type Questions.

DAA-120
DESIGN AND ANALYSIS OF ALGORITHM S

1mportance of P, NP classes: · ·
c) ·mportance of the P vs NP questions stems from the successful theories of NP-
The \eteness and complexity-b ased cryptography, as well as the potentially stunning
comt~cal consequences of a constructive proof of P =NP.
h . . b·1·
Prac theory
Th I
of NP-comp Ieteness as its roots m computa 1 1ty theory. The computability
e ursors of the classes P and NP are the classes of decidable and c.e. (computably
pre~erable) languages, respectively. We say that a language Lis c.e. (or semi-decidable)
~Fund only if L = L (M) for some Turing machine M. We say that L is decidable if and
1
~y if L = L (M) for some Turing machine M which satisfies the condition that M halts
on all input strings w. There is an equivalent definition of c.e. which brings out its
:~alogy with NP, namely Lis c.e. if and only if there is a computable "checking relation"
R(x, y) such that _L = {x I 3 y R(x, y➔}.
Using the notation (M) to denote a string describing a Turing machine M, we define the
Halting Problem HP as follows:
HP = {(M) I M is a Turing machine which halts on input ( M) }
Turing used a simple diagonal argument to show that HP is not decidable. On the other ·
hand, it is not hard to show that HP is c.e. Of central importance in co~putabilit y theory
is the notion.of reducibility.

· DAA-121

You might also like