You are on page 1of 12

SYLLABUS

141401 UNIT I DESIGN AND ANALYSIS OF ALGORITHMS 3104 9

Algorithm Analysis Time Space Tradeoff Asymptotic Notations Conditional asymptotic notation Removing condition from the conditional asymptotic notation - Properties of big-Oh notation Recurrence e uations Solving recurrence e uations Analysis of linear search! UNIT II 9 "ivide and Con uer# $eneral %ethod &inary Search 'inding %a(imum and %inimum %erge Sort $reedy Algorithms# $eneral %ethod Container )oading *napsac+ Problem! UNIT III 9 "ynamic Programming# $eneral %ethod %ultistage $raphs All-Pair shortest paths Optimal binary search trees ,-. *napsac+ Travelling salesperson problem ! UNIT IV 9 &ac+trac+ing# $eneral %ethod / 0ueens problem sum of subsets graph coloring 1amiltonian problem +napsac+ problem! UNIT V 9 $raph Traversals Connected Components Spanning Trees &iconnected components &ranch and &ound# $eneral %ethods 2'3'O 4 )C5 ,-. *napsac+ problem 3ntroduction to NP-1ard and NP-Completeness! TUTORIAL = 15 TEXT BOOK: .! 6llis 1oro7it89 Sarta: Sahni and Sanguthevar Ra:ase+aran9 Computer Algorithms- C;;9 Second 6dition9 <niversities Press9 =,,>! 2'or <nits 33 to ?5 =! *!S! 6as7ara+umar9 Ob:ect Oriented "ata Structures using C;;9 ?i+as Publishing 1ouse pvt! )td!9 =,,, 2'or <nit 35 REFERENCES: .! T! 1! Cormen9 C! 6! )eiserson9 R!)!Rivest9 and C! Stein9 @3ntroduction to Algorithms@9 Second 6dition9 Prentice 1all of 3ndia Pvt! )td9 =,,A! =! Alfred ?! Aho9 Bohn 6! 1opcroft and Beffrey "! <llman9 @The "esign and Analysis of Computer Algorithms@9 Pearson 6ducation9 .CCC! Total = 60

141401 -DESI N AND ANAL!SIS OF AL ORIT"#S


UNIT $I 1% &'at () a* Al+o,(t'-. An algorithm is a se uence of unambiguous instructions for solving a problem9 i!e!9 for obtaining a re uired output for any legitimate input in a finite amount of time /% &'at a,0 S0120*t(al Al+o,(t'-). The central assumption of the RA% model is that instructions are e(ecuted one after another9 one operation at a time! Accordingly9 algorithms designed to be e(ecuted on such machines are called Se uential algorithms! 3% &'at () Al+o,(t'- D0)(+* T04'*(120. An algorithm design techni ue is a general approach to solving problems algorithmically that is applicable to a variety of problems from different areas of computing! 4% D05(*0 6)027o 4o70% A Pseudo code is a mi(ture of a natural language and programming language li+e constructs! A pseudo code is usually more precise than a natural language9 and its usage often yields more succinct algorithm descriptions! 5% D05(*0 Flo84'a,t% A method of e(pressing an algorithm by a collection of connected geometric shapes containing descriptions of the algorithmDs steps! 6% E9:la(* Al+o,(t'-;) Co,,04t*0)) To prove that the algorithm yields a re uired result for every legitimate input in a finite amount of time! 6(ample# Correctness of 6uclidDs algorithm for computing the greatest common divisor stems from correctness of the e uality gcd 2m9 n5 E gcd 2n9 m mod n5! <% &'at () E55(4(0*4= o5 al+o,(t'-. 6fficiency of an algorithm can be precisely defined and investigated 7ith mathematical rigor! There are t7o +inds of algorithm efficiency .5 Time 6fficiency 3ndicates ho7 fast the algorithm runs =5 Space 6fficiency 3ndicates ho7 much e(tra memory the algorithm needs! >% &'at () +0*0,al(t= o5 a* al+o,(t'-. 3t is a desirable characteristic of an algorithm! $enerality of the problem the algorithm solves is sometimes easier to design an algorithm for a problem posed in more general terms! 9% &'at () al+o,(t'-;) O:t(-al(t=. Optimality is about the comple(ity of the problem that algorithm solves! Fhat is the minimum amount of effort any algorithm 7ill need to e(ert to solve the problem in uestion is called algorithmDs Optimality!

&'at 7o =o2 -0a* ?= &o,)t 4a)0-E55(4(0*4=@ o5 a* al+o,(t'-. The Forst case-6fficiencyG of an algorithm is its efficiency for the 7orst-case input of si8e n9 7hich is an input 2or inputs5 of si8e n for 7hich the algorithm runs the longest among all possible inputs of that si8e! 6(# if you 7ant to sort a list of numbers in ascending order 7hen the numbers are given in descending order! 3n this running time 7ill be the longest! 10% &'at 7o =o2 -0a* ?= B0)t 4a)0-E55(4(0*4=@ o5 a* al+o,(t'-. The &est case-6fficiencyG of an algorithm is its efficiency for the &est-case input of si8e n9 7hich is an input2or inputs5 of si8e n for 7hich the algorithm runs the fastest among all possible inputs of that si8e! 6(# if you 7ant to sort a list of numbers in ascending order 7hen the numbers are given in ascending order! 3n this running time 7ill be the smallest! 11% D05(*0 t'0 AA0,a+0-4a)0 055(4(0*4=@ o5 a* al+o,(t'-. The Average-case efficiencyG of an algorithm is its efficiency for the input of si8e n9 for 7hich the algorithm runs bet7een the best case and the 7orst case among all possible inputs of that si8e! 1/% 13% "o8 to -0a)2,0 t'0 al+o,(t'-;) 055(4(0*4=. 3t is logical to investigate the algorithmDs efficiency as a function of some parameter n indicating the algorithmDs input si8e! 6(ample# 3t 7ill be the si8e of the list for problems of sorting9 searching9 finding the listDs smallest element9 and most other problems dealing 7ith lists! 14% &'at () 4all07 t'0 ?a)(4 o:0,at(o* o5 a* al+o,(t'-. The most important operation of the algorithm is the operation contributing the most to the total running time is called basic operation of an algorithm! 15% "o8 to -0a)2,0 a* al+o,(t'-;) ,2**(*+ t(-0. )et Cop be the time of e(ecution of an algorithmDs basic iteration on a particular computer and let C 2n5 be the number of times this operation needs to be e(ecuted for this algorithm! Then 7e can estimate the running time T2n5 of a program implementing this algorithm on that computer by the formula T2n5 H Cop C2n5 16% D05(*0 o,70, o5 +,o8t'% The efficiency analysis frame7or+ concentrates on the order of gro7th of an algorithmDs basic operation count as the principal indicator of the algorithmDs efficiency! To compare and ran+ such orders of gro7th 7e use three notations .5 O 2&ig oh5 notation =5 I 2&ig Omega5 notation 4 A5 J 2&ig Theta5 notation 1<% D05(*0 B(+ o' *otat(o* #a=BC2*0 /006D A:,(lB#a= /00> A function t2n5 is said to be in O2g2n55 denoted t2n5 K O 2g2n559 if t2n5 is bounded above by some constant multiple of g2n5 for all large n9 i!e!9 if there e(ist some positive constant c and some non negative integer n, such that T 2n5 L c g 2n5 for n M n, A

1>%

6,oA0 t'at 100*E5 O F*/G. Clearly .,,n;N .,,n;n 2for all n N5 E .,.n.,.n= &y choosing n,EN and cE.,. 7e find that .,,n;NO 2n=5!

19%

D05(*0 H *otat(o* A function t2n5 is said to be in I 2g2n559 denoted t2n5 I 2g2n559 if t2n5 is bounded belo7 by some positive constant multiple of g2n5 for all large n9 i!e!9 if there e(ist some positive constant c and some non negative integer n, such that T 2n5 L c g 2n5 for n M n, /0% 6,oA0 t'at *3 F*/G. Clearly nA n= for all n ,!

i!e!9 7e can select cE. and n,E,!

/1%

D05(*0 I - *otat(o* A function t2n5 is said to be in J2g2n559 denoted t2n5 J 2g2n559 if t2n5 is bounded both above and belo7 by some positive constant multiples of g2n5 for all large n9 i!e!9 if there e(ist some positive constant c. and c= and some non negative integer n, such that c= g 2n5 L t 2n5 L c. g2n5 for n M n, 6,oA0 t'atF JG*F*-1G F*/G .-=n2n-.5E2.-=5n=-.-=n .-= n= for all n,!27e have proved upper ine uality5 no7 .-=n2n-.5E2.-=5n=-.-=n2.-=5n=-.-=nO.-=n2for all n=5E.-P n= hence 7e can select c=E.-P9c.E.-= and n,E=! //% /3% &'at () t'0 2)0 o5 A)=-:tot(4 Notat(o*). The notations O9 and and are used to indicate and compare the asymptotic orders of gro7th of functions e(pressing algorithm efficiencies! 6ART-B .! 2a5 "escribe the steps in analy8ing 4 coding an algorithm! 2.,5 2b5 6(plain some of the problem types used in the design of algorithm! 2Q5 =! 2a5 "iscuss the fundamentals of analysis frame7or+ ! 2.,5 2b5 6(plain the various asymptotic notations used in algorithm design! 2Q5 A! 2a5 6(plain the general frame7or+ for analy8ing the efficiency of algorithm! 2/5 2b5 6(plain the various Asymptotic efficiencies of an algorithm! 2/5 P! 2a5 6(plain the basic efficiency classes! 2.,5 2b5 6(plain briefly the concept of algorithmic strategies! 2Q5 N! "escribe briefly the notions of comple(ity of an algorithm! 2.Q5 Q! 2a5 Fhat is Pseudo-codeR 6(plain 7ith an e(ample! 2/5 2b5 'ind the comple(ity C2n5 of the algorithm for the 7orst case9 best case and average case! 26valuate average case comple(ity for nEA9Fhere n is the number of inputs5 2/5 >! Set up 4 solve a recurrence relation for the number of +ey comparisons made by above pseudo code! 2P5

UNIT II 1G E9:la(* 7(A(70 a*7 4o*120, al+o,(t'-) "ivide and con uer is probably the best +no7n general algorithm design techni ue! 3t 7or+ according to the follo7ing general plan i5A problemSs instance is divided into several smaller instances of the same problem9 ideally of about the same si8e! ii5 The smaller instances are solved iii5 3f necessary9 the solutions obtained for the smaller instances are combined to get a solution to the original problem /G D05(*0 #0,+0 So,t %erge sort is a perfect e(ample of a successful application of the divide and con uer techni ue! 3t sorts a given array AT,!!!n-lU by dividing it into t7o halves AT,!!!Tn-=U - .U and ATTn-=U!!!!n-lU9 sorting each of them recursively9 and then merging the t7o smaller sorted arrays into a single sorted one! 3G D05(*0 B(*a,= S0a,4' &inary Search is remar+ably efficient algorithm for searching in a sorted array! 3t 7or+s by comparing a search +ey * 7ith the arraySs middle element ATmU! 3f they match9 the algorithm stopsV Other7ise9 the same operation is repeated recursively for the first half of the array if *L ATmU and for the second half if * M ATmU AT,U!!!!!!!!!!!!!ATm-lU ATmU ATm;lU!!!!!!!!!!!!! ATn-lU 4G &'at 4a* 80 )a= a?o2t t'0 aA0,a+0 4a)0 055(4(0*4= o5 ?(*a,= )0a,4'. A sophisticated analysis sho7s that the average number of +ey comparisons made by binary search is only slightly smaller than that in the 7orst case Cavg2n5 log=n 5G "o8 7(A(70 a*7 4o*120, t04'*(120 4a* ?0 a::l(07 to ?(*a,= t,00). Since the binary tree definition itself divides a binary tree into t7o smaller structures of the same type9 the left subtree and the right subtree9 many problems about binary trees can be solved by applying the divide-con uer techni ue! 6G E9:la(* I*t0,*al a*7 E9t0,*al No70) To dra7 the treeSs e(tension by replacing the empty subtrees by special nodes!The e(tra nodes sho7n by little s uares are called e(ternal! The original nodes sho7n by littile circles are called internal! <G D05(*0 t'0 I*t0,*al 6at' L0*+t' The 3nternal Path )ength 3 of an e(tended binary tree is defined as the sum of the lengths of the paths - ta+en over all internal nodes- from the root to each internal node! >G D05(*0 t'0 E9t0,*al 6at' L0*+t' The 6(ternal Path )ength 6 of an e(tended binary tree is defined as the sum of the lengths of the paths - ta+en over all e(ternal nodes- from the root to each e(ternal node! 11G E9:la(* a?o2t +,007= t04'*(120 N

The greedy techni ue suggests constructing a solution to an optimi8ation problem through a se uence of steps9 each e(panding a partially constructed solution obtained so far9 until a complete solution to the problem is reached! On each step9 the choice made must be feasible, locally optimal and irrevocable.

6ART-B .5 Frite a pseudo code for divide 4 con uer algorithm for merging t7o sorted arrays in to a single sorted one!6(plain 7ith e(ample! 2.=5 =5 Construct a minimum spanning tree using *rus+alDs algorithm 7ith your o7n e(ample! 2.,5 A5 6(plain about *napsac+ Problem 7ith e(ample P5 6(plain "i:i+stra algorithm 2/5 N5 "efine Spanning tree!"iscuss design steps in PrimDs algorithm to construct minimum spanning tree 7ith an e(ample! 2.Q5 Q5 6(plain *rus+alDs algorithm! 2/5 >5 6(plain about binary search 7ith e(ample!

UNIT III 1GD05(*0 D=*a-(4 6,o+,a--(*+ "ynamic programming is a techni ue for solving problems 7ith overlapping problems! Typically9 these subproblems arise from a recurrence relating a solution to a given problem 7ith solutions to its smaller subproblems of the same type! Rather than solving overlapping subproblems again and again9 dynamic programming suggests solving each of the smaller sub problems only once and recording the results in a table from 7hich 7e can then obtain a solution to the original problem! /G D05(*0 B(*o-(al Co055(4(0*t n The &inomial Coefficient9 denoted C2n9+5 or + is the number of Combinations2subsets5 of + elements from an n-element set2,L+Ln5! The name @binomial coefficients@ comes from the participation of these numbers in the so called binomial formula 2a;b5n E2n9,5an;!!!!!!!!!!;C2n9i5an-ibi;!!!!!!!;C2n9n5bn 3G D05(*0 T,a*)(t(A0 4lo)2,0 The transitive closure of a directed graph 7ith n vertices can be defined as the n by n &oolean matri( T E Wti9X9 in 7hich the element in the i th ro7 2.LiLn5 and the :th column 2lL:Ln5 is . if there e(ists a non trivial directed path from the ith verte( to :th verte( V other7ise 9 ti: is ,! 4G E9:la(* &a,)'all) al+o,(t'FarshallSs algorithm constructs the transitive closure of a given digraph 7ith n vertices through a series of n by n &oolean matrices R2,59 YYY9 R2+-l5R2+59YY!!9 R2n5 6ach of these matrices provides certain information about directed paths in the digraph! 4G E9:la(* All-:a(, )'o,t0)t-:at') :,o?l0$iven a 7eighted connected graph 2undirected or directed59 the all pairs shortest paths problem as+s to find the distances2the lengths of the shortest path5 from each verte( to all other vertices! 5G E9:la(* Flo=7K) al+o,(t'3t is convenient to record the lengths of shortest paths in an n by n matri( " called the distance matri(# the element di: in the ith ro7 and the :th column of this matri( indicates the length of the shortest path from the ith verte( to the :th verte( ! Fe can generate the distance matri( 7ith an algorithm that is very similar to 7arshallSs algorithm! 3t is called 'loydSs algorithm! 6G &'at 7o0) Flo=7;) al+o,(t'- 7o. 3t is used to find the distances 2the lengths of the shortest paths5 from each verte( to all other vertices of a 7eighted connected graph! <G E9:la(* :,(*4(:l0 o5 O:t(-al(t= 3t says that an optimal solution to any instance of an optimi8ation problem is composed of optimal solutions to its subinstances! >G E9:la(* O:t(-al B(*a,= S0a,4' T,00) >

One of the principal application of &inary Search Tree is to implement the operation of searching! 3f probabilities of searching for elements of a set are +no7n9 it is natural to pose a uestion about an optimal binary search tree for 7hich the average number of comparisons in a search is the smallest possible! 9G E9:la(* K*a:)a4L :,o?l0$iven n items of +no7n 7eights 7.97=!!!!!!!!!!!7n and values v.9v=!!!!!!!!!!!!vn and a +napsac+ of capacity F9 find the most valuable subset of the items that fit into the +napsac+!2Assuming all the 7eights and the +napsac+Ss capacity are positive integers the item values do not have to be integers!5 10G E9:la(* t'0 #0-o,= F2*4t(o* t04'*(120 The %emory 'unction techni ue see+s to combine strengths of the top do7n and bottomup approaches to solving problems 7ith overlapping subproblems! 3t does this by solving9 in the top-do7n fashion but only once9 :ust necessary sub problems of a given problem and recording their solutions in a table! 11G E9:la(* T,aA0l(*+ )al0)-a* :,o?l0-@. A salesman has to travel n cities starting from any one of the cities and visit the remaining cities e(actly once and come bac+ to the city 7here he started his :ourney in such a manner that either the distance is minimum or cost is minimum! This is +no7n as traveling salesman problem! 6ART-B .5 Solve the all pair shortest path problem for the diagraph 7ith the 7eighted matri( given belo7#abcd a,ZAZ b=,ZZ cZ>,. d Q Z Z ,2.Q5 =5 6(plain FarshallDs 4 'loydDs Algorithm! 2.Q5 A5 6(plain about %ultistage graphs 7ith e(ample! P5 "efine optimal binary search trees 7ith e(ample! N5 6(plain ,-. +napsac+ problem 7ith e(ample! Q5 "iscuss the solution for Travelling salesman problem using branch 4 bound techni ue! 2.Q5

UNIT IV lG E9:la(* Ba4Lt,a4L(*+ The principal idea is to construct solutions one component at a time and evaluate such partially constructed candidates as follo7s! M 3f a partially constructed solution can be developed further 7ithout violating the problemSs constraints9 it is done by ta+ing the first remaininig legitimate option for the ne(t component! M 3f there is no legitimate option for the ne(t component9 no alternatives for any remaining component need to be considered! 3n this case9 the algorithm bac+trac+s to replace the last component of the partially constructed solution 7ith its ne(t option /G E9:la(* Stat0 S:a40 T,00 3f it is convenient to implement bac+trac+ing by constructing a tree of choices being made9 the tree is called a state space tree! 3ts root represents an initial state before the search for a solution begins! 3G E9:la(* :,o-()(*+ a*7 *o*:,o-()(*+ *o70 A node in a state space tree is said to be promising if it corresponds to a partially constructed solution that may still lead to a complete solutionVother7ise it is called nonpromising 4G E9:la(* *-M200*) :,o?l0The problem is to place n ueens on an n by n chessboard so that no t7o ueens attac+ each other by being in the same ro7 or same column or on the same diagonal! 5G E9:la(* S2?)0t-S2- 6,o?l0Fe consider the subset-sum problem# 'ind a subset of a given set SEWS.9S=9!!!!!!!!!!SnX of n positive integers 7hose sum is e ual to a given positive integer d! 6G E9:la(* B,a*4' a*7 Bo2*7 T04'*(120 Compared to bac+trac+ing9 branch and bound re uires The idea to be strengthened further if 7e deal 7ith an optimi8ation problem9 one that see+s to minimi8e or ma(imi8e an ob:ective function9 usually sub:ect to some constraints! <G D05(*0 F0a)(?l0 Sol2t(o* A feasible solution is a point in the problemSs search space that satisfies all the problemSs constraints! 6(# A 1amiltonian Circuit in the traveling salesman problem! A subset of items 7hose total 7eight does not e(ceed the +napsac+Ss Capacity >G D05(*0 O:t(-al )ol2t(o* 3s a feasible solution 7ith the best value of the ob:ective function 6g# The shortest 1amiltonian Circuit The most valuable subset of items that fit the +napsac+

9G#0*t(o* t8o ,0a)o*) to t0,-(*at0 a )0a,4' :at' at t'0 42,,0*t *o70 (* a )tat0-):a40 t,00 o5 a ?,a*4' a*7 ?o2*7 al+o,(t'-% The value of the nodeSs bound is not better than the value of the best solution seen so far! The node represents no feasible solutions because the constraints of the problem are already violated! 10G E9:la(* ,a:' 4olo,(*+@ :,o?l0-% The graph coloring problem as+s us to assign the smallest number of colors to vertices of a graph so that no t7o ad:acent vertices are the same color! 11G E9:la(* K*a:)a4L 6,o?l0'ind the most valuable subset of n items of given positive integer 7eights and values that fit into a +napsac+ of a given positive integer capacity! 6ART-B .! 6(plain the /-0ueenDs problem 4 discuss the possible solutions! 2.Q5 =! Solve the follo7ing instance of the +napsac+ problem by the branch 4 bound algorithm! 2.Q5 A! Apply bac+trac+ing techni ue to solve the follo7ing instance of subset sum problem # SEW.9A9P9NX and dE.. 2.Q5 N! 6(plain subset sum problem 4 discuss the possible solution strategies using bac+trac+ing!2.Q5 Q! 6(plain $raph coloring 7ith e(ample! >! 6(plain about *napsac+ Problem using bac+ trac+ing 7ith e(ample!

.,

UNIT V

1G D05(*0 t,a4ta?l0 a*7 (*t,a4ta?l0 :,o?l0-) Problems that can be solved in polynomial time are called tractable problems9 problems that cannot be solved in polynomial time are called intractable problems! /G E9:la(* t'0 t'0o,= o5 4o-:2tat(o*al 4o-:l09(t= A problemSs intractability remains the same for all principal models of computations and all reasonable input encoding schemes for the problem under consideration 3GE9:la(* 4la)) 6 :,o?l0-) Class P is a class of decision problems that can be solved in polynomial time by2deterministic5 algorithms! This class of problems is called polynomial! 4GE9:la(* 2*704(7a?l0 :,o?l0-) 3f the decision problem cannot be solved in polynomial time9 and if the decision problems cannot be solved at all by any algorithm! Such problems are called <ndecidable! 5G E9:la(* t'0 'alt(*+ :,o?l0$iven a computer program and an input to it9determine 7hether the program 7ill halt on that input or continue 7or+ing indefinitely on it! 6G E9:la(* 4la)) N6 :,o?l0-) Class NP is the class of decision problems that can be solved by nondeterministic polynomial algorithms!%ost decision problems are in NP! 'irst of all9 this class includes all the problems in P! This class of problems is called Nondeterministic polynomial! <GE9:la(* N6-4o-:l0t0 :,o?l0-) A decision problem d is said to be NP-complete if .5 it belongs to class NP =5 every problem in NP is polynomially reducible to "! >G&'0* a 704()(o* :,o?l0- () )a(7 to ?0 :ol=*o-(all= ,0724(?l0 A decision problem "l is said to be polynomially reducible to a decision problem "= if there e(ists a function t that transforms instances of "l to instances of"= such that i5 t maps all yes instances of d. to yes instances odf d= and all no instances of dl to no instances ofd= ii5 t is computable by a polynomial time algorithm 9G D05(*0 a "02,()t(4 A heuristic is a common-sense rule dra7n from e(perience rather than from a mathematically proved assertion! 6(# $oing to the nearest unvisited city in the traveling salesman problem is a good illustration for 1euristic

..

10G E9:la(* N6-"a,7 :,o?l0-) The notion of an NP-hard problem can be defined more formally by e(tending the notion of polynomial reducability to problems that are not necessary in class NP including optimi8ation problems! 11GD05(*0 T,aA0,)al)% Fhen the search necessarilyinvolves the e(amination of every verte( in the ob:ect being searched it is called a traversal! 1/GL()t o2t t'0 t04'*(120) 5o, t,aA0,)al) (* +,a:'% &readth first search "epth first search 13G&'at () a,t(42lat(o* :o(*t% A verte( v in a connected graph $ is an articulation point if and only if the deletion of verte( v together 7ith all edged incident to v disconnects the graph in to t7o or more nonempty components! 6ART-B .! $ive a suitable e(ample 4 e(plain the &readth first search 4 "epth first search! 2.Q5 =! 6(plain about biconnected components 7ith e(ample! A! &riefly e(plain NP-1ard and NP-Completeness 7ith e(amples! P! 6(plain about ,-. *napsac+ Problem using branch and bound 7ith e(ample!

.=

You might also like