Data Structures Introduction
Data Structures Introduction
RM.Periakaruppan
Associate Professor
Dept. of Applied Mathematics and Computational Sciences
PSG College of Technology
Introduction to Data Structures
• Data
- Data represents fact
eg. Temperature recorded in a city on specific day
• Information
- Processed data
eg. Average temperature of a city in a month
• Knowledge
- based on information
eg. A City is very hot during a specific period
Introduction to Data Structures
• Data Structure
- Data Structure is a way of storing and organizing data in such a
way that we can perform operations on these data in an effective
way.
Linked list
Linear Vs Non-Linear
• Non-linear
- When the elements stored in data structure do not form a
sequence, it is called non linear data structure. Here the relationship
between the elements is not linear. (ie. not one-one. It can be one-
many or many-many)
Eg.
Tree
Linear Vs Non-Linear Data Structures
• Big Theta (Θ) – gives tight bound, where the upper bound and lower
bound are same for an algorithm
Big Oh notation
Definition
f(n) = O(g(n)) means there are positive constants c and no, such that
0 ≤ f(n) ≤ cg(n) for all n ≥ no
• (ie) as n increases f(n) doesn’t grows faster than g(n)
•
Big Oh Example
• Consider the function f(n) = 2n + 3
• 2 n + 3 < = 4 n for every n>=2
• Here f(n) = 2 n + 3, g(n) = n, c = 4, n0=2
Here g(n)=n is an upper bound of f(n) and hence f(n)=O(g(n))= O(n)
Since O(n)<O(nlogn)<O(n^2)..<O(n^k)<O(2^n)<(3^n)..<O(k^n)
It is correct to say f(n) = O(n^2),...O(2^n).
However the closest upper bound is used. Hence f(n)=O(n) is more
meaningful.
Since log n < n it is incorrect to say f(n)=O(log n)
Big Oh Example -How to find Upper bound?
Consider the function f(n) = 2n + 3
0 3 0
1 5 5
2 7 10
3 9 15
4 11 20
5 13 25
Big Oh Example (Continued)
Chart Title
30
25
25
20
20
F(N) AND G(N)
15
15
13
11
10
10 9
7
5
5
3
0
0
0 1 2 3 4 5
INPUT N
f(n) c g(n)
Big Omega notation
Definition
f(n) = Ω(g(n)) means there are positive constants c and no, such that
0 ≤ f(n) >= cg(n) for all n ≥ no
• (ie) as n increases, f(n) doesn’t grows slower than g(n)
•
Big Omega Example
• Consider the function f(n) = 2n + 3
• 2 n + 3 >= n for every n>=1
• Here f(n) = 2 n + 3, g(n) = n, c = 1, n0=1
Here g(n)=n is an lower bound of f(n) and hence f(n)=Ω(g(n))= Ω(n)
Since 1<log n < n
It is correct to say f(n) = Ω(log n),Ω(1)
Since n^2 > n it is incorrect to say f(n)=Ω(n^2)
Big Omega Example -How to find Lower
bound?
Consider the function f(n) = n^2 + n
0 0 0
1 2 1
2 6 4
3 12 9
4 20 16
5 30 25
Big Omega Example (Continued)
Chart Title
35
30
30
25
25
20
F(N) AND G(N)
20
16
15
12
10 9
6
5 4
2
1
0
0
0 1 2 3 4 5
INPUT N
f(n) C g(n)
Theta notation
Definition
f(n) = Θ(g(n)) means there are positive constants c1, c2 and no, such
that c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ no
• (ie) as n increases f(n) grows at the same rate as g(n)
Theta Example
• Consider the function f(n) = 2n + 3
• n<=2 n + 3 < = 5 n for every n>=1
• Here f(n) = 2 n + 3, g(n) = n, c1 = 1, c2=5,n0=1
Here g(n)=n is an tight bound of f(n) and hence f(n)=Θ(g(n))= Θ(n)
Since log n # n or n^2 # n
It is incorrect to say f(n) = Θ(log n) or f(n)=Θ(n^2)
Big Theta Example (Continued)
Input n f(n)=2n+3 C1 g(n) = 1. n C2.g(n)=5.n
0 3 0 0
1 5 1 5
2 7 2 10
3 9 3 15
4 11 4 20
5 13 5 25
Theta Example (Continued)
Chart Title
30
25
25
20
20
F(N) AND G(N)
15
15 f(n)
13
C1 g(n)
11
10 C2(g(n)
10 9
7
5 5
5 4
3 3
2
1
0
0
0 1 2 3 4 5
INPUT N
Example 2
• Consider the function f(n) = n^2 log n + n
• Upper Bound
• n^2 log n + n <= n^2 log n + n^2 log n
• n^2 log n + n < = 2 n^2 log n for all n>=1
• Hence upper bound is O(n^2 log n).
• Lower Bound
• n^2 log n + n >= n^2 log n for all n>=1
Hence lower bound is Ω(n^2 log n).
Tight Bound
Since n^2 log n <= n^2 log n + n <= 2 n^2 log n for all n>=1
We have Θ(n^2 log n)
Asymptotic Notations Example 3
• Find upper bound for
• f(n) = 2 n^2 + 3 n + 4
• for(i=1;i<=n;i=i*2)
{
Stmt;
}
Time complexity is ?
Example 7
• for(i=1;i<=n;i=i*2)
{
Stmt;
}
Time complexity is O(log n)
Example 7
‘i’ takes values 1,2,2^2,....2^k
Loop will terminate, when i>n
2^k > n
(i.e) 2 ^ k =n
=> k = log n
• Time complexity is O(log n)
Example 8
• for(i=n;i>=1;i=i/2)
{
Stmt;
}
Time complexity is ?
Example 8
• for(i=n;i>=1;i=i/2)
{
Stmt;
}
Time complexity is O(log n)
( i takes values n, n/2, n/(2^2),...n/(2^k)
Loop will terminate when i<1 (ie) n/(2^k) <1
2^k > n => k= log n)
Example 9
• for(i=0;i*i<n;i++)
{
Stmt;
}
Time complexity is ?
Example 9
• for(i=0;i*i<n;i++)
{
Stmt;
}
Time complexity is O(sqrt(n))
( loop will terminate when i^2 >=n
i = sqrt(n) )
Example 10
• for(i=1;i<=n;i++)
•{
• for(j=1;j<=n;j++)
{
Stmt;
}
}
for(k=1;k<=n; k++)
{
Stmt;
}
Time complexity is ?
Example 10
• for(i=1;i<=n;i++)
•{
• for(j=1;j<=n;j++)
{
Stmt;
}
}
for(k=1;k<=n; k++)
{
Stmt;
}
Time complexity is O(n^2) ( we have f(n)= n^2 + n which is n^2)
Example 11
• p=0
• for(i=1;i<=n;i=i*2)
•{
• p++;
•}
• for(j=1; j<=p;j=j*2)
•{
• Stmt;
•}
Time complexity is ?
Example 11
p=0
for(i=1;i<=n;i=i*2)
p++;
for(j=1; j<=p;j=j*2)
Stmt;
After first for loop, the value of p=log n
and hence first for loop runs for O(log n) times.
The second loop runs for log p, substituting p, we have
log log n and hence second for loop runs for O(log log n).
The overall time complexity is max(logn, log log n)= O(log n)
Example 12
for(i=0;i<n;i++)
{
for(j=1;j<n;j=j*2)
{
Stmt;
}
}
Time complexity is ?
Example 12
for(i=0;i<n;i++)
{
for(j=1;j<n;j=j*2)
{
Stmt;
}
}
Time complexity is O(nlog n)
Inner for loop runs for log n times and outer for loop runs for n times,
=> total number of times is n log n
Commonly used time complexities
• O(1) – Constant
• O(log n) - logarithmic
• O(n) – linear
• O(n logn) – linear logarithmic
• O(n2)- Quadratic
• O(n3)- Cubic
• O(nk) – Polynomial (in general)
• O(2n ) – Exponential time complexity ( in general O(kn)
Comparison of time complexities
• O(1)<O(logn)<O(n)<O(nlogn)<O(n^2)..<O(n^k)<O(2^n)<(3^n)..<O(k^n)
Analysis of Recursive function
• A function which call itself is called recursive function.
• Terminates when a base case is reached
• Each recursive call uses stack memory
• Used in solving problems based on divide and conquer strategy
• (eg) int fact(int n)
• { if (n==0 || n ==1) return 1; //base case
• else
• return n*fact(n-1); // recursive call to function fact
• }
• Mathematically can be expressed as recurrence relation
• T(n) = T(n-1) + a , n>1
= 1 , n=0,1
Analysis of Recursive function
• Solving the following Recurrence Relation
• T(n) = T(n-1) + a , n>1
= 1 , n=0,1
T(n) = T(n-1) + a
= [T(n-2)+a] + a
= T(n-2)+2a
= T(n-3)+3a
Analysis of Recursive function
•
T(n) = T(n-k)+ka {k times}
When k=n
T(n) = T(n-n)+na
= T(0) + na
=1+an
= O(n)
Worst, best and average case time complexity
• Based on the input instances of the problem, the best, worst and
average case time complexities can be found.
• The input instances for which the algorithm takes maximum possible
time is called worst case.
• The input instances for which the algorithm takes minimum possible
time is called best case.
• The input instances for which the algorithm is neither behaves as best
case nor worst case is called average case
Worst, best and average case time complexity
Example Linear Search
• Worst Case happens when the element to be searched is not present
or the last element in the array. Hence worst case time complexity is
O(n) [or Ɵ(n) or Ω(n)].
•
Worst, best and average case time
complexity
Example Linear Search
In average case analysis, we take all possible inputs and calculate computing
time for all of the inputs. Sum all the calculated values and divide the sum by total
number of inputs. We must know (or predict) distribution of cases. For the linear
search problem, let us assume that all cases are uniformly distributed.