Data Structure and Algorithm Chapter 1
Data Structure and Algorithm Chapter 1
1
Introduction to Data Structures and Algorithms
How to solve a problem
Given a problem, the first step to solve the problem is obtaining one’s
own abstract view, or model, of the problem. This process of
modeling is called abstraction.
2
Abstraction
•The model defines an abstract view to the problem.
•This implies that the model focuses only on problem related and that
a programmer tries to define the properties of the problem.
These properties include
The data which are affected and
The operations that are involved in the problem.
15
Calculating the Space Complexity
For calculating the space complexity, we need to know the value of memory used
by different type of datatype variables, which generally varies for different
operating systems, but the method for calculating the space complexity remains
the same.
Ex(32 bits):-
2 bytes to store Integer value.
4 bytes to store Floating Point value.
1 byte to store Character value.
2 bytes of memory for return value.
16
Calculating the Space Complexity
• Example:-
17
Question-1:- Calculate the space complexity of the following
instruction/code.
18
Answer of Question-1
In the above piece of code it requires,
'n*2' bytes of memory to store array variable 'a[ ]’,
2 bytes of memory for integer parameter ’n’,
4 bytes of memory for local integer variables 'sum' and 'i' (2 bytes
each), and
2 bytes of memory for return value.
That means, totally it requires '2n+8' bytes of memory to complete
its execution.
Here, the total amount of memory required depends on the value of 'n’.
As 'n' value increases the space required also increases proportionately.
19
Time Complexity
20
Time Complexity(Cont…)
Generally, the running time of an algorithm depends upon the
following...
Whether it is running on Single processor machine or Multi processor
machine.
Whether it is a 32 bit machine or 64 bit machine.
Read and Write speed of the machine.
The amount of time required by an algorithm to
perform Arithmetic operations, logical operations, return value
and assignment operations etc.,
Input data
21
Time Complexity(Count…)
To calculate the time complexity of an algorithm, we need to define a
model machine.
Note:- Let us assume a machine with following configuration...
It is a Single processor machine
It is a 32 bit Operating System machine
It performs sequential execution
It requires 1 unit of time for Arithmetic and Logical operations
It requires 1 unit of time for Assignment and Return value
It requires 1 unit of time for Read and Write operations
22
• Now we can calculate the time complexity of following example code
by using the above-defined model machine...
• Example:-
23
Time complexity(cont.….)
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 same amount of time
i.e. 2 units.
24
Asymptotic Notations
What is Asymptotic Notation?
Whenever we want to perform analysis of an algorithm, we need to
calculate the complexity of that algorithm /time & space/.
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.
We use that general form (Notation) for analysis process.
Note:-Asymptotic notation of an algorithm is a mathematical
representation of its complexity.
25
Asymptotic Notations
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.
Example:-Consider the following time complexities of two algorithms...
• Algorithm 1 : 5n2 + 2n + 1
• Algorithm 2 : 10n2 + 8n + 3
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 has least significance than the term '10n2'.
26
Asymptotic Notations
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 complexity of an algorithm.
27
Types of Asymptotic Notations
28
Big Theta (Θ) Notation
the Big-Θ / Big Theta/ notation is like the average value or range
within which the actual time of execution of the algorithm will be.
For example, assume if time complexity of an algorithm is
represented by the expression 3n2 + 5n, and
we use the Big-Θ notation to represent this,
then the time complexity would be Θ(n2), ignoring the constant
coefficient and removing the insignificant part, which is 5n.
29
Big Oh(O)
This notation is known as the upper bound of the algorithm, or a
Worst Case of an algorithm
This notation is known as the upper bound of the algorithm, or a
Worst Case of an algorithm.
Example:- Consider Linear Search algorithm, in which we traverse an
array elements, one by one to search a given number
Using this notation n /worst case/
30
Big Omega (Ω)
31
Reading Assignment
Read for the following concepts
Applications of data structure and algorithms
Asymptotic notation in detail
Compare and contrast Littlie-o Notation vs Littlie-Omega notation
32
See You Next!!
33