DAA Unit - V
-
Upload
subathra-devi-mourougane -
Category
Documents
-
view
254 -
download
0
Transcript of DAA Unit - V
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 1/22
1
Mailam Engineering College(Approved by AICTE, New Delhi, Affiliated to Anna University, Chennai
& Accredited by National Board of Accreditation (NBA, New Delhi
Mailam (Po), Villupuram (Dt). Pin: 604 304
DEPARTMET !" C!MP#TER APP$%CAT%!&
DESIGN AND ANALYSIS OF ALGORITHMS – MC9223
Part ' A
. Deine P * P+
! is the set of all decision proble"s solvable by deter"inistic al#orith"s inpolyno"ial ti"e$ N! is the set of all decision proble"s solvable by nondeter"inistical#orith"s in polyno"ial ti"e$
. Deine re-ui/ilit+
%et % & %' be proble"s$ !roble" % redces to %' iff there is a way to solve % by adeter"inistic polyno"ial ti"e al#orith" sin# a deter"inistic al#orith" that solves %' inpolyno"ial ti"e$
3. Deine P12ar- * P 'omplete+
A proble" % is N! hard if and only if satisfiability redces to % $A proble" % is N!co"plete if and only if % is N!)hard and % * N!$
4. &tate oo5 t2eorem+ Any NP problem can be converted to SAT in polynomial time.
Cook’s Theorem States That Satisfiability is in P If and Only If P =NP.
In co"ptational co"ple+ity theory, the Coo-%evin theore", also nown as Coo.stheore", states that the Boolean satisfiability proble" is N!)co"plete$ That is, any proble"in N! can be redced in polyno"ial ti"e by a deter"inistic Trin# "achine to the proble" of deter"inin# whether a Boolean for"la is satisfiable$
. $i5t out t2e 5trateg to 52o7 t2at a pro/lem i5 P12ar-+
!ic a proble" % already nown to the N!)hard$ /how how to obtain an instance I0 of %' fro" any instance I of % sch that fro" the
soltion of I0 we can deter"ine the soltion to instance I of %$ Conclde fro" step ii that iii redces to %'
Conclde fro" steps i and iii and the transitivity of redce that %' is N!)hard$
6. Deine P1Complete pro/lem. 8De 00 * 09
Prepared ByMrs.A.Subathra Devi AP / MCA
#%T ' V P1ARD AD P1C!MP$ETE PR!;$EM&
! N! proble"s ) N!)co"plete proble"s ) Appro+i"ation al#orith"s for N!)hard
proble"s ) Travelin# sales"an proble" ) 1napsac proble"$
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 2/22
2
A decision proble" D is said to be N!)co"plete if it belon#s to class N!$ Everyproble" in N! is polyno"ial redcible to D$
E#$ 2 3a"iltonian circit proble" and decision version of travelin# sales"an proble"$
<. Deine lea tag * le=el one Tag+
A leaf node is a da# in which all shared nodes are leaves$ A level)one da# is a da# in
which all shared nodes are level one node$
E+a"ple2 leaf da#4ne level da#
>. ?2at are alle- Appro@imation algorit2m+ 8un 09
A feasible soltion with vale close to the vale of an opti"al soltion is calledappro+i"ate soltions$ An appro+i"ation al#orith" for ! is an al#orith" that #aranteesappro+i"ate soltions for !$
An e)appro+i"ate al#orith" is an f(n)appro+i"ate al#orith" for which f(n56E$
B. Deine a/5olute appro@imation algorit2m+
A is an absolte appro+i"ation al#orith" for proble" p if and only if for everyinstance I of p, 7f8(I)f9(I756 for so"e constant $
0. Deine (n) appro@imate algorit2m + 8De 09A is an f(n) appro+i"ate al#orith" if and only if and only if for every instance I for
si:e n, 7f8(I)f9(I7; f8(I 56 f(n for f8(I<=$
. Deine $PT 52e-ule+
An %!T schedle is one that is the reslt of an al#orith" that whenever the processorbeco"es free assi#ns to that processor a tas whose ti"e is the lar#est of those tass notyet assi#ned$ Ties are broen an arbitrary "anner$
3.let m3,n< an- (t,t,t3,t4,t,t6,t<)(,,4,4,3,3,3)&2o7 $PT * optimal
52e-ule+ %!T schedle finish ti"e 6
4pti"al schedle finish ti"e 6 >
4. ?2at i5 /in paing pro/lem+ 8De 00 * 09
In bin pacin# proble" we are #iven n ob?ects that have to be placed in bins of e@alcapacity$ The ob?ective is to deter"ine the "ini"" n"ber of bins needed toacco""odate all n ob?ects$ No ob?ect "ay be placed partly in one bin and partly in another$
. $i5t out t2e =ariou5 2euri5ti5 or /in paing pro/lem.
irst fit (
Best fit (B
irst fit decreasin# (D
Best fit decreasin# (BD
6. i=e t2e li5t o 5ome 7ell1no7n pro/lem5 t2at are P1Complete 72en
e@pre55e- a5 -ei5ion pro/lem+ 8un 09 3a"iltonian circit
1napsac proble"
Travelin# sales"an proble"$
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 3/22
3
<. Deine -etermini5ti, non -etermini5ti algorit2m+
If the al#orith" has the property that the reslt of every operation is ni@elydefined then it is said to be deter"inistic al#orith"
e can allow al#orith"s to contain operations whose otco"es are not ni@ely
defined bt are li"ited to specified set s of possibilities$ The "achine e+ectin# schoperations is allowed to choose any one of these otco"es sb?ect to ter"inationcondition $This leads to the concept of a nondeter"inistic al#orith"$
>. ?rite DA or 5ear2 algorit2m+Consider the proble" of searchin# for an ele"ent + in a #iven set of ele"ents$ e
are re@ired to deter"ine an inde+ ? sch that A? 6 + of ? 6 = if + is not A$
Al#orith"2
?2 6 choice (, nIf A? 6 + then Fwrite (? sccess ( Grite (= failre (
B. Deine -ei5ion algorit2m+Any proble" for which the answer is either :ero or one is called a decision proble"$
An al#orith" for a decision proble" is ter"ed a decision al#orith"$
0. Deine optimiation algorit2m+Any proble" that involves the identification of an opti"al either "a+i"" or
"ini"" vale of a #iven cost fnction is nown as an opti"i:ation proble"$ Anopti"i:ation al#orith" is sed to solve an opti"i:ation proble"$
. Deine trata/le an- intrata/le pro/lem5. i=e one e@ample o intrata/le
pro/lem.
Tractable problems 2 !roble"s that can be solved in polyno"ial ti"e are calledtractable proble"s$E#$ 2 sortin#, searchin#, "atri+ "ltiplication, H Intractable problems 2 !roble"s that cannot be solved in polyno"ial ti"e are
called tractable proble"s$E#$ 2 3a"iltonian circit, napsac proble", travelin# sales"an proble"$
. E@plain t2e t2eor o omputational [email protected] proble".s intractability re"ains the sa"e for all principal "odels of co"ptations
and all reasonable inpt encodin# sche"es for the proble" nder consideration$
3. E@plain un-e5ira/le pro/lem5If the decision proble" cannot be solved in polyno"ial ti"e, and if the decision
proble" cannot be solved at all by any al#orith"$ /ch proble"s are called Undesirable$
4. E@plain t2e 2alting pro/lem.iven a co"pter pro#ra" and an inpt to it, deter"ine whether the pro#ra" will
halt on that inpt or contine worin# indefinitely on it$
. ?2en a -ei5ion pro/lem i5 5ai- to /e polnomial re-ui/le.
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 4/22
4
A decision proble" Dl is said to be polyno"ial redcible to a decision proble" D' if there e+ists a fnction t that transfor"s instances of Dl to instances ofD' sch that,
It "aps all yes instances of d to yes instances of d' and all no instances of dl to no
instances ofd'$ It is co"ptable by a polyno"ial ti"e al#orith"
6. Deine a euri5ti.A heristic is a co""on)sense rle drawn fro" e+perience rather than fro" a
"athe"atically proved assertion$
E+2 oin# to the nearest nvisited city in the travelin# sales"an proble" is a #oodillstration for 3eristic$
<. Deine Tra=er5al5.hen the search necessarily involves the e+a"ination of every verte+ in the ob?ect
bein# searched it is called a traversal$
>. $i5t out t2e te2niFue5 or tra=er5al5 in grap2.
Breadth first search
Depth first search
B. ?2at i5 artiulation point+A verte+ v in a connected #raph is an articlation point if and only if the deletion of
verte+ v to#ether with all ed#ed incident to v disconnects the #raph in to two or "orenone"pty co"ponents
30. ?2at i5 neare5t neig2/or algorit2m+ 8un 09The nearest nei#hbor al#orith" was one of the first al#orith"s sed to deter"ine asoltion to the travellin# sales"an proble"$ In it, the sales"an starts at a rando" city andrepeatedly visits the nearest city ntil all have been visited$ It @icly yields a short tor,bt sally not the opti"al one$
3. ?2at i5 a polnomial time algorit2m+An al#orith" is said to be polyno"ial ti"e al#orith", if its worst)case ti"e efficiencybelon#s to 4(p(n, where p(n is a polyno"ial of the proble"0s inpt si:e n$
3. ?2at i5 a polnomiall re-ui/le -ei5ion pro/lem+ A decision proble" D is said to be polyno"ially nonredcible to a decision proble"
D' if there e+ists a fnction t that transfor"s instances of D to instances of D' schthat It "aps all yes instances of D to yes instances of D' and all no instances of Dto no instances of D'$
It is co"ptable by a polyno"ial)ti"e al#orith"$
33. o7 7ill ou alulate t2e relati=e error o Appro@imation algorit2m+re (sa 6 f(sa)f(s8f(s8where, /a is the appro+i"ate soltion and s8 is the e+act soltion$
34. ?2at i5 aura ratio+Accracy ratio is the ratio of accracy of appro+i"ate soltion to the e+act soltion$
r (sa 6 f(sa
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 5/22
5
f(s8
3. ?2at i5 perormane ratio+!erfor"ance ratio is the "etric indicatin# the @ality of the appro+i"ation al#orith"$
The accracy ratio r(sa taen over all the instances of the proble" is called perfor"anceratio$
36. ?rite t2e t7ie1aroun-1t2e1tree algorit2m.
The twice)arond)the)tree)al#orith" e+ploits a connection between 3a"iltonian circits andspannin# trees of the sa"e #raph$ The steps are2
Constrct a "ini"" spannin# tree of the #raph correspondin# to a #iven instance
of a travelin# sales"an proble"$
/tartin# at an arbitrary verte+, perfor" a wal arond the "ini"" spannin# tree
recordin# the vertices passed by$
/can the list of vertices obtained in step ' and eli"inate fro" it all repeated
occrrences of the sa"e verte+ e+cept the startin# one at the end of list$ Thevertices re"ainin# in the list for" the 3a"iltonian circit$
3<. Pro=e t2at t2e t7ie1aroun-1t2e1tree algorit2m i5 a 1appro@imation algorit2m
or t2e tra=eling 5ale5man pro/lem 7it2 Euli-ean -i5tane5.
Twice)arond)the)tree al#orith" is a polyno"ial al#orith", as we se the !ri"0s or1rsal0s al#orith" to #enerate a "ini"" spannin# tree$ As the len#th of the tor /aobtained by the twice)arond)the)tree al#orith" is at "ost twice the len#th of the opti"altor /8 that is f(sa 56 'f(s8
Je"ovin# any ed#e fro" /8 yields a spannin# tree T of wei#ht w(T, which "st be #reaterthan or e@al to the wei#ht of the #raph0s "ini"" spannin# tree w(T8$ Now the ine@ality
is2 f(s8 < w(T <6 w(T8 K 'f(s8 < 'w(T8$
Co"binin# the last two ine@alities, we #et the ine@ality 'f(s8 < f(sa$
Ths the twice)arond)the)tree al#orith" is a ')appro+i"ation al#orith" for the travelin#sales"an proble" with Eclidean distances$
3>. Pro=e t2at i P not eFual to P, t2en t2ere e@i5t5 no 1appro@imation algorit2mor t2e tra=eling 5ale5man pro/lem.
That is, there e+ists no polyno"ial)ti"e al#orith" for this proble" so that for allinstances f(sa 56 cf(s8 $ /ppose that sch an appro+i"ation al#orith" A and a constantC e+ists, to prove this by contradiction$ %et be an arbitrary #raph with n vertices$ E "ap to a co"plete wei#hted #raph 0 by assi#nin# wei#ht to each of its ed#es and addin# aned#e wei#hted cnL between each pair of vertices not ad?acent in $
If has a 3a"iltonian circit its len#th in 0 is n$ f(sa$ 56 cn$If does not have a 3a"iltonian circit, the shortest tor in 0 will contain at least
one ed#e of wei#ht cnL$ f(sa <6 f(s8 <cn$
Tain# both the ine@alities, we can solve the 3a"iltonian circit proble" withpolyno"ial al#orith", while "appin# to 0 and applyin# al#orith" A to #et the shortest
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 6/22
6
tor in 0, which is also based on constant c$ This is a contradiction to !6N!$ 3ence theassertion is ri#ht$
3B. ?2at i5 a ir5t1it algorit2m an- a ir5t1it -erea5ing algorit2m or /in
paing+
The first)fit al#orith" places each ite" in the order #iven, into the first bin the ite"
fits in when there are no sch bins, place the ite" in a new bin and add this bin to the endof bin list$
The fits)fit decreasin# al#orith" for the bin)pacin# proble" starts by sortin# theite"s in non)decreasin# order of their si:es and then acts as the first)fit al#orith"$
Part 1 ;
. E@plain P an- P pro/lem5 7it2 e@ample5. 8De 00, un 0 * 09
P 1 Pro/lem:
! verss N! polyno"ial verss nondeter"inistic polyno"ial refers to a theoretical@estion presented in >M by %eonid %evin and /tephen Coo, concernin# "athe"aticalproble"s that are easy to solve ! type as opposed to proble"s that are difficlt to solve N!type$
Any ! type proble" can be solved in polyno"ial ti"e$ A polyno"ial is a"athe"atical e+pression consistin# of a s" of ter"s, each ter" incldin# a variable orvariables raised to a power and "ltiplied by a coefficient$ A ! type proble" is a polyno"ialin the n"ber of bits that it taes to describe the instance of the proble" at hand$ Ane+a"ple of a ! type proble" is findin# the way fro" point A to point B on a "ap$ An N!type proble" re@ires vastly "ore ti"e to solve than it taes to describe the proble"$ Ane+a"ple of an N! type proble" is breain# a 'O)bit di#ital cipher$ The ! verss N!@estion is i"portant in co""nications, becase it "ay lti"ately deter"ine the
effectiveness or ineffectiveness of di#ital encryption "ethods$
P 1 Pro/lem:
An N! proble" defies any brte)force approach at soltion, becase findin# thecorrect soltion wold tae trillions of years or lon#er even if all the sperco"pters in theworld were pt to the tas$ /o"e "athe"aticians believe that this obstacle can besr"onted by bildin# a co"pter capable of tryin# every possible soltion to a proble"
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 7/22
7
si"ltaneosly$ This hypothesis is called ! e@als N!$ 4thers believe that sch a co"ptercannot be developed ! is not e@al to N!$ If it trns ot that ! e@als N!, then it will beco"epossible to crac the ey to any di#ital cipher re#ardless of its co"ple+ity, ths renderin# alldi#ital encryption "ethods worthless$
In co"ptational co"ple+ity theory, N! is one of the "ost fnda"ental co"ple+ity
classes$ The abbreviation N! refers to nondeter"inistic polyno"ial ti"e$
The e@ivalence of the two definitions follows fro" the fact that an al#orith" on scha non)deter"inistic "achine consists of two phases, the first of which consists of a #essabot the soltion, which is #enerated in a non)deter"inistic way, while the second consistsof a deter"inistic al#orith" that verifies or re?ects the #ess as a valid soltion to theproble"$
The co"ple+ity class ! is contained in N!, bt N! contains "any i"portant proble"s,the hardest of which are called N!)co"plete proble"s, for which no polyno"ial)ti"eal#orith"s are nown for solvin# the" altho#h they can be verified in polyno"ial ti"e$ The"ost i"portant open @estion in co"ple+ity theory, the ! 6 N! proble", ass whetherpolyno"ial ti"e al#orith"s actally e+ist for N!)co"plete, and by corollary, all N! proble"s$
It is widely believed that this is not the case$
. De5ign a polnomial time algorit2m or a oloring pro/lem -etermine 72et2er=ertie5 o a gi=en grap2 5an /e olore- in no more t2an olor5. 8De 009
In #raph theory, #raph colorin# is a special case of #raph labelin#, it is an assi#n"entof labels traditionally called colors to ele"ents of a #raph sb?ect to certain constraints$ Inits si"plest for", it is a way of colorin# the vertices of a #raph sch that no two ad?acentvertices share the sa"e color this is called a verte+ colorin#$ /i"ilarly, an ed#e colorin#assi#ns a color to each ed#e so that no two ad?acent ed#es share the sa"e color, and a facecolorin# of a planar #raph assi#ns a color to each face or re#ion so that no two faces that
share a bondary have the sa"e color$
Perte+ colorin# is the startin# point of the sb?ect, and other colorin# proble"s canbe transfor"ed into a verte+ version$ or e+a"ple, an ed#e colorin# of a #raph is ?st averte+ colorin# of its line #raph, and a face colorin# of a plane #raph is ?st a verte+ colorin#of its dal$ 3owever, non)verte+ colorin# proble"s are often stated and stdied as is$ That ispartly for perspective, and partly becase so"e proble"s are best stdied in non)verte+for", as for instance is ed#e colorin#$
The convention of sin# colors ori#inates fro" colorin# the contries of a "ap, whereeach face is literally colored$ This was #enerali:ed to colorin# the faces of a #raph e"beddedin the plane$ By planar dality it beca"e colorin# the vertices, and in this for" it #enerali:esto all #raphs$ In "athe"atical and co"pter representations, it is typical to se the first fewpositive or nonne#ative inte#ers as the colors$ In #eneral, one can se any finite set asthe color set$ The natre of the colorin# proble" depends on the n"ber of colors bt noton what they are$
raph colorin# en?oys "any practical applications as well as theoretical challen#es$Beside the classical types of proble"s, different li"itations can also be set on the #raph, oron the way a color is assi#ned, or even on the color itself$ It has even reached poplarity
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 8/22
8
with the #eneral pblic in the for" of the poplar n"ber p::le /do$ raph colorin# isstill a very active field of research$
Polnomial time:
Deter"inin# if a #raph can be colored with ' colors is e@ivalent to deter"inin#whether or not the #raph is bipartite, and ths co"ptable in linear ti"e sin# breadth)firstsearch$ Qore #enerally, the chro"atic n"ber and a correspondin# colorin# of perfect#raphs can be co"pted in polyno"ial ti"e sin# se"i definite pro#ra""in#$ Closedfor"las for chro"atic polyno"ial are nown for "any classes of #raphs, sch as forest,chordal #raphs, cycles, wheels, and ladders, so these can be evalated in polyno"ial ti"e$
If the #raph is planar and has low branch width, then it can be solved in polyno"ialti"e sin# dyna"ic pro#ra""in#$ In #eneral, the ti"e re@ired is polyno"ial in the #raphsi:e, bt e+ponential in the branch width$
E@at algorit2m5:
Brte)force search for a k )colorin# considers every of the assi#n"ents of k colorsto n vertices and checs for each if it is le#al$ To co"pte the chro"atic n"ber and the
chro"atic polyno"ial, this procedre is sed for every , i"practical forall bt the s"allest inpt #raphs$
Usin# dyna"ic pro#ra""in# and a bond on the n"ber of "a+i"al independent
sets, k )colorability can be decided in ti"e and space $ Usin# the principle of inclsion-e+clsion and Rates0s al#orith" for the fast :eta transfor", k )colorability can be
decided in ti"e for any k $ aster al#orith"s are nown for S) and )colorability,
which can be decided in ti"e and , respectively$Running time: !(
nn)
3. Di5u55 t2e appro@imation algorit2m P ar- or 5ol=ing Tra=elling &ale5man
pro/lem. i=e e@ample. 8De 009
The travelin# sales"an proble" consists of a sales"an and a set of cities$ Thesales"an has to visit each one of the cities startin# fro" a certain one (e$#$ the ho"etownand retrnin# to the sa"e city$ The challen#e of the proble" is that the travelin# sales"anwants to "ini"i:e the total len#th of the trip$ The travelin# sales"an proble" can bedescribed as follows2
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 9/22
T/! 6 F(, f, t2 6 (P, E a co"plete #raph,f is a fnction PP V, ! t " V, is a #raph that contains a travelin# sales"an tor with cost that does not e+ceed tG$
E@ample2Consider the followin# set of cities2
The proble" lies in findin# a "ini"al path passin# fro" all vertices once$ ore+a"ple the path !ath FA, B, C, D, E, AG and the path !ath' FA, B, C, E, D, AG pass all thevertices bt !ath has a total len#th of ' and !ath' has a total len#th of S$
Appro@imation1T&P
Inpt2 A co"plete #raph (P, E4tpt2 A 3a"iltonian cycle
$select a WrootX verte+ r " P $
'$se Q/T)!ri" (, c, r to co"pte a "ini"" spannin# tree fro" r$S$ass"e % to be the se@ence of vertices visited in a preorder tree wal of T$$retrn the 3a"iltonian cycle 3 that visits the vertices in the order %$
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 10/22
1#
(a a set of vertices is shown$ !art (b illstrates the reslt of the Q/T)!ri" ths the"ini"" spannin# tree Q/T)!ri" constrcts$ The vertices are visited lie FA, B, C, D, E, Aby a preorder wal$ !art (c shows the tor, which is retrned by the co"plete al#orith"$
Proof:
Appro+i"ation)T/! costs polyno"ial ti"e as was shown before$
Ass"e 38 to be an opti"al tor for a set of vertices$ A spannin# tree is constrcted bydeletin# ed#es fro" a tor$ Ths, an opti"al tor has "ore wei#ht than the "ini"")
spannin# tree, which "eans that the wei#ht of the "ini"" spannin# tree for"s a lowerbond on the wei#ht of an opti"al tor$
c(t Y c(38$ %et a fll wal of T be the co"plete list of vertices when they are visited re#ardless if theyare visited for the first ti"e or not$ The fll wal is $ In or e+a"ple2
6 A, B, C, B, D, B, E, B, A,$
The fll wal crosses each ed#e e+actly twice$ Ths,c( 6 'c(T$ '
ro" e@ations and ' we can write that
c( Y 'c(38, Shich "eans that the cost of the fll path is at "ost ' ti"e worse than the cost of anopti"al tor$
The fll path visits so"e of the vertices twice which "eans it is not a tor$ e cannow se the trian#le ine@ality to erase so"e visits withot increasin# the cost$ The fact weare #oin# to se is that if a verte+ a is deleted fro" the fll path if it lies between two visitsto b and c the reslt s##ests #oin# fro" b to c directly$
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 11/22
11
In or e+a"ple we are left with the tor2 A, B, C, D, E, A$ This tor is the sa"e asthe one we #et by a preorder wal$ Considerin# this preorder wal let 3 be a cycle derivin#fro" this wal$ Each verte+ is visited once so it is a 3a"iltonian cycle$ e have derived 3deletin# ed#es fro" the fll wal so we can write2
c(3 Y c(
ro" S and we can i"ply2c(3 Y ' c(38$ Z
This last ine@ality co"pletes the proof$
4. ?rite 52ort note an P1Complete pro/lem. 8un 0 * 09
Definition of NP-completeness:A decision proble" is N!)co"plete if2
$ is in N!, and'$ Every proble" in N! is redcible to in polyno"ial ti"e$
can be shown to be in N! by de"onstratin# that a candidate soltion to can beverified in polyno"ial ti"e$
A proble" satisfyin# condition ' is said to be N!)hard, whether or not it satisfiescondition $ A conse@ence of this definition is that if we had a polyno"ial ti"e al#orith"for , we cold solve all proble"s in N! in polyno"ial ti"e$
The concept of N!)co"pleteness was introdced in >M by /tephen$ N!)co"pleteproble"s cold be solved in polyno"ial ti"e on a deter"inistic Trin# "achine$ sincenobody had any for"al proofs for their clai"s one way or the other$ This is nown as the@estion of whether !6N!$
An interestin# e+a"ple is the #raph iso"orphis" proble", the #raph theory proble"
of deter"inin# whether a #raph iso"orphis" e+ists between two #raphs$ Two #raphs areiso"orphic if one can be transfor"ed into the other si"ply by rena"in# vertices$ Considerthese two proble"s2
• raph Iso"orphis"2 Is #raph iso"orphic to #raph '
• /b#raph Iso"orphis"2 Is #raph iso"orphic to a sb#raph of #raph '
The /b#raph Iso"orphis" proble" is N!)co"plete$ The #raph iso"orphis"proble" is sspected to be neither in ! nor N!)co"plete, tho#h it is in N!$ This is ane+a"ple of a proble" that is tho#ht to be hard, bt isn.t tho#ht to be N!)co"plete$
The easiest way to prove that so"e new proble" is N!)co"plete is first to prove thatit is in N!, and then to redce so"e nown N!)co"plete proble" to it$ Therefore, it is seflto now a variety of N!)co"plete proble"s$ The list below contains so"e well)nownproble"s that are N!)co"plete when e+pressed as decision proble"s$
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 12/22
12
Theorem:
%et ! and J be two proble"s$ If ! redces to J and J is polyno"ial, then ! is polyno"ial$
Proof:
• %et T be the transfor" that transfor"s ! to J$ T is a polyno"ial ti"e al#orith" that
transfor"s I! to IJ sch that• %et AJ be the polyno"ial ti"e al#orith" for proble" J$ Clearly, A taes as inpt IJ,
and retrns as otpt Answer([J,IJ• Desi#n a new al#orith" A! as follows2
Al#orith" A!(inpt2 I!be#in
IJ 26 T(I! + 26 AJ(IJ
retrn +end
• Note that this al#orith" A! retrns the correct answer Answer([!,I! becase + 6
AJ(IJ 6 Answer([J,IJ 6 Answer([!,I!$• Note also that the al#orith" A! taes polyno"ial ti"e becase both T and AJ
[$E$D$
The intition derived fro" the previos theore" is that if a proble" ! redces to proble" J,then J is at least as difficlt as !$
T2eorem:A proble" J is N!)co"plete if
$ J is N!, and'$ There e+ists an N!)co"plete proble" J= that redces to J
!roof2
/ince J is N!, it re"ain to show that any arbitrary N! proble" ! redces to J$
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 13/22
13
%et ! be an arbitrary N! proble"$
/ince J= is N!)co"plete, it follows that ! redces to J=
And since J= redces to J, it follows that ! redces to J$
[$E$D$
The previos theore" a"onts to a strate#y for provin# new proble"s to be N! co"plete$/pecifically, to proble" a new proble" J to be N!)co"plete, the followin# steps aresfficient2
!rove J to be N!
ind an already nown N!)co"plete proble" J=, and co"e p with a transfor" that
redces J= to J$
. ?rite a non-etermini5ti Gnap5a algorit2m. 8un 0, un * De 09
Deterministic algorithms
Al#orith"s with ni@ely defined reslts
!redictable in ter"s of otpt for a certain inpt Nondeter"inistic al#orith"s are allowed to contain operations whose otco"es are
li"ited to a #iven set of possibilities instead of bein# ni@ely defined /pecified with the help of three new 4( fnctions
. 2oie ( & )
Arbitrarily chooses one of the ele"ents of set /
+ 6 choice(,n can reslt in + bein# assi#ned any of the inte#ers in the ran#e , n,
in a co"pletely arbitrary "anner No rle to specify how this choice is to be "ade
. ailure()
/i#nals nsccessfl co"pletion of a co"ptation Cannot be sed as a retrn vale
3. 5ue55() /i#nals sccessfl co"pletion of a co"ptation
Cannot be sed as a retrn vale
If there is a set of choices that leads to a sccessfl co"pletion, then one choice
fro" this set "st be "ade A nondeter"inistic al#orith" ter"inates nsccessflly iff there e+ist no set of
choices leadin# to a sccess si#nal A "achine capable of e+ectin# a nondeter"inistic al#orith" as above is called a
nondeter"inistic "achine
Nondeter"inistic search of + in an nordered array A with n \ ele"ents Deter"ine an inde+ ? sch that A? 6 + or ? 6 ] if + ^' A
Non-deterministic algorithm for KNAPSACK
The 1NA!/AC1 proble" can be solved sin# the followin# non)deter"inistic al#orith"2
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 14/22
14
1NA!/AC1 (in 4/2 set of ob?ects[U4TA2 n"berCA!ACITR2 n"berot /2 set of ob?ects4UND2 booleanbe#in / 26 e"pty total\vale 26 = total\wei#ht 26 = 4UND 26 false
pic an order % over the ob?ects loop
choose an ob?ect 4 in % add 4 to / total\vale 26 total\vale L 4$vale total\wei#ht 26 total\wei#ht L 4$wei#ht if total_\wei#ht < CA!ACITR then fail else if total_\vale < 6 [U4TA
4UND 26 tre scceed endif endif delete all ob?ects p to 4 fro" % endloop
end
6. E@plain Appro@imation algorit2m5 or Tra=elling &ale5man Pro/lem. 8un 09
Qany proble"s of practical si#nificance are N!)co"plete bt are too i"portant toabandon "erely becase obtainin# an opti"al soltion is intractable$ If a proble" is N!)
co"plete, we are nliely to find a polyno"ial)ti"e al#orith" for solvin# it e+actly, bt thisdoes not i"ply that all hope is lost$ There are two approaches to #ettin# arond N!)co"pleteness$ irst, if the actal inpts are s"all, an al#orith" with e+ponential rnnin#ti"e "ay be perfectly satisfactory$ /econd, it "ay still be possible to find near!optimal
soltions in polyno"ial ti"e$ In practice, near)opti"ality is often #ood eno#h$ An al#orith"that retrns near)opti"al soltions is called an appro"imation al#orithm$
irst co"pte a strctre)a "ini"" spannin# tree)whose wei#ht is a lower bondon the len#th of an opti"al travelin#)sales"an tor$ e will then se the "ini"" spannin#tree to create a tor whose cost is no "ore than twice that of the "ini"" spannin# tree.swei#ht, as lon# as the cost fnction satisfies the trian#le ine@ality$ The followin# al#orith"i"ple"ents this approach, callin# the "ini"")spannin#)tree al#orith" Q/T)!JIQ fro" asa sbrotine$
A!!J4`)T/!)T4UJ($, c < select a verte+ r " % $ to be a root verte+' co"pte a "ini"" spannin# tree T for $ fro" root r sin# Q/T)!JIQ($, c , r S let & be the list of vertices visited in a preorder tree wal of T
return the ha"iltonian cycle ' that visits the vertices in the order &
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 15/22
15
Jecall fro" that a preorder tree wal recrsively visits every verte+ in the tree, listin# averte+ when it is first encontered, before any of its children are visited$
illstrates the operation of A!!J4`)T/!)T4UJ$ !art (a of the fi#re shows the #iven set of vertices, and part (b shows the "ini"" spannin# tree T #rown fro" root verte+ a byQ/T)!JIQ$ !art (c shows how the vertices are visited by a preorder wal of T , and part (d
displays the correspondin# tor, which is the tor retrned by A!!J4`)T/!)T4UJ$ !art (edisplays an opti"al tor, which is abot 'S shorter$
Before we present some polynomial approximate algorithms, consider weather or not it is possible to construct a finite c-approximate algorithm for the traveling salesman problem. The
answer is no, unless P = NP .
Theorem
If exist a polynomial time c-approximation algorithm for the traveling salesman problem then P= P
Proof
!e prove by construction, specifically we use the polynomial time c-approximation algorithm
for the traveling salesman problem to construct a polynomial time algorithm for "amilton circuit problem.
#uppose exists such an algorithm A with constant c then an approximate solution, sa, will have f $ sa% & cf $ s'%, where s' is the optimal solution.
!e use algorithm to construct the algorithm to solve the "amiltonian circuit in polynomial timefor any graph.
(et G be an arbitrary graph with n vertices. !e map G to a complete weighted graph G' by
assigning weight ) to each edge in G and weight cn*) to edges not in G.
). If G has a "amiltonian circuit, its length in G' is n.
It is an optimal solution to the traveling salesman problem for G' .
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 16/22
16
If sa is an approximate solution obtained for G' using algorithm A then f $ sa% & cn by assumption
$using f $ sa% & cf $ s'%%.
+eaning, if G has a "amiltonian circuit then f $ sa% & cn. $'%
. If G does not have a "amiltonian circuit then the shortest tour in G must contain at least oneedge cn*) and f $ sa% f $ s'% / cn.
+eaning, if G does not have a "amiltonian circuit then f $ sa% / cn. $''%
#o if the approximation algorithm for the traveling salesman problem runs in polynomial time
we can solve the "amiltonian circuit decision problem in polynomial time by using ine0ualities
$'% and $''%.
123
!e suspect that P 4 P, so we suspect there does not exist a polynomial time approximatealgorithm with finite c for the traveling salesman problem.
This does not mean that graphs with restrictions cannot have c-approximations.
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 17/22
17
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 18/22
18
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 19/22
1
;riel -i5u55 a/out eare5t eig2/or algorit2m. 8De 09
The nearest nei#hbor al#orith" was one of the first al#orith"s sed to deter"ine asoltion to the travellin# sales"an proble"$ In it, the sales"an starts at a rando" city andrepeatedly visits the nearest city ntil all have been visited$ It @icly yields a short tor,bt sally not the opti"al one$
These are the steps of the al#orith"2
$ stand on an arbitrary verte+ as crrent verte+$'$ find ot the shortest ed#e connectin# crrent verte+ and an nvisited verte+ P$S$ set crrent verte+ to P$$ "ar P as visited$Z$ if all the vertices in do"ain are visited, then ter"inate$^$ o to step '$
The nearest nei#hbor al#orith" is easy to i"ple"ent and e+ectes @icly, bt itcan so"eti"es "iss shorter rotes which are easily noticed with h"an insi#ht, de to its#reedy natre$ As a #eneral #ide, if the last few sta#es of the tor are co"parable inlen#th to the first sta#es, then the tor is reasonable if they are "ch #reater, then it isliely that there are "ch better tors$ Another chec is to se an al#orith" sch as thelower bond al#orith" to esti"ate if this tor is #ood eno#h$
In the worst case, the al#orith" reslts in a tor that is "ch lon#er than theopti"al tor$ To be precise, for every constant r there is an instance of the travelin#sales"an proble" sch that the len#th of the tor len#th co"pted by the nearestnei#hbor al#orith" is #reater than r ti"es the len#th of the opti"al tor$
Qoreover, for each n"ber of cities there is an assi#n"ent of distances between thecities for which the nearest nei#hbor heristic prodces the ni@e worst possible tor$ Thenearest nei#hbor al#orith" "ay not find a feasible tor at all, even when one e+ists$
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 20/22
2#
The Nearest)Nei#hbor Al#orith" (NN is an appro"imate al#orithm for findin# a sb)opti"al soltion to the T/!$ The Z)city plan, bt it cold be co"pted by hand for her Z=cities in less than Z "intes
Al#orith"2
[ ]( )
[ ]
[ ]
[ ]
[ ]
dist NN
ji NN i,jd idist
idist i,jd ji
n j
idist
ni
p||pi,jd
n jni p p p P
ji
n
,return
56756
then
56andif
to)for
to)for
88compute
5,)65,,)6allfor 9lgorithm )
←←
<≠
=
∞←
=
−=
∈∈
=
eare5t eig2/or -epen-5 ritiall on t2e -i5tane metri:
Normalize Featre !ales:
All featres shold have the sa"e ran#e of vales (e$#$, ,L$ 4therwise, featres withlar#er ran#es will be treated as "ore i"portant
"emo#e Irrele#ant Featres:
Irrelevant or noisy featres add rando" pertrbations to the distance "easre and hrtperfor"ance
$earn a Distance %etric:
4ne approach2 wei#ht each featre by its "tal infor"ation with the class$
Smoothing:
ind the nearest nei#hbors and have the" vote$ This is especially #ood when there isnoise in the class labels$
E@ample:
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 21/22
21
$ /tart at ho"e (of corse'$ Ne+t city will be the closest as!yet!(nvisited one (if there are two or "ore at the
sa"e closest distance, ?st pic any one of the"S$ o there$ Jepeat '$ and S$ ntil no "ore nvisited citiesZ$ o ho"e
Prepared ByMrs.A.Subathra Devi AP / MCA
8/11/2019 DAA Unit - V
http://slidepdf.com/reader/full/daa-unit-v 22/22
22
E+plain in detail abot Twice)arond)the tree al#orith"$ Dec '=
Describe appro+i"ation al#orith"s for the N!)hard proble"$ Dec '=
Prepared ByMrs.A.Subathra Devi AP / MCA