You are on page 1of 39

Unit 1

Syllabus
• Introduction to Data Structures: Concept of data, Data
object, Data structure, Concept of Primitive and non-primitive,
linear and Nonlinear, static and dynamic, persistent and
ephemeral data structures, Definition of ADT
• Analysis of algorithm: Frequency count and its importance in
analysis of an algorithm, Time complexity & Space
complexity of an algorithm Big 'O', ‘Ω' and 'Θ' notations,
• Sequential Organization: Single and multidimensional array
and address calculation.
• Linked Organization: Concept of linked organization, Singly
Linked List, Doubly Linked List, Circular Linked List
(Operations: Create, Display, Search, Insert, Delete).
Analysis of Algorithms

Input Algorithm Output

An algorithm is a step-by-step procedure for solving a problem in a finite


amount of time.
3
Algorithm

• An Algorithm is a finite set of instructions that, if followed,


accomplishes a particular task.
• All algorithms must satisfy the following criteria:
– Input : Zero or more quantities are externally supplied.
– Output : At least one quantity is produced.
– Definiteness : Each instruction is clear and unambiguous.
– Finiteness : If we trace out the instructions of an
algorithm, then for all cases, the algorithm terminates
after a finite number of steps.
– Effectiveness : Every instruction must be very basic and
effective. It is not enough that each operation be definite
as in criterion 3; it also must be feasible.

4
Uses of Algorithm
• It gives a language independent layout of the
program.
• So using the basic layout we can develop the
program in any desired language.
• Algorithm representation is very easy to understand.
• It facilitates or makes coding easy.

5
Example of Algorithm
Develop an algorithm to find maximum of 10 numbers.
•Start
•Let count = 1
•Read a number, say x
•Let max = x
•Check if count is equal to 10 , if yes go to Step 10
•Read a number, say x
•Add 1 to count
•Check if x is greater than max, if yes Set max = x
•go to step 5
•Display max
•Stop. 6
What is analysis

• Analysis of an algorithm provides background


information that gives us a general idea of how long
an algorithm will take for a given problem set.
• Studying the analysis of algorithms gives us the tools to
choose between algorithms.

7
Analysis of Algorithms

• Efficiency measure
– how long the program runs time complexity
– how much memory it uses space complexity
• Why analyze at all?
– Decide what algorithm to implement before actually
doing it

8
Input classes

• Input plays an important role in analyzing algorithms


because it is the input that determines what the path of
execution through an algorithm will be.
• When looking at the input, we will try to break up all the
different input sets into classes based on how the
algorithm behaves on each set.

9
Cases to consider
• Choosing what input to consider when analyzing an
algorithm can have a significant impact on how an
algorithm will perform.
• We will actually look for those input set that allow an
algorithm to perform the most quickly and most
slowly.
• We will also consider an overall average performance
of the algorithm as well.

10
Example
1 2 3 4 5 6 7 8 9 10

eg,.Linear search

For search no. 10


Worst case:10 Comparisions required

For search no.1


Best case:1 Comparisions required

For search no. 5


Average case:5 Comparisions required 11
Best Case
• Is the input that requires the algorithm to take the
shortest time.
• This input is the combination of values that causes the
algorithm to do the least amount of work.
• Because the best case for an algorithm will usually be a
very small and frequently constant value, we will not do
a best-case analysis very frequently.

12
Worst Case

• Important analysis because it gives us an idea of the


most time an algorithm will ever take.
• Worst case analysis requires that we identify the input
values that cause an algorithm to do the most work.
• The worst case gives us an upper bound on how slowly
parts of our programs may work based on our algorithm
choices.

13
Average Case

• The basic process begins by determining the


number of different groups into which all possible
input sets can be divided.
• The second step is to determine the probability that
the input will come from each of these groups.
• The third step is to determine how long the algorithm
will run for each of these groups.

14
Frequency count
• Total Computational time for an instruction = Instruction
execution time * Frequency count.
• Now instruction execution time depends upon
– speed of processor (instruction per millisecond)
– instruction set of the processor
– time required to execute instruction
– compiler for translating the instruction into machine
language.
• All these factors independent of the algorithm and external
to it. Hence performance of an algorithm (time complexity) is
measured in terms of Frequency count. Hence only is
important in deciding the performance (time complexity) of
an algorithm.

15
Stmt Statement s/e Frequency Total Steps
. No

1 Algorithm Sum(a,n) 0 -- 0

2 { 0 -- 0

3 s = 0; 1 1 1

4 for i = 1 to n do 1 n+1 n+1

5 s = s + a[i]; 1 n n

6 return s; 1 1 1

7 } 0 -- 0

Total 2n + 3
16
Stm Statement s/e Frequency Total Steps
t
.
N
o

1 Algorithm Add(a,b,c,m,n) 0 -- 0

2 { 0 -- 0

3 for i = 1 to m do 1 m+1 m+1

4 for j = 1 to n do 1 m(n+1) mn+m

5 c[i,j] = a[i,j] + b[i,j]; 1 mn mn

6 } 0 -- 0

Total 2mn + 2m +
1

17
Performance Analysis
There are many criteria upon which we can judge an
algorithm. For instance:
• Does it do what we want to do?
• Does it work correctly according to the original
specifications of the task?
• Is there documentation that describes how to use it and
how it works?
• Are procedures created in such a way that they perform
logical sub functions?
• Is the code readable?
• There are other criteria for judging algorithms that have
a more direct relationship to performance . These have
to do with their computing time and storage
requirements.

18
Performance Analysis
• Space Complexity :
• The space complexity of an algorithm is the
amount of memory it needs to run to completion.
• Time Complexity :
• The time complexity of an algorithm is the
amount of computer time it needs to run to
completion.
• Performance evaluation can be loosely divided into
two major phases :
– A Priori estimates and
– A posteriori testing
• We refer to these as performance analysis and
performance measurement respectively.

19
Space Complexity
• A fixed part that is independent of the characteristics
(e.g. number,size) of the inputs and outputs. This part
typically includes the instruction space (i.e. space for the
code), space for simple variables and fixed-size
component variables (also called aggregate), space for
constants, and so on…
• A variable part that consists of the space needed by
component variables whose size is dependent on the
particular problem instance being solved, the space
needed by referenced variables (to the extent that this
depends on instance characteristics), and the recursion
stack space.
• The space requirement S(P) of any algorithm P may
therefore be written as S(P) = c + Sp(instance
characteristics), where c is a constant.
20
Time Complexity

• The time T(P) taken by a program P is the sum of the


compile time and the run (or execution) time.
• The compile time does not depend on the instance
characteristics. Also, we may assume that a compiled program
will be run several times without recompilation.
• Consequently, we concern ourselves with just the run time of
a program. This runtime is denoted by tp(instance
characteristics).

21
Asymptotic Notations
• The main idea of asymptotic analysis is to have a measure of
efficiency of algorithms that doesn’t depend on machine
specific constants, and doesn’t require algorithms to be
implemented and time taken by programs to be compare.
• Algorithms perform f(n) basic operations to accomplish task
– Identify that function
– Identify size of problem (n)
– Count number of operations in terms of n

22
Execution time
• Time computer takes to execute f(n)
operations is cf(n)
• where c
– depends on speed of computer and
– varies from computer to computer

23
Development of Notation

• Not concerned with small values of n


• Concerned with VERY LARGE values of n
• Asymptotic – refers to study of function f as
n approaches infinity

• Example: f(n) = n2+4n + 20


n2 is the dominant term and the term 4n +
20 becomes insignificant as n grows larger
24
Asymptotic Algorithm Analysis
• That means Big - Oh notation always indicates the maximum
time required by an algorithm for all input values(worst case).
• The asymptotic analysis of an algorithm determines the running
time in big-Oh notation
• To perform the asymptotic analysis
– We find the worst-case number of primitive operations executed as a
function of the input size
– We express this function with big-Oh notation
• Example:
– We determine that algorithm arrayMax executes at most 8n  2
primitive operations
– We say that algorithm arrayMax “runs in O(n) time”
• Since constant factors and lower-order terms are eventually
dropped anyhow, we can disregard them when counting primitive
operations
25
More Big-Oh Examples
7n-2
7n-2 is O(n)

 3n3 + 20n2 + 5
3n3 + 20n2 + 5 is O(n3)

 3 log n + 5
3 log n + 5 is O(log n)

26
Big O notation

• Big O notation is used in Computer Science to describe the


performance or complexity of an algorithm.
• Big O specifically describes the worst-case scenario, and can
be used to describe the execution time required or the space
used (e.g. in memory or on disk) by an algorithm.
1. O(1)
• O(1) describes an algorithm that will always execute in the
same time (or space) regardless of the size of the input data
set.
• eg. Find odd or even number

27
Big O notation
2.O(N)
• O(N) describes an algorithm whose performance will grow
linearly and in direct proportion to the size of the input data set
• Eg.Sum of 1D array elemenets

3.O(N2)
• O(N2) represents an algorithm whose performance is directly
proportional to the square of the size of the input data set.
This is common with algorithms that involve nested iterations
over the data set. Deeper nested iterations will result in O(N3),
O(N4) etc.
• Eg.Sum of 2D array elemenets

28
Big O notation
4. O(2N)
• O(2N) denotes an algorithm whose growth doubles with each
additon to the input data set. The growth curve of an O(2N)
function is exponential. An example of an O(2N) function is the
eg.recursive calculation of Factorial numbers:
5.Logarithms
• Binary search is a technique used to search sorted data sets.
It works by selecting the middle element of the data set,
essentially the median, and compares it against a target value.
If the values match it will return success. If the target value is
higher than the value of the probe element it will take the
upper half of the data set and perform the same operation
against it. his type of algorithm is described as O(log N).
29
Development of Notation

• Drop insignificant terms and constants


• Say function is of O(n2) called Big-O of n2

• Common Big-O functions in algorithm analysis


– g(n) = 1 (growth is constant)
– g(n) = log 2 n (growth is logarithmic)
– g(n) = n (growth is linear)
– g(n) = n log 2 n (growth is faster than linear)
– g(n) = n2 (growth is quadratic)
– g(n) = 2n (growth is exponential) 30
n log2n nlog2n n^2 2^n

1 0 0 1 2

2 1 2 4 4

4 2 8 16 16

8 3 24 64 256

16 4 64 256 65536

32 5 160 1024 4294967296


31
Common Growth Functions
(How f(n) grows as n grows)

1200

1100

1000

900

800
n
700
log2n
600 nlog2n
n^2
500
2^n
400

300

200

100

0
1 2 3 4 5 6

32
O-notation
For function g(n), we define O(g(n)),
big-O of n, as the set:
O(g(n)) = {f(n) :
positive constants c and n0, such
that n  n0,
we have 0  f(n)  cg(n) }

Intuitively: Set of all functions whose rate of


growth is the same as or lower than that of
g(n).
g(n) is an asymptotic upper bound for f(n).
f(n) = (g(n))  f(n) = O(g(n)).
(g(n))  O(g(n)).
33
 -notation
For function g(n), we define (g(n)),
big-Omega of n, as the set:
(g(n)) = {f(n) :
positive constants c and n0, such
that n  n0,
we have 0  cg(n)  f(n)}
Intuitively: Set of all functions whose rate of
growth is the same as or higher than that of
g(n).

g(n) is an asymptotic lower bound for f(n).


f(n) = (g(n))  f(n) = (g(n)).
(g(n))  (g(n)).
34
-notation
For function g(n), we define (g(n)),
big-Theta of n, as the set:
(g(n)) = {f(n) :
 positive constants c1, c2, and n0,
such that n  n0,
we have 0  c1g(n)  f(n)  c2g(n)

}
Technically, f(n)  (g(n)).
Older usage, f(n) = (g(n)).
I’ll accept either…

f(n) and g(n) are nonnegative, for large n.


35
Relations Between Q, O, W

36
Asymptotic Notation
Big-Oh
 f(n) is O(g(n)) if f(n) is asymptotically less than or equal to

g(n)
big-Omega
 f(n) is (g(n)) if f(n) is asymptotically greater than or equal

to g(n)
big-Theta
 f(n) is (g(n)) if f(n) is asymptotically equal to g(n)

37
Example
1 2 3 4 5 6 7 8 9 10

eg,.Linear search

For search no. 10


O(n)

For search no.1


(1)

For search no. 5


(n/2) 38
#include <stdio.h> int factorial(int num)
int factorial(int); {

int main(){ if (num == 0 || num == 1)


int num; int result; return 1;
cout<<“Enter a number to find it's Factorial: “;
else
Cin>>num; return(num * factorial(num - 1));
if (num < 0) }
{
cout<<“Factorial of negative number not possible\
n";
}
else
{
result = factorial(num);
cout<<“The Factorial of “<<num<<“is”<<result;
}
return 0;
} 39

You might also like