CST201 M1 Ktunotes - in
CST201 M1 Ktunotes - in
MODULE I
Jacob P Cherian
Asst.Professor
Dept.of CSE, Saintgits College of Engineering
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Contents
System Life Cycle
Algorithms
Performance Analysis
Space Complexity
Time Complexity
Asymptotic Notation
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Algorithms- The Definition
3
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Criteria for an Algorithm
(i) input: there are zero or more quantities which are externally
supplied;
4
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Criteria for an Algorithm
5
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Real Life Example of an Algorithm
Preparing Tea
Preparing a Hamburger
Going to School/College
6
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Tea Recipe
1. Put the teabag in a cup.
8
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
A Real World Analysis
9
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Basic Idea of Time Complexity
If we solve a problem that’s ten times as large, how does the running
time change?
10
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
How to Analyze Time Complexity?
What are the factors which affect the time complexity of an algorithm?
input,
coding skill,
compiler,
T(n)
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
How to Analyze Time Complexity?
In general, an elementary operation must have two
properties:
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Fairness-Performance Analysis
TRIVANDRUM TO CHENNAI VIA MADURAI ROUTE A
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Types of Analysis(Time Complexity)
15
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Space Complexity
That means how much memory, in the worst case, is needed at any
point in the algorithm.
16
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Memory Requirement
Whenever a solution to a problem is written some memory is required to
complete. For any algorithm memory may be used for the following:
2. Program Instruction
3. Execution
17
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Memory Requirements
Data space is used to store data, variables, and constants which are
stored by the program and it is updated during execution.
18
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Space Complexity = Auxiliary Space
+
Input space (Data Space)
19
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Measuring Running Time
Algorithm Analysis
Asymptotic Recurrence
Step Count Operations Count Amortized Analysis
Analysis Relations
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Measuring Running Time
Algorithm Analysis
Asymptotic Recurrence
Step Count Operations Count
Relations
Amortized Analysis
Analysis
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Time Complexity:In-depth Analysis
Best Case
Worst Case
Average Case
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Best Case Complexity
Let T1(n), T2(n), ... be the execution times for all possible inputs of
size n.
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Worst Case Complexity
Let T1(n), T2(n), ... be the execution times for all possible inputs of
size n.
Click to View
Reference Article
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Average Case Complexity
Let T1(n), T2(n), … be the execution times for all possible inputs of
size n,
and let P1(n), P2(n), … be the probabilities of these inputs.
P1(n)T1(n) + P2(n)T2(n) + . . .
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understand the Loops
Let n = 3
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understand the Loops
n+1 times
sum++;
}
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understand the Loops
n+1 times n times
sum++;
}
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understand the Loops
n+1 times n times
Sum++; n times
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Example 1-Find Frequency Count
Ignore constants
Code Frequency
int sum=0; 1
for(i=0;i<n;i++) n+1
{ -----
sum++; n
} -----
TOTAL 2n+2
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Example 2
Algorithm sum(A,n) Frequency
{ -----
s=0 1
for(i=0;i<n;i++) n+1
{ -----
s=s+A[i] (n ) n
O
} -----
return s 1
} -----
for(i=0;i<n;i++)
Example 3
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j]
return s
Frequency Count
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Algorithm Array_Sum(A,B,n) Frequency
{ -----
for(i=0;i<n;i++) n+1
{ -----
Example 3
for(j=0;j<n;j++) n
{ -----
c[i][j]=a[i][j]+b[i][j] n
} -----
} -----
return s 1
} -----
Frequency Count
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Algorithm Array_Sum(A,B,n) Frequency
{ -----
for(i=0;i<n;i++) n+1
{ -----
Example 3
for(j=0;j<n;j++) n *(n+1)
{ -----
n ) 2
c[i][j]=a[i][j]+b[i][j] O( n *n
} -----
} -----
return s 1
} -----
Ignore
Frequency Count 2n2+2n+3 constants
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Homework Question-1
Find the time complexity
int fun(int n)
{
int i = 0, j = 0, k = 0, m = 0;
i = n;
for (i = 0; i<n; i++)
for (j = 0; j<n; j++)
m += 1;
for (i = 0; i<n; i++)
for (k = 0; k<n; k++)
m += 1;
return m;
}
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Homework Question-2
Find the time complexity
int fun(int n)
{
int i = 0, j = 0, k = 0, m = 0;
i = n;
for (i = 0; i<n; i++)
for (j = 0; j<n; j++)
for (k = 0; k<n; k++)
m += 1;
return m;
}
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Let’s Explore Linear Search
5 7 8 9 12 67 21 0 2 91
for(i=0;i<n;i++)
If a[i]=key
flag=1
break
return flag
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Frequency Count-Linear Search
Algorithm linear_search(A,key) Frequency
for(i=0;i<n;i++)
If a[i]=key
flag=1
break
return flag
Frequency Count
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Frequency Count-Linear Search
Algorithm linear_search(A,key) Frequency
for(i=0;i<n;i++) n+1
If a[i]=key n
flag=1 1
break 1
return flag 1
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Best Case,Worst Case & Average Case Time
Complexity-Linear Search
Best Case is when the first element of the array is the
element to be searched
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Linear Search-Time Complexity
WORST O(n)
CASE
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Let’s Play a Game
You have to guess the number in the fewer number of possible steps.
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Too
1 Low!
Linear Search
1 2 3 4 5 6 ... 100
Too
2 Low!
1 2 3 4 5 6 ... 100
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
5 Too
Linear Search
Low!
ARGGH
!
1 2 3 4 5 6 ... 100
6 Too
Low!
ARGGH
!
1 2 3 4 5 6 ... 100
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
A Better Way to Search
Too
50 Low!
Too
75 High!
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Too
63 High!
A Better Way
to Search
57 YES!
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
A Better Way to Search
Here’s how many numbers you can eliminate every time.
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Asymptotic Notations
Reference
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Basic Idea
We must focus on how fast a function grows with the input size. We call
this the rate of growth of the running time.
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Basic Idea
Source : https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/asymptotic-notation
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
It doesn't really matter what coefficients we use; as long as the running
time is an2+bn+c for some numbers a>0 ,b and c there will always be a
value n (say n0) for which an2 is greater than bn+c.
Basic Idea
Crossover
Point
Source : https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/asymptotic-notation
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Types of Asymptotic Notations
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Big-O Notation
Time
Input (n)
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Big O-The Definition
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
If a running time is O(f(n)),then for large enough n, the running time is
at most k.f(n) for some constant k.
k.f(n)
f(n)
Basic Idea
Time
Running time
Input (n)
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
What it tells?
Big O notation tells you how fast an algorithm is. For example, suppose you
have a list of size n. Simple search needs to check each element, so it will take
n operations. The run time in Big O notation is O(n).
Big O doesn’t tell you the speed in seconds. Big O notation lets you compare
the number of operations. It tells you how fast the algorithm grows.
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Big Omega Notation
We use big-Ω notation for
asymptotic lower bounds,
f(n)
since it bounds the growth of the
running time from below for large
g(n)
enough input sizes.
Time
Input (n)
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Big Omega Notation
The Big Omega(Ω) provides us with the best case scenario of running an
algorithm.
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Basic Idea
If a running time is
O(f(n)),then for large enough f(n)
n, the running time is at least
k.f(n) for some constant k. Running time
k.f(n)
Time
Input (n)
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Big Omega-The Definition
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Big Theta Notation
k1.f(n)
f(n)
When we use big-Θ notation,
we're saying that we have an e k2.f(n)
t im
n g
asymptotically tight bound on ni
Time
n
Ru
the running time.
Input (n)
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Big Theta -The Definition
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Common Asymptotic Notations
Constant O(1)
Logarithmic O(log n)
Linear O(n)
nlogn O(n.log n)
Quadratic O(n2)
Cubic O(n3)
Polynomial nO(1)
Exponential 2O(n)
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem 1
int fun(int n)
int m = 0;
m += 1;
return m;
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem 2
int fun(int n)
m += 1;
return m;
m += 1;
return m;
p += i;
return m;
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem 5
int fun(int n)
{
int i = 0, m = 0; i = n;
while (i > 0)
m += 1;
i = i / 2;
return m;
}
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem 6
int fun(int n)
int i = 0, j = 0, m = 0;
m += 1;
return m;
} Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem 7
int fun(int n)
int i = 0, j = 0, m = 0;
m += 1;
return m;
}
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering