SlideShare une entreprise Scribd logo
1  sur  13
Algorithms and Computational
         Complexity
  Reading: Chapter 9 (547-564)
      From the textbook


                                 1
Time Complexity
• The subject of the analysis of algorithms
  consists of the study of their efficiency.
• Two aspects of the algorithm efficiency are:
   1. The amount of time required to execute
      the algorithm
   2. The memory space it consumes.
• In this chapter we introduce the basic
  techniques for calculating time efficiency
  (Time complexity).                             2
Dominant Operations
    The following main operations are used in the
    algorithms
•   Assignment(⃪)
•   Comparison(=, ≠, <, >, ≤, ≥)
•   Arithmetic operation(+, -, ×, ÷)
•   Logical operations(and, or, not)
    How long does each of these operations take to
    execute?
•   In general, assignments are very fast ,while other
    operations are slower. Multiplication and division
    are often slower than addition and subtraction.      3
Time Complexity
• The running time of an algorithm is defined to
  be an estimate of the number of operations
  performed by it given a particular number of
  input values.
• Let T(n) be a measure of the time required to
  execute an algorithm of problem size n. We
  call T(n) the time complexity function of the
  algorithm.
• If n is sufficiently small then the algorithm will
  not have a long running time.                        4
Time Complexity
• How fast T(n) increases for large n?
  This is called the asymptotic behavior of the
  time complexity function, basically we will be
  focusing on the leading term of T(n).
• In our time analysis we will restrict ourselves
  to the worst case behavior of an algorithm;
  that is, the longest running time for any input
  of size n.
                                                    5
Example
• If T(n) = 4n3 + 2n2 + n + 5 then
              T(n) = n3(4 + 2   1 5
                                    )
                             2   3
                            n n n
  and for large n we have
                   T(n) ≃ 4n3.
• We say that T(n) has a growth of order n3.
• We say that one algorithm is more efficient
  then another if its worse case running time
  has a lower order of growth.
                                                6
Exercise
What is the run-time complexity based on n for
the following algorithm segment:
          1. for i = 1 to n do
                1.1 for j = 1 to n do
                        1.1.1 A(i,j) ⃪ x
Solution The inner loop 1.1 is executed n times
and the outer loop 1. is also executed n times.
Hence, T(n) = n2 so that the growth is of order n2.

                                                      7
Exercise
  Estimate the time complexity of the following algorithm:
                     1. i ⃪ 1
                     2. p ⃪ 1
                     3. for j = 1 to n do
                            3.1 p ⃪ p × i
                            3.2 i ⃪ i + 1
  Solution It takes two assignment statements 1. and 2. to
  initialize the variables i and p.
• The loop 3. is executed n times, and each time it executes
  two assignment statements and two arithmetic operations
  3.1 and 3.2.
• Thus, the time complexity of the algorithm is given by
  T(n) = 4n + 2 so the growth is of order n.               8
Remark
• In the latest algorithm, the dominant operation
  is the multiplication of 3.1.
• Because multiplication takes more computer
  time to execute, we only count in this case the
  number of multiplications. Thus
              T(n) = n (multiplications)
• In an algorithm, certain operations are usually
  dominant, so T(n) would be the number of
  dominant operations to accomplish the task.
                                               9
Order of Growth-O(g) notation
• In the latest two problems we found a precise
  expression for the time complexity of the
  algorithm. What usually interests us is the
  order of growth.
• Let g : ℕ ⟶ℕ . We say that a function f is order at
  most g or f “big O” of g if and only if there exists
  positive constant C and n0 ∈ ℕ such that
             |f(n)| ≤ C|g(n)|, for n ≥ n0
  We write f(n) = O(g(n)).
                                                     10
Example
  Find the minimum number of multiplications
  needed to calculate x13?
  Solution
• suppose x13 is expressed as x13 = x × x ×…× x,
  12 multiplications needed
• If it is expressed as x13 = x8 × x4 × x then
  x2 = x × x, x4 = x2 × x2, x8 = x4 × x4.Hence
   x13 = x8 × x4 × x, 2 + 2 + 1 = 5 multiplications
  needed.
                                                      11
Example
Two algorithms, A and B ,have time complexities f
and g respectively, where f(n) = 3 n2 - 3n + 1 and
g(n)=n2.Determine which algorithm is faster.
Solution
First, find the value of n such that f(n)=g(n).Thus
                    n2 = 3 n2 - 3n + 1
                     2 n2 - 3n + 1 = 0
                    (2n - 1)(n - 1) = 0
                     n = 1/2 or n = 1            12
Example
Take the larger value of n, that is for n > 1,
suppose n = 2,then
             f(2) = 3(2)2 - 3(2) + 1 = 7
and
                    g(2) = 22 = 4
Thus g(n) < f(n) for all n > 1.Hence g(n) is
faster, so algorithm B is faster

                                                 13

Contenu connexe

Tendances

Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Asymptotic Analysis
Asymptotic AnalysisAsymptotic Analysis
Asymptotic Analysissonugupta
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrencesMegha V
 
CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingJay Nagar
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysisDr. Rajdeep Chatterjee
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesHaitham El-Ghareeb
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O NotationJawad Khan
 
Complexity Analysis of Recursive Function
Complexity Analysis of Recursive FunctionComplexity Analysis of Recursive Function
Complexity Analysis of Recursive FunctionMeghaj Mallick
 
A note on word embedding
A note on word embeddingA note on word embedding
A note on word embeddingKhang Pham
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexityAbbas Ali
 
Lecture 4 asymptotic notations
Lecture 4   asymptotic notationsLecture 4   asymptotic notations
Lecture 4 asymptotic notationsjayavignesh86
 
CPSC 125 Ch 2 Sec 4
CPSC 125 Ch 2 Sec 4CPSC 125 Ch 2 Sec 4
CPSC 125 Ch 2 Sec 4David Wood
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.Tariq Khan
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsAbdullah Al-hazmy
 

Tendances (20)

Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Asymptotic Analysis
Asymptotic AnalysisAsymptotic Analysis
Asymptotic Analysis
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of Algorithms
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O Notation
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Complexity Analysis of Recursive Function
Complexity Analysis of Recursive FunctionComplexity Analysis of Recursive Function
Complexity Analysis of Recursive Function
 
A note on word embedding
A note on word embeddingA note on word embedding
A note on word embedding
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
Lecture 4 asymptotic notations
Lecture 4   asymptotic notationsLecture 4   asymptotic notations
Lecture 4 asymptotic notations
 
CPSC 125 Ch 2 Sec 4
CPSC 125 Ch 2 Sec 4CPSC 125 Ch 2 Sec 4
CPSC 125 Ch 2 Sec 4
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 

Similaire à Lecture 3 complexity

Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematicalbabuk110
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdfabay golla
 
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 analysisjayavignesh86
 
Lecture 4 (1).pptx
Lecture 4 (1).pptxLecture 4 (1).pptx
Lecture 4 (1).pptxworkwithiec
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesSreedhar Chowdam
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..KarthikeyaLanka1
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptxpallavidhade2
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxChSreenivasuluReddy
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms ISri Prasanna
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...2022cspaawan12556
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsNagendraK18
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1SSE_AndyLi
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusNANDINI SHARMA
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013Gary Short
 

Similaire à Lecture 3 complexity (20)

Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
 
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
 
Lecture 4 (1).pptx
Lecture 4 (1).pptxLecture 4 (1).pptx
Lecture 4 (1).pptx
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptx
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
AOA.pptx
AOA.pptxAOA.pptx
AOA.pptx
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013
 

Lecture 3 complexity

  • 1. Algorithms and Computational Complexity Reading: Chapter 9 (547-564) From the textbook 1
  • 2. Time Complexity • The subject of the analysis of algorithms consists of the study of their efficiency. • Two aspects of the algorithm efficiency are: 1. The amount of time required to execute the algorithm 2. The memory space it consumes. • In this chapter we introduce the basic techniques for calculating time efficiency (Time complexity). 2
  • 3. Dominant Operations The following main operations are used in the algorithms • Assignment(⃪) • Comparison(=, ≠, <, >, ≤, ≥) • Arithmetic operation(+, -, ×, ÷) • Logical operations(and, or, not) How long does each of these operations take to execute? • In general, assignments are very fast ,while other operations are slower. Multiplication and division are often slower than addition and subtraction. 3
  • 4. Time Complexity • The running time of an algorithm is defined to be an estimate of the number of operations performed by it given a particular number of input values. • Let T(n) be a measure of the time required to execute an algorithm of problem size n. We call T(n) the time complexity function of the algorithm. • If n is sufficiently small then the algorithm will not have a long running time. 4
  • 5. Time Complexity • How fast T(n) increases for large n? This is called the asymptotic behavior of the time complexity function, basically we will be focusing on the leading term of T(n). • In our time analysis we will restrict ourselves to the worst case behavior of an algorithm; that is, the longest running time for any input of size n. 5
  • 6. Example • If T(n) = 4n3 + 2n2 + n + 5 then T(n) = n3(4 + 2 1 5 ) 2 3 n n n and for large n we have T(n) ≃ 4n3. • We say that T(n) has a growth of order n3. • We say that one algorithm is more efficient then another if its worse case running time has a lower order of growth. 6
  • 7. Exercise What is the run-time complexity based on n for the following algorithm segment: 1. for i = 1 to n do 1.1 for j = 1 to n do 1.1.1 A(i,j) ⃪ x Solution The inner loop 1.1 is executed n times and the outer loop 1. is also executed n times. Hence, T(n) = n2 so that the growth is of order n2. 7
  • 8. Exercise Estimate the time complexity of the following algorithm: 1. i ⃪ 1 2. p ⃪ 1 3. for j = 1 to n do 3.1 p ⃪ p × i 3.2 i ⃪ i + 1 Solution It takes two assignment statements 1. and 2. to initialize the variables i and p. • The loop 3. is executed n times, and each time it executes two assignment statements and two arithmetic operations 3.1 and 3.2. • Thus, the time complexity of the algorithm is given by T(n) = 4n + 2 so the growth is of order n. 8
  • 9. Remark • In the latest algorithm, the dominant operation is the multiplication of 3.1. • Because multiplication takes more computer time to execute, we only count in this case the number of multiplications. Thus T(n) = n (multiplications) • In an algorithm, certain operations are usually dominant, so T(n) would be the number of dominant operations to accomplish the task. 9
  • 10. Order of Growth-O(g) notation • In the latest two problems we found a precise expression for the time complexity of the algorithm. What usually interests us is the order of growth. • Let g : ℕ ⟶ℕ . We say that a function f is order at most g or f “big O” of g if and only if there exists positive constant C and n0 ∈ ℕ such that |f(n)| ≤ C|g(n)|, for n ≥ n0 We write f(n) = O(g(n)). 10
  • 11. Example Find the minimum number of multiplications needed to calculate x13? Solution • suppose x13 is expressed as x13 = x × x ×…× x, 12 multiplications needed • If it is expressed as x13 = x8 × x4 × x then x2 = x × x, x4 = x2 × x2, x8 = x4 × x4.Hence x13 = x8 × x4 × x, 2 + 2 + 1 = 5 multiplications needed. 11
  • 12. Example Two algorithms, A and B ,have time complexities f and g respectively, where f(n) = 3 n2 - 3n + 1 and g(n)=n2.Determine which algorithm is faster. Solution First, find the value of n such that f(n)=g(n).Thus n2 = 3 n2 - 3n + 1 2 n2 - 3n + 1 = 0 (2n - 1)(n - 1) = 0 n = 1/2 or n = 1 12
  • 13. Example Take the larger value of n, that is for n > 1, suppose n = 2,then f(2) = 3(2)2 - 3(2) + 1 = 7 and g(2) = 22 = 4 Thus g(n) < f(n) for all n > 1.Hence g(n) is faster, so algorithm B is faster 13