0% found this document useful (0 votes)
11 views

Algorithms - Notes

The topics covered in this are: What is an algorithm? Algorithm Properties Efficiency of the algorithm Asymptotic analysis

Uploaded by

prathisimha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
11 views

Algorithms - Notes

The topics covered in this are: What is an algorithm? Algorithm Properties Efficiency of the algorithm Asymptotic analysis

Uploaded by

prathisimha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
Pulloort Prathibh, Lecturer Introduction to Algorithms What is Algorithm? > An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain predefined task. +> Algorithm is not the complete code or program, it is just the core logic (solution) of a problem, which can be expressed either as an informal high level description as pseudo code or using 2 flowchart. + Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output. > Algorithms are generally created independent of underlying languages, ie. an algorithm can be implemented in more than one programming language. Characteristics of an Algorithm An algorithm should have the following characteristics ~ 1. Unambiguous - Algorithm should be clear and unambiguous. Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning, Input - an algorithm should have 0 or more well-defined inputs. Output - an algorithm should have 1 or more well-defined outputs, and should match the desired output. 4. Finiteness - Algorithms must terminate after a finite number of steps. 5. Feasibility - should be feasible with the available resources. 6. Independent ~ an algorithm should have step-by-step directions, which should be independent of any programming code. Algorithm Analysis ‘+ Efficiency of an algorithm can be analyzed at two different stages, Before implementation and After implementation. They are ~ 1. Priori Analysis: [ “Priori” means “before”] © Hence Priori analysis means checking the algorithm before its implementation. * In this, the algorithm is checked when it is written in the form of theoretical steps. ie., this is a theoretical analysis of an algorithm. ‘Efficiency of an algorithm is measured by assuming that all other factors, For example, processor speed, are constant and have no effect on the implementation, 2.A Posterior Analysis: [“Posterior” means “after”] ‘* Hence Posterior analysis means checking the algorithm after its implementation. * inthis, the algorithm is checked by implementing it in any programming language and executing it this is an empirical analysis of an algorithm. ‘* This analysis helps to get the actual and real analysis report about correctness, space required, time consumed etc. Algorithm Efficiency © Ameasure of the average execution time necessary for an algorithm to complete work on a set of data, ‘© Algorithm efficiency is characterized by its order. ‘©The complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the amount of data the algorithm must process. © Usually there are natural units for the domain and range of this function, © There are two main measures of the efficiency of an algorithm. 1. Time Complexity 2. Space Complexity Pulloort Prathibh, Lecturer Time Complexity © Time comple: tocompletion. 'y of an algorithm represents the amount of time required by the algorithm to run * The time complexity of an algorithm is also calculated by determining following 2 components: © Constant time part: Any instruction that is executed just once comes in this part. For example, input, output, iFelse, switches, ete. © Variable Time Part: Any instruction that is executed more than once, say n times comes in this part, For example, loops, recursion, ete. ‘Time requirements can be defined as a numerical function T(n), where T(n) can be measured as the number of steps, provided each step consumes constant time. For example, addition of two n-bit integers takes n steps. * Consequently, the total computational time is T{n) = cn, where c is the time taken for the addition of two bits, Here, we observe that T(n) grows linearly as the input size increases. Space Complexity ‘Space complexity of an algorithm refers to the amount of memory that this algorithm requires to execute and get the result ‘+ This can be for inputs, temporary operations, or outputs. ‘The space complexity of an algorithm is calculated by determining following 2 components: (© Fixed Part: This refers to the space that is definitely required by the algorithm. For example, input variables, output variables, program size, etc. © Variable Part: This refers to the space that can be different based on the implementation of the algorithm. For example, temporary variables, dynamic memory allocation, recursion stack space, etc. Asymptotic Analysis / Asymptotic Notations ‘Asymptotic analysis of an algorithm refers to defining the mathematical foundation / framing of its run-time performance. © Using asymptotic analysis, we can very well conclude the best case, average case, and worst case scenario of an algorithm. ‘© Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is concluded to work ina constant time. Other than the "input" all other factors are considered constant. © Asymptotic analysis refers to computing the running time of any operati computation. mathematical units of © The time required by an algorithm falls under three types — ° Best Case - Minimum time required for program execution. ° Average Case - Average time required for program execution. © Worst Case - Maximum time req Asymptotic Notations od for program execution. Following are the commonly used asymptotic notations to calculate the running time complexity of an algorithm * ONotation * Notation © @ Notation Pulloort Prathibh, Lecturer Big Oh Notation, O 2 The notation O(n) is the formal way to express the upper bound of an algorithm's running time. 2 It measures the worst case time complexity or the longest amount of time an algorithm can possibly take to complete. For example, for a function fin) Z Omega Notation, Q # The notation O(n) is the formal way to express the lower bound of an algorithm's running time. It measures the best case time complexity or 9» the best amount of time an algorithm can possibly take to complete. fe For example, for a function fn) k Theta Notation, @ The notation @{n) is the formal way to express both the lower bound and the upper bound of an algorithm's running time. It is represented as follows — Algorithm Design Tools k ‘The two popular tools used in the representation of algorithms are the following: 1. Pseudo code 2. Flowchart Pseudo code % An algorithm can be written in any of the natural languages such as English, German, French, ete. % One of the commonly used tools to define algorithms is the pseudo code. % Pseudo code is one of the tools that can be used to write a preliminary plan that can be developed into a computer program. % A pseudo code is an English-like presentation of the code required for an algorithm, % Pseudo code is a generic way of describing an algorithm without use of any — specific programming language syntax. % It is, as the name suggests, pseudo code —it cannot be executed on a real computer, but it models and resembles real programming code, and is written at roughly the same level _ of detail. Flowchart > A very effective tool to show the logie flow of a program is the flowchart A flowchart is a pictorial representation of an algorithm, > It hides all the details of an algorithm by giving a picture; it shows how the algorithm flows from beginning to end. } A flowchart is a schematic representation of an algorithm or a stepwise process, showing the steps as boxes of various kinds, and their order by connecting these with arrows. i Flowcharts are used in designing or documenting a process or program > Flowcharts are usually drawn using some standard _ symbols; however, some special symbols can also be developed when required Pages PullootPrathibha Lecturer Flowchart Symbols To calculate the average of two numbers Symbol Name Function Take numa, Start/end nova represents a start ume rend point. Alive is connector that sh — ‘Arrows relationships between the Average = representative shapes. (oumtenum2y/2 Input/Output A parallelogram represents input or ouptut. an wo (=) Decision A diamond indicates a decision,

You might also like