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

PPTX
3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)
PPTX
Searching Algorithms
PPT
CS6702 Unit III coloring ppt
PDF
Hidden Markov Models
PPTX
Graphs data structures
PDF
Lecture 9 Perceptron
PPTX
Competition winning learning rates
PPTX
Conditional Random Fields - Vidya Venkiteswaran
3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)
Searching Algorithms
CS6702 Unit III coloring ppt
Hidden Markov Models
Graphs data structures
Lecture 9 Perceptron
Competition winning learning rates
Conditional Random Fields - Vidya Venkiteswaran

What's hot (20)

PPTX
PageRank Algorithm In data mining
PDF
Graph Theory: Planarity & Dual Graph
PPTX
Divide and Conquer - Part 1
PPTX
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
PPTX
Artificial neural network
PPTX
Activation Function.pptx
PPT
Backtracking
PDF
Artificial Neural Network Lect4 : Single Layer Perceptron Classifiers
PDF
An overview of Hidden Markov Models (HMM)
PPTX
PPTX
Tree and graph
PDF
Stemming And Lemmatization Tutorial | Natural Language Processing (NLP) With ...
PPTX
String Matching (Naive,Rabin-Karp,KMP)
PPTX
Introduction to data structure and algorithms
PDF
module5_backtrackingnbranchnbound_2022.pdf
PPT
Design and Analysis of Algorithms
PPTX
Graph theory
PDF
introduction to graph theory
PPT
Boundary fill algm
PageRank Algorithm In data mining
Graph Theory: Planarity & Dual Graph
Divide and Conquer - Part 1
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Artificial neural network
Activation Function.pptx
Backtracking
Artificial Neural Network Lect4 : Single Layer Perceptron Classifiers
An overview of Hidden Markov Models (HMM)
Tree and graph
Stemming And Lemmatization Tutorial | Natural Language Processing (NLP) With ...
String Matching (Naive,Rabin-Karp,KMP)
Introduction to data structure and algorithms
module5_backtrackingnbranchnbound_2022.pdf
Design and Analysis of Algorithms
Graph theory
introduction to graph theory
Boundary fill algm
Ad

Similar to Analysis of algorithms (20)

PDF
ESINF03-AlgAnalis.pdfESINF03-AlgAnalis.pdf
PPT
how to calclute time complexity of algortihm
PPT
Time complexity.ppt
PPT
Time complexity.pptr56435 erfgegr t 45t 35
PPT
How to calculate complexity in Data Structure
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
PDF
Algorithm analysis
PDF
Algorithm Analysis.pdf
PDF
Design & Analysis of Algorithms Lecture Notes
PDF
Analysis of Algorithms
PDF
Data Structure - Lecture 1 - Introduction.pdf
PDF
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
PDF
Data Structure & Algorithms - Introduction
PPTX
Data Structure Algorithm -Algorithm Complexity
PPTX
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
PPT
Lec03 04-time complexity
PPTX
DAA-Unit1.pptx
ESINF03-AlgAnalis.pdfESINF03-AlgAnalis.pdf
how to calclute time complexity of algortihm
Time complexity.ppt
Time complexity.pptr56435 erfgegr t 45t 35
How to calculate complexity in Data Structure
Algorithm And analysis Lecture 03& 04-time complexity.
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Algorithm analysis
Algorithm Analysis.pdf
Design & Analysis of Algorithms Lecture Notes
Analysis of Algorithms
Data Structure - Lecture 1 - Introduction.pdf
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structure & Algorithms - Introduction
Data Structure Algorithm -Algorithm Complexity
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
Lec03 04-time complexity
DAA-Unit1.pptx
Ad

Recently uploaded (20)

PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Top Generative AI Tools for Patent Drafting in 2025.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Event Presentation Google Cloud Next Extended 2025
PDF
How AI Agents Improve Data Accuracy and Consistency in Due Diligence.pdf
PDF
REPORT: Heating appliances market in Poland 2024
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
PDF
Chapter 2 Digital Image Fundamentals.pdf
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
PDF
Reimagining Insurance: Connected Data for Confident Decisions.pdf
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
Dell Pro 14 Plus: Be better prepared for what’s coming
PDF
KodekX | Application Modernization Development
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Top Generative AI Tools for Patent Drafting in 2025.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Event Presentation Google Cloud Next Extended 2025
How AI Agents Improve Data Accuracy and Consistency in Due Diligence.pdf
REPORT: Heating appliances market in Poland 2024
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
Chapter 2 Digital Image Fundamentals.pdf
CroxyProxy Instagram Access id login.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
Enable Enterprise-Ready Security on IBM i Systems.pdf
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Reimagining Insurance: Connected Data for Confident Decisions.pdf
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Dell Pro 14 Plus: Be better prepared for what’s coming
KodekX | Application Modernization Development

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*