Algorithm in Data Structure
Algorithm in Data Structure
What is an algorithm?
An algorithm is a step by step procedure to solve a problem. In normal language, the algorithm is
defined as a sequence of statements which are used to perform a task. In computer science, an
Algorithms are used to convert our problem solution into step by step statements. These
statements can be converted into computer programming instructions which form a program.
This program is executed by a computer to produce a solution. Here, the program takes required
data as input, processes data according to the program instructions and finally produces a result
Specifications of Algorithms
1. Input - Every algorithm must take zero or more number of input values from external.
number of steps.
5. Effectiveness - Every instruction must be basic enough to be carried out and it also
must be feasible.
Let us consider the following problem for finding the largest value in a given list of values.
Problem Statement : Find the largest number in the given list of numbers?
Input : A list of positive integer numbers. (List must contain at least one number).
Output : The largest number in the given list of positive integer numbers.
Consider the given list of numbers as 'L' (input), and the largest number as 'max' (Output).
Algorithm
2. Step 2: Compare first number (say 'x') in the list 'L' with 'max', if 'x' is larger than 'max',
In computer science, all algorithms are implemented with programming language functions. We
can view a function as something that is invoked (called) by another function. It executes its code
and then returns control to the calling function. Here, a function can be called by itself or it may
call another function which in turn call the same function inside it is known as recursion. A
The function which is called by itself is known as Direct Recursive function (or Recursive
function)
The function which calls a function and that function calls its called function is known
Most of the computer science students think that recursive is a technique useful for only a few
special problems like computing factorials, Ackermann's function, etc., This is unfortunate
because the function implemented using assignment or if-else or while or looping statements can
also be implemented using recursive functions. This recursive function is very easier to
Performance Analysis
What is Performance Analysis of an algorithm?
If we want to go from city "A" to city "B", there can be many ways of doing this. We can go by
flight, by bus, by train and also by bicycle. Depending on the availability and convenience, we
choose the one which suits us. Similarly, in computer science, there are multiple algorithms to
solve a problem. When we have more than one algorithm to solve a problem, we need to select
the best one. Performance analysis helps us to select the best algorithm from multiple algorithms
to solve a problem.
When there are multiple alternative algorithms to solve a problem, we analyze them and pick the
one which is best suitable for our requirements. The formal definition is as follows...
Performance of an algorithm is a process of making evaluative judgement about
algorithms.
That means when we have multiple algorithms to solve a problem, we need to select a suitable
We compare algorithms with each other which are solving the same problem, to select the best
algorithm. To compare algorithms, we use a set of parameters or set of elements like memory
required by that algorithm, the execution speed of that algorithm, easy to understand, easy to
implement, etc.,
1. Whether that algorithm is providing the exact solution for the problem?
When we want to analyse an algorithm, we consider only the space and time required by that
Based on this information, performance analysis of an algorithm can also be defined as follows...
1. Space required to complete the task of that algorithm (Space Complexity). It includes
Space Complexity
What is Space complexity?
When we design an algorithm to solve a problem, it needs some computer memory to complete
its execution. For any algorithm, memory is required for the following purposes...
4. And for few other things like funcion calls, jumping statements etc,.
Generally, when a program is under execution it uses the computer memory for THREE reasons.
instructions.
3. Data Space: It is the amount of memory used to store all the variables and constants.
Note - When we want to perform analysis of an algorithm based on its Space complexity, we
consider only Data Space and ignore Instruction Space as well as Environmental Stack.
That means we calculate only the memory required to store Variables, Constants, Structures,
etc.,
To calculate the space complexity, we must know the memory required to store different
datatype values (according to the compiler). For example, the C Programming Language
Example 1
int square(int a)
{
return a*a;
}
In the above piece of code, it requires 2 bytes of memory to store variable 'a' and another 2
That means, totally it requires 4 bytes of memory to complete its execution. And this 4
bytes of memory is fixed for any input value of 'a'. This space complexity is said to
If any algorithm requires a fixed amount of space for all input values then that space
Example 2
int sum(int A[ ], int n)
{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}
4 bytes of memory for local integer variables 'sum' and 'i' (2 bytes each)
total amount of memory required depends on the value of 'n'. As 'n' value increases the
space required also increases proportionately. This type of space complexity is said to
If the amount of space required by an algorithm is increased with the increase of input
Time Complexity
What is Time complexity?
Every algorithm requires some amount of computer time to execute its instruction to perform the
The time complexity of an algorithm is the total amount of time required by an algorithm
5. Input data
Note - When we calculate time complexity of an algorithm, we consider only input data and
ignore the remaining things, as they are machine dependent. We check only, how our program is
behaving for the different input values to perform all the operations like Arithmetic, Logical,
task because the configuration changes from one system to another system. To solve this
problem, we must assume a model machine with a specific configuration. So that, we can able to
To calculate the time complexity of an algorithm, we need to define a model machine. Let us
Now, we calculate the time complexity of following example code by using the above-defined
model machine...
Example 1
int sum(int a, int b)
{
return a+b;
}
In the above sample code, it requires 1 unit of time to calculate a+b and 1 unit of time to return
the value. That means, totally it takes 2 units of time to complete its execution. And it does not
change based on the input values of a and b. That means for all input values, it requires the
If any program requires a fixed amount of time for all input values then its time complexity
Example 2
int sum(int A[], int n)
{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}
In above calculation
Cost is the amount of computer time required for a single operation in each line.
Repeatation is the amount of computer time required by each operation for all its repeatations.
So above code requires '4n+4' Units of computer time to complete the task. Here the exact time
is not fixed. And it changes based on the n value. If we increase the n value then the time
Complexity.
If the amount of time required by an algorithm is increased with the increase of input
Asymptotic Notations
What is Asymptotic Notation?
that algorithm. But when we calculate the complexity of an algorithm it does not provide the exact
amount of resource required. So instead of taking the exact amount of resource, we represent
that complexity in a general form (Notation) which produces the basic nature of that algorithm.
Note - In asymptotic notation, when we want to represent the complexity of an algorithm, we use
only the most significant terms in the complexity of that algorithm and ignore least significant
terms in the complexity of that algorithm (Here complexity can be Space Complexity or Time
Complexity).
Algorithm 1 : 5n2 + 2n + 1
Algorithm 2 : 10n2 + 8n + 3
Generally, when we analyze an algorithm, we consider the time complexity for larger values of
input data (i.e. 'n' value). In above two time complexities, for larger value of 'n' the term '2n +
1' in algorithm 1 has least significance than the term '5n2', and the term '8n + 3' in algorithm 2
Here, for larger value of 'n' the value of most significant terms ( 5n2 and 10n2 ) is very larger than
the value of least significant terms ( 2n + 1 and 8n + 3 ). So for larger value of 'n' we ignore the
least significant terms to represent overall time required by an algorithm. In asymptotic notation,
we use only the most significant terms to represent the time complexity of an algorithm.
Majorly, we use THREE types of Asymptotic Notations and those are as follows...
1. Big - Oh (O)
Big - Oh notation is used to define the upper bound of an algorithm in terms of Time Complexity.
That means Big - Oh notation always indicates the maximum time required by an algorithm for all
input values. That means Big - Oh notation describes the worst case of an algorithm time
complexity.
Consider function f(n) as time complexity of an algorithm and g(n) is the most significant
term. If f(n) <= C g(n) for all n >= n0, C > 0 and n0 >= 1. Then we can represent f(n) as
O(g(n)).
f(n) = O(g(n))
Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-Axis
Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as O(g(n)) then it must satisfy f(n) <= C g(n) for all values of C >
0 and n0>= 1
⇒3n + 2 <= C n
3n + 2 = O(n)
Complexity.
That means Big-Omega notation always indicates the minimum time required by an algorithm for
all input values. That means Big-Omega notation describes the best case of an algorithm time
complexity.
Consider function f(n) as time complexity of an algorithm and g(n) is the most significant
term. If f(n) >= C g(n) for all n >= n0, C > 0 and n0 >= 1. Then we can represent f(n) as
Ω(g(n)).
f(n) = Ω(g(n))
Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-Axis
In above graph after a particular input value n0, always C g(n) is less than f(n) which indicates the
Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Ω(g(n)) then it must satisfy f(n) >= C g(n) for all values of C
> 0 and n0>= 1
⇒3n + 2 >= C n
By using Big - Omega notation we can represent the time complexity as follows...
3n + 2 = Ω(n)
Consider function f(n) as time complexity of an algorithm and g(n) is the most significant
term. If C1 g(n) <= f(n) <= C2 g(n) for all n >= n0, C1 > 0, C2 > 0 and n0 >= 1. Then we can
f(n) = Θ(g(n))
Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-Axis
Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Θ(g(n)) then it must satisfy C1 g(n) <= f(n) <= C2 g(n) for all
values of C1 > 0, C2 > 0 and n0>= 1
C1 g(n) <= f(n) <= C2 g(n)
⇒C1 n <= 3n + 2 <= C2 n
Above condition is always TRUE for all values of C1 = 1, C2 = 4 and n >= 2.
By using Big - Theta notation we can represent the time compexity as follows...
3n + 2 = Θ(n)