DATA STRUCTURES USING C [CSIT124]
LECTURE NOTES
MODULE- I
ASYMPTOTIC NOTATIONS
By Dr. Nancy Girdhar
In this session we will talk… ASET
• Course outline and overview
– What is your ideas on Data Structures
– What are we going to study
– Why we will study this
– What are we going to learn
• Quizzes and assignments
• And about you all………
2
Before we Start….. ASET
• Email: [email protected]
• Google Classroom Codes:
X Batch_Lab: v5t3ftw
Y Batch_Lab: tugcd3b
Theory_Lectures: dogxhmu
3
Course Content
Module 1: Introduction to Data Structures
Module 2: Stacks and Queues
Module 3: Programming with Linked Lists
Module 4: Trees
Module 5: Searching and Sorting
Module 6: Graph and their Applications
Module I : Introduction to Data Structures
• Definition, Types. Algorithm design, Complexity, Time-Space
Tradeoffs. Use of pointers in data structures.
• Array Definition and Analysis, Representation of Linear Arrays in
Memory, Traversing of Linear Arrays, Insertion And Deletion, Single
Dimensional Arrays, Two Dimensional Arrays, Multidimensional
Arrays, Function Associated with Arrays, Character String in C,
Character String Operations, Arrays as parameters, Implementing One
Dimensional Array, Sparse matrix.
Contents to be Covered
Algorithm and its Characteristics
Algorithm Analysis
Asymptotic Notations
6
Algorithm and its Characteristics
Algorithms
How will you
decide which
algorithm to
choose???
1. Run time of Algorithm
2. Memory requirement
Characteristics of an Algorithm
Unambiguous
• Algorithm should be clear and unambiguous.
• An algorithm should have 0 or more well
Input defined inputs.
• An algorithm should have 1 or more well
Output defined outputs.
Characteristics of an Algorithm
• Algorithms must terminate after a finite no. of steps.
Finiteness
• Should be feasible with the available resources.
Feasibility
• An algorithm should have step-by-step directions which
Independent should be independent of any programming code.
Algorithm Analysis
Analysis of Algorithm
Algorithm Analysis
• An algorithm is said to be efficient and fast, if it
• takes less time to execute
• consumes less memory space.
• The performance of an algorithm is measured on the basis of
• Time Complexity
• Space Complexity
(a) Space Complexity
• The amount of memory space required by the algorithm in its life cycle.
• A fixed part For example simple variables & constant used and program size etc.
• A variable part For example dynamic memory allocation, recursion stacks space
etc.
• Space complexity S(P) of any algorithm P is
S(P) = C + S(I)
Where, C is the fixed part
S(I) is the variable part of the algorithm
(b) Time Complexity - T(n)
The amount of time required by the algorithm to run to completion.
T(n) can be measured as the number of steps, provided each step consumes
constant time.
Measuring Running time of Algorithms
Implement algorithm in
S/W and H/W dependent
programing language
Experimental/ Run it on different Limited number of input can be
empirical method/ input variables tested
posteriori testing
Difficult to manage with
Record the exact run
algorithms which take long time
time
to execute
Measuring Run time of
Algorithms
Analyze running time
based on input size
Analytical Method/
Independent of H/w
theoretical/ apriori
and S/W
method
Considers all input
variables
Run time Analysis
• Size of input Running time of the algorithm.
• Small input size less time
Apriori Analysis
Apriori Analysis is interested in following computational efficiency-
Number of times a
statement is executed
Time taken for single Machine
execution of a statement Dependent
We are not considering
any kind of parallelism
Calculate time complexity
Single Statement b=20; T(n) = constant (c)
a = b+10;
for(i=0; i < N; i++)
Loop {
T(n) = N = O(N)
statement;
}
for(i=0; i < N; i++)
{
Double Loop for(j=0; j < N;j++)
{ T(n) = N * N= N2
statement;
}
}
Asymptotic Notations
Asymptotic Notation
• Asymptotic analysis refers to computing the running time of any
operation in mathematical units of computation.
• Complexity: Space (Memory) Complexity and Time Complexity
• Ο Notation (Upper bound)
• Ω Notation (Lower bound)
• θ Notation (Average bound)
Asymptotic Notation
Asymptotic Notations- (O, Ω, and θ)
The basic Asymptotic Notations are:
1. O(Big-“Oh”) Notation
[Maximum number of steps to solve a problem, (upper bound)]
2. Ω (Big-“Omega”) Notation
[Minimum number of steps to solve a problem, (lower bound)]
3. 𝚯(Theta) Notation
[Average number of steps to solve a problem, (Average bound)]
Note: Any function can be represented either in Upper bound or in Lower bound or in Average bound
Hint: 𝑛2 + 1000𝑛 ≤ 𝑛2 + 1000𝑛2 ≤ 1001𝑛2 ;
𝑙𝑜𝑔 𝑛 𝑙𝑜𝑔 𝑛
Log property: 𝑙𝑜𝑔𝑏 𝑛 = 𝑙𝑜𝑔𝑐 𝑏 Example: 𝑙𝑜𝑔3/2 𝑛 = 𝑙𝑜𝑔 23/2 = 𝑂(𝑙𝑜𝑔2 𝑛)
𝑐 2
Big Oh Notation, Ο
• Measures the running time based on input size n.
• f(n) is O(g(n)) if there exist constant C and C0 such that f(n) ≤ c g(n) for all n≥n0
• Big Oh (O) gives the upper bound
Ω (Big-Omega) Notation
• Ω notation provides an asymptotic lower bound
Θ (Theta) Notation
• Θ((g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that
0 <= c1*g(n) <= f(n) <= c2*g(n) for all n >= n0}
𝑛2 1 𝑛2 𝑛2 𝑛2 1
Hint: 𝐶1 . 𝑛2 ≤ − 2𝑛 ≤ 𝐶2 . 𝑛2 ; since 4 𝑛2 = − ≤ − 2𝑛 ≤ 2 . 𝑛2
2 2 4 2
Worst, Best and Average case of an algorithm
• Minimum running time that an algorithm will take for input n
• Consider an input for which algorithm take min time
Best case • Not very informative
• Maximum running time that an algorithm will take for input n
• Consider an input for which algorithm take max time
Worst case • Most informative
• Average running time that an algorithm will take for input n
• Consider all input and take average
Average
case • Difficult to compute
Growth of Functions
Thank you !