Data Structure Lecture 6
Data Structure Lecture 6
INTRODUCTION TO DATA
STRUCTURE
Lecture 6
Analysis of Algorithm
Lecture 6
What is Algorithm
An Algorithm is a sequence of steps to solve a
problem. Design and Analysis of Algorithm is very
important for designing algorithm to solve different
types of problems in the branch of computer
science and information technology.
What is Algorithm?
4
Algorithm
is any well-defined computational procedure that takes
some value, or set of values, as input and produces
some value, or set of values, as output.
is thus a sequence of computational steps that
transform the input into the output.
is a tool for solving a well - specified computational
problem.
Any special method of solving a certain kind of
problem .
Cont..
What is an algorithm?
An algorithm is a finite set of precise
Chapter 2 9
Example
How fast is a car traveling if it goes 50 miles in 2 hours?
1. Output:
a number giving the speed in miles
per hour
2. Input:
the distance and time the car has
traveled
3. Process:
speed = distance / time
Program Planning
Chapter 2 11
Program Planning Example
- A recipe
Chapter 2 12
What is a program?
13
Time complexity
How much time does it take to run the
algorithm
Space Complexity
Space complexity = The amount of memory required
by an algorithm to run to completion
[The most often encountered cause is “memory leaks” – the
amount of memory required larger than the memory
available on a given system]
Some algorithms may be more efficient if data
completely loaded into memory
Need to look also at system limitations
E.g. Classify 2GB of text in various categories [politics,
tourism, sport, natural disasters, etc.] – can I afford to load
the entire collection?
Space Complexity (cont’d)
1. Fixed part: The size required to store certain
data/variables, that is independent of the size of the
problem:
- e.g. name of the data collection
- same size for classifying 2GB or 1MB of texts
Value of function
function
eventually
becomes fB(n)=n2+1
larger...
Increasing n
Big-O Notation
Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N x N
O(n2)
Examples
i = 0;
while (i<N) {
X=X+Y; // O(1)
result = mystery(X); // O(N), just an example...
i++; // O(1)
}
The body of the while loop: O(N)
cn
N+1+n
Order of n O(n)
Linear time complexity
Informally this means that the time taken by
algorithm increases at most linear with the size of
the input.
In other words larger the input size greater the time
taken.
1<n<n2<n3<--------
Which is better?
Clearly algorithm B is better if our problem size small,
that is , if n<20.
Algorithm A is better for large problems, with n>20
So b is better for small problems.
But A is better for large problems.
We usually care the most about large problems
Analysis loops
For(i=1;i<=n;i++)
{
Cout<<I;
}
For(j=1;j<=n;j++)
{
Cout<<I;
}
Eg n+n---n 3n orfer of n O(n)
Loop analysis
For(i=1;i<=n;i++) Cost time
{
For(j=1;j<=n;j++) C1 N+1=n
C2 N+1=n
{
N
Cout<<i<<j; Cn (n)*(n)+n
=n^2
} Order of square O(n^2)
}
Loop analysis
For(i=1;i<=n;i++) Cost time
{
For(j=1;j<=n;j++)
C1 N+1=n
{ C2 N+1=n
For(j=1;j<=n;j++)
N
{ Cn (n)*(n) )*(n) +n
=n^3
Order of square O(n^2)
Cout<<i<<j;
}
}
}
Worst Case , Average Case and best case
45