0% found this document useful (0 votes)
2 views

Lecture 3

The document discusses time complexity in programming, emphasizing its importance for optimizing program execution and avoiding indefinite computer time usage. It covers various types of analysis, including exact and asymptotic analysis, and provides examples of step count analysis to illustrate how to calculate total steps in algorithms. Additionally, it highlights the significance of understanding growth trends in complexity for comparing different programming approaches.

Uploaded by

pcd.programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 3

The document discusses time complexity in programming, emphasizing its importance for optimizing program execution and avoiding indefinite computer time usage. It covers various types of analysis, including exact and asymptotic analysis, and provides examples of step count analysis to illustrate how to calculate total steps in algorithms. Additionally, it highlights the significance of understanding growth trends in complexity for comparing different programming approaches.

Uploaded by

pcd.programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Data Structures &

Algorithms
Lecture 3
Time
Why time complexity? complexity
• We have to fix up the time limit for a program to avoid indefinite use of computer
time.
• From the time complexity of program we can decide whether the program or
program module needs to be redesigned.
Time complexity of a program is

T(P)= compile time + run time


The compiled program can be executed several time so we have to consider only run
time.
Time
complexity
• Options
• Compile time (Independent of instance characteristics)
• Runtime (Dependent on instance characteristics)
• Types of analysis
• Detailed analysis
• Exact analysis
• Step count analysis
• Asymptotic analysis
TC – Exact
analysis
• Let n denotes the instance characteristics then
Tp (n) = Ca ADD(n) + Cs SUB(n) + Cm MUL(n) + Cd DIV(n) + …
Where Ca denotes the time needed for addition while ADD, SUB, MUL and
DIV are the functions whose value is the number of operations.
T(P) = C + 0
T(P) = C + Ca * n + ….
SUM(A, n)
ABC (a, b, c) { tp(n) is impossible to compute
{ s = 0; • Actual numbers are needed
return a+b+c*a-(a-b)/c; for i ← 1 to n • Its based on system load (other
} s = s + A[i]; programs running along with)
return s;
}
TC – Step count
analysis
• Program step: A segment of program has execution time independent
of instance characteristics.
• Example: return a+b+c*a-(a-b)/c; is a single program step.

SUM(A, n) Line Steps/execution (s/e) Frequency Total steps


1 s = 0; 1 1 1 1
2 for i ← 1 to n
2 1 n+1 n+1
3 s = s + A[i];
4 return s; 3 1 n n
4 1 1 1
Total number of steps 2n+3
TC – Step count analysis –
Exercise
SUM(A, n) Line Steps/execution (s/e) Frequency Total steps
1 s = 0; 1 1 1 1
2 for i ← 1 to n/2
2 1 n/2+1 n/2+1
3 s = s + A[i];
4 for i ← n/2+1 to n 3 1 n/2 n/2
5 s = s + a[i]; 4 1 n/2+1 n/2+1
6 return s;
5 1 n/2 n/2
6 1 1 1
Total number of steps 2n+4
TC – Step count analysis –
Importance
• Although it doesn’t give us exact figure but
• We can understand the trend in growth of complexity
• We may compare different approaches
TC – Step count analysis –
Understand it
ADD(A, B, C, m, n) Line Steps/ Frequency Total steps
1 for i ← 1 to m execution (s/e)
2 for j ← 1 to n 1 1 m+1 m+1
3 C[i][j] = A[i][j] + B[i][j];
2 1 m(n+1) mn+m
3 1 mn mn
Total number of steps 2mn+2m+1
Lets be language specific to better
understand a few
important points
Different type of statements and
step
Statement count
Comment
Steps/exec
0
Statement
Declarative statement
Steps/exec
0
Expression/Assignment Normally 1 but Iteration statement Normally 1 but may be
statement depends on length of higher if condition
elements being dependent on instance
assigned characteristics

Switch statement 1 but may be higher if If-else statement Depends on condition


dependent on instance and dependent
characteristics statements
Function invocation Normally 1 but may be Memory management Normally 1 but may be
higher if instance statements higher if constructor or
characteristics based destructors are involved
pass-by-value
parameters are there

Function statement 0 because cost included Jump statement (break, 1 if not based on
in invocation continue, goto, return) instance characteristic
A Few language specific important points
regarding
Statement TC
Steps/Cost Frequency Total step cost
for(i = 1; i <= n; ++i) 1 n+1 n+1
for(i = 0; i < n; ++i) 1 n+1 n+1
for(i = 0; i <= n; ++i) 1 n+2 n+2
for(i = 1; i < n; ++i) 1 n n
for(i = 5; i <= n; ++i) 1 n-4+1 = n-3 n-3

OR
Statement Steps/Cost Frequency Total step cost
for(i = 1; i <= n; ++i) 1, 1, 1 1, n+1, n 1+n+1+n = 2n+2
for(i = 0; i < n; ++i) 1, 1, 1 1, n+1, n 1+n+1+n = 2n+2
for(i = 0; i <= n; ++i) 1, 1, 1 1, n+2, n+1 1+n+2+n+1 = 2n+4
for(i = 1; i < n; ++i) 1, 1, 1 1, n, n-1 1+n+n-1 = 2n
for(i = 5; i <= n; ++i) 1, 1, 1 1, n-4+1, n-5 1+n-3+n-5 = 2n-7

Dry run and check whether it’s


true or not with some value of n
Nested loop and
squared/cubic
Statement
TC
Steps/Cost Frequency Total step cost

for(i = 1; i <= n; ++i) 1 n+1 n+1


for(j = 1; j <= m; ++j) 1 n(m+1) nm+n

for(i = 0; i < n; ++i) 1 n+1 n+1


for(j = 0; j <= m; ++j) 1 n(m+2) nm+2n

for(i = 0; i < n; ++i) 1 n+1 n+1


for(j = 0; j <= m; ++j) 1 n(m+2) nm+2n
for(k = 1; k < p; ++k) 1 n[(m+1 nmp+np
)(p)] =
n(m+1)
p
If not sure about execution of a
statement
Statement Steps/Cost Frequency Total step cost

int num; 1 1 1
cin>>num; 1 1 1
if (num%2 1 1 1
== 0) 1 n+1 n+1
for(i = 1;
i <= n; +
+i)
Analysis
• num may or may not be divided on 2.
• So lets take average and we say there are 50% chances the condition will be true
• ½ (n+1) is the average frequency of execution of loop statement
• So total program steps on the average = 1+1+1+ ½ (n+1) = 2.5+ ½ n
Logarithmic time
complexity Steps/Cost
Statement Frequency Total step cost

for(i = n; i >= 1; n/=2) 1 (log2 n+1) + 1 log2 n + 2

Explanation Relationship between iteration# and denominator


Iteration # Value of i
1 n = n/1
Denominator = 2 (iteration# - 1)
2 n/2
3 n/4 For example,
8 = 2 (4 - 1) = 23
4 n/8
Similarly
…. …. n = 2 (x - 1) = 2x-1
log2 n +1 n/n = 1 (last iteration) To find x, take log on both sides
log2 n = log2 (2x-1 )
log2 n = x-1
x = log2 n + 1

You might also like