You are on page 1of 36

DATA STRUCTURES AND ALGORITHM

University of Sialkot
What is Algorithm?
2

 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 (Webster Dictionary)
Analysis of Algorithm
Tuesday, September 21, 2021
ALGORITHMICS

It is the science that lets designers study and


evaluate the effect of algorithms based on various
factors so that the best algorithm is selected to meet
a particular task in given circumstances.
It is also the science that tells how to design a new
algorithm for a particular job.

Tuesday, September 21, 2021


Analysis of Algorithm
What is a program?
4

 A program is the expression of an algorithm in a


programming language
 a set of instructions which the computer will follow to
solve a problem

Analysis of Algorithm
Tuesday, September 21, 2021
Introduction
5

 Why need algorithm analysis ?


 writing a working program is not good enough
 The program may be inefficient!

 If the program is run on a large data set, then the


running time becomes an issue

Analysis of Algorithm
Tuesday, September 21, 2021
Where We're Going
6

 Learn general approaches to algorithm design


 Divide and conquer
 Greedy method
 Dynamic Programming
 Basic Search and Traversal Technique
 Graph Theory
 Linear Programming
 Approximation Algorithm
 NP (nondeterministic polynomial) Problem
Analysis of Algorithm Tuesday, September 21, 2021
What do we analyze about them?
7

 Correctness
 Does the input/output relation match algorithm
requirement?
 Amount of work done (Time complexity)
 Basic operations to do task finite amount of time
 Amount of space used
 Memory used

Analysis of Algorithm Tuesday, September 21, 2021


The Selection Problem
8

Which algorithm is better?

Analysis of Algorithm
Tuesday, September 21, 2021
Which algorithm is better?
The algorithms are correct,
but which is the best?
 Measure the running time
(number of operations
needed).
 Measure the amount of
memory used.
 Note that the running time
of the algorithms increase as
the size of the input
increases.

Analysis of Algorithm Tuesday, September 21, 2021 9


Running Time
 Most algorithms transform input best case
average case
objects into output objects. worst case
 The running time of an 120

algorithm typically grows with 100


the input size.

Running Time
80
 Average case time is often 60
difficult to determine.
40
 We focus on the worst case
20
running time.
0
 Easier to analyze 1000 2000 3000 4000
 Crucial to applications such as Input Size
games, finance and robotics

Tuesday, September
Analysis of Algorithm 10
21, 2021
Experimental Studies

 Write a program 9000

implementing the algorithm 8000

 Run the program with inputs 7000

of varying size and 6000

Time (ms)
composition 5000

 Use a method like 4000


System.currentTimeMillis() to 3000
get an accurate measure of 2000
the actual running time
1000
 Plot the results 0
0 50 100
Input Size
Tuesday, September
Analysis of Algorithm 11
21, 2021
Limitations of Experiments
12

 It is necessary to implement the algorithm, which


may be difficult
 Results may not be indicative of the running time on
other inputs not included in the experiment.
 In order to compare two algorithms, the same
hardware and software environments must be used

Analysis of Algorithm
Tuesday, September 21, 2021
Theoretical Analysis
13

 Uses a high-level description of the algorithm


instead of an implementation
 Characterizes running time as a function of the
input size, n.
 Takes into account all possible inputs
 Allows us to evaluate the speed of an algorithm
independent of the hardware/software
environment

Analysis
Tuesday, September 21, of Algorithm
2021
Asymptotic Analysis
14

 Asymptotic analysis of an algorithm refers to


defining the mathematical boundation/framing of
its run-time performance. Using asymptotic
analysis, we can very well conclude the best case,
average case, and worst case scenario of an
algorithm.
 Asymptotic analysis is input bound i.e., if there's no
input to the algorithm, it is concluded to work in a
constant time. Other than the "input" all other
factors are considered constant.

thanks to www.tutorialspoint.com
Asymptotic Analysis (Continued…)
15

 Asymptotic analysis refers to computing the running


time of any operation in mathematical units of
computation.
 For example, the running time of one operation is
computed as f(n) and may be for another
operation it is computed as g(n2). This means the
first operation running time will increase linearly
with the increase in n and the running time of the
second operation will increase exponentially
when n increases. Similarly, the running time of
both operations will be nearly the same if n is
significantly small.
Asymptotic Notations
16

 Following are the commonly used asymptotic


notations to calculate the running time
complexity of an algorithm.
 Ο (Big-O) Notation (Worst Time) (Upper Bound)
 Ω (Omega) Notation (Best Time) (Lower Bound)
 θ (Theta) Notation (Average Time) (Exact)
Big Oh, Notation, O
17

 The notation Ο(n) is the formal


way to express the upper
bound of an algorithm's running
time. It measures the worst case
time complexity or the longest
amount of time an algorithm
can possibly take to complete.
 g(n) = O(f(n)) if there are
positive constants c and n0 such
that f(n) ≤ cg(n) when n ≥ no.

thanks to www.tutorialspoint.com
Omega Notation, Ω
18

 The notation Ω(n) is the formal


way to express the lower bound
of an algorithm's running time.
It measures the best case time
complexity or the best amount
of time an algorithm can
possibly take to complete.
 g(n) = Ω(f(n)) if there are
positive constants c and n0 such
that g(n) ≤ cf(n) when n ≥ no.

thanks to www.tutorialspoint.com
Theta Notation, θ
19

 The notation θ(n) is the formal


way to express both the lower
bound and the upper bound of
an algorithm's running time. It is
represented as follows.
 g(n) = θ(f(n)) if and only if g(n)
= O(f(n)) and g(n) = Ω(f(n))

thanks to www.tutorialspoint.com
Big-Oh Notation
20

 To simplify the running time estimation,


for a function f(n), we ignore the constants and
lower order terms.

Example: 10n3+4n2-4n+5 is O(n3).

Analysis of Algorithm
Tuesday, September 21, 2021
Theta Notation, θ
21

 Θ Notation: The theta notation bounds a function from


above and below, so it defines exact asymptotic
behavior.
A simple way to get Theta notation of an expression is
to drop low order terms and ignore leading constants.
For example, consider the following expression.
3n3 + 6n2 + 6000 = Θ(n3)
Dropping lower order terms is always fine because
there will always be a number(n) after which Θ(n3) has
higher values than Θ(n2) irrespective of the constants
involved.
Example of Asymptotic Notations
22
Counting Primitive Operations
 By inspecting the pseudo code, we can determine the maximum
number of primitive/basic operations executed by an
algorithm, as a function of the input size

Algorithm arrayMax(A, n) # operations


currentMax  A[0] 2
for (i =1; i<n; i++) c1+c2n
(i=1 once, i<n n times, i++ (n-1)
times:post increment)
if A[i]  currentMax then n
currentMax  A[i] n
return currentMax 1

Analysis of Algorithm Tuesday, September 21, 2021 23


Counting Primitive Operations
24

 Ex. Find the maximum element in a list


 Algorithm findMax(A, n)
Input list A of n integers
Output the maximum element of A
 currentMax ← A[0]
for currentValue ← nextElementInA (starting from
the 1st element in A) to EndOfA do
if currentValue > currentMax then
currentMax ← currentValue
return currentMax
Counting Primitive Operations
25

 Line 1, currentMax ← A[0], is 2 operations since we're


accessing a single element in a list by index and
assigning a value to a variable.
 Line 2, for currentValue ← nextElementInA (starting from
the 1st element in A) to End Of A , is c1​+c2​n operations
where c1 represents the constant number of primitive
operations associated with the initializing and the
terminating of the for loop, and c2 represents the
number of primitive operations associated with the
maintenance of the iterator which is done n times, so the
total amount of maintenance of the iterator c2​n
Counting Primitive Operations
26

 Line 3, if currentValue > currentMax then, is n operations


since we're comparing two numbers during each
iteration of the loop.
 Line 4, currentMax ← currentValue, consists
of n operations, since we're assuming worst-case input
which means currentMax will be updated on each
iteration of the loop.
 Line 5, return currentMax, consists of 1 operation since
we're only returning a value from a function.
 Therefore, the running time of the algorithm is:
t=2+c1​+c2​n+n+n+1=3+c1​+c2​n+2n
Function of Growth rate
27

Analysis of Algorithm
Tuesday, September 21, 2021
Common Asymptotic Notations
28

constant − Ο(1)
logarithmic − Ο(log n)
linear − Ο(n)
n log n − Ο(n log n)
2
quadratic − Ο(n )
3
cubic − Ο(n )
Ο(1)
polynomial − n
Ο(n)
exponential − 2
Comparing Growth Rates
29
Graph of Various Algorithms
30
Graph of Various Algorithms
31
Example
32

 Sum all of the elements in a list


 Algorithm calculateSum(A)
Input list A of n integers
Output sum of all the elements of A
 currentSum ← 0
for valueToBeAdded ← nextElementInA (starting
from the 1st element in A) to EndOfA
currentSum ← currentSum + valueToBeAdded
return currentSum
Explanation
33

 Line 1, currentSum ← 0, is 1 operation since we're


assigning a value to a variable.
 Line 2, for valueToBeAdded ← nextElementInA (starting
from the 1st element in A) to EndOfA , is once
again c1​+c2​n operations where c1​ once again
represents the constant number of primitive operations
associated with the initializing and the terminating of
the for loop, and c2​ once again represents the number
of primitive operations associated with the maintenance
of the iterator which is done n times, so the total amount
of maintenance of the iterator is c2​n.
Explanation
34

 Line 3, currentSum ← currentSum + valueToBeAdded,


is 2n operations since we're performing an
arithmetic operation and assigning the result to a
variable during each iteration of the loop.
 Line 4, return currentSum, consists of 1 operation
since we're only returning a value from a function.
 Therefore, the running time of the algorithm is:
t=1+c1​+c2​n+2n+1=2+c1​+c2​n+2n
Home Work
35

 Find how many elements in a list are even


 Algorithm findNumberOfEvenElements(A)
Input list A of n integers
Output the number of elements in list A that are
even
 numberOfEvenElements ← 0
for currentValue ← nextElementInA (starting from the
1st element in A) to EndOfA do
if currentValue mod 2 = 0 then
numberOfEvenElements ← numberOfEvenElem
ents + 1
return numberOfEvenElements
Assignment 1
36

 Write the details of following programming languages.


 Java
 C++

 C# (C Sharp)

 PHP

 CSS/HTML

 Detail of programming language.


 Advantages and Disadvantages
 Types of Application.

You might also like