0% found this document useful (0 votes)
24 views25 pages

Algorithm Analiysis

Uploaded by

HUSNA JABEEN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views25 pages

Algorithm Analiysis

Uploaded by

HUSNA JABEEN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

DATA STRUCTURE USING PYTHON CODE: 20CS41P

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

SEARCH EDUCATIONS Page 1


DATA STRUCTURE USING PYTHON CODE: 20CS41P

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

- "Set of procedure & this procedure is called an Algorithm"

- Algorithm may be correct and it may also be Incorrect

- An Algorithm is said to be correct if

- if it produces the desired out put on every input instance

That is in this case if we give any set of numbers it will be sorted correctly

Algorithm may be Incorrect

- if it produces undesired output on any input instance

- It is set to be an incorrect algorithm

Incorrect

 So the algorithm should produce the desired output on every input instance

 We can write the algorithm in simple English as step by step

instructionsor using a computer programme and also

 There can be more than one algorithm to solve the same problem .

 But What is important here is how efficiently algorithm solve the problem ?

Ex: Algorithm for SWAP numbers or exchanges Numbers

But what is important here is how efficiently it solves 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

 2.Algorithm which takes one month time and utilizes 1 GB of memory

SEARCH EDUCATIONS Page 2


DATA STRUCTURE USING PYTHON CODE: 20CS41P

 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

But what if the searching algorithm is in-efficient here, then

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

so will you use this kind of an application ? Definitely not Right ?

so it is important to write the correct algorithm but it is even moreimportant to


write it more efficiently

What are the Characteristics of an Algorithm?

SEARCH EDUCATIONS Page 3


DATA STRUCTURE USING PYTHON CODE: 20CS41P

 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.

How to Design an Algorithm?

In order to write an algorithm, the following things are needed as a pre-requisite:

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.

SEARCH EDUCATIONS Page 4


DATA STRUCTURE USING PYTHON CODE: 20CS41P

How to Write Algorithms :

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:

The algorithm should be clear and unambiguous.


There should be 0 or more well-defined inputs in an algorithm.
An algorithm must produce one or more well-defined outputs that are equivalent to
the desired output.
After a specific number of steps, algorithms must ground to a halt.
Algorithms must stop or end after a finite number of steps.
In an algorithm, step-by-step instructions should be supplied, and they should be
Independent of any computer code.
If we want swap two numbers we can write it in simple English …

Algorithm 1: Add two numbers entered by the user

Step 1: Start

Step 2: Declare variables num1,

num2 and sum.Step 3: Read values

num1 and num2.

Step 4: Add num1 and num2 and assign the

result to sum.sum←num1+num2

Step 5:

Display sum

Step 6: Stop
SEARCH EDUCATIONS Page 5
DATA STRUCTURE USING PYTHON CODE: 20CS41P

Algorithm 2: Find the largest number among three numbers

Step 1: Start

Step 2: Declare variables

a,b and c.Step 3: Read

variables a,b and c.

Step 4: If a > b

If a > c

Display a is the largest

number.Else

Display c is the largest

number.Else

If b > c

Step 5: Stop

Algorithm 3: Calculate the Interest of a Bank Deposit

Step 1: Read

amount.Step

2: Read

years.

Step 3: Read rate.

Step 4: Calculate the interest with the formula

"Interest=Amount*Years*Rate/100.Step 5: Print interest.

SEARCH EDUCATIONS Page 6


DATA STRUCTURE USING PYTHON CODE: 20CS41P

Why Analysis of Algorithms?

-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.

 Algorithm analysis helps us to determine which algorithm is most efficient in terms of


time and space consumed.

How to analyze an Algorithm?

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.

 Posterior Analysis: “Posterior” means “after”.


 Hence Posterior analysis means checking the algorithm after its implementation. In this,
 the algorithm is checked by implementing it in
 any programming language and executing it.
 This analysis helps to get the actual and real analysis report about correctness, space
required, time consumed etc.
 That is, it is dependent on the language of the compiler and the type of hardware used.

SEARCH EDUCATIONS Page 7


DATA STRUCTURE USING PYTHON CODE: 20CS41P

Goal of the Analysis of Algorithms :


- The goal of the analysis of algorithms is to compare algorithms (or solutions) mainly in
terms of running time but also in terms of other factors memory,
developer effort, etc.

How to Analyze Algorithms:

• Let us know how to analyze the Algorithm?


• Generally two major factors are used to analyze any algorithm
Time that is how much time an algorithm takes to produce the desired output called as
“Time Complexity”
Space (Memory) how much space the algorithm consumes to produce the desired output
called as “Space Complexity”

Time complexity: Of an algorithm depends on various factors. such as

 Machine architecture that is whether a machine is 32 bit or 64 bit.

 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.

 Input: what input we are giving to particular algorithm

SEARCH EDUCATIONS Page 8


DATA STRUCTURE USING PYTHON CODE: 20CS41P

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:

 In this swap function we have two parameters a and b

 let us a takes 1 unit of memory and

 b also takes one unit of memory then in the first statement we have
temp and it also takes oneunit of memory

 and we have 2 and 3 statements a=b and b=temp they already


take one unit of memory sothere is no issue

 so the total memory taken by the


algorithm is 1+1+1=3 that iss(x)=3 also
constant

SEARCH EDUCATIONS Page 9


DATA STRUCTURE USING PYTHON CODE: 20CS41P

see this graph for every value of x the t of x t(x) value remains constant so

The space complexity of swap Algorithm is order O(1)

What is Running Time Analysis?

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.

he following are the common types of inputs.

 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.

SEARCH EDUCATIONS Page 10


DATA STRUCTURE USING PYTHON CODE: 20CS41P

There are three types of analysis:

1. Worst case:

Defines the input for which the algorithm takes a


long time(slowest time to complete).

2. Best case:

Defines the input for which the algorithm takes the least time (fastest time to
complete).

3. Average case:

 Run the algorithm many times, using many different inputs


that come fromsome distribution that generates these
inputs, compute the total running
 time (by adding the individual times), and divide by the
number of trials.

Asymptomatic notations :

Asymptotic notations are the mathematical notations used to describe


the running time of an algorithm when give the input

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,

SEARCH EDUCATIONS Page 11


DATA STRUCTURE USING PYTHON CODE: 20CS41P

Omega Notation,

Theta Notation.

1. Big-O notation :

Big oh notation is used to describe an asymptotic upper bound.

Mathematically, if f(n) describes the running time of an algorithm;

f(n) is O(g(n)) if and only if there exist positive constants c and n°

such that:f(n)=O(g(n))

f(n) <= c*g(n) where n >= n0

Graphical notation of Big(O) notation

SEARCH EDUCATIONS Page 12


DATA STRUCTURE USING PYTHON CODE: 20CS41P

[Upper Bond]n= x axis -> represents the

Input size

T= y axis -> represents the time growth of the particular algorithm

when input size increases and the time growth also increases...

with respect to this we define upper bound function that is c.g(n)

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)

Graphical notation of Big(O) notation [Upper Bond]

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

SEARCH EDUCATIONS Page 13


DATA STRUCTURE USING PYTHON CODE: 20CS41P

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

c1*g(n)<=f(n)<=c2(g(n)) for all

n>=n0Among all three notations

1. Big oh - Upper Bound

2. Omega -Lower Bound

3. Theta - Average Bound

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

SEARCH EDUCATIONS Page 14


DATA STRUCTURE USING PYTHON CODE: 20CS41P

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

Time function can be any of time function

Best Case Time function

Worst Case Time Function

Average Case Time Function

SEARCH EDUCATIONS Page 15


DATASTRUCTURE USING WITH PYTHON CODE:20CS41P

SEARCH EDUCATIONS Page 1


DATASTRUCTURE USING WITH PYTHON CODE:20CS41P
Design a Data structure for handling Student Records- Designing a
Solution, Implementation
(Using ADT).
In this session, Student ADT is implemented, In this experiment Student
with properties Name, Roll Number and three subject marks are
considered. And in application average marks of each student is
calculated.
In python Student ADT is created with keyword class as
show below.class 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)
self.marks.append(ma
rk3)

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

SEARCH EDUCATIONS Page 2


DATASTRUCTURE USING WITH PYTHON CODE:20CS41P

getmarks(self)
:return
self.marks

in above python code constructor init (): is used to initialize Student


name, rollno, and marks list. display () function is used to display student
information and function getmarks () is used to return marks to calling
function.
Let us save above student ADT python code (Student_ADT.py) as a
module. Write another python code (Student_ADT_main.py) to import
Student_ADY module and calculate average marks of student.
Student_ADT_main.py is as given below.

from Student_ADT import *

#create student objects s1,s2,s3 s1=Student("Anil",1,20,25,30)


s2=Student("Sunil",2,25,20,35) s3=Student("Manu",3,30,35,40)
#display Student information and calculate average marks of student and
display the #same
s1.display() studentmarks=s1.getmarks()
average=sum(studentmarks)/len(studentmarks)
print("Average Marks of Student is",average)
s2.display() studentmarks=s2.getmarks()
average=sum(studentmarks)/len(studentmarks)
print("Average Marks of Student is",average)
s3.display() studentmarks=s3.getmarks()
average=sum(studentmarks)/len(studentmarks)
print("Average MarkS of Student is",average)

SEARCH EDUCATIONS Page 3


DATASTRUCTURE USING WITH PYTHON CODE:20CS41P

Output of a program is. Student Details are Name: Anil


Roll Number: 1
Marks of student are [20, 25, 30] Average Marks of Student is 25.0 Student
Details are
Name: Sunil Roll Number: 2
Marks of student are [25, 20, 35]
Average Marks of Student is 26.666666666666668 Student Details are
Name: Manu Roll Number: 3
Marks of student are [30, 35, 40] Average Marks of Student is 35.0

SEARCH EDUCATIONS Page 4


DATASTRUCTURE USING WITH PYTHON CODE:20CS41P
Practice -1: Implement an ADT and Compute space and time complexities
In week 2 practice session main aim is to compare space and time
complexities between Abstract Data Type and Array.
In practice-1 Student ADT is implemented which accepts student name,
rollno marks of three test of a student and to stores in the computer
memory. Student ADT also provides the following functionalities to the
developer
1.To retrieve the marks of the
student 2.To display the details
of the student
The Student ADT is used in the application program to store three
student’s details and access the marks of each student using ADT while
computing average marks student. We also calculate space, time
complexities for this application

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)

[Type text] Page 1


DATASTRUCTURE USING WITH PYTHON CODE:20CS41P
self.marks.append(ma
rk3)
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 above python code constructor
init (): is used to initialize Student
name, rollno, and marks list.
display () function is used to
display student information
function getmarks () is used to
return marks to calling function.
Let us save above student ADT
python code (Student_ADT.py) as
a module
Application program to store three
student details and compute
average.
Write another python code
(Student_ADT_main.py) to import
Student_ADY module and
calculate average marks of student.

[Type text] Page 2


DATASTRUCTURE USING WITH PYTHON CODE:20CS41P
Student_ADT_main.py is as given
below.
# Save this program as
Student_ADT_main.py from
Student_ADT import *
import time
from sys import getsizeof
start_time = time.time() #create
student objects s1,s2,s3
s1=Student("Anil",1,20,25,30)
s2=Student("Sunil",2,25,20,35)
s3=Student("Manu",3,30,35,40)
#display Student information and
calculate average marks of student
and display the #same
s1.display()
studentmarks=s1.getmarks()
average=sum(studentmarks)/len(stu
dentmarks) print("Average Marks
of Student is",average)

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

[Type text] Page 3


DATASTRUCTURE USING WITH PYTHON CODE:20CS41P
dentmarks) print("Average Marks
of Student is",average)
#calculate space and time
complexity of program
print("space taken by
program",getsizeof(s1)+getsizeof(s
2)+getsizeof(s3)) print("time taken
by program is",(time.time()-
start_time))
from above code to calculate space
complexity use below statement in
program.
from sys import getsizeof
to calculate time taken by program
to execute use below statement in
program.
import time
Finally output of a program is.
Student Details are
Name: Anil Roll Number: 1
Marks of student are [20, 25, 30]
Average Marks of Student is 25.0
Student Details are
Name: Sunil Roll Number: 2
Marks of student are [25, 20, 35]
Average Marks of Student is
26.666666666666668 Student
Details are
Name: Manu Roll Number: 3

[Type text] Page 4


DATASTRUCTURE USING WITH PYTHON CODE:20CS41P
Marks of student are [30, 35, 40]
Average Marks of Student is 35.0
space taken by program 144
time taken by program is
0.10921788215637207
The time and space taken by
Student_ADT_main.py is
0.10921788215637207 and 144
bytes.
Same application is implemented
using arrays in the next session.

[Type text] Page 5


DATASTRUCTURE USING WITH PYTHON CODE:20CS41P

[Type text] Page 6

You might also like