8
Most read
13
Most read
14
Most read
Big O Notation
outline
 Performance vs Complexity
 Algorithm Efficiency
 Big O Notation
Performance vs Complexity
• Performance:
 How much time/memory/disk/… is used by the algorithm
 Depends on the machine, compiler, code,…
• Complexity:
 How do the resource requirements of a program/algorithm scale
Complexity affects performance
but not the other way around
If I have an algorithm with high complexity, it will take longer time to run.
However, if I have a very slow program, that does not mean that my algorithm is very complex;
that could be all other kind of issues that made the program run slow.
Complexity of an Algorithm
 We are going to count the number of the steps that are taken during the
execution
 Assumption (simplification):
 Each step costs the same amount
 +, -, return a value, access an array element, ….
Complexity of an Algorithm (cont.)
• Example 1:
• private int getLastElement (int [] numbers)
• {
return numbers[numbers.length -1];
}
Number of steps: 4
Constant time function (independent of array size)
Notes: Let’s look at each step that need to be executed. It is always 4 steps to access the last element,
no matter the number of elements.
Complexity of an Algorithm (cont.)
• Example 2:
• private static int sum(int [] numbers)
• {
int sum = 0;
for (int i = 0; i < number.length; i++){
sum = sum + number[i];
}
return sum;
}
Outside loop: 3 + 2 Number of steps:6n + 5
Linear time functionLoop body: 6
INSTRUCTOR-NOTES:This time we use a loop.
We will start by the steps that will be executed only once and the are outside the loop.
Then we count the steps that are going to be repeated over and over again.
Algorithm Efficiency
 Let’s look at the following algorithm for initializing the values in an array:
final int N = 500;
int [] counts = new int[N];
for (int i=0; i<counts.length; i++)
counts[i] = 0;
 The length of time the algorithm takes to execute depends on the value of N
Algorithm Efficiency (cont.)
 In that algorithm, we have one loop that processes all of the elements in the array
 Intuitively:
– If N was half of its value, we would expect the algorithm to take half the time
– If N was twice its value, we would expect the algorithm to take twice the time
 That is true and we say that the algorithm efficiency relative to N is linear
Algorithm Efficiency (cont.)
 Let’s look at another algorithm for initializing the values in a different array:
final int N = 500;
int [] [] counts = new int[N][N];
for (int i=0; i<counts.length; i++)
for (int j=0; j<counts[i].length; j++)
counts[i][j] = 0;
 The length of time the algorithm takes to execute still depends on the value of N
Algorithm Efficiency (cont.)
 However, in the second algorithm, we have two nested loops to process the
elements in the two dimensional array
 Intuitively:
– If N is half its value, we would expect the algorithm to take one quarter the time
– If N is twice its value, we would expect the algorithm to take quadruple the time
 That is true and we say that the algorithm efficiency relative to N is quadratic
Big O notation
Why do we need Big-O?
 In computer science, we use Big-O to classify algorithms based on their scalability.
 Basically, it tells you how fast a function grows or declines.
 The letter O is used because the rate of growth of a function is also called its order.
 It also allows us to estimate the worst case running time of an algorithm as a
function of the input size.
Big O notation
 Examples:
– We can say that the first algorithm is O(n)
– We can say that the second algorithm is O(n2)
 For any algorithm that has a function g(n) of the parameter n that describes its
length of time to execute, we can say the algorithm is O(g(n))
 We only include the fastest growing term and ignore any multiplying by or adding of
constants (which makes sense because those depend on the particular hardware the
program is run on)
Big O notation (cont.)
Growth Functions:
Eight functions O(n) that occur frequently in the analysis of algorithms (in order of
increasing rate of growth relative to n):
– Constant  1
– Logarithmic  log n
– Linear  n
– Log Linear  n log n
– Quadratic  n2
– Cubic  n3
– Exponential  2n
– Exhaustive Search  n!
Big O notation (cont.)
Growth Rates Compared:
Big O notation (cont.)
Big O notation (cont.)
 The growth list is useful because of the following fact:
 if a function f(n) is a sum of functions, one of which grows faster than the
others, then the faster growing one determines the order of f(n).
 Example:
 If f(n) = 10 log(n) + 5 (log(n))3 + 7 n + 3 n 2 + 6 n 3,
 then f(n) = O(n 3).
Big O notation (cont.)
Big-O for a Problem:
 O(g(n)) for a problem means there is some O(g(n)) algorithm that solves the
problem
 Don’t assume that the specific algorithm that you are currently using is the
best solution for the problem
 There may be other correct algorithms that grow at a smaller rate with
increasing n
 Many times, the goal is to find an algorithm with the smallest possible
growth rate
Big O notation (cont.)
Role of Data Structures:
 That brings up the topic of the structure of the data on which the algorithm
operates
 If we are using an algorithm manually on some amount of data, we
intuitively try to organize the data in a way that minimizes the number of
steps that we need to take
 Publishers offer dictionaries with the words listed in alphabetical order to
minimize the length of time it takes us to look up a word
Big O notation (cont.)
Role of Data Structures:
 We can do the same thing for algorithms in our computer programs
 Example: Finding a numeric value in a list
 If we assume that the list is unordered, we must search from the beginning
to the end
 On average, we will search half the list
 Worst case, we will search the entire list
 Algorithm is O(n), where n is size of array
Big O notation (cont.)
Example:
int [] list = {7, 2, 9, 5, 6, 4};
for (int i=0; i<list.length, i++)
if (value == list[i])
statement; // found it
// didn’t find it
Big O notation (cont.)
 If we assume that the list is ordered, we can still search the entire list
from the beginning to the end to determine if we have a match
 But, we do not need to search that way
 Because the values are in numerical order, we can use a binary search
algorithm
 Algorithm is O(logn), where n is size of array
Big O notation (cont.)
Role of Data Structures:
 The difference in the structure of the data between an unordered list and an
ordered list can be used to reduce algorithm Big-O
 This is the role of data structures and why we study them
 We need to be as clever in organizing our data efficiently as we are in
figuring out an algorithm for processing it efficiently
Big O notation (cont.)
Example:
 Find an order for
 f(x) = 7 x5 + 5 x3 – x + 4
 O(x5)
Big O notation (cont.)
Sorting Algorithms Big-O:
http://bigocheatsheet.com/

More Related Content

PPTX
Big o notation
PPTX
Complexity analysis - The Big O Notation
DOC
Time and space complexity
PPT
Asymptotic notations
PPT
Complexity of Algorithm
PPTX
Asymptotic notations(Big O, Omega, Theta )
PPTX
Asymptotic Notations
PPT
Functions in C++
Big o notation
Complexity analysis - The Big O Notation
Time and space complexity
Asymptotic notations
Complexity of Algorithm
Asymptotic notations(Big O, Omega, Theta )
Asymptotic Notations
Functions in C++

What's hot (20)

PDF
Lecture 4 asymptotic notations
PPTX
Big o notation
PPT
Fundamentals of the Analysis of Algorithm Efficiency
PPT
how to calclute time complexity of algortihm
PPTX
Analysis of algorithm
PDF
Time and Space Complexity
PDF
Algorithms Lecture 2: Analysis of Algorithms I
PDF
Algorithms Lecture 3: Analysis of Algorithms II
PPTX
Algorithm analysis (All in one)
PDF
Algorithms Lecture 1: Introduction to Algorithms
PDF
sparse matrix in data structure
PDF
Master theorem
PPTX
Decision Tree - C4.5&CART
PPTX
Asymptotic notations
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
PPTX
Algorithm big o
PPT
Algorithm analysis
PDF
DBMS 9 | Extendible Hashing
PDF
Big omega
Lecture 4 asymptotic notations
Big o notation
Fundamentals of the Analysis of Algorithm Efficiency
how to calclute time complexity of algortihm
Analysis of algorithm
Time and Space Complexity
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 3: Analysis of Algorithms II
Algorithm analysis (All in one)
Algorithms Lecture 1: Introduction to Algorithms
sparse matrix in data structure
Master theorem
Decision Tree - C4.5&CART
Asymptotic notations
Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm big o
Algorithm analysis
DBMS 9 | Extendible Hashing
Big omega
Ad

Viewers also liked (20)

PPTX
Data structures and Big O notation
PDF
PPT
Time complexity
PPTX
Divide and Conquer - Part 1
PPT
Complexity
PPT
Analysis of algo
PPT
Algorithum Analysis
PPT
Analysis Of Algorithms I
PDF
02 asymptotic-notation-and-recurrences
RTF
Amortized complexity
PDF
P versus NP
PDF
03 Analysis of Algorithms: Probabilistic Analysis
PDF
13 Amortized Analysis
PDF
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
PPTX
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
PPTX
Introduction to Algorithms and Asymptotic Notation
PPTX
Asymptotic Notation and Data Structures
PPTX
Algorithm and pseudo codes
PDF
Writing algorithms
PPTX
Introduction to Pseudocode
Data structures and Big O notation
Time complexity
Divide and Conquer - Part 1
Complexity
Analysis of algo
Algorithum Analysis
Analysis Of Algorithms I
02 asymptotic-notation-and-recurrences
Amortized complexity
P versus NP
03 Analysis of Algorithms: Probabilistic Analysis
13 Amortized Analysis
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Introduction to Algorithms and Asymptotic Notation
Asymptotic Notation and Data Structures
Algorithm and pseudo codes
Writing algorithms
Introduction to Pseudocode
Ad

Similar to 9 big o-notation (20)

PPT
Lecture 1 and 2 of Data Structures & Algorithms
PPTX
Searching Algorithms
PPT
Cs1311lecture23wdl
PDF
ESINF03-AlgAnalis.pdfESINF03-AlgAnalis.pdf
PDF
Ch24 efficient algorithms
PPTX
Computational Complexity.pptx
PPTX
Data structures and ALGORITHMS methd binary SEARCH
PPTX
Data structures and algorithms (DSA) are foundational concepts in computer sc...
PPTX
Class[1][23ed may] [algorithms]
PPTX
Algorithm Complexity and Main Concepts
PDF
Algorithm analysis
PPT
Time complexity.ppt
PPT
Time complexity.pptr56435 erfgegr t 45t 35
PPT
Data Structures- Part2 analysis tools
PPT
How to calculate complexity in Data Structure
PPTX
algorithmanalysis and effciency.pptx
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
PPTX
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
PPTX
Time complexity.pptxghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjjjjjjjjjjj
PPTX
Analysis of algorithms
Lecture 1 and 2 of Data Structures & Algorithms
Searching Algorithms
Cs1311lecture23wdl
ESINF03-AlgAnalis.pdfESINF03-AlgAnalis.pdf
Ch24 efficient algorithms
Computational Complexity.pptx
Data structures and ALGORITHMS methd binary SEARCH
Data structures and algorithms (DSA) are foundational concepts in computer sc...
Class[1][23ed may] [algorithms]
Algorithm Complexity and Main Concepts
Algorithm analysis
Time complexity.ppt
Time complexity.pptr56435 erfgegr t 45t 35
Data Structures- Part2 analysis tools
How to calculate complexity in Data Structure
algorithmanalysis and effciency.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
Time complexity.pptxghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjjjjjjjjjjj
Analysis of algorithms

More from irdginfo (20)

PPTX
Quicksort Presentation
PPTX
10 merge sort
PPTX
8 elementary sorts-bubble
PPTX
8 elementary sorts-shell
PPTX
8 elementary sorts-insertion
PPTX
8 elementary sorts-selection
PPTX
7 searching injava-binary
PPTX
6 arrays injava
PPTX
5 data structures-hashtable
PPTX
5 data structures-tree
PPTX
5 data structures-stack
PPTX
5 data structures-arraysandlinkedlist
PPTX
4 character encoding-unicode
PPTX
4 character encoding-ascii
PPTX
4 character encoding
PPTX
3 number systems-floatingpoint
PPTX
2 number systems-scientificnotation
PPTX
1 number systems-hex
PPTX
1 number systems-unsignedsignedintegers
PPTX
1 number systems-octal
Quicksort Presentation
10 merge sort
8 elementary sorts-bubble
8 elementary sorts-shell
8 elementary sorts-insertion
8 elementary sorts-selection
7 searching injava-binary
6 arrays injava
5 data structures-hashtable
5 data structures-tree
5 data structures-stack
5 data structures-arraysandlinkedlist
4 character encoding-unicode
4 character encoding-ascii
4 character encoding
3 number systems-floatingpoint
2 number systems-scientificnotation
1 number systems-hex
1 number systems-unsignedsignedintegers
1 number systems-octal

Recently uploaded (20)

PDF
Literature_Review_methods_ BRACU_MKT426 course material
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PDF
Hazard Identification & Risk Assessment .pdf
PPTX
Computer Architecture Input Output Memory.pptx
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
IP : I ; Unit I : Preformulation Studies
PDF
Journal of Dental Science - UDMY (2022).pdf
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
International_Financial_Reporting_Standa.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
My India Quiz Book_20210205121199924.pdf
PDF
semiconductor packaging in vlsi design fab
Literature_Review_methods_ BRACU_MKT426 course material
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
Hazard Identification & Risk Assessment .pdf
Computer Architecture Input Output Memory.pptx
A powerpoint presentation on the Revised K-10 Science Shaping Paper
AI-driven educational solutions for real-life interventions in the Philippine...
Journal of Dental Science - UDMY (2021).pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
Introduction to pro and eukaryotes and differences.pptx
FORM 1 BIOLOGY MIND MAPS and their schemes
Race Reva University – Shaping Future Leaders in Artificial Intelligence
IP : I ; Unit I : Preformulation Studies
Journal of Dental Science - UDMY (2022).pdf
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
International_Financial_Reporting_Standa.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
My India Quiz Book_20210205121199924.pdf
semiconductor packaging in vlsi design fab

9 big o-notation

  • 2. outline  Performance vs Complexity  Algorithm Efficiency  Big O Notation
  • 3. Performance vs Complexity • Performance:  How much time/memory/disk/… is used by the algorithm  Depends on the machine, compiler, code,… • Complexity:  How do the resource requirements of a program/algorithm scale Complexity affects performance but not the other way around If I have an algorithm with high complexity, it will take longer time to run. However, if I have a very slow program, that does not mean that my algorithm is very complex; that could be all other kind of issues that made the program run slow.
  • 4. Complexity of an Algorithm  We are going to count the number of the steps that are taken during the execution  Assumption (simplification):  Each step costs the same amount  +, -, return a value, access an array element, ….
  • 5. Complexity of an Algorithm (cont.) • Example 1: • private int getLastElement (int [] numbers) • { return numbers[numbers.length -1]; } Number of steps: 4 Constant time function (independent of array size) Notes: Let’s look at each step that need to be executed. It is always 4 steps to access the last element, no matter the number of elements.
  • 6. Complexity of an Algorithm (cont.) • Example 2: • private static int sum(int [] numbers) • { int sum = 0; for (int i = 0; i < number.length; i++){ sum = sum + number[i]; } return sum; } Outside loop: 3 + 2 Number of steps:6n + 5 Linear time functionLoop body: 6 INSTRUCTOR-NOTES:This time we use a loop. We will start by the steps that will be executed only once and the are outside the loop. Then we count the steps that are going to be repeated over and over again.
  • 7. Algorithm Efficiency  Let’s look at the following algorithm for initializing the values in an array: final int N = 500; int [] counts = new int[N]; for (int i=0; i<counts.length; i++) counts[i] = 0;  The length of time the algorithm takes to execute depends on the value of N
  • 8. Algorithm Efficiency (cont.)  In that algorithm, we have one loop that processes all of the elements in the array  Intuitively: – If N was half of its value, we would expect the algorithm to take half the time – If N was twice its value, we would expect the algorithm to take twice the time  That is true and we say that the algorithm efficiency relative to N is linear
  • 9. Algorithm Efficiency (cont.)  Let’s look at another algorithm for initializing the values in a different array: final int N = 500; int [] [] counts = new int[N][N]; for (int i=0; i<counts.length; i++) for (int j=0; j<counts[i].length; j++) counts[i][j] = 0;  The length of time the algorithm takes to execute still depends on the value of N
  • 10. Algorithm Efficiency (cont.)  However, in the second algorithm, we have two nested loops to process the elements in the two dimensional array  Intuitively: – If N is half its value, we would expect the algorithm to take one quarter the time – If N is twice its value, we would expect the algorithm to take quadruple the time  That is true and we say that the algorithm efficiency relative to N is quadratic
  • 11. Big O notation Why do we need Big-O?  In computer science, we use Big-O to classify algorithms based on their scalability.  Basically, it tells you how fast a function grows or declines.  The letter O is used because the rate of growth of a function is also called its order.  It also allows us to estimate the worst case running time of an algorithm as a function of the input size.
  • 12. Big O notation  Examples: – We can say that the first algorithm is O(n) – We can say that the second algorithm is O(n2)  For any algorithm that has a function g(n) of the parameter n that describes its length of time to execute, we can say the algorithm is O(g(n))  We only include the fastest growing term and ignore any multiplying by or adding of constants (which makes sense because those depend on the particular hardware the program is run on)
  • 13. Big O notation (cont.) Growth Functions: Eight functions O(n) that occur frequently in the analysis of algorithms (in order of increasing rate of growth relative to n): – Constant  1 – Logarithmic  log n – Linear  n – Log Linear  n log n – Quadratic  n2 – Cubic  n3 – Exponential  2n – Exhaustive Search  n!
  • 14. Big O notation (cont.) Growth Rates Compared:
  • 15. Big O notation (cont.)
  • 16. Big O notation (cont.)  The growth list is useful because of the following fact:  if a function f(n) is a sum of functions, one of which grows faster than the others, then the faster growing one determines the order of f(n).  Example:  If f(n) = 10 log(n) + 5 (log(n))3 + 7 n + 3 n 2 + 6 n 3,  then f(n) = O(n 3).
  • 17. Big O notation (cont.) Big-O for a Problem:  O(g(n)) for a problem means there is some O(g(n)) algorithm that solves the problem  Don’t assume that the specific algorithm that you are currently using is the best solution for the problem  There may be other correct algorithms that grow at a smaller rate with increasing n  Many times, the goal is to find an algorithm with the smallest possible growth rate
  • 18. Big O notation (cont.) Role of Data Structures:  That brings up the topic of the structure of the data on which the algorithm operates  If we are using an algorithm manually on some amount of data, we intuitively try to organize the data in a way that minimizes the number of steps that we need to take  Publishers offer dictionaries with the words listed in alphabetical order to minimize the length of time it takes us to look up a word
  • 19. Big O notation (cont.) Role of Data Structures:  We can do the same thing for algorithms in our computer programs  Example: Finding a numeric value in a list  If we assume that the list is unordered, we must search from the beginning to the end  On average, we will search half the list  Worst case, we will search the entire list  Algorithm is O(n), where n is size of array
  • 20. Big O notation (cont.) Example: int [] list = {7, 2, 9, 5, 6, 4}; for (int i=0; i<list.length, i++) if (value == list[i]) statement; // found it // didn’t find it
  • 21. Big O notation (cont.)  If we assume that the list is ordered, we can still search the entire list from the beginning to the end to determine if we have a match  But, we do not need to search that way  Because the values are in numerical order, we can use a binary search algorithm  Algorithm is O(logn), where n is size of array
  • 22. Big O notation (cont.) Role of Data Structures:  The difference in the structure of the data between an unordered list and an ordered list can be used to reduce algorithm Big-O  This is the role of data structures and why we study them  We need to be as clever in organizing our data efficiently as we are in figuring out an algorithm for processing it efficiently
  • 23. Big O notation (cont.) Example:  Find an order for  f(x) = 7 x5 + 5 x3 – x + 4  O(x5)
  • 24. Big O notation (cont.) Sorting Algorithms Big-O: http://bigocheatsheet.com/

Editor's Notes

  • #4: Complexity affects performance but not the other way around means: If I have an algorithm with high complexity, it will take longer time to run. However, if I have a very slow program, that does not mean that my algorithm is very complex; that could be all other kind of issues that made the program run slow.
  • #7: This time we use a loop. We will start by the steps that will be executed only once and the are outside the loop. Then we count the steps that are going to be repeated over and over again.