DESIGN AND ANALYSIS OF ALGORITHMS – MC9223

%N&' & undamentals

&N'()D%*'&)N of algorithmic problem solving ) Important problem types )

undamentals of the analysis of algorithm efficiency ) analysis frame work ) Asymptotic notations ) *athematical analysis for recursive and non-recursive algorithms.

Part – A 1. What is meant by algorithm? An algorithm is a step by step process for solving a problem or program. It is easy to understand by the programmer who is going to implement the algorithm. 2. Definition / Notation of algorithm. An algorithm is a sequence of unambiguous instructions for solving a computational problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. . "roblem

Algorithm

Input

+ompiler

'utput

3. Write the rules / important points to de elop an algorithm.  The non-ambiguity requirement for each step of an algorithm.  The range of inputs for which an algorithm works has been specified.  The same algorithms are representing in different ways.  Several algorithms for solving the same problem. !. "ist out the implementation of algorithm.  inding a method for solving a problem.  !very step of an algorithm should be in a clear manner.  "suedcode and flowchart are also used to describe the algorithm.  All the steps in an algorithm should be done in manually.  inally implement the algorithm in terms of programming language. #. What are $hara$teristi$s of an algorithm?    I#"$T  %ero or more quantities are e&ternally supplied. '$T"$T  At least one quantity is produced. (! I#IT!#!SS  !ach instruction is clear and unambiguous.

 

I#IT!#!SS  If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after a finite number of steps. ! !+TI,!#!SS  !very instruction must very basic so that it can be carried out, in principle, by a person using only pencil - paper.

+. "ist out the types of algorithms.  !uclid.s algorithm  +onsecutive integer checking algorithm  *iddle-school algorithm. ,. What are the fundamentals steps in ol ed in Algorithmi$ Problem -ol ing? ./%N 20111  $nderstanding the problem  (eciding on • Exact vs. approximate problem solving • Appropriate data structure  (esign an algorithm  "roving correctness  Analy/ing an algorithm • Time efficiency : how fast the algorithm runs • Space efficiency: how much extra memory the algorithm needs. • Simplicity and generality  +oding an algorithm 2. Define se3uential algorithms. The central assumption is that instructions are e&ecuted one after another, one operation at a time. Accordingly algorithms are designed to be e&ecuted on such machines are called sequential algorithms. 4. Define parallel algorithms Some new computers can e&ecute operations concurrently0 i.e. in parallel. Algorithms that take advantage of this capability are called parallel algorithms 10. 5o6 to *hoosing bet6een 78a$t and Appropriate Problem -ol ing? The principal decision is to choose between solving the problem e&actly and solving it appropriately. The former case algorithm is called e&act algorithm latter case, an algorithm is called appropriate algorithm. 11. What is Data -tru$ture? Algorithms + Data structures !rograms

A data structure is a speciali/ed format for organi/ing and storing data. 1eneral data structure types include the array, the file, the record, the table, the tree, and so on. Any data structure is designed to organi/e data to suit a specific purpose so that it can be accessed and worked with in appropriate ways. 12. What is an algorithm design te$hni3ue? An algorithm design technique is a general approach to solving problems algorithmically that is applicable to a variety of problems for different areas of computing. 13. What is pseudo$ode?

A pseudocode is a mi&ture of a natural language and programming language like constructs. A pseudocode is usually more precise than an natural language. 1!. What are the measures used to analy9e an algorithm? There are two kinds of algorithm efficiency2   .D7* 20101

Time efficiency: indicates how fast the algorithm runs. Space efficiency: indicates how much extra memory the algorithm needs.

1#. Write the &mportant Problem 'ypes of algorithm?  Sorting  Searching  String processing  1raph problems  +ombination problems  1eometric problems  #umerical problems 1+. Define -orting 6ith an e8amples. 3earrange the items of a given list in ascending order. Sorting can be done to sort list of numbers, characters from an alphabet, character strings and records maintained by schools about students and libraries about books and companies about their employees. !&amples of sorting algorithms  Selection sort  4ubble sort  Insertion sort  *erge sort  5eap sort 1,. What is meant by -orting :ey? A specially chosen piece of information used to guide sorting. I.e., sort student records by names. 12. What are the t6o properties of sorting algorithm?  Stability2 A sorting algorithm is called stable if it preserves the relative order of any two equal elements in its input.  In place2 A sorting algorithm is in place if it does not require e&tra memory, e&cept, possibly for a few memory units. 14. Define -ear$hing 6ith e8amples The searching problem deals with finding a given value, called a search key, in a given set. !&amples of searching algorithms  Sequential searching  4inary searching 20. What is meant by -tring? A string is a sequence of characters from an alphabet. Strings comprises of Te&t strings2 letters, numbers, and special characters. 4it strings comprises of /eros and ones. 21. What is -tring mat$hing?

eometri$ Problems. evaluating functions.raph $oloring problem. These problems ask to find a combinatorial ob8ect ) such as permutation. a combination. "ist out the e8amples of 6idely used graph algorithms. Ancient 1reeks have invented procedures for solving problems of constructing simple geometric shapes ) triangles.raph Problems? A graph is a collection of points called vertices. 24. are problems that involve mathematical ob8ects of continuous nature2 solving equations and systems of equations. 5o6 6ill you measuring an &nput>s -i9e of a problem? *ost programs runs longer on larger inputs e&ample it takes longer to sort larger arrays. 2. Define tra eling salesman problem. 2+. Define . best-case and average efficiency 30. some of which are connected by line segments called edges. 1raphs can be used for2      *odeling real-life problems *odeling 777 Transportation +ommunication networks "ro8ect scheduling 23. 5o6 to analysis =rame6or: of algorithm?  *easuring an input.s efficiency function:  7orst-base. or a subset ) that satisfies certain constraints and has some desired property. What is *ombinational Problems? The traveling salesman problems and the graph coloring problems are e&amples of combination problems. Define Numeri$al Problems< #umerical problems another large area of application. 22..  !fficiency is defined as a function of input si/e. The graph coloring problem assigns the smallest number of colors to vertices of a graph so that no two ad8acent vertices are the same color.  1raph traversal algorithms  Shortest-path algorithms  Topological sorting 2!. What is meant by .String matching means searching for a given word 6 pattern in a te&t. multiply larger matrices. 2#. Traveling salesman problem which finds the shortest tour through n cities that visits every city e&actly once. 1eometric algorithms deal with geometric ob8ects such as points. . computing definite integrals.s si/e  *easuring running time  'rders of growth 9of the algorithm. lines and polygons.  Input si/e depends on the problem. Define . circles etc. 22.

The most important operation of the algorithm. Define ?asi$ operation. 78amples Problem Input size measure Basic operation >ey comparison addition multiplication Search for a key in a list of n items #umber of items in list.. 3!. or which the algorithm runs the fastest among all possible inputs of si/e n. 33. 3#. what is the input si/e of adding two n by n matrices< 31.:  The probability of the first match occuririn in the ith position of the list is the same for every i. or which the algorithm runs the longest among all possible inputs of si/e n. n "rder of the polynomial 31.s innermost loop. Define A erage $ase 7ffi$ien$y The average case efficiency of an algorithm is its !fficiency for a typical6random input of si/e n. T@nA B copC @nA. #'T the average of worst and best case. Analy/e the algorithm to see what kind of inputs yield largest value of the basic operations count +9n: among the possible inputs of si/e n and compute the worst case value +worst9n:. Then we can estimate the running time T9n: of a program implementing this algorithm on that computer by the formula. the operation contributing the most to the total running time. Define Worst $ase 7ffi$ien$y The worst case efficiency of an algorithm is its !fficiency for the worst case input of si/e n. 78plain the formula of ?asi$ operation. 32.!&ample . What are the standard assumptions of A erage $ase 7ffi$ien$y?  The probability of a successful search is equal to p 9AB@pB@. best case inputs will be lists of si/e n with their first elements equal to a search key according to +best9n: @. Define ?est $ase 7ffi$ien$y The best case efficiency of an algorithm is its !fficiency for the best case input of si/e n.. !&ample for sequential search. . what is the input si/e of the problem of sorting n numbers< !&ample =. or e&ample.  ?et cop # 4e the time of e&ecution of an algorithms basic operation on a particular computer  ?et *@nA ) be the number of times this operation needs to be e&ecuted for this algorithm. Then we should ascertain the value of +9n: on these inputs. n Add two n by n matrices "olynomial !valuation (imensions of matrices. the basic operation is usually the most time-consuming operation in the algorithm.

average case. either find a closed form formula for the count or. if there e&ist some positive constant c.e. A function t$n% is said to be in Ω$g$n%%& denoted t$n% ∈ Ω$g$n%%& if t$n% is bounded below by some constant multiple of g$n% for all large n. if there e&ist some positive constant c and some nonnegative integer n' such that t$n% ≥ cg$n% for all n ≥ n' 34./%N 20121 A function t$n% is said to be in Θ$g$n%%& denoted t$n% ∈ Θ$g$n%%& if t$n% is bounded both above and below by some positive constant multiples of g$n% for all large n.eneral plan to follo6 in analysis re$ursi e algorithms?  (ecide on parameter n indicating input si/e  Identify algorithm. at the very least. i. If it depends on some additional properties the worst case. An algorithm that calls itself is direct recursive. which in turn calls A. i. Define Θ Enotation @'hetaA. !2. What is re$ursi e algorithm? An algorithm is said to be recursive if the same algorithm is invoked in the body.s basic operation is e&ecuted. . .s basic operation  (etermine worst.. average.e.  +heck whether the number of times the basic operation is e&ecuted depends only in the si/e of an input.s basic operation count for the algorithms efficiency. and best case for input of si/e n .  Identify the algorithm. !1.D7* 20101 Asymptotic notations used to compare orders of growth of an algorithm.D7* 20121 3. best case efficiencies have to be investigated. Define Ω Enotation. Algorithm A is said to be indeed recursive if it calls another algorithm.s basic operation. Write a brief note on )Enotation.s si/e. and c= and some nonnegative integer n' such that c( g$n% ≤ t$n% ≤ c) g$n% for all n ≥ n' !0...  Set up sum e&pressing the number of times the algorithm. Write the . if there e&ist some positive constant c and some nonnegative integer n' such that t$n% ≤ cg$n% for all n ≥ n' 32.eneral plan for analy9ing the effi$ien$y for non re$ursi e algorithms? ..  $sing standard formulas and rules of sum manipulation. Write the . i. Define Asymptoti$ Notations 6ith e8amples.3+. A function t$n% is said to be in "$g$n%%& denoted t$n% ∈"$g$n%%& if t$n% is bounded above by some constant multiple of g$n% for all large n. establish its order of growth./%N 20111  (ecide on a parameter indicating an input. Types are    ) @big )hA C @big omegaA D @big thetaA .e.

on the other hand.: D a9n:0 !4. !!. Write algorithm using NonEre$ursi e fun$tion to fine sum of n numbers. ?etIs look at a quick sort.n: C s 2 @ A. which can perform terribly if you always choose . Distinguish bet6een Worst $ase and ?est $ase effi$ien$y.  !ven though mathematical functions can be easily implemented using recursion it is always at the cost of e&ecution time and memory space. but in opposite sequential orders. The input si/e of any instance of a problem is defined to be the number of words 9or the number of elements: needed to describe that instance.. Define Doubly "in:ed list. describes the absolute worst set of input for a given algorithm.  The recursive programs needs considerably more storage and will take more time.s D aEiF0 return s0 G !. !ach node contains two fields. respectively. . Define input si9e.. algorithm sum9a.D7* 20111 A (oubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. !+. Write an algorithm using (e$ursi e fun$tion to fine sum of n numbersF algorithm rsum 9a. It can be conceptuali/ed as two singly linked lists formed from the same data items. to n do s 2 . to facilitate traversal of the list. If there is only one sentinel node. !#. recursive mathematical function is implemented using iterative methods. #0. . a return to the proper location will result when the return to a calling statement is made. Solve the recurrence or estimate the order of magnitude of the solutions !3.A for i@.n: C if9nH A: then return A. point to some kind of terminator.  A recursive procedure can be called from within or outside itself and to ensure its proper functioning it has to save in some order the return addresses so that.D7* 20111 The worst case scenario. n.  Set up a recurrence relation and initial condition9s: for +9n:-the number of times the basic operation will be e&ecuted for an input of si/e n. What is performan$e measurement? "erformance measurement is concerned with obtaining the space and the time requirements of a particular algorithm. called links that are references to the previous and to the ne&t node in the sequence of nodes. The beginning and ending nodesI previous and ne&t links. then the list is circularly linked via the sentinel node.A0 else return rsum9a. Write demerits of re$ursi e algorithms2  *any programming languages do not support recursion0 hence. typically a sentinel node or null.

i. one operation at a time. Parallel algorithms< Some new computers can e&ecute operations concurrently0 i. The best case scenario for an algorithm is the arrangement of data for which this algorithm performs best. Dis$uss about algorithm and its types 6ith e8amples. As an e&ample of illustrating the notion of algorithm. So the best case time comple&ity for this would be '9. Part – ? 1. 1reatest common divisor of two non negative.s algorithm +onsecutive integer checking algorithm *iddle-school algorithm. Differen$e bet6een se3uential and parallel algorithms. 'n the one hand.D7* 20121 The term Kalgorithmic problem solvingL is deliberately ambiguous. denoted by g$d@mFnAF is defined as the largest integer that divides both m and n evenly. The best case scenario for this search is that the target value is at the very center of the data youIre searching.e.. 'ypes of algorithms<    !uclid. 'n the other hand. it can be read as Kalgorithmic-problem solvingL0 this means solving problems that require the formulation of an algorithm for their solution. we usually want to look at the average performance of an algorithm. Why is an algorithm important in problem sol ing? . @iA 7u$lid>s algorithm< . Algorithms that take advantage of this capability are called parallel algorithms #2. (iscounting the best and worst cases. we consider three methods for solving the same problem2 *omputing the greatest $ommon di isor. not.the smallest or largest element of a sub list for the pivot value. it can be read as Kalgorithmic problem-solvingL0 this means e&ploiting what has been learned from the e&perience of developing computer software over the last MA years that has helped us to hone our problem-solving skills./%N 20121 -e3uential algorithms< The central assumption is that instructions are e&ecuted one after another.e. Accordingly algorithms are designed to be e&ecuted on such machines are called sequential algorithms. This will cause quick sort to degenerate to '9n=:.:.both-/ero integers m and n. These are the cases for which the algorithm performs Jnormally. in parallel. An algorithm is a sequence of unambiguous instructions for solving a computational problem. for obtaining a required output for any legitimate input in a finite amount of time.J #1. . Take a binary search for e&ample.

and Step=. not-both-/ero integers m and n 66'utput2 1reatest common divisor of m and n while n Q A do r ß m mod n mßn nßr return m @iiA *onse$uti e &nteger Algorithm     Step.=. StepO2 (ecrease the value of t by . "nG times.2 Assign the value of minCm. 1o to Step .=O: @ = . P gcd9NA. = . = . irst try -.D7* 2011F /%N7 H D7* 20121 @)(A .A:@.. go to Step= StepP2 (ivide n by t. StepP2 Identify all the common factors in the two prime e&pansions found in Step. Pseudo$ode of 7u$lid>s Algorithm< Algorithm Euclid$m& n% 66+omputes gcd9m. . m mod n: Step.!uclid. M =O @ = .2 ind the prime factors of m.. n: @ gcd9n.=O we get2 NA @ = . n: by !uclid.   gcd9m.7u$lid>s algorithm is a famous method which applies repeated equality. StepP2 Assign the value of n to m and the value of r to n. Dis$uss briefly about analy9e phase of algorithm. = .=. go to StepP0otherwise.. If the remainder of this division is A. Step=2 (ivide m by t.s algorithm 66Input2 Two nonnegative. proceed to Step=2 (ivide m by n and assign the value of the remainder to r. "ist out the fundamentals steps of Problem sol ing algorithm. respectively. nG to t. If the remainder of this division is A.=O: @ gdc9=O. or e&ample2 gcd9NA. proceed to StepO.s algorithm2 gcd9m. 9If p is a common factor occurring "m and "n times in m and n.2 If n @ A.: StepO2 +ompute the product of all the common factors and return it as the gcd of the numbers given. P @ . return the value of t as the answer and stop0 otherwise. = .  Thus for the numbers NA. n:2 the largest integer that divides both m and n. P . it should be repeated in minC"m. return the value of m as the answer and stop0 otherwise. 1o to Step=.=:@gcd9. Step=2 ind the prime factors of n.= 2. @iiiA GiddleEs$hool pro$edure    Step.

o Simplicity and generality  *oding an algorithm Understanding the problem  . etc.  De$iding on o !&act vs. appro&imate problem solving o Appropriate data structure  Design an algorithm  Pro ing $orre$tness  Analy9ing an algorithm o Time efficiency 2 how fast the algorithm runs o Space efficiency2 how much e&tra memory the algorithm needs.D7* 20121 Structure of Fundamentals of Algorithmic Problem Solving %nderstanding the problem o 3eading the problems description carefully. Asking questions do a few e&amples by hand0 think about special cases. .Dis$uss in detail< =eatures of the top do6n design and bottom up design approa$hes in problem sol ing.

Ascertaining the Capabilities of a Computational !evice 'nce having understood a problem. Algorithms that take advantage of this capability are called parallel algorithms Choosing bet"een #$act and Appropriate Problem Solving The ne&t principal decision is to choose between solving the problem e&actly or solving it appropriately. A pseudocode is a mi&ture of a natural language and programming language like constructs. A pseudocode is usually more precise than an natural language. an algorithm is called appropriate algorithm. Analyzing an Algorithm . The former case algorithm is called e8a$t algorithm latter case. Algorithms + Data structures !rograms Algorithm !esign Techni%ues An algorithm design technique is a general approach to solving problems algorithmically that is applicable to a variety of problems for different areas of computing. or algorithms. &ethods of Specifying an Algorithm 'nce having designed an algorithm.e. A correct algorithm is not one that works most of the time but one that works correctly for all legitimate inputs. in parallel. you have to prove its correctness.s iteration provides a natural sequence of steps needed for such proofs. one operation at a time. data structures remain crucially important for both design and analysis of algorithms. !eciding on Appropriate !ata Structure In the new world of ob8ect oriented programming. you need to specify it in some fashion. Some new computers can e&ecute operations concurrently0 i. one must ascertain the capabilities of the computational device the algorithm is intended for. In the case of random-access memory 93A*:.An input to an algorithm specifies an instance of the problem the algorithms solves. That is you have to prove that the algorithm yields a required result for every legitimate input in a finite amount of time. $sing natural language is obviously difficult. Proving an Algorithm's Correctness 'nce an algorithm has been specified. the algorithm may work correctly for a ma8ority of input but crash on some KboundaryL value. ailure to do this. It is very important to specify e&actly to range of instances the algorithms needs to handle. a proof of correctness is to use mathematical induction because an algorithm. Accordingly algorithms are designed to be e&ecuted on such machines are called se3uential algorithms. !rrors for appro&imation algorithms should not e&ceed a predefined limit. The notion of correctness for appro&imation algorithms is less straightforward than it is for e&act algorithms. its central assumption is that instructions are e&ecuted one after another.

U aTn. I. aT=. R. 4ut there are situations where designing a more general algorithm is unnecessary or difficult or even impossible. sort student records by names. Another characteristic of an algorithm is generality. . The range of inputs it accepts ) your main concern should be designing an algorithm that can handle a range of inputs that is natural for the problem at hand. Testing of computer programs is an art rather than science.. Sorting can be done to sort list of numbers. !&amples of sorting algorithms  Selection sort . character strings and records maintained by schools about students and libraries about books and companies about their employees.After correctness. • Coding an Algorithm *ost algorithms are ultimately implemented as computer programs. 3esulting programs usually contain fewer bugs.. Simper algorithms are also more efficient than more complicated alternatives. Simplicity is an important algorithm characteristic.. 3. -orting :ey A specially chosen piece of information used to guide sorting. aT nS of the input sequence such that aT )U a T( U . Transition of algorithm to a program must be done carefully. R. There are two issues here2 • *enerality of the problem the algorithm solves ) it is easier to design an algorithm for a more general problem. 4ecause simple algorithms are easier to understand and easier to program.alidity of the computer program is established by testing.e.        Sorting Searching String processing 1raph problems +ombination problems 1eometric problems #umerical problems . the most important is effi$ien$y. a=. There are two kinds of algorithm efficiency2  Time efficiency2 indicates how fast the algorithm runs.  Space efficiency2 indicates how much e&tra memory the algorithm needs. anS "utput: A reordering BaT. Another characteristic of an algorithm is simplicity. characters from an alphabet. +nput: A sequence of n numbers Ba. 7numerate and e8plain briefly important types of problem./%N 20111 Sorting 3earrange the items of a given list in ascending order.

'6o properties  Stability2 A sorting algorithm is called stable if it preserves the relative order of any two equal elements in its input. possibly for a few memory units. e&cept. called a search key.  Searching The searching problem deals with finding a given value. Strings comprises of te&t strings2 letters. some of which are connected by line segments called edges.    4ubble sort Insertion sort *erge sort 5eap sort !valuate sorting algorithm comple&ity2 the number of key comparisons. (raph Problems A graph is a collection of points called vertices. 4it strings comprises of /eros and ones. numbers. and special characters. in a given set. Combinational Problems  . 1raphs can be used for2      *odeling real-life problems *odeling 777 transportation communication networks "ro8ect scheduling R 78amples of 6idely used graph algorithms<    *raph traversal algorithms Shortest#path algorithms Topological sorting 'he 6idely :no6n graph problems are<  Traveling salesman problem ) 7hich finds the shortest tour through n cities that visits every city e&actly once.   Sequential searching 4inary searching String A string is a sequence of characters from an alphabet. In place2 A sorting algorithm is in place if it does not require e&tra memory. The e&amples of searching algorithms are as follows. String matching2 searching for a given word6pattern in a te&t. The graph coloring problem ) assigns the smallest number of colors to vertices of a graph so that no two ad8acent vertices are the same color.

What is an algorithm? What are the analysis frame6or:s in measuring the effi$ien$y? . lines and polygons. . or a subset ) that satisfies certain constraints and has some desired property. evaluating functions. Ancient 1reeks have invented procedures for solving problems of constructing simple geometric shapes ) triangles.s efficiency with respect to resources2 running time and memory space.The traveling salesman problems and the graph coloring problems are e&amples of combination problems.e. Input si/e depends on the problem. computing definite integrals. Space efficiency2 the space an algorithm requires. )umerical Problems #umerical problems another large area of application. a combination. i. circles etc. for obtaining a required output for any legitimate input in a finite amount of time. (eometric Problems 1eometric algorithms deal with geometric ob8ects such as points. multiply larger matrices.i e e8amples. Analysis of algorithms means to investigate an algorithm. !&ample .D7* 20101 Algorithm An algorithm is a sequence of unambiguous instructions for solving a computational problem. best-case and average efficiency &easuring an Input's Size *ost programs runs longer on larger inputs e&ample it takes longer to sort larger arrays. are problems that involve mathematical ob8ects of continuous nature2 solving equations and systems of equations. Analysis Frame"or*     *easuring an input..s efficiency function: 7orst-base.     !fficiency is defined as a function of input si/e. what is the input si/e of adding two n by n matrices< Units for &easuring +unning Time . what is the input si/e of the problem of sorting n numbers< !&ample =.. !. Time efficiency2 how fast an algorithm runs.s si/e *easuring running time 'rders of growth 9of the algorithm. These problems ask to find a combinatorial ob8ect ) such as permutation.

. minutes. the basic operation is usually the most time-consuming operation in the algorithm. such as seconds. +ount the number of times each of an algorithm. belong to the same order of growth.s basic operation is e&ecuted. Then we can estimate the running time T9n: of a program implementing this algorithm on that computer by the formula. o This (epends on the speed of the computer. the operation contributing the most to the total running time. .s innermost loop. "et cop # 4e the time of e&ecution of an algorithms basic operation on a particular computer ?et *@nA ) be the number of times this operation needs to be e&ecuted for this algorithm. n (imensions of matrices. )rder of gro6th '9. or e&ample.s operations is e&ecuted. o This approach is (ifficult and unnecessary +ount the number of times an algorithm.: '9logb n: '9n: '9n logb n: '9n=: '9n : '9c : n P Name constant logarithmic 9for any b: linear Ken log enL quadratic cubic e&ponential 9for any c: .rder of gro"th T@nA B copC @nA An order of growth is a set of functions whose asymptotic growth behavior is considered equivalent. ?asi$ operation2 the most important operation of the algorithm. or e&ample.AAn and n D . = n.   *easure the running time using standard unit of time measurements.peration #$amples Problem Search for a key in a list of n items Add two n by n matrices "olynomial !valuation Input size measure #umber of items in list. which is written "9n: in 4ig-'h notation and often called linear because every function in the set grows linearly with n. Input Size and Basic . n "rder of the polynomial Basic operation >ey comparison addition multiplication 'ime effi$ien$y is analy9ed by determining the number of repetitions of the basi$ operation as a fun$tion of input si9e.

Then we should ascertain the value of +9n: on these inputs.Case #fficiency Algorithm efficiency depends on the input si/e n but for some algorithms efficiency depends on not only on the input si/e also on type of input.n-. or which the algorithm runs the longest among all possible inputs of si/e n. best case inputs will be lists of si/e n with their first elements equal to a search key according to +best9n: @.F. Analy/e the algorithm to see what kind of inputs yield largest value of the basic operations count +9n: among the possible inputs of si/e n and compute the worst case value +worst9n:. Average case #fficiency The average case efficiency of an algorithm is its !fficiency for a typical6random input of si/e n.n-.F and a search key > 66'utput2 3eturns the inde& of the first element of A that matches > or ). if there are no matching elements i ßA while i B n and AEiF Q > do ißiD.-orst. or which the algorithm runs the fastest among all possible inputs of si/e n...Case/ Best. 5ow to find the average case efficiency< +onsider the sequential search.: .. Best case #fficiency The best case efficiency of an algorithm is its !fficiency for the best case input of si/e n. if i B n 66AEIF @ > return i else return -. >: 66Searches for a given value in a given array by sequential search 66Input2 An array AEA.Case/ and Average. The standard assumptions are2 a: The probability of a successful search is equal to p 9AB@pB@. *onsider an e8ample< -e3uential -ear$h "roblem2 1iven a list of n elements and a search key >. #'T the average of worst and best case. -orst case #fficiency The worst case efficiency of an algorithm is its !fficiency for the worst case input of si/e n. if any. !&ample for sequential search. Algorithm2 Scan the list and compare its successive elements with > until either a matching element is found 9successful search: of the list is e&hausted 9unsuccessful search: -e3uential -ear$h Algorithm A?1'3IT5* SequentialSearch9AEA. find an element equal to >.

∈ '9n=: . )Enotation  ormal definition A function t$n% is said to be in "$g$n%%& denoted t$n% ∈"$g$n%%& if t$n% is bounded above by some constant multiple of g$n% for all large n.AAn + .AMn To complete the proof with c @ . n9nD. and the number of comparisons made by the algorithm is i.-p:. Asymptotic notations used to compare orders of growth of an algorithm.-p: @ p6n E .AAn D Mn 9for all nS.AAn D M≤..: @ .:6= D n9. . if there e&ist some positive constant c and some nonnegative integer n' such that t9n: ≤ cg9n: for all n ≥ nA !&ample2 . D nF D n 9.AAn + . Types are • • • .≤ )'' n + n $for all n ≥M: @ . In the case of an unsuccessful search. .e.p6n D =. D n. .A.. D i.b: The probability of the first match occurring in the position of the list is the same for every i. . D I D . 9.p6nF D n. .-p: @ p6n .p6n D .-p: @ p9nD. t9n: ) algorithms running time +9n: ) indicates the basic operation count g9n: ) simple functions to compare the count with. .D=D. The average number of key comparisons +avg9n: as follows2 The first match occurring in the ith position of the list is p6n for every i. +avg9n: @ E. . .n ∈ )') n( Thus the value of the constants c and nA .AM and nA@ .:6= D n9.-p: #. ?riefly e8plain asymptoti$ Notations and types 6ith its graphs. @big )hA C @big omegaA D @big thetaA    )@g@nAA< class of functions f9n: that grow no faster than g9n: C@g@nAA< class of functions f9n: that grow at least as fast as g9n: D @g@nAA< class of functions f9n: that grow at same rate as g9n: 7here.p6n D .s basic operation count for the algorithms efficiency. the number of comparisons is n with the probability of such a search being 9. i. .

Ω Enotation  ormal definition A function t$n% is said to be in Ω$g$n%%& denoted t$n% ∈ Ω$g$n%%& if t$n% is bounded below by some constant multiple of g$n% for all large n. if there e&ist some positive constant c and some nonnegative integer n' such that t9n: ≥ cg9n: for all n ≥ nA !&ample of the formal proof that n. i. i. ≥ n= for n≥ A..e.e. i. and c= and some nonnegative integer n' such that .. ∈ Ω9n=:2 n. if there e&ist some positive constant c. we can select c@. and nA @ A Θ Enotation  ormal definition A function t$n% is said to be in Θ$g$n%%& denoted t$n% ∈ Θ$g$n%%& if t$n% is bounded both above and below by some positive constant multiples of g$n% for all large n.e.

There are two operations in the loops body2 .F: 66(etermines the value of the largest element in a given array 66Input 2 An array AEA.: ∈ Θ9n=: irst we prove the right inequality 9upper bound:2 ..6=n= for all n ≥ A..: @ .n-.6=n9n-.6=n= .c= g9n: ≤ t9n: ≤ c.n-. Algorithm *a&!lement 9AEA.6=n= . The following is a pseudocode of a standard algorithm for solving the problem. The operation to be e&ecuted most often is for loop. Second we prove the left inequality 9lower bound:2 .@ W and no @ = +.: @ .. do if AEiF S ma&val *a&val ßAEiF return ma&val Inout Si/e here is the array n. to n ) .D7* 2011 H /%N7 20121 78ample 12 +onsider the problem of finding the value of the largest element in a list of n numbers..6=n for all n ≥ = @ .6=n= . g9n: for all n ≥ nA !&ample let us prove that .6=n9n-.6=n9n-. Des$ribe briefly about mathemati$al analysis of non re$ursi e algorithms.F of real numbers 66'utput2 The value of the largest element in A *a&val ß AEAF for i ß .6=n ≤ .6=n .. c.6On= 5ence we can select c= @ V.6=n ≥ . Assume the list is implemented as array. .

s basic operation. Therefore we get the following sum for +9n:2 +9n: @ X . (ecide on a parameter indicating an input. The 1eneral plan to follow in analy/ing non recursive algorithms is2 . i@. If it depends on some additional properties the worst case. the choice is not that important.• +omparison AEiFSma&val • Assignment ma&val ßAEiF 7hich of these two operations must we consider basic operation< +omparison is e&ecuted on each repetition of the loop and not assignment ?et us denote +9n: the number of lines this comparison is e&ecuted. best case efficiencies have to be investigated. number of binary digits in the binary A". the answer would be log=n. .s si/e. P. O. Set up sum e&pressing the number of times the algorithm.. which is repeated for each value of the loop. average case.s basic operation is e&ecuted. nßEn6=F return count In this the most frequently e&ecuted operation is not inside the 6hile loop but rather the comparison nS. Since the number if times the comparison will be e&ecuted is larger than the number of repetitions of the loop. ∈ Θ9n:..s binary representation count ß . 6hile nS.s body by e&actly . either find a closed form formula for the count or . and n-. +heck whether the number of times the basic operation is e&ecuted depends only in the si/e of an input. Identify the algorithm.D7* 2010 H /%N7 20121 . establish its order of growth. The algorithm makes one comparison on each e&ecution of the loop. A more significant feature of this e&ample is the fact that the loop. Since the value of n is about halved on each repetition of the loop. 78ample 2< The following algorithm finds the representation of a positive decimal integer.s variable i within the bounds between . M.s variable takes on only a few values between its lower and upper limits0 therefore we have to use an alternative way of computing the number of times the loop is e&ecuted. Define (e$ursi e algorithm..)(&'5G 4inary9n: 66Input2 A positive decimal integer n 66'utput2 The number of binary digits in n. @ n-. =. at the very least.s body will be e&ecuted. that determines whether the loop. n-.. do count ß count D. . 78plain the mathemati$al analysis pro$edures 6ith 'o6ers of 5anoi pu99le e8ample. $sing standard formulas and rules of sum manipulation.

: D . else return / 9n -. the largest on the bottom and the smallest on top. and finally move recursively n ) . we first moce recursively n ) . and best case for input of si/e n Set up a recurrence relation and initial condition9s: for +9n:-the number of times the basic operation will be e&ecuted for an input of si/e n. To move nS. to peg P. if necessary. 3ecursive evaluation of nY  3ecursive solution to the Towers of 5anoi pu//le  3ecursive solution to the number of binary digits problem !&ample 3ecursive evaluation of n Y 9. to peg = 9with peg P as au&iliary:. Solve the recurrence or estimate the order of magnitude of the solutions !&ample 2 The Towers of 5anoi "u//le In this pu//le we have n disks of different si/es and three pegs. Initially all the disks are in the first peg in order of si&w. disks from peg . then move the largest disk directly from peg .s basic operation (etermine worst. using the second one as an au&iliary. for every n S A *9A: @ A *9n: ∈ [ 9n: -teps in Gathemati$al Analysis of (e$ursi e Algorithms      (ecide on parameter n indicating input si/e Identify algorithm. disks from peg . *9n: *9n: @ *9n ) . disks from peg .:   Iterative (efinition 3ecursive definition Algorithm /$n% 66 +omputes nY recursively 66 Input2 A nonnegative integer n 66'utput2 the value of nY if n@A return .: Z n for every n S A 9A: @ .: Z n 66base case 66general case !&ample 3ecursive evaluation of n Y 9=:    Two 3ecurrences o The one for the factorial function value2 9n: 9n: @ 9n ) . average. 7e can move only one disk at a time. an it is forbidden to place a larger disk on top of the smaller one. to peg P9with peg = as au&iliary:. o The one for number of multiplications to compute nY. The goal is to move all the disks to the third peg.

n-. -. 7e solve this recurrence by te same method of backward substitutions2 *9n: @ =*9n ) . -.: D . which is achieved for i@n-. *9. as au&iliary:. D= n-. we get *9n: @ =i *9n ) i:D =i-.:: D = n-. sub. @== E=*9n ) P:D. we have the following recurrence relation for the number of moves *9n:2 *9n: @ =*9n ) . and we get the following recurrence equation for it2 *9n: @ *9n ) . ..F D . for every n S .: D .D =i-=DRD=D. -. 7ith the obvious initial condition *9..FD=D. *9n ) 9n -.. *9.D*9n .: @ . @ =i *9n ) i:D =i-. @ =P *9n ) P:D ==D=D. @ ==*9n ) =:D=D. Since the initial condition is specified for n@.: @ . then we can directly move the disk directly from the source peg to the destination peg. @=E=*9n ) =: D . *9n ) . The ne&t one will be =O *9n ) O:D =PD ==D=D.= to pe P9using peg. *9n ) =: @ =*9n ) P: D .: @ =*9n ) =: D . @ = n -. And after the ith substitution. If n@. @ = n-.: D = n-. The number of moves *9n: depends on n only.: for nS... sub.: D . we get the following formula for the solution to recurrence *9n:@ = @= Problem n-.

.

.

. The algorithm makes one comparison on each e&ecution of the loop. to n ) . i@.2.. The operation to be e&ecuted most often is for loop. . @ n-.hS . Therefore we get the following sum for +9n:2 +9n: @ X .F: 66(etermines the value of the largest element in a given array 66Input 2 An array AEA. and n-. do if AEiF S ma&val *a&val ßAEiF return ma&val Input Si/e here is the array n./%N 20111 +onsider the problem of finding the value of the largest element in a list of n numbers. Program< \includeBstdio.F of real numbers 66'utput2 The value of the largest element in A *a&val ß AEAF for i ß . n-. ∈ Θ9n:.n-. The following is a pseudocode of a standard algorithm for solving the problem. which is repeated for each value of the loop.s variable i within the bounds between . There are two operations in the loops body2 • +omparison AEiFSma&val • Assignment ma&val ßAEiF 7hich of these two operations must we consider basic operation< +omparison is e&ecuted on each repetition of the loop and not assignment ?et us denote +9n: the number of lines this comparison is e&ecuted. Algorithm *a&!lement 9AEA.n-. Assume the list is implemented as array. Write an algorithm for finding ma8imum element of an array perform bestF 6orst and a erage $ase $omple8ity 6ith appropriate order notations..

PO G0 int ma&val @ aEAF0 for 9i @ A0 i B .6n to be in each place..6n D =Z99n-. which is O(n) as well 9lim to infinity is 1/e * n:.A0 iDD: C if 9aEiF S ma&val: C ma&val @ aEiF0 G 3eturn ma&val G printf 9J*a&imum element in an array 2 ^d_nJ. O.the number n has probability . then the e&pected number of iterations for this part is . ==. o This totals in O(n) average time solution. =PO.:6n:`=6n D . To find the average case . 78plain .AF @ C . 4. MM.:6n:`9n-=:6n .A. OA. 1raph theory is the study of the properties . .eometri$ problems in detail.D7* 2011 H /%N 20121 1raph and geometric problems have been studied by computer science researchers using the framework of analysis of algorithms. ma&val:0 return A0 G ?est $ase< 4est case .:6n:6n D P Z 99n-. o o The above formula yields an ugly formula which is O(n) o The last element is going to be checked if the first n-. we can split the problem into two parts2 EA.n-.raph and . D 9n-. Worst *ase< 7orst case .finding the ma& element as the first 9'9.. ]. A erage *ase< o o o The tricky part is the average case.int main 9: C int i0 int aE.:2 Since on average 9assuming uniform independent distribution for each element: . elements did not contain the value n2 so you need to add to the above n* ((n-1)/n)^(n-1).we need the e&pected number of iterationsY Since you can stop after you find the ma&imum. PA.: Z 99n-.::.it is the last element checked 9O(n):. =A.

1raph algorithms are one of the oldest classes of algorithms and they have been studied for almost PAA years. and at the same time. understanding and solving graph and geometric problems. it is e&pected that computer scientists and professional programmers know frequently used algorithms and generic techniques for efficient organi/ation and retrieval of data. Ancient 1reeks have invented procedures for solving problems of constructing simple geometric shapes ) triangles. computer-aided design and manufacturing. . The graph coloring problem ) assigns the smallest number of colors to vertices of a graph so that no two ad8acent vertices are the same color. circles etc. algorithms are also designed for geometric problems that are classical in nature. 1raph and geometric algorithms are at the heart of many computer applications. the solutions obtained. +orrespondingly. (eometric Problems 1eometric algorithms deal with geometric ob8ects such as points. the algorithmic viewpoint of computer science has stimulated much research in graph theory. some of which are connected by line segments called edges. they are fascinating ob8ects of study in pure and applied mathematics. in which geometric algorithms play a crucial role. (raph Problems A graph is a collection of points called vertices. robotics and others. The main impetus for the development of geometric algorithms came from the progress in computer graphics.computer graphics. lines and polygons. 1raphs can be used for2  *odeling real-life problems  *odeling 777  transportation  communication networks  "ro8ect scheduling R 78amples of 6idely used graph algorithms<    *raph traversal algorithms Shortest#path algorithms Topological sorting 'he 6idely :no6n graph problems are<   Traveling salesman problem ) 7hich finds the shortest tour through n cities that visits every city e&actly once. So. 1raphs provide essential models for many applications areas of computer science. There have been a number of e&citing recent developments in graph theory that are important for designers of algorithms to know about.of graphs. The success of the field can be e&plained from the beauty of the geometry problems studied. 1raph theory and graph algorithms are inseparably intertwined sub8ects. geographic information systems. and by the many application domains. modeling. In addition.

you can throw away the middle item and the second half of the list and search the first half of the list by the same process. 6orstE$ase< Searching for the item that is greater than or equal to the item in the last position. f9n: @ n . This results in searching from among n6= items. This results in searching from among n6= items. you can throw away the middle item and the first half of the list and search the second half of the list by the same process. This additional overhead will affect the efficiency of the search. f@nA I log2n 9see below for the derivation of this result: Determining the 6orstE$ase $al$ulation for the binary sear$h. The binary search begins by looking at the middle item in the array 9at position n6=:. in which case you are now done. The item you are looking for is less than 9i. comes before: the middle item. WorstE$ase< #ot found or searching for the item that would be the last one checked before determining it is not in the array. c. This is the item you are looking for. comes after: the middle item. or which the algorithm runs the longest among all possible inputs of si/e n. f@nA I n ?inary -ear$h 2 3equires the precondition that the data in the array is ordered by the key field that will be used for the search. This results in three possibilities2 a. Dis$uss about WorstE*ase effi$ien$y.10.e. The item you are looking for is greater than 9i. This additional overhead will affect the efficiency of the search. o . but for now will not be considered 9efficiency of sorting algorithms is the topic of the other half of this chapter:.e.rdered Se%uential Search 3equires the precondition that the data in the array is ordered by the key field that will be used for the search. . but for now will not be considered. Analy/e the algorithm to see what kind of inputs yield largest value of the basic operations count +9n: among the possible inputs of si/e n and compute the worst case value +worst9n:. Therefore.. 7orst-case2 #ot found or searching for the item that is in the last position.D7* 20111 The worst case efficiency of an algorithm is its !fficiency for the worst case input of si/e n. The first comparison results in n6= items left over. b.. Therefore. therefore we will ignore this possibility. 78amples< Se%uential Search Assume that the data in the array is in no particular order. 4ut this would not be the worst case.

These methods involve as much e&ternal processing as processing in the +"$. item left over. i0 Internal Sorting If the data to be sorted remains in main memory and also the sorting is carried out in main memory it is called internal sorting. Therefore. The third comparison results in 99n6=:6=:6= @ n69=P: items left over. After k comparisons.. • Insertion sort • Selection sort • *erge Sort • 3adi& Sort • auick Sort • 5eap Sort • 4ubble Sort ii0 #$ternal Sorting If the data resides in au&iliary memory and is brought into main memory in blocks for sorting and then result is returned back to au&iliary memory is called e&ternal sorting. n/@2:A I 1 2: I n : I log2n Since log=n is less than n. !ventually. *any Sorting algorithms are available to sort the given set of elements. • • • Sorting with (isk Sorting with tapes. The types of internal sorting are as follows. appro&imately k comparisons are needed to search the array. If this results in one item left then only one more check needs to be made. Therefore. The fourth comparison results in n69=O: items left over./%N 20121 Sorting is arranging or ordering the data in some logical order either increasing or decreasing. we may conclude that a binary search is more efficient that a linear search. Write short notes on sorting . the kth comparison results in n69= k: items left over. -orting Algorithms o Insertion Sort o Selection Sort o auicksort . 11..o o o o The second comparison results in 9n6=:6= @ n69==: items left over. there would be only . The !&ternal sorting methods are applied only when the number of data elements to be sorted is too large. The following are the e&amples of e&ternal sorting.

9n=:. 7e will select the first element in the array and put it in its final place in the array. to n. 4uic*sort . another element is placed in its final position in the sorted array.. A.1n230 Selection Sort In this sorting we find the smallest element in this list and put it in the first position. and two additional subarrays are created. . The basic algorithm for a one-dimensional array is as follows. 1ecursive Step: 3epeat the process on each unsorted subarray. ind smallest item in list. 3. e&ternal device. =. shift the remaining cards. =. beginning with the rest of the list. Then find the second smallest element in the list and put it in the second position. Insertion Sort . and more . !&change it with JfirstJ item in list. An e&ample of an insertion sort occurs in everyday life while playing cards. that subarray is sorted and the element is in its final location. inserting each element into its proper position. 3epeat. !artition Step: Select an element to place in its final position in the array. It is built on the ideal of partitions. and it uses divide#and# con0uer strategy. and all the elements to the right will be greater than the chosen element. 4oth average and worst-case time is . .o *ergesort Sorting time may depend on2        initial order of data data structure used to represent data memory available si/e of record sort algorithm ?ocation of list2 internal storage.1nlog31n00 auicksort was invented and named by +. and then insert the e&tracted card in the correct place. This process is repeated until all the cards are in the correct sequence. &erge Sort .. And so on.1n305 Sorting the given elements from . etc. 5oare and is one of the best generalpurpose sorting algorithms. To sort the cards in your hand you e&tract a card. all the elements to the left will be less than selected element. That is. 7hen a subarray eventually contains only one element. P. Then we have one element in its proper location and two unsorted subarrays. !ach time the partition step is repeated.

(efine (oubly ?inked list. or e&ample A is a sorted list with r elements and 4 is a sorted list with s elements. (efine Asymptotic #otations with e&amples. #o. #o. #o. O]F E3ef. PNF O.2 . The operation that combines the elements of A and 4 into a single sorted list + with n @ r D s elements is called merging. #o. ANNA %N&J7(-&'K L%7-'&)NPart E A . After combing the two lists the elements are sorted by using the following merging algorithm suppose one is given two sorted decks of cards. #o.2 E3ef. #o. P]F N.2 E3ef.. #o.2 E3ef.2 E3ef.OF P.F b. (efine a-notation 9Theta:. 7hat are the fundamentals steps involved in Algorithmic "roblem Solving< bF =. 7hat are the measures used to analy/e an algorithm< .+ombing the two lists is called as merging.2 O. 7rite the 1eneral plan for analy/ing the efficiency for non recursive algorithms< E3ef. 7rite a brief note on '-notation. PbF M.2 E3ef.

!&plain 1raph and 1eometric problems in detail. #o. 7hat is an algorithm< 7hat are the analysis frameworks in measuring the efficiency< N. c.2 ]F E3ef. (iscuss about 7orst-+ase efficiency. ?ist out the fundamentals steps of "roblem solving algorithm.2 .2 =F E3ef. ].2 OF E3ef.c. M.2 bF E3ef.F . M.2 =. E3ef. 7rite an algorithm for finding ma&imum element of an array perform best. 7hy is an algorithm important in problem solving< M=F Part – ? E3ef. #o.2 #o.2 E3ef. average case comple&ity with appropriate order notations. #o.F E3ef.A. worst and . (ifference between sequential and parallel algorithms.2 cF E3ef. 1ive e&amples. #o. !&plain the mathematical analysis procedures with Towers b. #o. 7rite short notes on sorting . MAF ].A. problem solving.2 PF E3ef. #o.AF . #o. #o.2 #o. (iscuss briefly about analy/e phase of algorithm. . (iscuss in detail2 eatures of the top down design and bottom up design approaches in O.. #o.2 NF E3ef. !numerate and e&plain briefly important types of problem. #o. (escribe briefly about mathematical analysis of non recursive algorithms. (istinguish between 7orst case and 4est case efficiency. P. (efine 3ecursive algorithm.2 =F E3ef. #o. E3ef. of 5anoi pu//le e&ample..