SlideShare uma empresa Scribd logo
1 de 24
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/

Mais conteúdo relacionado

Mais procurados

Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
chidabdu
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
Aakash deep Singhal
 

Mais procurados (20)

Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 
CS8391 Data Structures 2 mark Questions - Anna University Questions
CS8391 Data Structures 2 mark Questions - Anna University QuestionsCS8391 Data Structures 2 mark Questions - Anna University Questions
CS8391 Data Structures 2 mark Questions - Anna University Questions
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Numpy tutorial
Numpy tutorialNumpy tutorial
Numpy tutorial
 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
 
Unit 1 chapter 1 Design and Analysis of Algorithms
Unit 1   chapter 1 Design and Analysis of AlgorithmsUnit 1   chapter 1 Design and Analysis of Algorithms
Unit 1 chapter 1 Design and Analysis of Algorithms
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
Data Structure and Algorithms.pptx
Data Structure and Algorithms.pptxData Structure and Algorithms.pptx
Data Structure and Algorithms.pptx
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Apriori Algorithm
Apriori AlgorithmApriori Algorithm
Apriori Algorithm
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Data structures and Big O notation
Data structures and Big O notationData structures and Big O notation
Data structures and Big O notation
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
 

Destaque

Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Nikhil Sharma
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Ehtisham Ali
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
Sri Prasanna
 
02 asymptotic-notation-and-recurrences
02 asymptotic-notation-and-recurrences02 asymptotic-notation-and-recurrences
02 asymptotic-notation-and-recurrences
Noushadur Shoukhin
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
 

Destaque (20)

Big o
Big oBig o
Big o
 
Time complexity
Time complexityTime complexity
Time complexity
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Complexity
ComplexityComplexity
Complexity
 
Analysis of algo
Analysis of algoAnalysis of algo
Analysis of algo
 
Algorithum Analysis
Algorithum AnalysisAlgorithum Analysis
Algorithum Analysis
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O Notation
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
 
02 asymptotic-notation-and-recurrences
02 asymptotic-notation-and-recurrences02 asymptotic-notation-and-recurrences
02 asymptotic-notation-and-recurrences
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
P versus NP
P versus NPP versus NP
P versus NP
 
03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis
 
13 Amortized Analysis
13 Amortized Analysis13 Amortized Analysis
13 Amortized Analysis
 
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUSQUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 

Semelhante a 9 big o-notation

Aad introduction
Aad introductionAad introduction
Aad introduction
Mr SMAK
 

Semelhante a 9 big o-notation (20)

TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
 
DA lecture 3.pptx
DA lecture 3.pptxDA lecture 3.pptx
DA lecture 3.pptx
 
Aad introduction
Aad introductionAad introduction
Aad introduction
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
Theory of algorithms final
Theory of algorithms final Theory of algorithms final
Theory of algorithms final
 
Ds
DsDs
Ds
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptx
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
DATA STRUCTURE.pdf
DATA STRUCTURE.pdfDATA STRUCTURE.pdf
DATA STRUCTURE.pdf
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
Chapter two
Chapter twoChapter two
Chapter two
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 
Algoritmos
AlgoritmosAlgoritmos
Algoritmos
 
Module 1_ Introduction.pptx
Module 1_ Introduction.pptxModule 1_ Introduction.pptx
Module 1_ Introduction.pptx
 

Mais de irdginfo

10 merge sort
10 merge sort10 merge sort
10 merge sort
irdginfo
 
8 elementary sorts-bubble
8 elementary sorts-bubble8 elementary sorts-bubble
8 elementary sorts-bubble
irdginfo
 
8 elementary sorts-shell
8 elementary sorts-shell8 elementary sorts-shell
8 elementary sorts-shell
irdginfo
 
8 elementary sorts-insertion
8 elementary sorts-insertion8 elementary sorts-insertion
8 elementary sorts-insertion
irdginfo
 
8 elementary sorts-selection
8 elementary sorts-selection8 elementary sorts-selection
8 elementary sorts-selection
irdginfo
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binary
irdginfo
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
irdginfo
 
5 data structures-hashtable
5 data structures-hashtable5 data structures-hashtable
5 data structures-hashtable
irdginfo
 
5 data structures-tree
5 data structures-tree5 data structures-tree
5 data structures-tree
irdginfo
 
5 data structures-stack
5 data structures-stack5 data structures-stack
5 data structures-stack
irdginfo
 
5 data structures-arraysandlinkedlist
5 data structures-arraysandlinkedlist5 data structures-arraysandlinkedlist
5 data structures-arraysandlinkedlist
irdginfo
 
4 character encoding-unicode
4 character encoding-unicode4 character encoding-unicode
4 character encoding-unicode
irdginfo
 
4 character encoding-ascii
4 character encoding-ascii4 character encoding-ascii
4 character encoding-ascii
irdginfo
 
4 character encoding
4 character encoding4 character encoding
4 character encoding
irdginfo
 
3 number systems-floatingpoint
3 number systems-floatingpoint3 number systems-floatingpoint
3 number systems-floatingpoint
irdginfo
 
2 number systems-scientificnotation
2 number systems-scientificnotation2 number systems-scientificnotation
2 number systems-scientificnotation
irdginfo
 
1 number systems-hex
1 number systems-hex1 number systems-hex
1 number systems-hex
irdginfo
 
1 number systems-unsignedsignedintegers
1 number systems-unsignedsignedintegers1 number systems-unsignedsignedintegers
1 number systems-unsignedsignedintegers
irdginfo
 
1 number systems-octal
1 number systems-octal1 number systems-octal
1 number systems-octal
irdginfo
 

Mais de irdginfo (20)

Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
10 merge sort
10 merge sort10 merge sort
10 merge sort
 
8 elementary sorts-bubble
8 elementary sorts-bubble8 elementary sorts-bubble
8 elementary sorts-bubble
 
8 elementary sorts-shell
8 elementary sorts-shell8 elementary sorts-shell
8 elementary sorts-shell
 
8 elementary sorts-insertion
8 elementary sorts-insertion8 elementary sorts-insertion
8 elementary sorts-insertion
 
8 elementary sorts-selection
8 elementary sorts-selection8 elementary sorts-selection
8 elementary sorts-selection
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binary
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
5 data structures-hashtable
5 data structures-hashtable5 data structures-hashtable
5 data structures-hashtable
 
5 data structures-tree
5 data structures-tree5 data structures-tree
5 data structures-tree
 
5 data structures-stack
5 data structures-stack5 data structures-stack
5 data structures-stack
 
5 data structures-arraysandlinkedlist
5 data structures-arraysandlinkedlist5 data structures-arraysandlinkedlist
5 data structures-arraysandlinkedlist
 
4 character encoding-unicode
4 character encoding-unicode4 character encoding-unicode
4 character encoding-unicode
 
4 character encoding-ascii
4 character encoding-ascii4 character encoding-ascii
4 character encoding-ascii
 
4 character encoding
4 character encoding4 character encoding
4 character encoding
 
3 number systems-floatingpoint
3 number systems-floatingpoint3 number systems-floatingpoint
3 number systems-floatingpoint
 
2 number systems-scientificnotation
2 number systems-scientificnotation2 number systems-scientificnotation
2 number systems-scientificnotation
 
1 number systems-hex
1 number systems-hex1 number systems-hex
1 number systems-hex
 
1 number systems-unsignedsignedintegers
1 number systems-unsignedsignedintegers1 number systems-unsignedsignedintegers
1 number systems-unsignedsignedintegers
 
1 number systems-octal
1 number systems-octal1 number systems-octal
1 number systems-octal
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Último (20)

Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 

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/

Notas do Editor

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