Chapter 1. Introduction 1.1 Need for studying algorithms: The study of algorithms is the cornerstone of computer science.It can be recognized as the core of computer science. Computer programs would not exist without algorithms. With computers becoming an essential part of our professional & personal life’s, studying algorithms becomes a necessity, more so for computer science engineers. Another reason for studying algorithms is that if we know a standard set of important algorithms ,They further our analytical skills & help us in developing new algorithms for required applications 1.2 ALGORITHM An algorithm is finite set of instructions that is followed, accomplishes a particular task. In addition, all algorithms must satisfy the following criteria: 1. Input. Zero or more quantities are externally supplied. 2. Output. At least one quantity is produced. 3. Definiteness. Each instruction is clear and produced. 4. Finiteness. If we trace out the instruction of an algorithm, then for all cases, the algorithm terminates after a finite number of steps. 5. Effectiveness. Every instruction must be very basic so that it can be carried out, in principal, by a person using only pencil and paper. It is not enough that each operation be definite as in criterion 3; it also must be feasible.
COMPUTER Fig 1.a. An algorithm is composed of a finite set of steps, each of which may require one or more operations. The possibility of a computer carrying out these operations necessitates that certain constraints be placed on the type of operations an algorithm can include. The fourth criterion for algorithms we assume in this book is that they terminate after a finite number of operations. Criterion 5 requires that each operation be effective; each step must be such that it can, at least in principal, be done by a person using pencil and paper in a finite amount of time. Performing arithmetic on integers is an example of effective operation, but arithmetic with real numbers is not, since some values may be expressible only by infinitely long decimal expansion. Adding two such numbers would violet the effectiveness property. • Algorithms that are definite and effective are also called computational procedures. • The same algorithm can be represented in same algorithm can be represented in several ways • Several algorithms to solve the same problem • Different ideas different speed Example:
Problem:GCD of Two numbers m,n Input specifiastion :Two inputs,nonnegative,not both zero Euclids algorithm -gcd(m,n)=gcd(n,m mod n) Untill m mod n =0,since gcd(m,0) =m Another way of representation of the same algorithm Euclids algorithm Step1:if n=0 return val of m & stop else proceed step 2 Step 2:Divide m by n & assign the value of remainder to r Step 3:Assign the value of n to m,r to n,Go to step1. Another algorithm to solve the same problem Euclids algorithm Step1:Assign the value of min(m,n) to t Step 2:Divide m by t.if remainder is 0,go to step3 else goto step4 Step 3: Divide n by t.if the remainder is 0,return the value of t as the answer and stop,otherwise proceed to step4 Step4 :Decrease the value of t by 1. go to step 2 1.3 Fundamentals of Algorithmic problem solving • Understanding the problem • Ascertain the capabilities of the computational device • Exact /approximate soln. • Decide on the appropriate data structure • Algorithm design techniques • Methods of specifying an algorithm • Proving an algorithms correctness • Analysing an algorithm
Understanding the problem:The problem given should be understood completely.Check if it is similar to some standard problems & if a Known algorithm exists.otherwise a new algorithm has to be devised.Creating an algorithm is an art which may never be fully automated. An important step in the design is to specify an instance of the problem. Ascertain the capabilities of the computational device: Once a problem is understood we need to Know the capabilities of the computing device this can be done by Knowing the type of the architecture,speed & memory availability. Exact /approximate soln.: Once algorithm is devised, it is necessary to show that it computes answer for all the possible legal inputs. The solution is stated in two forms,Exact solution or approximate solution.examples of problems where an exact solution cannot be obtained are i)Finding a squareroot of number. ii)Solutions of non linear equations. Decide on the appropriate data structure:Some algorithms do not demand any ingenuity in representing their inputs.Someothers are in fact are predicted on ingenious data structures.A data type is a well-defined collection of data with a well-defined set of operations on it.A data structure is an actual implementation of a particular abstract data type. The Elementary Data Structures are ArraysThese let you access lots of data fast. (good) .You can have arrays of any other da ta type. (good) .However, you cannot make arrays bigger if your program decides it needs more space. (bad) . RecordsThese let you organize non-homogeneous data into logical packages to keep everything together. (good) .These packages do not include operations, just data fields (bad, which is why we need objects) .Records do not help you process distinct items in loops (bad, which is why arrays of records are used) SetsThese let you represent subsets of a set with such operations as intersection, union, and equivalence. (good) .Built-in sets are limited to a certain small size. (bad, but we can build our own set data type out of arrays to solve this problem if necessary)