You are on page 1of 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
(. De)ine P * P+
! is the set of all decision proble"s solvable by deter"inistic al#orith"s in
polyno"ial ti"e$ N! is the set of all decision proble"s solvable by nondeter"inistic
al#orith"s in polyno"ial ti"e$
,. De)ine re-u.i/ilit0+
%et %& & %' be proble"s$ !roble" %& red(ces to %' iff there is a way to solve %& by a
deter"inistic polyno"ial ti"e al#orith" (sin# a deter"inistic al#orith" that solves %' in
polyno"ial ti"e$
3. De)ine P12ar- * P '.omplete+
A proble" % is N! hard if and only if satisfiability red(ces to % $A proble" % is N!
co"plete if and only if % is N!)hard and % * N!$
4. &tate .oo345 t2eorem+
Any NP problem can be converted to SAT in polynomial time.
Cooks Theorem States That Satisfiability is in P If and Only If P =NP.
In co"p(tational co"ple+ity theory, the Coo,-%evin theore", also ,nown as Coo,.s
theore", states that the Boolean satisfiability proble" is N!)co"plete$ That is, any proble"
in N! can be red(ced in polyno"ial ti"e by a deter"inistic T(rin# "achine to the proble" of
deter"inin# whether a Boolean for"(la is satisfiable$
6. $i5t out t2e 5trateg0 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 %& s(ch that fro" the
sol(tion of I0 we can deter"ine the sol(tion to instance I of %&$
Concl(de fro" step ii that iii red(ces to %'
Concl(de fro" steps i and iii and the transitivity of red(ce that %' is N!)hard$
6. De)ine P1Complete pro/lem. 8De. ,0(0 * ,0((9
Prepared By
Mrs.A.Subathra Devi AP / MCA
#%T ' V P1:ARD 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"$
2
A decision proble" D is said to be N!)co"plete if it belon#s to class N!$ Every
proble" in N! is polyno"ial red(cible to D$
E#$ 2 3a"iltonian circ(it proble" and decision version of travelin# sales"an proble"$
<. De)ine 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+ 8Aun ,0((9
A feasible sol(tion with val(e close to the val(e of an opti"al sol(tion is called
appro+i"ate sol(tions$ An appro+i"ation al#orith" for ! is an al#orith" that #(arantees
appro+i"ate sol(tions for !$
An e)appro+i"ate al#orith" is an f(n)appro+i"ate al#orith" for which f(n56E$
B. De)ine a/5olute appro@imation algorit2m+
A is an absol(te appro+i"ation al#orith" for proble" p if and only if for every
instance I of p, 7f8(I)f9(I756, for so"e constant ,$
(0. De)ine )(n) appro@imate algorit2m + 8De. ,0((9
A 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<=$
(,. De)ine $PT 5.2e-ule+
An %!T sched(le is one that is the res(lt of an al#orith" that whenever the processor
beco"es free assi#ns to that processor a tas, whose ti"e is the lar#est of those tas,s not
yet assi#ned$ Ties are bro,en an arbitrary "anner$
(3.let mC3,nC< an- (t(,t,,t3,t4,t6,t6,t<)C(6,6,4,4,3,3,3)&2o7 $PT * optimal
5.2e-ule+
%!T sched(le finish ti"e 6 &&
4pti"al sched(le finish ti"e 6 >
(4. ?2at i5 /in pa.3ing pro/lem+ 8De. ,0(0 * ,0(,9
In bin pac,in# proble" we are #iven n ob?ects that have to be placed in bins of e@(al
capacity$ The ob?ective is to deter"ine the "ini"(" n("ber of bins needed to
acco""odate all n ob?ects$ No ob?ect "ay be placed partly in one bin and partly in another$
(6. $i5t out t2e =ariou5 2euri5ti.5 )or /in pa.3ing pro/lem.
Airst fit (AA
Best fit (BA
Airst fit decreasin# (AAD
Best fit decreasin# (BAD
(6. Di=e t2e li5t o) 5ome 7ell13no7n pro/lem5 t2at are P1Complete 72en
e@pre55e- a5 -e.i5ion pro/lem+ 8Aun ,0((9
3a"iltonian circ(it
1napsac, proble"
Travelin# sales"an proble"$
Prepared By
Mrs.A.Subathra Devi AP / MCA
3
(<. De)ine -etermini5ti., non -etermini5ti. algorit2m+
If the al#orith" has the property that the res(lt of every operation is (ni@(ely
defined then it is said to be deter"inistic al#orith"
Be can allow al#orith"s to contain operations whose o(tco"es are not (ni@(ely
defined b(t are li"ited to specified set s of possibilities$ The "achine e+ec(tin# s(ch
operations is allowed to choose any one of these o(tco"es s(b?ect to ter"ination
condition $This leads to the concept of a nondeter"inistic al#orith"$
(>. ?rite DA )or 5ear.2 algorit2m+
Consider the proble" of searchin# for an ele"ent + in a #iven set of ele"ents$ Be
are re@(ired to deter"ine an inde+ ? s(ch that AC?D 6 + of ? 6 = if + is not A$
Al#orith"2
?2 6 choice (&, nE
If AC?D 6 + then Fwrite (?E s(ccess ( EG
Brite (=E fail(re (E
(B. De)ine -e.i5ion 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. De)ine optimiEation algorit2m+
Any proble" that involves the identification of an opti"al either "a+i"(" or
"ini"(" val(e of a #iven cost f(nction is ,nown as an opti"i:ation proble"$ An
opti"i:ation al#orith" is (sed to solve an opti"i:ation proble"$
,(. De)ine tra.ta/le an- intra.ta/le pro/lem5. Di=e one e@ample o) intra.ta/le
pro/lem.
Tractable problems 2 !roble"s that can be solved in polyno"ial ti"e are called
tractable 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 circ(it, ,napsac, proble", travelin# sales"an proble"$
,,. E@plain t2e t2eor0 o) .omputational .omple@it0.
A proble".s intractability re"ains the sa"e for all principal "odels of co"p(tations
and all reasonable inp(t encodin# sche"es for the proble" (nder consideration$
,3. E@plain un-e5ira/le pro/lem5
If 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.
Iiven a co"p(ter pro#ra" and an inp(t to it, deter"ine whether the pro#ra" will
halt on that inp(t or contin(e wor,in# indefinitely on it$
,6. ?2en a -e.i5ion pro/lem i5 5ai- to /e pol0nomial re-u.i/le.
Prepared By
Mrs.A.Subathra Devi AP / MCA
4
A decision proble" Dl is said to be polyno"ial red(cible to a decision proble" D' if
there e+ists a f(nction t that transfor"s instances of Dl to instances ofD' s(ch 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"p(table by a polyno"ial ti"e al#orith"
,6. De)ine a :euri5ti..
A he(ristic is a co""on)sense r(le drawn fro" e+perience rather than fro" a
"athe"atically proved assertion$
E+2 Ioin# to the nearest (nvisited city in the travelin# sales"an proble" is a #ood
ill(stration for 3e(ristic$
,<. De)ine Tra=er5al5.
Bhen 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 te.2niFue5 )or tra=er5al5 in grap2.
Breadth first search
Depth first search
,B. ?2at i5 arti.ulation point+
A verte+ v in a connected #raph I is an artic(lation 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 "ore
none"pty co"ponents
30. ?2at i5 neare5t neig2/or algorit2m+ 8Aun ,0(,9
The nearest nei#hbo(r al#orith" was one of the first al#orith"s (sed to deter"ine a
sol(tion to the travellin# sales"an proble"$ In it, the sales"an starts at a rando" city and
repeatedly visits the nearest city (ntil all have been visited$ It @(ic,ly yields a short to(r,
b(t (s(ally not the opti"al one$
3(. ?2at i5 a pol0nomial time algorit2m+
An al#orith" is said to be polyno"ial ti"e al#orith", if its worst)case ti"e efficiency
belon#s to 4(p(n, where p(n is a polyno"ial of the proble"0s inp(t si:e n$
3,. ?2at i5 a pol0nomiall0 re-u.i/le -e.i5ion pro/lem+
A decision proble" D& is said to be polyno"ially nonred(cible to a decision proble"
D' if there e+ists a f(nction t that transfor"s instances of D& to instances of D' s(ch
that It "aps all yes instances of D& to yes instances of D' and all no instances of D&
to no instances of D'$
It is co"p(table by a polyno"ial)ti"e al#orith"$
33. :o7 7ill 0ou .al.ulate t2e relati=e error o) Appro@imation algorit2m+
re (sa 6 f(sa)f(s8
f(s8
where, /a is the appro+i"ate sol(tion and s8 is the e+act sol(tion$
34. ?2at i5 a..ura.0 ratio+
Acc(racy ratio is the ratio of acc(racy of appro+i"ate sol(tion to the e+act sol(tion$
r (sa 6 f(sa
Prepared By
Mrs.A.Subathra Devi AP / MCA
5
f(s8
36. ?2at i5 per)orman.e ratio+
!erfor"ance ratio is the "etric indicatin# the @(ality of the appro+i"ation al#orith"$
The acc(racy ratio r(sa ta,en over all the instances of the proble" is called perfor"ance
ratio$
36. ?rite t2e t7i.e1aroun-1t2e1tree algorit2m.
The twice)aro(nd)the)tree)al#orith" e+ploits a connection between 3a"iltonian circ(its and
spannin# trees of the sa"e #raph$ The steps are2
Constr(ct 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, aro(nd the "ini"(" spannin# tree
recordin# the vertices passed by$

/can the list of vertices obtained in step ' and eli"inate fro" it all repeated
occ(rrences of the sa"e verte+ e+cept the startin# one at the end of list$ The
vertices re"ainin# in the list for" the 3a"iltonian circ(it$
3<. Pro=e t2at t2e t7i.e1aroun-1t2e1tree algorit2m i5 a ,1appro@imation algorit2m
)or t2e tra=eling 5ale5man pro/lem 7it2 Eu.li-ean -i5tan.e5.
Twice)aro(nd)the)tree al#orith" is a polyno"ial al#orith", as we (se the !ri"0s or
1r(s,al0s al#orith" to #enerate a "ini"(" spannin# tree$ As the len#th of the to(r /a
obtained by the twice)aro(nd)the)tree al#orith" is at "ost twice the len#th of the opti"al
to(r /8E 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 #reater
than 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$
Th(s the twice)aro(nd)the)tree al#orith" is a ')appro+i"ation al#orith" for the travelin#
sales"an proble" with E(clidean distances$
3>. Pro=e t2at i) P not eFual to P, t2en t2ere e@i5t5 no .1appro@imation algorit2m
)or t2e tra=eling 5ale5man pro/lem.
That is, there e+ists no polyno"ial)ti"e al#orith" for this proble" so that for all
instances f(sa 56 cf(s8 $ /(ppose that s(ch an appro+i"ation al#orith" A and a constant
C e+ists, to prove this by contradiction$ %et I be an arbitrary #raph with n vertices$ BE "ap
I to a co"plete wei#hted #raph I0 by assi#nin# wei#ht & to each of its ed#es and addin# an
ed#e wei#hted cnL& between each pair of vertices not ad?acent in I$
If I has a 3a"iltonian circ(it its len#th in I0 is n$ f(sa$ 56 cn$
If I does not have a 3a"iltonian circ(it, the shortest to(r in I0 will contain at least
one ed#e of wei#ht cnL&$ f(sa <6 f(s8 <cn$
Ta,in# both the ine@(alities, we can solve the 3a"iltonian circ(it proble" with
polyno"ial al#orith", while "appin# I to I0 and applyin# al#orith" A to #et the shortest
Prepared By
Mrs.A.Subathra Devi AP / MCA
6
to(r in I0, which is also based on constant c$ This is a contradiction to !6N!$ 3ence the
assertion is ri#ht$
3B. ?2at i5 a )ir5t1)it algorit2m an- a )ir5t1)it -e.rea5ing algorit2m )or /in
pa.3ing+
The first)fit al#orith" places each ite" in the order #iven, into the first bin the ite"
fits inE when there are no s(ch bins, place the ite" in a new bin and add this bin to the end
of bin list$
The fits)fit decreasin# al#orith" for the bin)pac,in# proble" starts by sortin# the
ite"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. ,0(0, Aun ,0(( * ,0(,9
P 1 Pro/lem:
! vers(s N! polyno"ial vers(s nondeter"inistic polyno"ial refers to a theoretical
@(estion presented in &>M& by %eonid %evin and /tephen Coo,, concernin# "athe"atical
proble"s that are easy to solve ! type as opposed to proble"s that are diffic(lt to solve N!
type$
Any ! type proble" can be solved in Npolyno"ial ti"e$N A polyno"ial is a
"athe"atical e+pression consistin# of a s(" of ter"s, each ter" incl(din# a variable or
variables raised to a power and "(ltiplied by a coefficient$ A ! type proble" is a polyno"ial
in the n("ber of bits that it ta,es to describe the instance of the proble" at hand$ An
e+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 ta,es to describe the proble"$ An
e+a"ple of an N! type proble" is brea,in# a &'O)bit di#ital cipher$ The ! vers(s N!
@(estion is i"portant in co""(nications, beca(se it "ay (lti"ately deter"ine the
effectiveness or ineffectiveness of di#ital encryption "ethods$
P 1 Pro/lem:
An N! proble" defies any br(te)force approach at sol(tion, beca(se findin# the
correct sol(tion wo(ld ta,e trillions of years or lon#er even if all the s(perco"p(ters in the
world were p(t to the tas,$ /o"e "athe"aticians believe that this obstacle can be
s(r"o(nted by b(ildin# a co"p(ter capable of tryin# every possible sol(tion to a proble"
Prepared By
Mrs.A.Subathra Devi AP / MCA
7
si"(ltaneo(sly$ This hypothesis is called ! e@(als N!$ 4thers believe that s(ch a co"p(ter
cannot be developed ! is not e@(al to N!$ If it t(rns o(t that ! e@(als N!, then it will beco"e
possible to crac, the ,ey to any di#ital cipher re#ardless of its co"ple+ity, th(s renderin# all
di#ital encryption "ethods worthless$
In co"p(tational co"ple+ity theory, N! is one of the "ost f(nda"ental co"ple+ity
classes$ The abbreviation N! refers to Nnondeter"inistic polyno"ial ti"e$N
The e@(ivalence of the two definitions follows fro" the fact that an al#orith" on s(ch
a non)deter"inistic "achine consists of two phases, the first of which consists of a #(ess
abo(t the sol(tion, which is #enerated in a non)deter"inistic way, while the second consists
of a deter"inistic al#orith" that verifies or re?ects the #(ess as a valid sol(tion to the
proble"$
The co"ple+ity class ! is contained in N!, b(t N! contains "any i"portant proble"s,
the hardest of which are called N!)co"plete proble"s, for which no polyno"ial)ti"e
al#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", as,s whether
polyno"ial ti"e al#orith"s act(ally 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 pol0nomial time algorit2m )or a , .oloring pro/lem -etermine 72et2er
=erti.e5 o) a gi=en grap2 5an /e .olore- in no more t2an , .olor5. 8De. ,0(09
In #raph theory, #raph colorin# is a special case of #raph labelin#, it is an assi#n"ent
of labels traditionally called NcolorsN to ele"ents of a #raph s(b?ect to certain constraints$ In
its si"plest for", it is a way of colorin# the vertices of a #raph s(ch that no two ad?acent
vertices share the sa"e colorE 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 face
colorin# of a planar #raph assi#ns a color to each face or re#ion so that no two faces that
share a bo(ndary have the sa"e color$
Perte+ colorin# is the startin# point of the s(b?ect, and other colorin# proble"s can
be transfor"ed into a verte+ version$ Aor e+a"ple, an ed#e colorin# of a #raph is ?(st a
verte+ colorin# of its line #raph, and a face colorin# of a plane #raph is ?(st a verte+ colorin#
of its d(al$ 3owever, non)verte+ colorin# proble"s are often stated and st(died as is$ That is
partly for perspective, and partly beca(se so"e proble"s are best st(died in non)verte+
for", as for instance is ed#e colorin#$
The convention of (sin# colors ori#inates fro" colorin# the co(ntries of a "ap, where
each face is literally colored$ This was #enerali:ed to colorin# the faces of a #raph e"bedded
in the plane$ By planar d(ality it beca"e colorin# the vertices, and in this for" it #enerali:es
to all #raphs$ In "athe"atical and co"p(ter representations, it is typical to (se the first few
positive or nonne#ative inte#ers as the NcolorsN$ In #eneral, one can (se any finite set as
the Ncolor setN$ The nat(re of the colorin# proble" depends on the n("ber of colors b(t not
on what they are$
Iraph 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, or
on the way a color is assi#ned, or even on the color itself$ It has even reached pop(larity
Prepared By
Mrs.A.Subathra Devi AP / MCA
8
with the #eneral p(blic in the for" of the pop(lar n("ber p(::le /(do,($ Iraph colorin# is
still a very active field of research$
Pol0nomial 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 th(s co"p(table in linear ti"e (sin# breadth)first
search$ Qore #enerally, the chro"atic n("ber and a correspondin# colorin# of perfect
#raphs can be co"p(ted in polyno"ial ti"e (sin# se"i definite pro#ra""in#$ Closed
for"(las for chro"atic polyno"ial are ,nown for "any classes of #raphs, s(ch as forest,
chordal #raphs, cycles, wheels, and ladders, so these can be eval(ated in polyno"ial ti"e$
If the #raph is planar and has low branch width, then it can be solved in polyno"ial
ti"e (sin# dyna"ic pro#ra""in#$ In #eneral, the ti"e re@(ired is polyno"ial in the #raph
si:e, b(t e+ponential in the branch width$
E@a.t algorit2m5:
Br(te)force search for a k)colorin# considers every of the assi#n"ents of k colors
to n vertices and chec,s for each if it is le#al$ To co"p(te the chro"atic n("ber and the
chro"atic polyno"ial, this proced(re is (sed for every , i"practical for
all b(t the s"allest inp(t #raphs$
Usin# dyna"ic pro#ra""in# and a bo(nd on the n("ber of "a+i"al independent
sets, k)colorability can be decided in ti"e and space $ Usin# the principle of
incl(sion-e+cl(sion and Rates0s al#orith" for the fast :eta transfor", k)colorability can be
decided in ti"e for any k$ Aaster al#orith"s are ,nown for S) and T)colorability,
which can be decided in ti"e and , respectively$
Running time: !(,
n
n)
3. Di5.u55 t2e appro@imation algorit2m P :ar- )or 5ol=ing Tra=elling &ale5man
pro/lem. Di=e e@ample. 8De. ,0(09
The travelin# sales"an proble" consists of a sales"an and a set of cities$ The
sales"an has to visit each one of the cities startin# fro" a certain one (e$#$ the ho"etown
and ret(rnin# to the sa"e city$ The challen#e of the proble" is that the travelin# sales"an
wants to "ini"i:e the total len#th of the trip$ The travelin# sales"an proble" can be
described as follows2
Prepared By
Mrs.A.Subathra Devi AP / MCA

T/! 6 F(I, f, t2 I 6 (P, E a co"plete #raph,


f is a f(nction PUP V, !
t " V,
I is a #raph that contains a travelin# sales"an to(r with cost that does not e+ceed tG$
E@ample2
Consider the followin# set of cities2
The proble" lies in findin# a "ini"al path passin# fro" all vertices once$ Aor
e+a"ple the path !ath& FA, B, C, D, E, AG and the path !ath' FA, B, C, E, D, AG pass all the
vertices b(t !ath& has a total len#th of 'T and !ath' has a total len#th of S&$
Appro@imation1T&P
Inp(t2 A co"plete #raph I (P, E
4(tp(t2 A 3a"iltonian cycle
&$select a WrootX verte+ r " P CID$
'$(se Q/T)!ri" (I, c, r to co"p(te a "ini"(" spannin# tree fro" r$
S$ass("e % to be the se@(ence of vertices visited in a preorder tree wal, of T$
T$ret(rn the 3a"iltonian cycle 3 that visits the vertices in the order %$
Prepared By
Mrs.A.Subathra Devi AP / MCA
1#
(a a set of vertices is shown$ !art (b ill(strates the res(lt of the Q/T)!ri" th(s the
"ini"(" spannin# tree Q/T)!ri" constr(cts$ The vertices are visited li,e FA, B, C, D, E, A
by a preorder wal,$ !art (c shows the to(r, which is ret(rned 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 to(r for a set of vertices$ A spannin# tree is constr(cted by
deletin# ed#es fro" a to(r$ Th(s, an opti"al to(r has "ore wei#ht than the "ini"(")
spannin# tree, which "eans that the wei#ht of the "ini"(" spannin# tree for"s a lower
bo(nd on the wei#ht of an opti"al to(r$
c(t Y c(38$ &
%et a f(ll wal, of T be the co"plete list of vertices when they are visited re#ardless if they
are visited for the first ti"e or not$ The f(ll wal, is B$ In o(r e+a"ple2
B 6 A, B, C, B, D, B, E, B, A,$
The f(ll wal, crosses each ed#e e+actly twice$ Th(s,
c(B 6 'c(T$ '
Aro" e@(ations & and ' we can write that
c(B Y 'c(38, S
Bhich "eans that the cost of the f(ll path is at "ost ' ti"e worse than the cost of an
opti"al to(r$
The f(ll path visits so"e of the vertices twice which "eans it is not a to(r$ Be can
now (se the trian#le ine@(ality to erase so"e visits witho(t increasin# the cost$ The fact we
are #oin# to (se is that if a verte+ a is deleted fro" the f(ll path if it lies between two visits
to b and c the res(lt s(##ests #oin# fro" b to c directly$
Prepared By
Mrs.A.Subathra Devi AP / MCA
11
In o(r e+a"ple we are left with the to(r2 A, B, C, D, E, A$ This to(r is the sa"e as
the 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$ Be have derived 3
deletin# ed#es fro" the f(ll wal, so we can write2
c(3 Y c(B T
Aro" S and T we can i"ply2
c(3 Y ' c(38$ Z
This last ine@(ality co"pletes the proof$
4. ?rite 52ort note an P1Complete pro/lem. 8Aun ,0(( * ,0(,9
Definition of NP-completeness:
A decision proble" is N!)co"plete if2
&$ is in N!, and
'$ Every proble" in N! is red(cible to in polyno"ial ti"e$
can be shown to be in N! by de"onstratin# that a candidate sol(tion to can be
verified in polyno"ial ti"e$
A proble" satisfyin# condition ' is said to be N!)hard, whether or not it satisfies
condition &$ A conse@(ence of this definition is that if we had a polyno"ial ti"e al#orith"
for , we co(ld solve all proble"s in N! in polyno"ial ti"e$
The concept of N!)co"pleteness was introd(ced in &>M& by /tephen$ N!)co"plete
proble"s co(ld be solved in polyno"ial ti"e on a deter"inistic T(rin# "achine$ since
nobody 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 are
iso"orphic if one can be transfor"ed into the other si"ply by rena"in# vertices$ Consider
these two proble"s2

Iraph Iso"orphis"2 Is #raph I& iso"orphic to #raph I'

/(b#raph Iso"orphis"2 Is #raph I& iso"orphic to a s(b#raph of #raph I'


The /(b#raph Iso"orphis" proble" is N!)co"plete$ The #raph iso"orphis"
proble" is s(spected to be neither in ! nor N!)co"plete, tho(#h it is in N!$ This is an
e+a"ple of a proble" that is tho(#ht to be hard, b(t 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 that
it is in N!, and then to red(ce so"e ,nown N!)co"plete proble" to it$ Therefore, it is (sef(l
to ,now a variety of N!)co"plete proble"s$ The list below contains so"e well),nown
proble"s that are N!)co"plete when e+pressed as decision proble"s$
Prepared By
Mrs.A.Subathra Devi AP / MCA
12
Theorem:
%et ! and J be two proble"s$ If ! red(ces 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 s(ch that
%et AJ be the polyno"ial ti"e al#orith" for proble" J$ Clearly, A ta,es as inp(t IJ,
and ret(rns as o(tp(t Answer([J,IJ
Desi#n a new al#orith" A! as follows2
Al#orith" A!(inp(t2 I!
be#in
IJ 26 T(I!E
+ 26 AJ(IJE
ret(rn +E
end
Note that this al#orith" A! ret(rns the correct answer Answer([!,I! beca(se + 6
AJ(IJ 6 Answer([J,IJ 6 Answer([!,I!$
Note also that the al#orith" A! ta,es polyno"ial ti"e beca(se both T and AJ
[$E$D$
The int(ition derived fro" the previo(s theore" is that if a proble" ! red(ces to proble" J,
then J is at least as diffic(lt as !$
T2eorem:
A proble" J is N!)co"plete if
&$ J is N!, and
'$ There e+ists an N!)co"plete proble" J= that red(ces to J
!roof2
/ince J is N!, it re"ain to show that any arbitrary N! proble" ! red(ces to J$
Prepared By
Mrs.A.Subathra Devi AP / MCA
13
%et ! be an arbitrary N! proble"$
/ince J= is N!)co"plete, it follows that ! red(ces to J=
And since J= red(ces to J, it follows that ! red(ces to J$
[$E$D$
The previo(s theore" a"o(nts 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 are
s(fficient2
!rove J to be N!
Aind an already ,nown N!)co"plete proble" J=, and co"e (p with a transfor" that
red(ces J= to J$
6. ?rite a non-etermini5ti. Gnap5a.3 algorit2m. 8Aun ,0((, Aun * De. ,0(,9
Deterministic algorithms
Al#orith"s with (ni@(ely defined res(lts
!redictable in ter"s of o(tp(t for a certain inp(t
Nondeter"inistic al#orith"s are allowed to contain operations whose o(tco"es are
li"ited to a #iven set of possibilities instead of bein# (ni@(ely defined
/pecified with the help of three new 4(& f(nctions
(. .2oi.e ( & )
Arbitrarily chooses one of the ele"ents of set /
+ 6 choice(&,n can res(lt in + bein# assi#ned any of the inte#ers in the ran#e C&, nD,
in a co"pletely arbitrary "anner
No r(le to specify how this choice is to be "ade
,. )ailure()
/i#nals (ns(ccessf(l co"pletion of a co"p(tation
Cannot be (sed as a ret(rn val(e
3. 5u..e55()
/i#nals s(ccessf(l co"pletion of a co"p(tation
Cannot be (sed as a ret(rn val(e
If there is a set of choices that leads to a s(ccessf(l co"pletion, then one choice
fro" this set "(st be "ade
A nondeter"inistic al#orith" ter"inates (ns(ccessf(lly iff there e+ist no set of
choices leadin# to a s(ccess si#nal
A "achine capable of e+ec(tin# 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+ ? s(ch that AC?D 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 By
Mrs.A.Subathra Devi AP / MCA
14
1NA!/AC1 (in 4/2 set of ob?ectsE[U4TA2 n("berECA!ACITR2 n("berEo(t /2 set of ob?ectsE
A4UND2 boolean
be#in / 26 e"ptyE
total\val(e 26 =E
total\wei#ht 26 =E
A4UND 26 falseE
pic, an order % over the ob?ectsE
loop
choose an ob?ect 4 in %E
add 4 to /E
total\val(e 26 total\val(e L 4$val(eE
total\wei#ht 26 total\wei#ht L 4$wei#htE
if total_\wei#ht < CA!ACITR then fail
else if total_\val(e < 6 [U4TA
A4UND 26 tr(eE
s(cceedE
endif endif
delete all ob?ects (p to 4 fro" %E
endloop
end
6. E@plain Appro@imation algorit2m5 )or Tra=elling &ale5man Pro/lem. 8Aun ,0((9
Qany proble"s of practical si#nificance are N!)co"plete b(t are too i"portant to
abandon "erely beca(se obtainin# an opti"al sol(tion is intractable$ If a proble" is N!)
co"plete, we are (nli,ely to find a polyno"ial)ti"e al#orith" for solvin# it e+actly, b(t this
does not i"ply that all hope is lost$ There are two approaches to #ettin# aro(nd N!)
co"pleteness$ Airst, if the act(al inp(ts are s"all, an al#orith" with e+ponential r(nnin#
ti"e "ay be perfectly satisfactory$ /econd, it "ay still be possible to find near!optimal
sol(tions in polyno"ial ti"e$ In practice, near)opti"ality is often #ood eno(#h$ An al#orith"
that ret(rns near)opti"al sol(tions is called an appro"imation al#orithm$
Airst co"p(te a str(ct(re)a "ini"(" spannin# tree)whose wei#ht is a lower bo(nd
on the len#th of an opti"al travelin#)sales"an to(r$ Be will then (se the "ini"(" spannin#
tree to create a to(r whose cost is no "ore than twice that of the "ini"(" spannin# tree.s
wei#ht, as lon# as the cost f(nction 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" as
a s(bro(tine$
A!!J4`)T/!)T4UJ($, cN<
& select a verte+ r " % C$D to be a NrootN verte+
' co"p(te 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
T return the ha"iltonian cycle ' that visits the vertices in the order &
Prepared By
Mrs.A.Subathra Devi AP / MCA
15
Jecall fro" that a preorder tree wal, rec(rsively visits every verte+ in the tree, listin# a
verte+ when it is first enco(ntered, before any of its children are visited$
ill(strates 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 by
Q/T)!JIQ$ !art (c shows how the vertices are visited by a preorder wal, of T , and part (d
displays the correspondin# to(r, which is the to(r ret(rned by A!!J4`)T/!)T4UJ$ !art (e
displays an opti"al to(r, which is abo(t 'Sa 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 time
for 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 By
Mrs.A.Subathra Devi AP / MCA
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 one
edge 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 approximate
algorithm with finite c for the traveling salesman problem.

This does not mean that graphs with restrictions cannot have c-approximations.
Prepared By
Mrs.A.Subathra Devi AP / MCA
17
Prepared By
Mrs.A.Subathra Devi AP / MCA
18
Prepared By
Mrs.A.Subathra Devi AP / MCA
1
;rie)l0 -i5.u55 a/out eare5t eig2/or algorit2m. 8De. ,0((9
The nearest nei#hbo(r al#orith" was one of the first al#orith"s (sed to deter"ine a
sol(tion to the travellin# sales"an proble"$ In it, the sales"an starts at a rando" city and
repeatedly visits the nearest city (ntil all have been visited$ It @(ic,ly yields a short to(r,
b(t (s(ally not the opti"al one$
These are the steps of the al#orith"2
&$ stand on an arbitrary verte+ as c(rrent verte+$
'$ find o(t the shortest ed#e connectin# c(rrent verte+ and an (nvisited verte+ P$
S$ set c(rrent verte+ to P$
T$ "ar, P as visited$
Z$ if all the vertices in do"ain are visited, then ter"inate$
^$ Io to step '$
The nearest nei#hbo(r al#orith" is easy to i"ple"ent and e+ec(tes @(ic,ly, b(t it
can so"eti"es "iss shorter ro(tes which are easily noticed with h("an insi#ht, d(e to its
N#reedyN nat(re$ As a #eneral #(ide, if the last few sta#es of the to(r are co"parable in
len#th to the first sta#es, then the to(r is reasonableE if they are "(ch #reater, then it is
li,ely that there are "(ch better to(rs$ Another chec, is to (se an al#orith" s(ch as the
lower bo(nd al#orith" to esti"ate if this to(r is #ood eno(#h$
In the worst case, the al#orith" res(lts in a to(r that is "(ch lon#er than the
opti"al to(r$ To be precise, for every constant r there is an instance of the travelin#
sales"an proble" s(ch that the len#th of the to(r len#th co"p(ted by the nearest
nei#hbo(r al#orith" is #reater than r ti"es the len#th of the opti"al to(r$
Qoreover, for each n("ber of cities there is an assi#n"ent of distances between the
cities for which the nearest nei#hbor he(ristic prod(ces the (ni@(e worst possible to(r$ The
nearest nei#hbo(r al#orith" "ay not find a feasible to(r at all, even when one e+ists$
Prepared By
Mrs.A.Subathra Devi AP / MCA
2#
The Nearest)Nei#hbor Al#orith" (NN is an appro"imate al#orithm for findin# a s(b)
opti"al sol(tion to the T/!$ The Z)city plan, b(t it co(ld be co"p(ted by hand for her Z=
cities in less than Z "in(tesb
Al#orith"2
[ ] ( )
[ ]
[ ]
[ ]
[ ]
dist NN
j i NN i,j d i dist
i dist i,j d j i
n j
i dist
n i
p ||p i,j d
n j n i
p p p P
j i
n
, return
5 6 7 5 6
then
5 6 and if
to ) for

to ) for
88 compute
5 , ) 6 5, , ) 6 all for
9lgorithm
, )

<
=

=
=

=
eare5t eig2/or -epen-5 .riti.all0 on t2e -i5tan.e metri.:
Normalize Featre !ales:
All feat(res sho(ld have the sa"e ran#e of val(es (e$#$, C&,L&D$ 4therwise, feat(res with
lar#er ran#es will be treated as "ore i"portant
"emo#e Irrele#ant Featres:
Irrelevant or noisy feat(res add rando" pert(rbations to the distance "eas(re and h(rt
perfor"ance
$earn a Distance %etric:
4ne approach2 wei#ht each feat(re by its "(t(al infor"ation with the class$
Smoothing:
Aind the , nearest nei#hbors and have the" vote$ This is especially #ood when there is
noise in the class labels$
E@ample:
Prepared By
Mrs.A.Subathra Devi AP / MCA
21
&$ /tart at ho"e (of co(rse
'$ 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$ Io there
T$ Jepeat '$ and S$ (ntil no "ore (nvisited cities
Z$ Io ho"e
Prepared By
Mrs.A.Subathra Devi AP / MCA
22
E+plain in detail abo(t Twice)aro(nd)the tree al#orith"$ CDec '=&&D
Describe appro+i"ation al#orith"s for the N!)hard proble"$ CDec '=&&D
Prepared By
Mrs.A.Subathra Devi AP / MCA

You might also like