SlideShare a Scribd company logo
Data Structures & Algorithms
Lecturer: Dr. Muhammad M Iqbal
Ph.D (DCU), M.Phil (GCU)
1
Overview
– The concept of algorithm analysis
– Big-Oh notation
– Experimental analysis of algorithms in Java
– Comparing various growth functions
– Efficiency goals
AlgorithmInput Output
2
Computational and
Asymptotic Complexity
• Computational Complexity measures the degree of
difficulty of an algorithm
• Indicates how much effort is needed to apply an
algorithm or how costly it is
• To evaluate an algorithm’s efficiency, use logical units
that express a relationship such as:
– The size n of a file or an array
– The amount of time t required to process the data 3
Analysis of Algorithms
• Method 1:
• Transform the algorithm into some
programming language and measure the
execution time for a set of input values.
• Method 2:
• Use the Big O notation (mathematical
notation) to analyse the behaviour of the
algorithms. (Independent of software and
hardware).
4
• Let us consider two algorithms to solve a problem:
– First algorithm (X) spends 100 * x time units
– Second algorithm (Y) spends 5000 time units
• Which one is better?
– Algorithm X is better if our problem size is small, that is, if x < 50
– Algorithm Y is better for larger problems, with x > 50
• A challenge is to handle the large size of the problems. 5
Complexity of Algorithms
• To understand the performance of an algorithm, it is best to
observe how well or poorly it does for different size of inputs.
• This indicates that the selection of the algorithm for a particular
problem demands analysis based on the input size.
Algorithm Analysis
Consider the problem of summing
FIGURE: Three algorithms for computing the sum
1 + 2 + . . . + n for an integer n > 0 6
TimedAlgorithms.java
Running Time
• Most algorithms transform input
objects into output objects.
• The running time of an algorithm
typically grows with the input size.
• Average case time is often difficult
to determine.
• We focus on the worst case running
time.
– Easier to analyze
– Crucial to applications such as games,
finance and robotics
0
20
40
60
80
100
120
RunningTime 1000 2000 3000 4000
Input Size
best case
average case
worst case
7
Algorithm Efficiency
• The efficiency of an algorithm is usually expressed in
terms of its use of CPU time.
• The analysis of algorithms involves categorizing an
algorithm in terms of efficiency.
• An everyday example: washing dishes.
• Suppose washing a dish takes 30 seconds and drying a
dish takes an additional 30 seconds.
• Therefore, n dishes require n minutes to wash and dry.
8
Algorithm Efficiency
• Now consider a less efficient approach that
requires us to redry all previously washed
dishes after washing another one
seconds4515
2
)1(30
30dishes)(time
)30*()wash timeseconds30(*
2
n
1i
nn
nn
nn
in



 
9
Problem Size
• For every algorithm we want to analyze,
we need to define the size of the problem
• The dishwashing problem has a size n –
number of dishes to be washed/dried
• For a search algorithm, the size of the
problem is the size of the search pool
• For a sorting algorithm, the size of the
program is the number of elements to be
sorted
10
Growth Functions
• We must also decide what we are trying to
efficiently optimize
i. time complexity – CPU time
ii. space complexity – memory space
• CPU time is generally the focus
• A growth function shows the relationship
between the size of the problem (n) and the
value optimized (time)
11
Asymptotic Complexity
• The growth function of the second dishwashing algorithm is
t(n) = 15n2 + 45n
• It is not typically necessary to know the exact growth
function for an algorithm
• We are mainly interested in the asymptotic complexity of an
algorithm – the general nature of the algorithm as n
increases
• Asymptotic complexity helps to explore the performance of
the algorithms 12
Asymptotic Complexity
• Asymptotic complexity is
based on the dominant
term of the growth
function – the term that
increases most quickly as
n increases
• The dominant term for
the second dishwashing
algorithm is n2:
13
t(n) = 15n2 + 45n
14
Computational and
Asymptotic Complexity (continued)
Figure 2-1 The growth rate of all terms of function
f (n) = n2 + 100n + log10n + 1,000
Big-Oh Notation
• The coefficients and the lower order terms
become increasingly less relevant as n
increases
• So we say that the algorithm is order n2,
which is written O(n2)
• This is called Big-Oh notation
• There are various Big-Oh categories
• Two algorithms in the same category are
generally considered to have the same
efficiency, but that doesn't mean they have
equal growth functions or behave exactly
the same for all values of n 15
Big-Oh
Categories
• Some sample
growth functions
and their Big-Oh
categories:
• As n increases, the
various growth
functions diverge
dramatically:
16
Picturing Efficiency
FIGURE:
An O(n)
algorithm
FIGURE
An O(n2)
algorithm
17
Analyzing Loop Execution
• First determine the order of the body of the
loop, then multiply that by the number of times
the loop will execute
for (int count = 0; count < n; count++)
// some sequence of O(1) steps
• N loop executions times O(1) operations
results in a O(n) efficiency
18
Analyzing Loop Execution
• Consider the following loop:
count = 1;
while (count < n)
{
count *= 2;
// some sequence of O(1) steps
}
• The loop is executed log2n times, so the loop is
O(log n)
19
Analyzing Nested Loops
• When the loops are nested, we multiply the complexity of the outer loop
by the complexity of the inner loop
for (int count = 0; count < n; count++)
for (int count2 = 0; count2 < n; count2++)
{
// some sequence of O(1) steps
}
• Both the inner and outer loops have complexity of O(n)
• The overall efficiency is O(n2)
20
• The body of a loop may contain a call to a method
• To determine the order of the loop body, the order of the method must be
taken into account
• The overhead of the method call itself is generally ignored
Experimental Studies
• Write a program
implementing the algorithm
• Run the program with inputs
of varying size and
composition, noting the time
needed:
• Plot the results 0
1000
2000
3000
4000
5000
6000
7000
8000
9000
0 50 100
Input Size
Time(ms)
21
Limitations of Experiments
• 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
22
Theoretical Analysis
• 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
23
Comparison of Two Algorithms
Insertion sort is
n2 / 4
Merge sort is
2 n lg n
sort a million items?
insertion sort takes
roughly 70 hours
while
merge sort takes
roughly 40 seconds
This is a slow machine, but if
100 x as fast then it’s 40 minutes
versus less than 0.5 seconds24
More Big-Oh Examples
 7n - 2
7n-2 is O(n)
need c > 0 and n0  1 such that 7 n - 2  c n for n  n0
this is true for c = 7 and n0 = 1
 3 n3 + 20 n2 + 5
3 n3 + 20 n2 + 5 is O(n3)
need c > 0 and n0  1 such that 3 n3 + 20 n2 + 5  c n3 for n  n0
this is true for c = 4 and n0 = 21
 3 log n + 5
3 log n + 5 is O(log n)
need c > 0 and n0  1 such that 3 log n + 5  c log n for n  n0
this is true for c = 8 and n0 = 2
25
Prefix Averages 2 (Linear)
The following algorithm uses a running summation to improve
the efficiency
Algorithm prefixAverage2 runs in O(n) time! 26
Experimental Study
• A simple mechanism for collecting such running times in Java is based on use
of the currentTimeMillis method of the System class.
long startTime = System.currentTimeMillis( ); // record the starting time
/* (run the algorithm) */
long endTime = System.currentTimeMillis( ); // record the ending time
long elapsed = endTime − startTime; // compute the elapsed time
27
StringExperiment.java
Experimental Studies
• A simple mechanism for collecting such running times in Java is
based on use of the currentTimeMillis method of the System
class.
StringExperiment.zip
n
50000
repeat1
1607
repeat2
3
100000 5074 4
200000 11727 5
400000 37939 12
800000 152135 12
1600000 648259 16
28
• To analyze the algorithms effectively, a general methodology for analysis
of the running time of algorithms (theoretical analysis)
• In contrast to the "experimental approach", this methodology:
– Uses a high-level description of the algorithm instead of testing one
of its implementations.
– Characterizes running time as a function of the input size, n.
– Takes into account all possible inputs.
– Allows one to evaluate the efficiency of any algorithm in a way that is
independent from the hardware and software environment.
Theoretical Analysis
29
Performance Analysis of the
Algorithms
• In order to analyse the algorithms
i. We should calculate the time using some formula based on the input
size.
ii. We should find a method to determine the memory requirement based
on the input size.
• We can choose the “size of input” to be the parameter that most
influences the actual time/space required
– It is usually obvious what this parameter is
• e.g., number of elements in the array you want to sort
– Sometimes we need two or more parameters
• However, the time performs a major role in all evaluations. 30
Time and Space
Big-Oh Notation
BETTER
WORSE
order 1
order n
order n squared
31
Three-Way Set Disjointness
Suppose we are given three sets, A, B, and C, stored in
three different integer arrays. We will assume that no
individual set contains duplicate values, but that there
may be some numbers that are in two or three of the
sets. The three-way set disjointness problem is to
determine if the intersection of the three sets is empty,
namely, that there is no element x such that x ∈ A, x ∈ B,
and x ∈ C. 32
Three-Way Set Disjointness
Worst-case
running time of
disjoint1 is O(n3).
Worst-case
running time for
disjoint2 is O(n2).
33
Testing.java
Big-Oh Notation
Statements with method calls
• When a statement involves a method call, the complexity of the
statement includes the complexity of the method call.
f(k); // O(1)
g(k); // O(N)
• When a loop is involved, the same rule applies
for (j = 0; j < N; j++)
g(N);
– It has complexity (N2).
– The loop executes N times
– Each method call g(N) is of complexity O(N). 34
Question
• A program’s execution time depends in part on
– the speed of the computer it is running on (time)
– the memory capacity
– the language the algorithm is written in
– all of the above
35
Resources/ References
• Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson,
Ronald L. Rivest and Clifford Stein 3rd, 2009, MIT Press and
McGraw-Hill.
• Data Structures: Abstraction and Design Using Java, Elliott B.
Koffmann, 2nd, 2010, Wiley.
• Data Structures and algorithm analysis in java, Mark A. Weiss, 3rd,
2011, Prentice Hall.
• Frank M. Carrano, Data Structures and Abstractions with Java™, 3rd
Edition, Prentice Hall, PEARSON, ISBN-10: 0-13-610091-0.
• This lecture used some images and videos from the Google search
repository to raise the understanding level of students.
https://www.google.ie/search?
Dr. Muhammad M Iqbal*

More Related Content

What's hot

Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
Afaq Mansoor Khan
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
Big o notation
Big o notationBig o notation
Big o notation
hamza mushtaq
 
DATA STRUCTURE AND ALGORITHM FULL NOTES
DATA STRUCTURE AND ALGORITHM FULL NOTESDATA STRUCTURE AND ALGORITHM FULL NOTES
DATA STRUCTURE AND ALGORITHM FULL NOTES
Aniruddha Paul
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
Mary Margarat
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithms
Sumathi MathanMohan
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
Afaq Mansoor Khan
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
university of Gujrat, pakistan
 
Algorithem complexity in data sructure
Algorithem complexity in data sructureAlgorithem complexity in data sructure
Algorithem complexity in data sructureKumar
 
Applications of data structures
Applications of data structuresApplications of data structures
Applications of data structures
Wipro
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
Hanif Durad
 
Introduction to Data Structures & Algorithms
Introduction to Data Structures & AlgorithmsIntroduction to Data Structures & Algorithms
Introduction to Data Structures & Algorithms
Afaq Mansoor Khan
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
jehan1987
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structureeShikshak
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Afaq Mansoor Khan
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
Ashim Lamichhane
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
Nguync91368
 
Cupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithmCupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithm
TarikuDabala1
 

What's hot (20)

Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Big o notation
Big o notationBig o notation
Big o notation
 
DATA STRUCTURE AND ALGORITHM FULL NOTES
DATA STRUCTURE AND ALGORITHM FULL NOTESDATA STRUCTURE AND ALGORITHM FULL NOTES
DATA STRUCTURE AND ALGORITHM FULL NOTES
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithms
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Algorithem complexity in data sructure
Algorithem complexity in data sructureAlgorithem complexity in data sructure
Algorithem complexity in data sructure
 
Applications of data structures
Applications of data structuresApplications of data structures
Applications of data structures
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Introduction to Data Structures & Algorithms
Introduction to Data Structures & AlgorithmsIntroduction to Data Structures & Algorithms
Introduction to Data Structures & Algorithms
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structure
 
U nit i data structure-converted
U nit   i data structure-convertedU nit   i data structure-converted
U nit i data structure-converted
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
 
Cupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithmCupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithm
 

Similar to Analysis of algorithms

Ch1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdfCh1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdf
zoric99
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptx
ChSreenivasuluReddy
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
Akshay Dagar
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
abay golla
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
jinkhatima
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
rajesshs31r
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
rajesshs31r
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
Akhil Kaushik
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
Saranya Natarajan
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysis
jayavignesh86
 
Algorithms
Algorithms Algorithms
Algorithms
yashodhaHR2
 
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm AnalysisData Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Ferdin Joe John Joseph PhD
 
Analysis of Algorithms
Analysis of AlgorithmsAnalysis of Algorithms
Analysis of Algorithms
Amna Saeed
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
FellowBuddy.com
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithmsrajatmay1992
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisAnindita Kundu
 

Similar to Analysis of algorithms (20)

Ch1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdfCh1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdf
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptx
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysis
 
Algorithms
Algorithms Algorithms
Algorithms
 
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm AnalysisData Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm Analysis
 
Analysis of Algorithms
Analysis of AlgorithmsAnalysis of Algorithms
Analysis of Algorithms
 
Daa
DaaDaa
Daa
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithms
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
 

Recently uploaded

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
ViralQR
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 

Recently uploaded (20)

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 

Analysis of algorithms

  • 1. Data Structures & Algorithms Lecturer: Dr. Muhammad M Iqbal Ph.D (DCU), M.Phil (GCU) 1
  • 2. Overview – The concept of algorithm analysis – Big-Oh notation – Experimental analysis of algorithms in Java – Comparing various growth functions – Efficiency goals AlgorithmInput Output 2
  • 3. Computational and Asymptotic Complexity • Computational Complexity measures the degree of difficulty of an algorithm • Indicates how much effort is needed to apply an algorithm or how costly it is • To evaluate an algorithm’s efficiency, use logical units that express a relationship such as: – The size n of a file or an array – The amount of time t required to process the data 3
  • 4. Analysis of Algorithms • Method 1: • Transform the algorithm into some programming language and measure the execution time for a set of input values. • Method 2: • Use the Big O notation (mathematical notation) to analyse the behaviour of the algorithms. (Independent of software and hardware). 4
  • 5. • Let us consider two algorithms to solve a problem: – First algorithm (X) spends 100 * x time units – Second algorithm (Y) spends 5000 time units • Which one is better? – Algorithm X is better if our problem size is small, that is, if x < 50 – Algorithm Y is better for larger problems, with x > 50 • A challenge is to handle the large size of the problems. 5 Complexity of Algorithms • To understand the performance of an algorithm, it is best to observe how well or poorly it does for different size of inputs. • This indicates that the selection of the algorithm for a particular problem demands analysis based on the input size.
  • 6. Algorithm Analysis Consider the problem of summing FIGURE: Three algorithms for computing the sum 1 + 2 + . . . + n for an integer n > 0 6 TimedAlgorithms.java
  • 7. Running Time • Most algorithms transform input objects into output objects. • The running time of an algorithm typically grows with the input size. • Average case time is often difficult to determine. • We focus on the worst case running time. – Easier to analyze – Crucial to applications such as games, finance and robotics 0 20 40 60 80 100 120 RunningTime 1000 2000 3000 4000 Input Size best case average case worst case 7
  • 8. Algorithm Efficiency • The efficiency of an algorithm is usually expressed in terms of its use of CPU time. • The analysis of algorithms involves categorizing an algorithm in terms of efficiency. • An everyday example: washing dishes. • Suppose washing a dish takes 30 seconds and drying a dish takes an additional 30 seconds. • Therefore, n dishes require n minutes to wash and dry. 8
  • 9. Algorithm Efficiency • Now consider a less efficient approach that requires us to redry all previously washed dishes after washing another one seconds4515 2 )1(30 30dishes)(time )30*()wash timeseconds30(* 2 n 1i nn nn nn in      9
  • 10. Problem Size • For every algorithm we want to analyze, we need to define the size of the problem • The dishwashing problem has a size n – number of dishes to be washed/dried • For a search algorithm, the size of the problem is the size of the search pool • For a sorting algorithm, the size of the program is the number of elements to be sorted 10
  • 11. Growth Functions • We must also decide what we are trying to efficiently optimize i. time complexity – CPU time ii. space complexity – memory space • CPU time is generally the focus • A growth function shows the relationship between the size of the problem (n) and the value optimized (time) 11
  • 12. Asymptotic Complexity • The growth function of the second dishwashing algorithm is t(n) = 15n2 + 45n • It is not typically necessary to know the exact growth function for an algorithm • We are mainly interested in the asymptotic complexity of an algorithm – the general nature of the algorithm as n increases • Asymptotic complexity helps to explore the performance of the algorithms 12
  • 13. Asymptotic Complexity • Asymptotic complexity is based on the dominant term of the growth function – the term that increases most quickly as n increases • The dominant term for the second dishwashing algorithm is n2: 13 t(n) = 15n2 + 45n
  • 14. 14 Computational and Asymptotic Complexity (continued) Figure 2-1 The growth rate of all terms of function f (n) = n2 + 100n + log10n + 1,000
  • 15. Big-Oh Notation • The coefficients and the lower order terms become increasingly less relevant as n increases • So we say that the algorithm is order n2, which is written O(n2) • This is called Big-Oh notation • There are various Big-Oh categories • Two algorithms in the same category are generally considered to have the same efficiency, but that doesn't mean they have equal growth functions or behave exactly the same for all values of n 15
  • 16. Big-Oh Categories • Some sample growth functions and their Big-Oh categories: • As n increases, the various growth functions diverge dramatically: 16
  • 18. Analyzing Loop Execution • First determine the order of the body of the loop, then multiply that by the number of times the loop will execute for (int count = 0; count < n; count++) // some sequence of O(1) steps • N loop executions times O(1) operations results in a O(n) efficiency 18
  • 19. Analyzing Loop Execution • Consider the following loop: count = 1; while (count < n) { count *= 2; // some sequence of O(1) steps } • The loop is executed log2n times, so the loop is O(log n) 19
  • 20. Analyzing Nested Loops • When the loops are nested, we multiply the complexity of the outer loop by the complexity of the inner loop for (int count = 0; count < n; count++) for (int count2 = 0; count2 < n; count2++) { // some sequence of O(1) steps } • Both the inner and outer loops have complexity of O(n) • The overall efficiency is O(n2) 20 • The body of a loop may contain a call to a method • To determine the order of the loop body, the order of the method must be taken into account • The overhead of the method call itself is generally ignored
  • 21. Experimental Studies • Write a program implementing the algorithm • Run the program with inputs of varying size and composition, noting the time needed: • Plot the results 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 0 50 100 Input Size Time(ms) 21
  • 22. Limitations of Experiments • 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 22
  • 23. Theoretical Analysis • 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 23
  • 24. Comparison of Two Algorithms Insertion sort is n2 / 4 Merge sort is 2 n lg n sort a million items? insertion sort takes roughly 70 hours while merge sort takes roughly 40 seconds This is a slow machine, but if 100 x as fast then it’s 40 minutes versus less than 0.5 seconds24
  • 25. More Big-Oh Examples  7n - 2 7n-2 is O(n) need c > 0 and n0  1 such that 7 n - 2  c n for n  n0 this is true for c = 7 and n0 = 1  3 n3 + 20 n2 + 5 3 n3 + 20 n2 + 5 is O(n3) need c > 0 and n0  1 such that 3 n3 + 20 n2 + 5  c n3 for n  n0 this is true for c = 4 and n0 = 21  3 log n + 5 3 log n + 5 is O(log n) need c > 0 and n0  1 such that 3 log n + 5  c log n for n  n0 this is true for c = 8 and n0 = 2 25
  • 26. Prefix Averages 2 (Linear) The following algorithm uses a running summation to improve the efficiency Algorithm prefixAverage2 runs in O(n) time! 26
  • 27. Experimental Study • A simple mechanism for collecting such running times in Java is based on use of the currentTimeMillis method of the System class. long startTime = System.currentTimeMillis( ); // record the starting time /* (run the algorithm) */ long endTime = System.currentTimeMillis( ); // record the ending time long elapsed = endTime − startTime; // compute the elapsed time 27 StringExperiment.java
  • 28. Experimental Studies • A simple mechanism for collecting such running times in Java is based on use of the currentTimeMillis method of the System class. StringExperiment.zip n 50000 repeat1 1607 repeat2 3 100000 5074 4 200000 11727 5 400000 37939 12 800000 152135 12 1600000 648259 16 28
  • 29. • To analyze the algorithms effectively, a general methodology for analysis of the running time of algorithms (theoretical analysis) • In contrast to the "experimental approach", this methodology: – Uses a high-level description of the algorithm instead of testing one of its implementations. – Characterizes running time as a function of the input size, n. – Takes into account all possible inputs. – Allows one to evaluate the efficiency of any algorithm in a way that is independent from the hardware and software environment. Theoretical Analysis 29
  • 30. Performance Analysis of the Algorithms • In order to analyse the algorithms i. We should calculate the time using some formula based on the input size. ii. We should find a method to determine the memory requirement based on the input size. • We can choose the “size of input” to be the parameter that most influences the actual time/space required – It is usually obvious what this parameter is • e.g., number of elements in the array you want to sort – Sometimes we need two or more parameters • However, the time performs a major role in all evaluations. 30 Time and Space
  • 32. Three-Way Set Disjointness Suppose we are given three sets, A, B, and C, stored in three different integer arrays. We will assume that no individual set contains duplicate values, but that there may be some numbers that are in two or three of the sets. The three-way set disjointness problem is to determine if the intersection of the three sets is empty, namely, that there is no element x such that x ∈ A, x ∈ B, and x ∈ C. 32
  • 33. Three-Way Set Disjointness Worst-case running time of disjoint1 is O(n3). Worst-case running time for disjoint2 is O(n2). 33 Testing.java
  • 34. Big-Oh Notation Statements with method calls • When a statement involves a method call, the complexity of the statement includes the complexity of the method call. f(k); // O(1) g(k); // O(N) • When a loop is involved, the same rule applies for (j = 0; j < N; j++) g(N); – It has complexity (N2). – The loop executes N times – Each method call g(N) is of complexity O(N). 34
  • 35. Question • A program’s execution time depends in part on – the speed of the computer it is running on (time) – the memory capacity – the language the algorithm is written in – all of the above 35
  • 36. Resources/ References • Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein 3rd, 2009, MIT Press and McGraw-Hill. • Data Structures: Abstraction and Design Using Java, Elliott B. Koffmann, 2nd, 2010, Wiley. • Data Structures and algorithm analysis in java, Mark A. Weiss, 3rd, 2011, Prentice Hall. • Frank M. Carrano, Data Structures and Abstractions with Java™, 3rd Edition, Prentice Hall, PEARSON, ISBN-10: 0-13-610091-0. • This lecture used some images and videos from the Google search repository to raise the understanding level of students. https://www.google.ie/search? Dr. Muhammad M Iqbal*