Professional Documents
Culture Documents
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
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
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
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
12
Worst Case
13
Average Case
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
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
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
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
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
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
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
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) }
}
Technically, f(n) (g(n)).
Older usage, f(n) = (g(n)).
I’ll accept either…
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