Adsa Unit - 1
Adsa Unit - 1
INTRODUCTION TO ALGORITHM
Algorithm Definition:
An Algorithm is a set of well-defined instructions designed to
perform a specific set of tasks. Algorithms are used in Computer
science to perform calculations, automatic reasoning, data
processing, computations, and problem-solving.
ANALYSIS OF ALGORITHM
The analysis is a process of estimating the efficiency of an
algorithm. There are two fundamental parameters based on
which wecan analysis the algorithm:
• Space Complexity: The space complexity can be understood
as the amount of space required by an algorithm to run to
completion.
• Time Complexity: Time complexity is a function of
input size n that refers to the amount of time needed by an
algorithm to run to completion.
if there is a problem P1, then it may have many solutions,
such that each of these solutions is regarded as an algorithm. So,
there may be many algorithms such as A1, A2, A3, …, An.
Before you implement any algorithm as a program, it is better
to find out which among these algorithms are good in terms of time
and memory It would be best to analyze every algorithm
in terms of Time that relates to which one could
execute faster and Memory corresponding to which one will take
less memory.
So, the Design and Analysis of Algorithm talks about how to
design various algorithms and how to analyze them. After designing
and analyzing, choose the best algorithm that takes the least time
and the least memory and then implement it as a program in C.
Memory is relatively more flexible. We can increase the
memory as when required by simply adding a memory card. So, we
will focus on timethan that of the space.
The running time is measured in terms of a particular piece
of hardware, not a robust measure. When we run the same
algorithm on a
Complexity of Algorithm:
The term algorithm complexity measures how many steps are
required by the algorithm to solve the given problem. It evaluates
the order of count of operations executed by an algorithm as a
function of input data size.
To assess the complexity, the order (approximation) of the
count of operation is always considered instead of counting the
exact steps.
O(f) notation represents the complexity of an algorithm,
which is also termed as an Asymptotic notation or "Big O" notation.
Here the f corresponds to the function whose size is the same as
that of the input data. The complexity of the asymptotic
computation O(f) determines in which order the resources such as
CPU time, memory, etc. are consumed by the algorithm that is
articulated as a function of the size of the input data.
The complexity can be found in any form of the following
forms. It is nothing but the order of constant, logarithmic, linear
and so on, the number of steps encountered for the completion of
a particular algorithm. To make it even more precise, we often
call the complexity ofan algorithm as "running time".
• Quadratic Complexity:
It imposes a complexity of O(n2). For N input data size, it
undergoes the order of N2 count of operations on N number of
elements for solving a given problem.
• Cubic Complexity:
• Big-O notation
• Omega notation
• Theta notation
Example:
As an example, let us formally prove one of the assertions
made in the introduction: 100n + 5 ∈ O(n2). Indeed,
100n + 5 ≤ 100n + n (for all n ≥ 5) = 101n ≤ 101n2 .
Example
Here is an example of the formal proof that n3 ∈ (n2):
n3 ≥ n2 for all n ≥
0, i.e.,we can select c = 1 and n0 =
0.
n2 − 1/2 n
n2 − 1/2 n 1/2 n (for all
n ≥ 2)n2 .
Hence, we can select c2 = 1/4 , c1 = 1/2 , and n0 = 2.
<= 2cn/2Log(n/2) + n
= cnLogn - cnLog2 + n
= cnLogn - cn + n
<= cnLogn
cn2
/ \
T(n/4) T(n/2)
c
n
2
/ \
c(n2)/16 c(n2)/4
cn2
/ \
c(n2)/16 c(n2)/4
/ \ / \
c(n2)/256 c(n2)/64 c(n2)/64 c(n2)/16
/ \ / \ / \ / \
To know the value of T(n), we need to calculate sum of tree nodes
level by level. If we sum the above tree level by level, we get the
following series
T(n) = c(n^2 + 5(n^2)/16 + 25(n^2)/256) + ....
• int
• float
• double
• pointer
Characters are internally considered as int and floats also falls
under double and the predefined operations are addition,
subtraction, etc.
• Non-primitive Data Structures (User Defined Data Structures)
Non-primitive data structures are more complicated data structures
and they are derived from primitive data structures. Non-primitive
data structures are used to store large and connected data. Some
example of Non-primitive data structures are:
• Linked List
• Tree
• Graph
• Stack
• Queue etc.
All these data structures allow us to perform different
operations on data. We select these data structures based on
which type of operationis required.
Algorithm Definition:
An algorithm is a finite set of unambiguous instructions,
written in order, to accomplish a certain predefined task or
obtaining a required output for any legitimate input in a finite
amount of time. Algorithm is not the complete code or program, it
is just the core logic of a problem.
Data Definition:
Data can be defined as an elementary value or the collection of
values,for example, student's name and its id are the data about the
student.