Você está na página 1de 20

Unit-I Introduction Why do we need to study algorithms? There are two reasons for studying algorithms 1. Practical 2.

Theoretical 1. Practical: Standard set of important algorithms from different areas of computing Design new algorithms Analyze their efficiency 2. The study of algorithms. It is known as algorithmics. (The spirit of computing) Another important reason of studying an algorithm is their usefulness in developing analytical skills. Different kinds of problems are solved with an algorithm Donald knuth Computer scientists A person should know answer for following question. Then they can analysis the problem. 1. How to deal with algorithms 2. How to construct them 3. How to manipulate them 4. how to understand them 5. how to analyze them Notion of algorithm. What is 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. Instruction: Something or someone capable of understanding and following the instructions given Problem Algorithm

Input

Computer

Output Notion of algorithm

Computer: meant a human being involved in performing numeric calculator. It is an electronic device that has become indispensable in almost everything we do. Example: Computing the greatest common divisor of two integers There are three methods for solving the same problem. It consist of several important point o The nonambiguity requirements for each step of an algorithm cannot be compromised o The range of inputs for which an algorithm works has to be specified carefully o The same algorithm can be represented in several different ways o Several algorithms for solving the same problem may exist o Algorithms for the same problem can be based on very different ideas. Problem: The greatest common divisor of two non-negative, not-both-zero integers m and n denoted gcd(m,n) is defined as the largest integer that divides both m and n evenly i.e. with a remainder of zero. First method to solve the problem: Euclid of Alexandria invented the Euclids algorithm to solve gcd problem Example: gcd(m,n) = gcd (n, m mod n) m mod n is the remainder of the division of m by n stop the process when m mod n is equal to 0. Suppose m=60, n=24 then gcd (60, 24) = gcd (24, 12) = gcd(12, 0) = 12 gcd (24, 60 mod 24) gcd (24, 12) gcd (12, 24 mod 12) gcd(12, 0) Now the value for n=0 so the greatest common divisor is 12. Algorithm Euclid (m, n) while n 0 do r m mod n

m n n r return m Eg: m = 60 n= 24 while 24 0 do r 60 mod 24 r 12 m 24 n 12 12 0 do r 24 mod 12 r 0, m 12 n 0 Now n 0 so return 12 Euclids algorithm for computing gcd(m,n) Step 1: If n = 0, return the value of m as the answer and stop. Otherwise proceed to step 2 Step 2: Divide m by n and assign the value of the remainder to n Step 3: Assign the value of n to m and the value of r to n. go to step 1 Second way to solve problem: Consecutive integer checking algorithm For constructing gcd(m, n) 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. therwise, proceed to step 4. Step 4: Decrease the value of t by 1 go to step 2. Drawback: It doesnt work correctly when one of its input number is zero. Example: t = min {m,n} m=60 n=24 4 1. t min {60, 24} t24 2. 60/24 [m/t] 12 [not equal to zero] 3. so decrease the value of t by 1 t23 4. 60/23 = 14 t22 5. 60/22 = 16 t21 6. 60/21 = 18 t20

7. 60/20 = 0 [divide n by t]

n = 24 t = 20 24/20 = 4 [decrease t by 1] t = 19 8. 60/19 = 3 t2 9. 60/2 = 0 n = 24 t =2 24/2 = 0 GCD = 12 Third way to solve problem: Middle school procedure for computing gcd (m,n) Step 1: Find the prime factor s of m Step 2: Find the prime factors of n Step 3: Identify all the common factors in two prime Step 4: Computer the product of the all the common factor and return it as the greatest common divisor of the number given. Numbers m=60, n=24 Prime factor of m = 2. 2. 3. 5 Prime factor of n = 2. 2. 2. 3 Gcd (60, 24) = 2. 2. 3 = 12 Drawback: More complex slower than Euclids algorithm Fundamentals of Algorithm Solving Sequence of steps in designing and analyzing an algorithm Understanding the problem Ascertaining the capabilities of a computational device Choosing between exact and approximate problem solving Deciding an appropriate data structure Algorithm design techniques Methods of specifying an algorithm Proving an algorithms correctness Analyzing an algorithm Coding Understanding the problem: 1. The first thing is to understand the given problem 2. Read the problems description carefully and ask question if any doubts 3. Use different known algorithm for solving it. 4. Know how such algorithm works and its strengths and weakness.

5. An input to an algorithm specifies an instances of the problem the algorithm solves 6. It is very important to specify exactly the range of instances the algorithm needs to handle. Ascertaining the capabilities of a computational device 1. The cast majority of algorithms in use today are still destined to be programmed for a computer closely resembling the Von Neumann machine. 2. RAM Random Access machine its central assumption is that instructions are executed one after another. One operation at a time. 3. Sequential algorithm Executing instruction in sequential order Drawbacks of RAM The central assumption of the RAM model does not hold for some newer computers that is concurrent access. 4. Parallel algorithms: It have to process huge volume of data or deal with applications where time is critical. 5. It is imperative to be aware of the speed and memory available on a particular computer system. Choosing between exact and approximate problem solving: 1. The most principal decision is to choose between solving the problem exactly or solving it approximately. 2. Two types of an algorithm i. Exact algorithm ii. Approximate algorithm 3. Exact algorithm: available algorithms for solving a problem exactly can be solve. Ex. Traveling salesman problem. 4. Approximation algorithm: Problems that cant be solved exactly. Ex. Extracting square roots, solving nonlinear equation, evaluating definite integrals. Deciding on Appropriate Data Structure: 1. Algorithms + Data Structures = programs Algorithm Design techniques: 1. Definition: ADT is a general approach to solving problems algorithmically that is applicable to a variety of problems from different areas of computing. 2. They provide guidance for designing algorithms for new problems. 3. Algorithms are the cornerstone of computer science. ATD make it possible to classify algorithms according to an underlying design idea. 4. They can server as a natural way to both categorize and study algorithms. Method of specifying an algorithm: 1. A pseudo code is a mixture of a natural language and programming language like constructs. 2. A pseudo code is more precise than a natural language and its usage often yields more succinct algorithm description. 3. Omit declarations of variables and use indentation to show the scope of such statements as for if and while. 4. Use an arrow for the assignment operation and 2 slashes // for comments. 5. The dominant vehicle for specifying algorithm was a flowchart.( a method of expressing an

algorithm by a collection of connected geometric shapes containing descriptions of an algorithms steps. Drawback: Inconvenient for all 6. Algorithms Description: whether in a natural language or a pseudo code can be fed into an electronic computer directly. Proving an algorithms correctness: 1. Once an algorithm has been specified, we have to prove its correctness. 2. To prove that the algorithm yield a required result for every legitimate input in a finite amount of time. 3. A common technique for proving correctness is to use mathematical induction because an algorithm iteration provide a natural sequence of steps needed for such proofs. Analyzing an algorithm: 1. There are two kinds of algorithm efficiency. Time efficiency Space efficiency 2. Time efficiency indicates how fast the algorithm runs 3. Space efficiency indicates how much extra memory the algorithm needs. 4. Simplicity Simplex algorithm are easier to understand and easier to program. 5. Generality There are two issues a. Generality of the problem the algorithm solves. b. The range of inputs it accepts. 6. If we are not satisfied with the algorithms efficiency. Simplicity or generality, we must return to the drawing board and redesign the algorithm. Coding an algorithm 1. Programming an algorithm presents both a peril and an opportunity. 2. The Peril lies in the possibility of making the transition from an algorithm to a program either incorrectly or very inefficiently. 3. Testing of computer programs is an art rather than a science, but that does not mean that there is nothing in it to learn. 4. If inputs to algorithms fall within their specified ranges and hence require no verification. 5. Implementing an algorithm correctly is necessary but not sufficient. 6. The analysis is based on timing the program on several inputs and then analyzing the results obtained. 7. A good algorithm is a result of repeated effort and rework. 8. Optimality what is the minimum amount of effort any algorithm will need to solve the problem is known as optimality.

Discuss about important problem types in design and analysis of an algorithm The important problem types are

o o o o o o o

Sorting Searching String processing Graph problems Combinational problems Geometric problems Numerical Problems

Sorting The sorting problem ask us to rearrange the items of a given list in ascending order. We need to sort list of numbers, characters from an alphabet, character strings and records similar to those maintained by schools about their students, libraries about their holding and companies about their employee We need piece of information to guide sorting. Piece of information is known as key. Why would we want a sorted list? o The most important of them is searching o Dictionaries o Telephone books o Class lists o And so on are sorted There are few good sorting algorithms that sort arbitrary array of size n using about nlog2n comparison A sorting algorithm is called stable if it preserves the relative order of any two equal elements in its input. An algorithm is said to be in place if it does not require extra memory. Except, possibly, for a few memory units. Searching The searching problem deals with finding a given value called a search key in a given set Different types of searching algorithm o Sequential Search o Binary Search o Some algorithms work faster than others but require more memory o Some are very fast but applicable only to sorted array. Different Operations o Number of searches o Addition and deletion from the data set of an item String processing A string is sequence of characters from an alphabet. Strings of particular interest are test strings which comprise letters, numbers and special

characters (Bit strings[0s and 1s], gene sequence [four character alphabet {A, C, G, T}]) Drawback: Searching for a given word in a test has attracted special attention from researchers called string matching. Graph Problems A graph can be thought of as a collection of points called vertices. Some of which are connected by line segments called edges. Graphs are used to study both Theoretical and Practical reasons Graphs can be used for modeling a wide variety of real-life applications like o Transportation o Communication networks o Project scheduling o Games Graph algorithms o Graph traversal algorithms How can one visit all the points in a network o Shortest path algorithms What is the best route between two cities o Topological sorting for graphs with directed graph Graph Problems o Traveling Salesman Problem (Finding the shortest tour through n cities) o Assign the smallest number of colors to vertices of a graph [no two adjacent vertices are the small color]) Combinatorial Problems Geometric algorithms deal with geometric objects such as points, lines and polygons It is used to construct simple geometric shapes, triangles, circles Applications: Computer graphics, Robotics and tomography Two classic problems o Closest Pair problem o Convex hull problem Closest Pair problem It is self explanatory given n points in the plane, find the closest pair among them Convex hull problem It asks to find the smallest convex polygon that would include all the points of a given set. Numerical Problems: Numerical Problems that involve mathematical objects of continuous nature o Solving equations and systems of equation

o Computing definite integrals o Evaluating functions It plays a critical role in many scientific and engineering applications. Write about the fundamentals of Analysis framework Introduction The American Heritage Dictionary defines analysis the separation of an intellectual or substantial whole into its constituent parts for individual study. The term analysis of algorithms an investigation of an algorithms an investigation of an algorithms efficiency with two resources: running time and memory space. Running time = efficiency, Memory Space = speed and memory Analysis Framework: There are two kinds of efficiency, time efficiency and space efficiency Time efficiency indicates how fast an algorithm in question runs Space efficiency deals with the extra space the algorithm requires. o Measuring an inputs size o Unit for measuring Running Time o Orders of Growth o Worst case, best case and average case efficiencies o Recapitulation of the analysis framework Meaning an Inputs Size Almost all algorithms run longer on longer inputs Example: it takes longer to sort larger arrays, multiple larger matrices o Selecting Parameter n indicating the algorithm input size. It will be the size of the list for problem sorting, searching, and finding the lists smallest elements. The choice of a parameter indicating an input size. Example: Computing the product of two n by n matrices. There are two natural measures of size for this problem. The first and more frequently used is the matrix order n. other natural contender is the total number of elements N in the matrices being multiplied. If the algorithm examines individual characters of its input, then we should measure the size by the number of characters, if it works by processing words, we should count their number in the input. The computer scientists prefer measuring size by the number b of bits in the ns binary representation. b = [log2 n]+1 UNIT for measuring Running Time: o The next issue concerns units for measuring an algorithms running time. We can use some standard unit of time measurement, a second, a milli second and so on to measure the running time of a program implementing the algorithms. o One possible approach is to count the number of times each of the algorithms operations is executed.

o The thing to do is to identify the most important operation of the algorithm called the basic operation. The operation contributing the most to the total running time and compute the number of times the basic operation is executed. o Let Cop be the time of execution of an algorithms basic operation on a particular computer and let C(n) be the number of times the operation needs to be executed for this algorithm The running time T(n) Cop C(n) assuming that C(n) = n(n-1) C(n) = 1/2n(n-1) = n2 n n2 and therefore Order of growth: It is a functions order of growth that counts value of few functions particularly important for analysis of algorithms The function growing the slowest among these is the logarithmic function. It grows so slowly that we should expect a program implementing an algorithm with a logarithm metric basic operation count to run practically instantaneously on input of all realistic sizes. The formula Logan = logablogbn makes it possible to switch from one base to another, leaving the count logarithmic but with a new multiplicative constant. Algorithms that require an exponential number of operations are practical for solving only problem of very small sizes. Worst case, Best case and average case Efficiencies o Many algorithms for which running time depends not only on an input size but also on the specifies of a particular input. Example Sequential Search o Searches for a given item in a list of n elements by checking successive elements of the list until either a match with the search key is found or not.

Algorithm Sequential Search (A[0. n-1], k) i0 while i<n and A[i] k do ii+1 if i<n return i else return -1 Worst Case efficiency

The worst case efficiency of an algorithm is its efficiency for the worst case input of size n, which is an input of size n for which the algorithm runs the longest among all possible inputs of that size. Cworst(n) = n Best case efficiency The best-case efficiency of an algorithm is its efficiency for the best case input of size n, which is an input of size n for which the algorithm runs the fastest among all possible inputs of that size Cbest(n) = 1 Average case efficiency To analyze the algorithms average case efficiency we must make some assumptions about possible inputs of size n Ex: Sequential Search Successful search Unsuccessful search The probability of the first match occurring The number of comparison is n with the in the ith position of the list is p/n for every i probability of such a search being (1-p) Amortized efficiency It applies not to a single run of an algorithm but rather to a sequence of operations performed on the same data structure. Recapitulation of the analysis framework: Both time and space efficiencies are measured as functions of the algorithms input size Time efficiency is measured by counting the number of times the algorithms basic operation is executed. The efficiencies of some algorithms may differ significantly for input of the same size The frameworks primary interest lies in the order of growth of the algorithms running time as its input size goes to infinity.

Asymptotic Notations and Basic Efficiency classes:

To compare and rank order of growth, computer scientists use three notations o O (big oh) o (big omega) o (big theta) t (n) and g (n) can be any non negative functions defined on the set of natural numbers t (n) will be an algorithms running time and g (n) will be some simple function to compare the count with Informal Introduction O (g(n)) is the set of all functions with a smaller or same order of growth as g (n)

Example n O(n2) 100n + 5 O(n2) The first two functions are linear and have a smaller

n(n-1) O(n2) order of growth than g (n) = n2 n3 O(n2) The function n3 and 0.00001n3 are both cubic and 0.00001n3 O(n2) hence have a higher order of growth than n2 n4 + n + 1 O(n2)

The second notation (g(n)), stands for the set of all functions with a larger or same order of growth as g(n). Example n3 (n2) n(n-1) (n2) but 100n + 5 (n2) The third notation (g(n)) is the set of all functions that have the same order of growth as g(n) O notation

Definition: A function t(n) is said to be in O(g(n)), denoted t(n) O(g(n)), if t(n) is bounded above by some constant multiple of g(n) for all large b, i.e. if there exists some positive constant c and some non negative integer n0 such that

t(n) cg(n) for all n n0

Example: 100n + 5 O(n2)

100n + 5 100n + n (n 5) = 101n 101n2 Thus, as values of the constants c and no required by the definition, we can take 101 and

100n + 5 100n + 5n (n 1) = 105n

c = 105

no =1 notation

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 positive constant multiple of g(n) for all large n. i.e, if there exist some positive constant c and some non negative integer no such that t(n) cg(n) for all nno

- notation

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 i.e., if there exist some positive constant c1 and c2 and some non negative integer no such that C2g (n) t (n) c1g(n) for all n n0 Useful property involving the Asymptotic Notations Theorem: If t1(n) O(g1(n)) and t2(n) O(g2(n)), then t1(n) + t2(n) O(max{g1(n), g2(n)}). Proof: Since t1(n) O(g1(n)), there exist some constant c1 and some non negative

integer n1 such that t1 (n) c1g1(n) for all n n1 Since t2(n) O(g2(n))

t2 (n) c2g2(n) for all n n2 Let us denote c3 = max { c1, c2} and consider n max [n1, n2] so that we can use both inequalities additing the two inequalities above yields the following t1 (n) + t2 (n) c1g1 (n) + c2g2(n)

c3g1 (n) + c3g2(n) = c3 [g1(n) + g2(n)]

c3 2 max {g1(n), g2(n)} t1 (n) + t2 (n) O max {g1(n), g2(n)} with the constant c and no required by the O definition being 2c3 = 2max {c1, c2) t1 (n) O(g1(n)) t t 1 (n) + t2 (n) O max {g1(n), g2(n)} 2 (n) O(g2(n))

Using Limits for comparing orders of Growth

Three principal cases may arise

The first two cases = t1 (n) O (g1(n)) Last two cases = t1 (n) (g1(n)) Second case = t1 (n) (g1(n)) The Limit based approach lim n t(n) / g(n) = lim n t`(n) / g`(n) and stirling formula n! for large values of n Example: Compare orders of growth of n(n-1) and n2

The limit is equal to a positive constant, the functions have the same order of growth (n-1) (n2) Basic Efficiency Classes Class Name Comments 1 Constant Short of best case efficiencies A result of cutting a problems size by a constant factor on each iteration logn Logarithmic of the algorithm n Linear Algorithm that scan a list of size n belong to this class Many divide and conquer algorithms including mergesort and quicksort nlogn n-log-n in the average case n2 Quadratic

Algorithm with 2 embedded loops n3 Cubic Algorithm 3 embedded loops 2n Exponential Algorithm with all subsets of an n-element set n! Factorial Algorithm that generate all permutations of an n-element set End of Unit I

Você também pode gostar