Cs6402 Design and Analysis of Algorithms
Total Page:16
File Type:pdf, Size:1020Kb
CS6402/DESIGN AND ANALYSIS OF ALGORITHMS M.I.E.T. ENGINEERING COLLEGE (Approved by AICTE and Affiliated to Anna University Chennai) TRICHY – PUDUKKOTTAI ROAD, TIRUCHIRAPPALLI – 620 007 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING COURSE MATERIAL CS6402 DESIGN AND ANALYSIS OF ALGORITHMS II YEAR - IV SEMESTER M.I.E.T. /CSE/ II /DESIGN AND ANALYSIS OF ALGORITHMS CS6402/DESIGN AND ANALYSIS OF ALGORITHMS M.I.E.T. ENGINEERING COLLEGE (Approved by AICTE and Affiliated to Anna University Chennai) TRICHY – PUDUKKOTTAI ROAD, TIRUCHIRAPPALLI – 620 007 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING SYLLABUS (THEORY) Sub. Code : CS6402 Branch / Year / Sem : CSE/II/IV Sub.Name : DESIGN AND ANALYSIS OF ALGORITHMS Staff Name : P.CHRISTOPHER CS6402 DESIGN AND ANALYSIS OF ALGORITHMS L T P C 3 0 0 3 UNIT I INTRODUCTION 9 Notion of an Algorithm Fundamentals of Algorithmic Problem Solving Important Problem Types Fundamentals of the Analysis of Algorithm Efficiency Analysis Framework Asymptotic Notations and– its properties Mathematical analysis for –Recursive and Non- recursive– algorithms. – – UNIT II BRUTE FORCE AND DIVIDE-AND-CONQUER– 9 Brute Force - Closest-Pair and Convex-Hull Problems-Exhaustive Search - Travelling Salesman Problem - Knapsack Problem - Assignment problem. Divide and conquer methodology Merge sort Quick sort Binary search Multiplication of Large Integers Multiplication-Closest-Pair and Convex-Hull Problems. – UNIT– III DYNAMIC– PROGRAMMING– AND GREEDY TECHNIQUE – Strassen’s 9 Matrix Computing a Binomial Coefficient Optimal Binary Search Trees Knapsack Problem and Memory functions. Greedy Technique - Kruskal's Algorithm- Dijkstra's Algorithm-Huffman– Warshall’s and Tr Floyd’ees. algorithm – UNIT IV– ITERATIVE IMPROVEMENT – Prim’s 9 algorithm The Simplex Method-The Maximum-Flow Problem Maximum Matching in Bipartite Graphs- The Stable marriage Problem. UNIT V COPING WITH THE LIMITATIONS OF ALGORIT– HM POWER 9 Limitations of Algorithm Power-Lower-Bound Arguments-Decision Trees-P, NP and NP- Complete Problems--Coping with the Limitations - Backtracking n-Queens problem Hamiltonian Circuit Problem Subset Sum Problem-Branch and Bound Assignment problem Knapsack Problem Travelling Salesman Problem- Approximation Algorithms– for NP Hard– Problems Travelling Salesman– problem Knapsack problem. – – – TOTAL: 45 PERIODS– TEXT BOOK:– – Education, 2012. Anany Levitin, Introduction to the Design and Analysis of Algorithms, Third Edition, Pearson REFERENCES: 1. Thomas H.Cormen, Charles E.Leiserson, Ronal d L. Rivest and Clifford Stein, Introduction to PearsonAlgorithms, Education, Third Edition, Reprint PHI2006. Learning Private Limited, . 3.. DonaldAlfred V.E. Aho,Knuth John E. Hopcroft and Jeffrey D. Ullman, Data Structures and Algorithms, 4. http://nptel.ac.in/, The Art of Computer Programming, Volumes & Pearson Education, . Steven S. Skiena, The Algorithm Design Manual, Second Edition, Springer, . SUBJECT IN-CHARGE HOD M.I.E.T. /CSE/ II /DESIGN AND ANALYSIS OF ALGORITHMS CS6402/DESIGN AND ANALYSIS OF ALGORITHMS M.I.E.T. ENGINEERING COLLEGE (Approved by AICTE and Affiliated to Anna University Chennai) TRICHY – PUDUKKOTTAI ROAD, TIRUCHIRAPPALLI – 620 007 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Sub. Code : CS6402 Branch/Year/Sem : CSE/II/IV Sub Name : DESIGN AND ANALYSIS OF ALGORITHMS Staff Name : P.CHRISTOPHER COURSE OBJECTIVE 1. Learn the algorithm analysis techniques. 2. Become familiar with the different algorithm design techniques. 3. Understand the limitations of Algorithm power. COURSE OUTCOMES 1. Interpret the fundamental needs of algorithms in problem solving. 2. Classify the different algorithm design techniques for problem solving. 3. Develop algorithms for various computing problems. 4. Analyze the time and space complexity of various algorithms. 5. Identify the limitations of algorithms in problem solving. 6. To identify the types of problem, formulate, analyze and compare the efficiency of algorithms. Prepared by Verified By STAFF NAME HOD (P.CHRISTOPHER) Approved by PRINCIPAL M.I.E.T. /CSE/ II /DESIGN AND ANALYSIS OF ALGORITHMS CS6402/DESIGN AND ANALYSIS OF ALGORITHMS M.I.E.T. ENGINEERING COLLEGE (Approved by AICTE and Affiliated to Anna University Chennai) TRICHY – PUDUKKOTTAI ROAD, TIRUCHIRAPPALLI – 620 007 UNIT I INTRODUCTION Notion of an Algorithm – Fundamentals of Algorithmic Problem Solving – Important Problem Types – Fundamentals of the Analysis of Algorithm Efficiency – Analysis Framework – Asymptotic Notations and its properties – Mathematical analysis for Recursive and Non-recursive algorithms. 1.1 NOTION OF AN ALGORITHM An algorithm is a sequence of unambiguous instructions for solving a problem. i.e., for obtaining a required output for any legitimate input in a finite amount of time. Problem Algorithm Input Computer output There are various methods to solve the same problem. The important points to be remembered are: 1. The non-ambiguity requirement for each step of an algorithm cannot be compromised. 2. The range of input for which an algorithm works has to be specified carefully. 3. The same algorithm can be represented in different ways. 4. Several algorithms for solving the same problem may exist. 5. Algorithms for the same problem can be based on very different ideas and can solve the problem with dramatically different speeds. The example here is to find the gcd of two integers with three different ways: The gcd of two nonnegative, not-both –zero integers m & n, denoted as gcd (m, n) is defined as the largest integer that divides both m & n evenly, i.e., with a remainder of zero. Euclid of Alexandria outlined an algorithm, for solving this problem in one of the volumes of his Elements. Gcd (m, n) = gcd (n, m mod n) is applied repeatedly until m mod n is equal to 0; since gcd (m, o) = m. {the last value of m is also the gcd of the initial m & n.} M.I.E.T. /CSE/ II /DESIGN AND ANALYSIS OF ALGORITHMS CS6402/DESIGN AND ANALYSIS OF ALGORITHMS The structured description of this algorithm is: Step 1: If n=0, return the value of m as the answer and stop; otherwise, proceed to step2. Step 2: Divide m by n and assign the value of the remainder to r. Step 3: Assign the value of n to m and the value of r to n. Go to step 1. 1.1.1 Euclid”s algorithm : ALGORITHM Euclid(m, n) //Computes gcd(m, n) by Euclid’s algorithm //Input: Two nonnegative, not-both-zero integers m and n //Output: Greatest common divisor of m and n while n _= 0 do r ←m mod n m←n n←r return m This algorithm comes to a stop, when the 2nd no becomes 0. The second number of the pair gets smaller with each iteration and it cannot become negative. Indeed, the new value of n on the next iteration is m mod n, which is always smaller than n. hence, the value of the second number in the pair eventually becomes 0, and the algorithm stops. Example: gcd (60, 24) = gcd (24,12) = gcd (12,0) = 12. The second method for the same problem is: obtained from the definition itself. i.e., gcd of m & n is the largest integer that divides both numbers evenly. Obviously, that number cannot be greater than the second number (or) smaller of these two numbers,which we will denote by t = min {m, n }. So start checking whether t divides both m and n: if it does t is the answer ; if it doesn’t t is decreased by 1 and try again. (Do this repeatedly till you reach 12 and then stop for the example given below) 1.1.2 Consecutive integer checking algorithm: Step 1: Assign the value of min {m, n} to t. Step 2: Divide m by t. If the remainder of this division is 0, go to step 3; otherwise go to step 4. Step 3: Divide n by t. If the remainder of this division is 0, return the value of t as the answer and stop; otherwise, proceed to step 4. Step 4: Decrease the value of t by 1. Go to step 2. Note: this algorithm, will not work when one of its input is zero. So we have to specify the range of input explicitly and carefully. M.I.E.T. /CSE/ II /DESIGN AND ANALYSIS OF ALGORITHMS CS6402/DESIGN AND ANALYSIS OF ALGORITHMS The third procedure is as follows: Step 1: Find the prime factors of m. Step 2: Find the prime factors of n. Step 3: Identify all the common factors in the two prime expansions found in step 1 & 2. (If p is a common factor occurring pm & pn times is m and n, respectively,it should be repeated min { pm, pn } times.). Step 4: Compute the product of all the common factors and return it as gcd of the numbers given. Example: 60 = 2.2.3.5 24 = 2.2.2.3 gcd (60,24) = 2.2.3 = 12 . This procedure is more complex and ambiguity arises since the prime factorization is not defined. So to make it as an efficient algorithm, incorporate the algorithm to find the prime factors. 1.2 FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING Algorithms can be considered to be procedural solutions to problems. There are certain steps to be followed in designing and analyzing an algorithm. Code the algorithm Understand the problem Decide on: Computational means, exact vs.approximate problem solving, data structure, algorithm design technique Design an algorithm Prove the correctness Analyze the algorithm Code the algorithm M.I.E.T. /CSE/ II /DESIGN AND ANALYSIS OF ALGORITHMS CS6402/DESIGN AND ANALYSIS OF ALGORITHMS Understanding the problem ∗ An input to an algorithm specifies an instance of the problem the algorithm solves. It’s also important to specify exactly the range of instances the algorithm needs to handle. Before this we have to clearly understand the problem and clarify the doubts after leading the problems description. Correct algorithm should work for all possible inputs. Ascertaining the capabilities of a computational Device ∗ The second step is to ascertain the capabilities of a machine.