Algorithm Analiysis
Algorithm Analiysis
What is an Algorithm?
Algorithm is finite set of steps that must be followed to solve the any problem
An Algorithm is a well-defined computational procedure that takes a set of values as input and
transform them in to the desired output
Ex: - If we have some numbers as the input as we are expecting these number in a sorted order
as output that is our goal.
- And to achieve this goal we need to follow some
That is in this case if we give any set of numbers it will be sorted correctly
Incorrect
So the algorithm should produce the desired output on every input instance
There can be more than one algorithm to solve the same problem .
But What is important here is how efficiently algorithm solve the problem ?
for example here we have 4 Algorithms ..f or single problem… as follows for Example
1.Algorithm which takes one year time and utilizes 100 GB of memory
3.Third algorithm which takes one day time and utilizing 1 MB of memory
4.The fourth Algorithm which takes 1 second time and utilizes the 100kb of memory
So in our day-to-day life we all use Google Search engine and it takes our query as input and
it's efficient algorithm produces useful links as an output In just milliseconds
It will use lot of memory to each search query which caused a lot to
Google and you will and you will also get the search result in a very long time
As one would not follow all written instructions to cook the recipe, but only the standard
one. Similarly,
Not all written instructions for programming is an algorithms.
In order for some instructions to be an algorithm, it must have the following
characteristics:
Clear and Unambiguous: The algorithm should be clear and
unambiguous. Each of its steps should be clear in all aspects and
must lead to only one meaning.
Well-Defined Inputs: If an algorithm says to take inputs, it should be
well-defined inputs.
Well-Defined Outputs: The algorithm must clearly define what
output will be yielded and it should be well-defined as well.
Finite-ness: The algorithm must be finite, i.e. it should terminate after
a finite time.
Feasible: The algorithm must be simple, generic, and practical, such
that it can be executed with the available resources. It must not
contain some future technology or anything.
Language Independent: The Algorithm designed must be language-
independent, i.e. it must be just plain instructions that can be
implemented in any language, and yet the output will be the same, as
expected.
Properties of Algorithm:
It should terminate after a finite time.
It should produce at least one output.
It should take zero or more input.
It should be deterministic means giving the same output for the same input case.
Every step in the algorithm must be effective i.e. every step should do some work.
The problem that is to be solved by this algorithm i.e. clear problem definition.
o The constraints of the problem must be considered while solving the problem.
o The input to be taken to solve the problem.
o The output to be expected when the problem is solved.
o The solution to this problem, is within the given constraints.
Algorithm is finite set of steps that must be followed to solve the any problem
Let us learn How to write Algorithm
Algorithm is generally develop before the actual coding is done
we can write the algorithm step by step using simple English
Creating an Algorithm
Since the algorithm is language independent, we write the steps to demonstrate the logic
behind the solution to be used for solving a problem. But before writing an algorithm, keep the
following points in mind:
Step 1: Start
result to sum.sum←num1+num2
Step 5:
Display sum
Step 6: Stop
SEARCH EDUCATIONS Page 5
DATA STRUCTURE USING PYTHON CODE: 20CS41P
Step 1: Start
Step 4: If a > b
If a > c
number.Else
number.Else
If b > c
Step 5: Stop
Step 1: Read
amount.Step
2: Read
years.
-To go from city “Bangalore” to city “Mumbai”, there can be many ways of accomplishing
this: by flight, by bus, by train and also by bicycle.
Depending on the availability and convenience, we choose the one that suits us.
Similarly,
in computer science, multiple algorithms are available for solving the same problem
for example, a sorting problem has many algorithms, like insertion sort, selection sort,
quick sort and many more.
For a standard algorithm to be good, it must be efficient. Hence the efficiency of an algorithm
must be checked and maintained. It can be in two stages:
Priori Analysis: “Priori” means “before”. Hence Priori analysis means checking the
algorithm before its implementation. In this, the algorithm is checked when it is written
in the
form of theoretical steps.
This Efficiency of an algorithm is measured by assuming that all other factors, for
example, processor speed, are constant and have no effect on theimplementation.
This is done usually by the algorithm designer.
This analysis is independent of the type of hardware and language of the compiler.
It gives the approximate answers for the complexity of the program.
Processor that the machine that machine can have single core processor or multicore
processor
Execution time that the machine can execute instructions one by one sequentially or
execute multiple instructions parallel.
Memory access that is how fast machine can read and write data to or from the memory.
Then we analyze the time complexity of an algorithm we do not bother about all these
factors such as
Machine architecture, Processor, Execution time etc. because these are machine
dependent factors that may vary machine to machine..
Every machine is unique in its way. Some machine can have sequential execution or
some machines can have parallel execution and we cannot analyze the
algorithm for every individual machine
In General we analyze the time complexity of an algorithm based on the given input that
is for given how much time and space algorithm takes to produce the output.
Let us analyze the Time and Space complexity of swapping Algorithm with our
model machine firstlet us talk about the time complexity
So what is total time taken?
It is t of x equal to 1+1+1=3
That is the time complexity function t of x is equal to 3
This means whatever may be the value of x the t of x function will remain some
constant value
see this graph for every value of x the t of x t(x) value remains constant so we can
call this constant time algorithm so the time complexity of swap Algorithm is order
O(1)
Space Complexity:
b also takes one unit of memory then in the first statement we have
temp and it also takes oneunit of memory
see this graph for every value of x the t of x t(x) value remains constant so
It is the process of determining how processing time increases as the size of the problem
(input size) increases.
Input size is the number of elements in the input, and depending on the problem
type, the input may be of different types.
Size of an array
Polynomial degree
Number of elements in a matrix
Number of bits in the binary representation of the input
Vertices and edges in a graph.
Types of Analysis:
To analyze the given algorithm, we need to know with which inputs the algorithm takes
lesstime and with which inputs the algorithm takes a long time.
1. Worst case:
2. Best case:
Defines the input for which the algorithm takes the least time (fastest time to
complete).
3. Average case:
Asymptomatic notations :
For example:
In bubble sort, when the input array is already sorted, the time taken by thealgorithm is
linear i.e. the best case.
But, when the input array is in reverse condition, the algorithm takes the maximum time tosort
the elements i.e. the worst case
Asymptomatic notations:
There are 3 Asymptomatic Notations
Big-O Notation,
Omega Notation,
Theta Notation.
1. Big-O notation :
such that:f(n)=O(g(n))
Input size
when input size increases and the time growth also increases...
upper bound is why is because the value of f(n) it will be not greater than g(n)
functionhence it is called as upper bond.
That is value of f(n) is less than or equal to g(n) function.
In graph can notice the point where interact both lines ie.n0after n0
instance you can any input n value you will see that g(n) is greater than
f(n)
In Above program n0 The point from which we need to consider the rate of
growth for a given algorithm.
Below n0 , the rate of growth could be different. n0 is called threshold forthe given function.
2. Omega- Ω Notation:
Omega Represents Lower Bound of a Function Let us say our function is f(n) that its
size is grow as its input increases than what will be the lower bound of f(n)
we will find other function g(n) i.e. lower bound of function f(n) in such way that
c*g(n) is always lesser than f(n) for the all the values greater than equal to n0
if we find any such function we can say f(n) =omega(g(n)) so that brings us the
definition of omega notation f(n) >=c*g(n) for all n>=n0
3. Theta notation Ɵ
It Represents Average Bound we found upper bound of a function in Big O and also
found lower bound of a function in Omega Notation
Here we need find function that can be even upper and lower bound of a function
Definition: f(n)=theta(g(n))
if and only if
Theta notation Ɵ is more accurate use full In analyzing the Time complexity always
try to find the Average bound if you find the exact time function for the and represents
with M Theta notation Ɵif not we can go for Big oh or Omega notation
Is Big O for worst case, Theta for average case, Omega for best case? No
We often confuse with worst/Avg/Best Case we think that Big Oh represent Worst Case
i.e.
Upper Bond & Theta represents Average Bound i.e.
Average case Best Case representsOmega i.e. lower bound
This is not true we can use any notation for any case
Asymptotic notations are the simplest way to Time function it has nothing to do with
Best or Worstor Average case of algorithm
def display(self):
print("Student Details
are")
print("Name:",self.nam
e)
print("Roll Number:",self.rollno)
print("Marks of student
are",self.marks)
def
getmarks(self)
:return
self.marks
In this experiment Student with properties Name, Roll Number and three
subject marksare considered.
#Save this file as Student_ADT.py
# Student
ADTclass
Student:
def init (self,name,rollno,mark1,mark2,mark3):
self.name=name
self.rollno=rollno
self.marks=[]
self.marks.append(ma
rk1)
self.marks.append(ma
rk2)
s2.display()
studentmarks=s2.getmarks()
average=sum(studentmarks)/len(stu
dentmarks) print("Average Marks
of Student is",average)
s3.display()
studentmarks=s3.getmarks()
average=sum(studentmarks)/len(stu