Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Algorithm

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Data structures using C++

Bsc II year III Sem


ALGORITHMS
Definition: An Algorithm is a method of representing the step-by-step procedure
for solving a problem. It is a method of finding the right answer to a problem or to a
different problem by breaking the problem into simple cases.
It must possess the following properties

1. Finiteness: An algorithm should 4. Generality: Algorithm should be


terminate in a finite number of steps. complete in itself, so that it can be
2. Definiteness: Each step of the used to solve all problems of given
algorithm must be precisely (clearly) type for any input data.
stated.
5. Input/output: Each algorithm
3. Effectiveness: Each step must be
effective. i.e.; it should be easily must take zero, one or more
convertible into program statement and quantities as input data and gives
can be performed exactly in a finite one of more output values.
amount of time.
Pseudo Code

It’s simply an implementation of an algorithm in the form of annotations and informative text written in plain
English. It has no syntax like any of the programming language and thus can’t be compiled or interpreted by the
computer.

Example: To find the average of 3 numbers, the algorithm is as shown below.

Step1: Read the numbers a, b, c, and d.


Step2: Compute the sum of a, b, and c.
Step3: Divide the sum by 3.
Step4: Store the result in variable of d.
Step5: End the program.
Development Of An Algorithm

The steps involved in the development of an algorithm are as follows:

● Specifying the problem statement.


● Designing an algorithm.
● Coding.
● Debugging
● Testing and Validating
● Documentation and Maintenance.
Development Of An Algorithm
Specifying the problem statement: The problem which has to be implemented in to a program
must be thoroughly understood before the program is written. Problem must be analyzed to
determine the input and output requirements of the program.

Designing an Algorithm: Once the problem is cleared then a solution method for solving the
problem has to be analyzed. There may be several methods available for obtaining the required
solution. The best suitable method is designing an Algorithm. To improve the clarity and
understandability of the program flowcharts are drawn using algorithms.
Coding: The actual program is written in the required programming language with the help of
information depicted in flowcharts and algorithms.
Development Of An Algorithm
Debugging: There is a possibility of occurrence of errors in program. These errors must be removed for proper working of programs. The process of
checking the errors in the program is known as ̳Debugging‘.

There are three types of errors in the program.

Syntactic Errors: They occur due to wrong usage of syntax for the statements.

Ex: x=a*%b
Here two operators are used in between two operands.

Runtime Errors : They are determined at the execution time of the program

Ex: Divide by zero


Range out of bounds.

Logical Errors : They occur due to incorrect usage of instructions in the program. They are neither displayed during compilation or execution
nor nor cause any obstruction to the program execution. They only cause incorrect outputs.
Development Of An Algorithm
Testing and Validating: Once the program is written , it must be tested and then
validated. i.e., to check whether the program is producing correct results or not for
different values of input.
Documentation and Maintenance: Documentation is the process of collecting,
organizing and maintaining, in written the complete information of the program for
future references.
Maintenance is the process of upgrading the program, according to the changing
requirements.
ALGORITHM COMPLEXITY
Suppose X is an algorithm and n is the size of input data, the time and space used by the algorithm
X are the two main factors, which decide the efficiency of X.

● Time Factor − Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
● Space Factor − Space is measured by counting the maximum memory space required by
the algorithm.

The complexity of an algorithm f(n) gives the running time and/or the storage space required by the
algorithm in terms of n as the size of input data.
Space Complexity
Space complexity of an algorithm represents the amount of memory space required by the algorithm in its life cycle. The space
required by an algorithm is equal to the sum of the following two components −
● A fixed part that is a space required to store certain data and variables, that are independent of the size of the problem. For
example, simple variables and constants used, program size, etc.
● A variable part is a space required by variables, whose size depends on the size of the problem. For example, dynamic
memory allocation, recursion stack space, etc.

Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed part and S(I) is the variable part of the algorithm,
which depends on instance characteristic I. Following is a simple example that tries to explain the concept −
Algorithm: SUM(A, B)
Step 1 - START
Step 2 - C ← A + B + 10
Step 3 - Stop

Here we have three variables A, B, and C and one constant. Hence S(P) = 1 + 3. Now, space depends on data types of given
variables and constant types and it will be multiplied accordingly.
Time Complexity
Time complexity of an algorithm represents the amount of time required by the algorithm to
run to completion. 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) = c ∗ n, 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.
ALGORITHM ANALYSIS
Efficiency of an algorithm can be analyzed at two different stages, before implementation and
after implementation. They are the following –

● A Priori Analysis or Performance or Asymptotic Analysis − 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.
● A Posterior Analysis or Performance Measurement − This is an empirical analysis of an
algorithm. The selected algorithm is implemented using programming language. This is
then executed on target computer machine. In this analysis, actual statistics like running
time and space required, are collected.

We shall learn about a priori algorithm analysis. Algorithm analysis deals with the execution or
running time of various operations involved. The running time of an operation can be defined as
the number of computer instructions executed per operation.
ALGORITHM ANALYSIS
Analysis of an algorithm is required to determine the amount of resources such as time and
storage necessary to execute the algorithm. Usually, the efficiency or running time of an
algorithm is stated as a function which relates the input length to the time complexity or space
complexity.

Algorithm analysis framework involves finding out the time taken and the memory space required
by a program to execute the program. It also determines how the input size of a program
influences the running time of the program.

In theoretical analysis of algorithms, it is common to estimate their complexity in the asymptotic


sense, i.e., to estimate the complexity function for arbitrarily large input. BigO notation, Omega
notation, and Theta notation are used to estimate the complexity function for large arbitrary input.
Types of Analysis
The efficiency of some algorithms may vary for inputs of the same size. For such algorithms, we need to
differentiate between the worst case, average case and best case efficiencies.
● Best Case Analysis: If an algorithm takes the least amount of time to execute a specific set of input, then it
is called the best case time complexity. The best case efficiency of an algorithm is the efficiency for the best
case input of size n. Because of this input, the algorithm runs the fastest among all the possible inputs of the
same size.
● Average Case Analysis: If the time complexity of an algorithm for certain sets of inputs are on an average,
then such a time complexity is called average case time complexity. Average case analysis provides
necessary information about an algorithm’s behavior on a typical or random input. You must make some
assumption about the possible inputs of size n to analyze the average case efficiency of algorithm.
● Worst Case Analysis: If an algorithm takes maximum amount of time to execute for a specific set of input,
then it is called the worst case time complexity. The worst case efficiency of an algorithm is the efficiency for
the worst case input of size n. The algorithm runs the longest among all the possible inputs of the similar
size because of this input of size n.
Summary
● Algorithm
● Properties of an algorithm
● Pseudo code
● Development of an algorithm
● Algorithm analysis

You might also like