Download as pdf
Download as pdf
You are on page 1of 26
Je Vishal R- Mone ST Subset - Sum cr eee Toh echo que “& is the. “posi ve= number Sell 12 8. = FS), 89,3, . 843 then_we have to Anpd_a_ “Subset ushose._sum_ 15 equed te gtver ___ __pasitire. integer ——C—C “Tl a) “the aot of the bimang doce zreprcsted 0 Dy ~__8)| ik: tem 1s gelectes od Dreat J tle | y= Lele MEO. —9)| AB tern ss geleabedt at geamal_jevel_thenwde. __| tg =) ebse x20. “hl The LePb cbualal Reprereral therm 1 ineduclect § Ryle le nepesel idem is_met_foelerleal. —_ Ual mode ef the deee. wepresen_c deol enol oR abe Mem, i te 6 tng eae the Set ems in_aacedling. order. Bocarnples’. 5 Ot tek _W =| = Cu 18.24 2 aaa Bumneal . Bay 4 © scanned with OKEN Scanner Example Il= $3,5,6, 43 arel_ M218. ‘ Susm-of subseh —== —o= | 22 $6, 10,1215, 15, 181 ard A=B0 © scanned with OKEN Scanner “The Queen's Rooblemss- Ty bas Aype. of technique nen chessboard, ewhich we howe to plore. T queens 3¢ thek two queens ablack each other by veg? tothe me WHO OR Bame coburn oRTI9 aoe Queens! ab) Queen’ must be plued a tine Chessbooaad_tn the Coburn 1,2, 9,070) 4, Wainy — 9 fGiacen' Paoblems take) 6S Neder art: drs oh ane leat Nodes. © scanned with OKEN Scanner lato @| @ & IQ LI Ia! J Cle Salut 4 Bebdtun 2) i} S—Sr= Ae Howeildonian Cyele (Ci Cee b) Poobler, - Toth, type of Ashen Goa GECV,2) rs ah Sx Haraikteoten Seams. Mearlen cts) “ey Soa Ths Geeph Bo 1. j Senden Ys @ Herzen i © scanned with OKEN Scanner ND! «e hiaue. - Bouneh ane Boand * Beane Branching 1s a process to parlid m is dome at vache erch rot R cle tree » Baxey mple & ~the wt maine of he -locer 1s " pevetiineel into Bie, aR rnc Bub codes, are there sw ahoges ove the beanehes af coop Q So A 18 work are] B,C ave tHe dese. Bearley, an |Bourod | 5. this Process frest Fina lest solution — “antl earl Suo,node ss laoaunded rot cthe beat Sobedivey, LR ttre mecle_nrelue ts mab. hesk Salut Hen Bounol tre onede ebse Contnoue tpiabdain beat Solectron, ot Fromple 9246,9,4 = 82 32,4,53 ae best Saleheo 4 B wha Bocinded beanwec 2 (5 ters theo & op 45 1S grader then Ge Sratlorly Node Motue > &O) are bounded © scanned with OKEN Scanner uw h-@acen a Gureey 4: Buen exormplas usr Beare arel Bou - Asphngue a shoe, oe co 4 7 A Assignment Roo ber = ; = Pen ble Stedermeub = ae oo a necl: fash Job a hirctcelal ay ml Dacha et one ——the ak Bult Same “hoo gebs_arel eso peoples = + Lem men me “elena bd © scanned with OKEN Scanner “Seleest SVOrBalagelS | [ex esetrecte] [LBP SD] | [[ b=4 b=1 [[ be3 | Les or Gears | [LB=o4BeSteal [ip-oeF ee F=f | a — / go lo nen! C=3 ae Cy nn Lpaareero2g _ Lower Bounel alae rs —| © scanned with OKEN Scanner = aa. 0 #_Kmapsak Posblem Weng Baanth & Bound 3 M-murmloeet of tems avel rt weigtet (0,050, WA ara) Proked- is CP, Pay 222 Pa). The kn k Z TS) My 9b.ts Requtoed do Pol a Subsel fd rte, cthekpapsack.__ : ao “Ke mam objective. “pro objeut nb - thekmapsatk 0 that menuimum probi! 1s ablained ~Oprol_weipt at tte chosen abjyenb is mat greeter un chen hs . a . _ Mascimize. < PrMy Subyedcto He consteank Laie 2 Te’ —“hperny | Upper bound Cob) can be colealedea tame;Zlloan Sas tang fg —_| eb = y+ (M= 00 Je Vir feoar | E +—— i= =_ == —=. oe eee tthe tedlowing knapsack, problem cma beanch — beune| giten tie YaLtosny ceca. Capaty eto Wem [weg [vale i Ao = et = #% Ae = = : HE |b |ro |= | to © scanned with OKEN Scanner Step 10 —_|___ofalue tre, Neo ,wWro Wp = V+ (M- 0) fea bb = O+Ci0-0 Ja 16 __|_ [oe = loo Shep iRe=b veo wee IE HO, w= 4” worth then 4 toitheat ier 4. VEO, W=0 b oro = ho N= O40 =O uptira= 4 W=Ota =o —trb Hho-tlo fess Ob =404 6x 7 hz ho+éas [ilo-=s-9_ Ube 40 +(1o-4 J2 Vb fig, © scanned with OKEN Scanner I< : Ne 2s, 0925 Ns OS +40 265° wr arg =9 GPE VAC REB I Wy UBER) a Ub = ho+ € HY b= 6S #1) #4 ab= 64 Fines} [eHer] 265 w29, Vue SO Gtep 4 if beg —__upeth item 4% ~ potthouk Hem 4% V6: w= 3 veo, w2@ i Ns 6St0=65" OR ARB ETE We 4to = 9 hens tad lin $b (a0) Ys path a =65+(lo-“Dao “[eesee] final. Site Spee dooce arenes cel an aon \ZON © scanned with OKEN Scanner Nod Feast ble af 4 woutheed 3 [o=a, ve6s] [o=4voto ‘ Llepzes | | ebzey | pouty Sy sthouk 4 | wW2Id |_| W=9,v26S Uub=65" Not Feast, opdurned Sabetiory Foal Sebbon 1 113% : ee VAlith sthe. helP of Slade space tree solve. th — es inedance_of the knaps p = ranch ‘tana! Bocinol cabgerctttren. Tee _[weght [Value I \o loo 2 T 63 3 8 5 ae fj _knapsack Copan is a I6 a — dd © scanned with OKEN Scanner ep ize =o SUF ja, at ote © scanned with OKEN Scanner Shep S I= 2210, vain bors then OL Ne 63, w= Ne. te €8t\mo = 6g | F\O+ 4 = \q WM dhs ps mob feasible Solprn, — A Fe : jo | i if 2040 =0 Rea = Veliog =F i ups 63s(e-g)a Fe 2x t gh F a | Bre > ee en we = \\Q4 a) Va leou = 3 bs Gb 119 +(16-1s)% 3 Fe Coreray i= een - serene © scanned with OKEN Scanner Te ™" tObep. Ie 4 weis, v= J19 c : Je ay 4 worth them ato SRE 8D = Ly P= Geis =19 — Oa bpmeatferible Solute 2; _ | Net Pease, ee Final tolaton rs {2,33 as ——<—=— = Ronton © scanned with OKEN Scanner “Taye ling Selespe ser) Frocblers — if there mem cilies arel cost a aly | Room a ethy to other city 1s_ given Ythen w have joSobsain rv, sherds Heth Such @oh ethy isis Heo Si ence arel then vetwrn “sto sheet Ty drs teil ae 1s fare aed weyshtec| q eaph » Faample + Salve the cTsP_uairg rane Faria | Poon ee are ae = reoheliGl elias net hook -snallaksdisanee deo \ ash _ city i GQebrcrdte pAmteriprreueie aa (B= © scanned with OKEN Scanner 4g te wes 0d smallest Mone i / 4 Semple boorer ed) ioe a a+b, 26, deol dye (aio) = = Fa ae TCE) RCS) i. | raga LEON smears Aa e A aa-S AS xe0 “ay ne ay-r(ers Ateton 7 © scanned with OKEN Scanner Cis mlkkeb Broa betpare 2273 32. 2 1 Cees 2400. —ugehisriretaee ors go = m9 a pe dea a.bLeaa padecal fades __ er 24 f LB= 13 Le=o4] [16-16 | © scanned with OKEN Scanner s ‘ 1 e at e Cel) ancl: loot mot wvisttel verter is 2” j Hier edye (de) and finaly Peash do stontany aded-c colly e Fe, 2), aaa thot we carn wute ede (4, b,0,d,€,a) OCT aia A= two edyes (ab) +(%€) = (348) 21 b= two edges (a-b )tCb-c) = (326) =9 c= (belt Ced) = 644 = 10 | d= Gdjtcdhel= Yrs = 4 | € =(de}+fea) =Ars = TieteSh LO bse Ty eae ‘ edge (a,b. © 2.d,a) ia epee eee Bc aleac aes *y si Cae) n(n) ng HEA) C218) aes ie “eblye(a. bid. 2,c,a). see Tt CarieCarz) (tt D(a tot) 2 1¢ Lg <5? om: a Tsp path ts arbadverers _| Ths TSP Pa = 16 Lenton © scanned with OKEN Scanner —_ Design and Analysis of Algorithms 7-2 NP-Comploto and NP-Hard Proton, EAD Basic Concepts There are two 8roups in which a problem can be classified. The first group consist of the problems that can be solved in polynomial time. For example : searching of an element from the list O(logn), sorting of elements O(logn). The second group consists of problems that can be solved in non-determiniatic Polynomial time. For example : Knapsack problem O(22) and Travelling Salesperson problem (O(n?2")). * Any pivitom for which answer is either yes or no is called decision problem, The algorithm for decision problem is called decision algorithm. Any problem that involves the identification of optimal cost (minimum or maximum) is called optimization problem. The algorithm for optimization problem is called optimization algorithm. Definition of P - Problems that can be solved in polynomial time. (“P” stands for Polynomial). The polynomial time is nothing but the time expressed in terms of | polnomial. Examples - Searching of key element, Sorting of elements, All pair shortest path. Definition of NP - It stands for “non-deterministic polynomial time”. Note that NP does not stand for “non-polynomial”. Examples - Travelling Salesperson problem, Graph coloring problem, Knapsack problem, Hamiltonian circuit problems. The NP class problems can be further categorized into NP-complete and NP hard problems. © A problem D is called NP-complete if - i) It belongs to class NP ii) Every problem in NP can also be solved in polynomial time. ¢ If an NP-hard problem can be solved in polynomial time then all NP-complete problems can also be solved in polynomial time. + All NP-complete problems are NP-hard but all NP-hard problems cannot be NP-complete. Fig. 7.1.4 TECHNICAL PUBLICATIONS”. An_ Up thrust for knowledge © Scanned with OKEN Scanner ‘Analysis of Algorithms oes Ge NP-Complete and NP-Hard Problems + The NP class problems are the q non-deterministic polynomial algorith: « Computational Complexity - The co; of instances with a solution for every lecision problems that can be solyed by ims. mputational problems is an infinite collection t instance. In computational complexity the solution to the problem can be “yes” or “no” type. Such type of problems are called decision problem. The computational problems can be function problem. The function problem is a computational problem where single output is expected for every input. The output of such type of problems is more complex than the decision problem. The computational problems also consists of a class of problems, whose output can be obtained in polynomial time. * Complexity classes - The complexity classes is a set of problems of related complexity. It includes function problems, P classes, NP classes, optimization problem. * Intractability - Problems that can be solved by taking a long time for their solutions is known as intractable problems. IENP is not same as P then NP complete problems are called intractable problems. Example of P Class Problem Kruskal’s algorithm : In Kruskal's algorithm the minimum weight is obtained. In this algorithm also the circuit should not be formed. Each time the edge of minimum weight has to be selected, from the graph. It is not necessary in this algorithm to have edges of ninimum weights to be adjacent. Let us Solve one example by Kruskal’s algorithm. Example : Find the minimum spanning tree for the following figure using Kruskal's algorithm. 1 5 Fig. 7.4.2 TECHNICAL PUBLICATIONS Anup tus for rowed © Scanned with OKEN Scanner — Design and Analysis of Algonthms 7-4 'NP-Complote and NP-Hard Problem ae Kruskal’s algorithm, we will start with some vertex and will cover all the vertices minimum weight. The vertices need not be adjacent. d a ‘ b 1 f a, 1 b 1 a f 2 Fig. 7.1.3 EAE¥ Example of NP Class Problem Travelling Salesman's Problem (TSP) : This problem can be stated as " Given a set of cities and cost to travel between each pair of cities, determine whether there is a path that visits every city once and returns to the first city. Such that the cost travelled is less”. For example : Refer Fig. 7.1.4. The tour path will be a-b-d-e-c-a and total cost of tour will be 16. This problem is NP problem as there may exist some path with shortest distance between the cities. If you get the solution by applying certain algorithm then Travelling Salesman problem is NP Complete Problem. If we get no solution at all by applying an algorithm then the travelling salesman problem belongs to NP hard class. TECHNICAL PUBLICATIONS” An up thrust for knowledge © Scanned with OKEN Scanner 7-5 NP-Complote and NP-Hard Problems Difference between P and NP Class Problems _ P Clase Problems ss problems are basically after particular stage. All the NP class problems are basically non deterministic. which is a P class is also in NP Every problem which is in NP is not the P class. problem. ‘can be solved efficiently. NP class problems can not be solved efficiently 1s efficiently as P class problems. Binary Search, Bubble Sort Examples : Knapsack problem, Traveling Salesperson problem. (EEEIIEZED ist three problems that have polynomial time algorithms, Justify your answer. Solution : The problems that can be solved in polynomial time are called P - class problems. For example - 1. Binary search - In searching an element using binary search method, the list is simply divided at the mid and either left or right sublist is searched for key element. This process is carried out in O(logn). TECHNICAL PUBLICATIONS” An up thrust for knowledge © Scanned with OKEN Scanner Design and Analysis of Algorithms __1: 4 Mamet ere tert aaa 2. Evaluation of polynomial - In a polynomial evaluation we make out the Summation of each term of polynomial. The evaluated result is simply an ‘inte er. This process is carried out in O(n) time. 3. Sorting a list - The elements in a list can be arranged Ch in ascending of descending order. This procedure is carried out in O(nlogn) time. This shows that all the above problems can be solved in polynomial time. EZ] Non-Deterministic Algorithms The algorithm in which every operation is uniquely defined is called deterministic algorithm. The algorithm in which every operation may not have unique result, rather there can be specified set of possibilities for every operation. Such an algorithm is called non-deterministic algorithm. Non-deterministic means that no Particular rule is followed to make the guess. The non-deterministic algorithm is a two stage algorithm - i) Non-deterministic (Guessing) stage - Generate an arbitrary string that can be thought of as a candidate solution. 1) Deterministic (“Verification”) stage - In this stage it takes as input the candidate solution and the instance to the problem and returns yes if the candidate solution represents actual solution. Algorithm Non_Determin() 1/ Alin] is a set of elements q ; #/ we have to determine the index i of A at which element x is | /Nocated. | // The following for-loop is the guessing stage for i=1 ton do choose(i); Ab: // Next is the verification(deterministic) stage if (Afi] = x) then { 5 oie write(i); seg success(); write(0); . fail(); In the above given non-deterministic al; lgorithm there are three functions used - 1) Choose - Arbitrarily chooses one of the element from given input set. TECHNICAL PUBLICATIONS”. An up tut for knowledge © Scanned with OKEN Scanner lard Problema Design and Analysis of Algorithms 7-7 NP-Complote and NP 2) Fail - Indicates the unsuccessful completion, 3) Success - Indicates successful completion, The algorithm is of non-deterministic complexity O(1), when A is not ordered then the deterministic search algorithm has a complexity 2 (n). Give the non-deterministic algorithm for sorting elements in non decreasing, onder. Solution : Algorithm Non_DSort(A,n) // Alta] is an array that stores n elements which are positive integers B[1:n] be an oon guessing stage for il ton do Biij<-0;//initialize B to 0 for i-1 ton do t je-choose(1,n)//select any element from the input set Titime requ ini rithm wit input set is minimum ‘The time required by a non-deterministic algorithm with some inpu number of os nel to reach to a successful completion if there are multiple choices from input set leading to successful completion. In short, a non-deterministic algorithm is of complexity O(f(n)) where n is the total size of input. P, NP, NP-Complete and NP-Hard Classes deterministic polynomial language e As know, P denotes the class of all nistic Prices ‘and NP denotes the class of all non-deterministic polynomial language problems. Hence P c NP. © The question of whether or not P = NP holds, is the most famous outstanding problem in the computer science, TECHNICAL PUBLICATIONS An up thust for knowledge © Scanned with OKEN Scanner so _ Dest andAnapasotAbatnms 7 =8_ NR Corp and NP Herd Prob Problems which are known to lie in P are often called as tractable. Problems which lie outside of P are often termed as intractable, Thus, the question of whether P = NP or P # NP is the same as that of asking whether there exist problems in NP which are intractable or not. The relationship between P and NP is depicted by following figure - Crt P-problems Fig. 7.3.1 P and NP problems assuming P + NP We don’t know if P = NP. However in 1971 S.A. Cook proved that a particular NP problem known as SAT(Satisfiability of sets of boolean clauses) has the property that, if it is solvable in polynomial time, so are all NP problems. This is what is called a “NP-complete” problem. Let A and B are two problems then problem A reduces to B if and only if there is a way to solve A by deterministic polynomial time algorithm using a deterministic algorithm that solves B in polynomial time. A reduces to B can be denoted as A = B. In other words we can say that if there exists any polynomial time algorithm which solves B then we can solve A in polynomial time. We can also state that if A « B and B« C then A «C. A NP problem such that, if it is in P, then NP = P. If a (not necessarily NP) problem has this same property then it is called “NP-hard”. Thus the class of NP-complete problem is the intersection of the NP and NP-hard classes. NP complete P class problems problems Fig. 7.3.2 Relationship between P, NP, NP-complete and NP-hard Problems TECHNICAL PUBLICATIONS”. An up thrust for inowtod © Scanned with OKEN Scanner pene one Abel of Alvorttene 7-9 [NP-Complto and NP-Hard Problems * Normally the decision problems are NP-complete but optimization problems are NP-hard. However if problem A is a decision problem and B is optimization Problem then it is possible that A « B, For instance the knapsack decision problem can be knapsack optimization problem. * There are some NP-hard problems that are not NP-complete. For example halting problem. The halting problem states that: “Is it possible to determine whether an algorithm will ever halt or enter in a loop on certain input?” . ‘Two problems P and Q are said to be polynomially equivalent if and only if Pe«QandQ«P. NP. Fig. 7.3.3 1. Define P, NP and NP-complete problems. Pee o nee 2. Distinguish between P, NP and NP-complete problems. Give examples for ench category. 3. Explain P and NP problems with examples. Q00, TEGHNICAL PUBLICATIONS”- Anup thst or nowtedze © Scanned with OKEN Scanner

You might also like