Daa Part 4
Daa Part 4
Daa Part 4
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.
DAA-106
DESIGN AND ANALYSIS OF ALGORITHMS
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.
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
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.
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.
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.
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
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
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
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
C: =--. x, V .l1 V X3
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.
. 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 }
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).
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