SlideShare una empresa de Scribd logo
1 de 45
Descargar para leer sin conexión
Fundamentals of the Analysis
of Algorithm Efficiency
Analysis of Algorithms
Analysis of algorithms means to
investigate an algorithm’s efficiency with
respect to resources: running time and
memory space.
    Time efficiency: how fast an algorithm runs.
    Space efficiency: the space an algorithm requires.




                                                     2
Analysis Framework
Measuring an input’s size
Measuring running time
Orders of growth (of the algorithm’s
efficiency function)
Worst-base, best-case and average
efficiency


                                       3
Measuring Input Sizes
Efficiency is define as a function of input
size.
Input size depends on the problem.
  Example 1, what is the input size of the problem of
  sorting n numbers?
  Example 2, what is the input size of adding two n by n
  matrices?




                                                      4
Units for Measuring Running Time
Measure the running time using standard unit of time
measurements, such as seconds, minutes?
  Depends on the speed of the computer.
count the number of times each of an algorithm’s
operations is executed.
  Difficult and unnecessary
count the number of times an algorithm’s basic operation
is executed.
  Basic operation: the most important operation of the algorithm,
  the operation contributing the most to the total running time.
  For example, the basic operation is usually the most time-
  consuming operation in the algorithm’s innermost loop.

                                                                5
Input Size and Basic Operation Examples

                         Input size
 Problem                                      Basic operation
                         measure
 Search for a key in a   Number of items in
                                              Key comparison
 list of n items         list, n

 Add two n by n          Dimensions of
                                              addition
 matrices                matrices, n

 Polynomial              Order of the
                                              multiplication
 Evaluation              polynomial




                                                                6
Theoretical Analysis of Time Efficiency
 Time efficiency is analyzed by determining the number of
 repetitions of the basic operation as a function of input
 size. Assuming C(n) = (1/2)n(n-1),
        how much longer will the algorithm run if we double the input size?

                             input size                         The efficiency analysis
                                                                framework ignores the
                                                                multiplicative constants
                  T(n) ≈ copC (n)                               of C(n) and focuses on
                                                                the orders of growth of
                                                                the C(n).
running time       execution time         Number of times the
               for the basic operation     basic operation is
                                               executed
                                                                                    7
Why do we care about the order of growth of an
 algorithm’s efficiency function, i.e., the total number
 of basic operations?
 Examples
  GCD( 60, 24):
    Euclid’s algorithm?
    Consecutive Integer Counting?
  GCD( 31415, 14142):
    Euclid’s algorithm?
    Consecutive Integer Counting?
We care about how fast the efficiency function grows as n gets greater.
                                                                   8
Orders of Growth                   Exponential-growth functions




Orders of growth:
•     consider only the leading term of a formula
•     ignore the constant coefficient.
                                                            9
700


600


500

                                      n*n*n
400                                   n*n
                                      n log(n)
300                                   n
                                      log(n)

200


100


  0
      1   2   3   4   5   6   7   8



                                            10
Worst-Case, Best-Case, and Average-
     Case Efficiency
   Algorithm efficiency depends on the input size n
   For some algorithms efficiency depends on type
   of input.
      Example: Sequential Search
          Problem: Given a list of n elements and a search key K, find
          an element equal to K, if any.
          Algorithm: Scan the list and compare its successive
          elements with K until either a matching element is found
          (successful search) of the list is exhausted (unsuccessful
          search)
Given a sequential search problem of an input size of n,
what kind of input would make the running time the longest?
How many key comparisons?                                            11
Sequential Search Algorithm
ALGORITHM SequentialSearch(A[0..n-1], K)
//Searches for a given value in a given array by sequential search
//Input: An array A[0..n-1] and a search key K
//Output: Returns the index of the first element of A that matches K
or –1 if there are no matching elements
i 0
while i < n and A[i] ‡ K do
      i   i+1
if i < n       //A[I] = K
      return i
else
      return -1


                                                                12
Worst-Case, Best-Case, and
       Average-Case Efficiency
Worst case Efficiency
  Efficiency (# of times the basic operation will be executed) for the
  worst case input of size n.
  The algorithm runs the longest among all possible inputs of size n.
Best case
  Efficiency (# of times the basic operation will be executed) for the
  best case input of size n.
  The algorithm runs the fastest among all possible inputs of size n.
Average case:
  Efficiency (#of times the basic operation will be executed) for a
  typical/random input of size n.
  NOT the average of worst and best case
  How to find the average case efficiency?                            13
Summary of the Analysis Framework
Both time and space efficiencies are measured as functions of input
size.
Time efficiency is measured by counting the number of basic
operations executed in the algorithm. The space efficiency is
measured by the number of extra memory units consumed.
The framework’s primary interest lies in the order of growth of the
algorithm’s running time (space) as its input size goes infinity.
The efficiencies of some algorithms may differ significantly for
inputs of the same size. For these algorithms, we need to
distinguish between the worst-case, best-case and average case
efficiencies.



                                                               14
Asymptotic Growth Rate
Three notations used to compare orders of growth
of an algorithm’s basic operation count
  O(g(n)): class of functions f(n) that grow no faster than
  g(n)
  Ω(g(n)): class of functions f(n) that grow at least as fast
  as g(n)
  Θ (g(n)): class of functions f(n) that grow at same rate as
  g(n)



                                                        15
O-notation




             back


                    16
O-notation
Formal definition
   A function t(n) is said to be in O(g(n)), denoted t(n) ∈O(g(n)), if
   t(n) is bounded above by some constant multiple of g(n) for all
   large n, i.e., if there exist some positive constant c and some
   nonnegative integer n0 such that
   t(n) ≤ cg(n) for all n ≥ n0

Exercises:   prove the following using the above definition
   10n2 ∈ O(n2)
   10n2 + 2n ∈ O(n2)
   100n + 5 ∈ O(n2)
   5n+20 ∈ O(n)


                                                                  17
Ω-notation




             back


                    18
Ω-notation
Formal definition
   A function t(n) is said to be in Ω(g(n)), denoted t(n) ∈
   Ω(g(n)), if t(n) is bounded below by some constant multiple of
   g(n) for all large n, i.e., if there exist some positive constant c
   and some nonnegative integer n0 such that
   t(n) ≥ cg(n) for all n ≥ n0

Exercises:   prove the following using the above definition
   10n2 ∈ Ω(n2)
   10n2 + 2n ∈ Ω(n2)
   10n3 ∈ Ω(n2)



                                                                 19
Θ-notation




             back


                    20
Θ-notation
Formal definition
   A function t(n) is said to be in Θ(g(n)), denoted t(n) ∈
   Θ(g(n)), if t(n) is bounded both above and below by some
   positive constant multiples of g(n) for all large n, i.e., if there
   exist some positive constant c1 and c2 and some nonnegative
   integer n0 such that
    c2 g(n) ≤ t(n) ≤ c1 g(n) for all n ≥ n0

Exercises:   prove the following using the above definition
   10n2 ∈ Θ(n2)
   10n2 + 2n ∈ Θ(n2)
   (1/2)n(n-1) ∈ Θ(n2)



                                                                  21
>=
        Ω(g(n)), functions that grow at least as fast as g(n)


                            =
       Θ(g(n)), functions that grow at the same rate as g(n)
g(n)


                            <=
        O(g(n)), functions that grow no faster than g(n)




                                                        22
Theorem
If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then
t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}).
  The analogous assertions are true for the Ω-notation
  and Θ-notation.

Implication: The algorithm’s overall efficiency will
be determined by the part with a larger order of
growth, I.e., its least efficient part.
  For example,
     5n2 + 3nlogn ∈ O(n2)



                                                    23
Using Limits for Comparing Orders of
                   Growth

                      0       order of growth of T(n)    <       order of growth of g(n)

                      c>0       order of growth of T(n)      =    order of growth of g(n)
limn→∞ T(n)/g(n) =
                          ∞    order of growth of T(n)       >   order of growth of g(n)


   Examples:
   • 10n        vs.               2n2

   • n(n+1)/2   vs.               n2

   • logb n     vs.              logc n
                                                                                     24
L’Hôpital’s rule
If
     limn→∞ f(n) = limn→∞ g(n) = ∞

     The derivatives f ´, g ´ exist,
                     f(n)         f ´(n)
               lim            lim
Then           n→∞   g(n) =   n→∞ g ´(n)




            • Example: log2n vs. n

                                           25
Summary of How to Establish Orders of Growth
   of an Algorithm’s Basic Operation Count

 Method 1: Using limits.
    L’Hôpital’s rule
 Method 2: Using the theorem.
 Method 3: Using the definitions of O-,
 Ω-, and Θ-notation.




                                         26
The time
                                    efficiencies of
                                    a large number

       Basic Efficiency classes     of algorithms
                                    fall into only a
                                    few classes.
fast
             1         constant    High time efficiency

           log n     logarithmic
             n           linear
           n log n      n log n
              n2      quadratic
              n3         cubic
             2n      exponential
slow
             n!        factorial   low time efficiency
                                                 27
Homework 1
 Problem 2, 4.b, 5, and 6.a of Exercises
 2.2, Page 60. Due date: Jan. 27 (Thu).




                                      28
Time Efficiency of Nonrecursive Algorithms

   Example: Finding the largest element in a given array
Algorithm MaxElement (A[0..n-1])
//Determines the value of the largest element in a given array
//Input: An array A[0..n-1] of real numbers
//Output: The value of the largest element in A
maxval      A[0]
for i    1 to n-1 do
   if A[i] > maxval
         maxval      A[i]
return maxval


                                                                 29
Time Efficiency of Nonrecursive Algorithms

Steps in mathematical analysis of nonrecursive algorithms:
  Decide on parameter n indicating input size

  Identify algorithm’s basic operation

  Check whether the number of times the basic operation is executed
  depends only on the input size n. If it also depends on the type of
  input, investigate worst, average, and best case efficiency
  separately.

  Set up summation for C(n) reflecting the number of times the
  algorithm’s basic operation is executed.

  Simplify summation using standard formulas (see Appendix A)
                                                                 30
Example: Element uniqueness problem

Algorithm UniqueElements (A[0..n-1])
//Checks whether all the elements in a given array are
   distinct
//Input: An array A[0..n-1]
//Output: Returns true if all the elements in A are distinct
   and false otherwise
for i     0 to n - 2 do
   for j     i + 1 to n – 1 do
         if A[i] = A[j] return false
return true

                                                          31
Example: Matrix multiplication

Algorithm MatrixMultiplication(A[0..n-1, 0..n-1], B[0..n-1, 0..n-1] )
//Multiplies two square matrices of order n by the definition-based
   algorithm
//Input: two n-by-n matrices A and B
//Output: Matrix C = AB
for i    0 to n - 1 do
   for j    0 to n – 1 do                          M(n) ∈ Θ (n3)
         C[i, j]    0.0
          for k    0 to n – 1 do
                  C[i, j]   C[i, j] + A[i, k] * B[k, j]
return C

                                                                      32
Example: Find the number of binary digits
in the binary representation of a positive
decimal integer
Algorithm Binary(n)
//Input: A positive decimal integer n (stored in binary
   form in the computer)
//Output: The number of binary digits in n’s binary
   representation
count     0
while n >= 1 do //or while n > 0 do
   count    count + 1
   n    ⎣n/2⎦
return count                 C(n) ∈ Θ (⎣ log2n ⎦ +1)

                                                      33
Example: Find the number of binary digits
in the binary representation of a positive
decimal integer
Algorithm Binary(n)
//Input: A positive decimal integer n (stored in binary
   form in the computer)
//Output: The number of binary digits in n’s binary
   representation
count     1
while n > 1 do
   count    count + 1
   n    ⎣n/2⎦
return count

                                                      34
Mathematical Analysis of
Recursive Algorithms
 Recursive evaluation of n!
 Recursive solution to the Towers of
 Hanoi puzzle
 Recursive solution to the number of
 binary digits problem




                                       35
Example Recursive evaluation of n ! (1)
Iterative Definition
     F(n) = 1                       if n = 0
     n * (n-1) * (n-2)… 3 * 2 * 1              if n > 0

Recursive definition
     F(n) = 1                       if n = 0
     n * F(n-1)                     if n > 0

   Algorithm F(n)
      if n=0
              return 1                    //base case
      else
              return F (n -1) * n         //general case
                                                          36
Example Recursive evaluation of n ! (2)
Two Recurrences
  The one for the factorial function value: F(n)
     F(n) = F(n – 1) * n for every n > 0
     F(0) = 1
  The one for number of multiplications to compute n!,
  M(n)
      M(n) = M(n – 1) + 1 for every n > 0
      M(0) = 0

      M(n) ∈ Θ (n)
                                                    37
Steps in Mathematical Analysis of
             Recursive Algorithms
Decide on parameter n indicating input size

Identify algorithm’s basic operation

Determine worst, average, and best case for input of size n

Set up a recurrence relation and initial condition(s) for C(n)-the
number of times the basic operation will be executed for an
input of size n (alternatively count recursive calls).

Solve the recurrence or estimate the order of magnitude of the
solution (see Appendix B)

                                                                38
The Towers of Hanoi Puzzle
Recurrence Relations
  Denote the total number of moving as M(n)
    M(n) = 2M(n – 1) + 1 for every n > 0
    M(1) = 1              M(n) ∈ Θ (2n)
Succinctness vs. efficiency
 Be careful with recursive algorithms because
 their succinctness mask their inefficiency.


                                                39
Example: Find the number of binary digits
  in the binary representation of a positive
  decimal integer (A recursive solution)
Algorithm Binary(n)
//Input: A positive decimal integer n (stored in binary
   form in the computer)
//Output: The number of binary digits in n’s binary
   representation
if n = 1 //The binary representation of n contains only one bit.
   return 1
else //The binary representation of n contains more than one bit.
   return BinRec( ⎣n/2⎦) + 1

                                                                40
Smoothness Rule
Let f(n) be a nonnegative function defined on the set of
natural numbers. f(n) is call smooth if it is eventually
nondecreasing and
f(2n) ∈ Θ (f(n))
   Functions that do not grow too fast, including logn, n, nlogn, and
   nα where α>=0 are smooth.
Smoothness rule
let T(n) be an eventually nondecreasing function and f(n)
be a smooth function. If
T(n) ∈ Θ (f(n))     for values of n that are powers of b,
where b>=2, then
     T(n) ∈ Θ (f(n)) for any n.
                                                                  41
Important Recurrence Types
Decrease-by-one recurrences
  A decrease-by-one algorithm solves a problem by exploiting a
  relationship between a given instance of size n and a smaller size n – 1.
   Example: n!
  The recurrence equation for investigating the time efficiency of such
  algorithms typically has the form
  T(n) = T(n-1) + f(n)
Decrease-by-a-constant-factor recurrences
  A decrease-by-a-constant algorithm solves a problem by dividing its
  given instance of size n into several smaller instances of size n/b,
  solving each of them recursively, and then, if necessary, combining the
  solutions to the smaller instances into a solution to the given instance.
  Example: binary search.
  The recurrence equation for investigating the time efficiency of such
  algorithms typically has the form
   T(n) = aT(n/b) + f (n)


                                                                       42
Decrease-by-one Recurrences
One (constant) operation reduces problem size by one.
  T(n) = T(n-1) + c              T(1) = d
  Solution: T(n) = (n-1)c + d                   linear

A pass through input reduces problem size by one.
   T(n) = T(n-1) + c n             T(1) = d
   Solution: T(n) = [n(n+1)/2 – 1] c + d       quadratic



                                                     43
Decrease-by-a-constant-factor
     recurrences – The Master Theorem
T(n) = aT(n/b) + f (n),      where f (n) ∈ Θ(nk) , k>=0

1.   a < bk        T(n) ∈ Θ(nk)
2.   a = bk        T(n) ∈ Θ(nk log n )
3.   a > bk        T(n) ∈ Θ(nlog b a)

     Examples:
       T(n) = T(n/2) + 1              Θ(logn)
       T(n) = 2T(n/2) + n             Θ(nlogn)
       T(n) = 3 T(n/2) + n            Θ(nlog23)

                                                          44
Homework 2
 Exercise 2.3, P68
   Problem 6
 Exercise 2.4, P76-77
   Problem 1, part b., c., e.
   Problem 7
 Due: Feb. 3 (Thu)


                                45

Más contenido relacionado

La actualidad más candente

Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIMohamed Loey
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree methodRajendran
 
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
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsMohamed Loey
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithmRezwan Siam
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structureZaid Shabbir
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencySaranya Natarajan
 
Divide And Conquer.pptx
Divide And Conquer.pptxDivide And Conquer.pptx
Divide And Conquer.pptxSHAILIPATEL19
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and boundAbhishek Singh
 
Basics & asymptotic notations
Basics & asymptotic notationsBasics & asymptotic notations
Basics & asymptotic notationsRajendran
 

La actualidad más candente (20)

Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms II
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Big O Notation
Big O NotationBig O Notation
Big O Notation
 
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
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
 
Divide And Conquer.pptx
Divide And Conquer.pptxDivide And Conquer.pptx
Divide And Conquer.pptx
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Recursion
RecursionRecursion
Recursion
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 
Basics & asymptotic notations
Basics & asymptotic notationsBasics & asymptotic notations
Basics & asymptotic notations
 

Destacado

Algorithm Analysis and Design Class Notes
Algorithm Analysis and Design Class NotesAlgorithm Analysis and Design Class Notes
Algorithm Analysis and Design Class NotesKumar Avinash
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms NotesAndres Mendez-Vazquez
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2Kumar
 
04 brute force
04 brute force04 brute force
04 brute forceHira Gul
 
Algorithm chapter 5
Algorithm chapter 5Algorithm chapter 5
Algorithm chapter 5chidabdu
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: IntroductionAndres Mendez-Vazquez
 
Sienna 12 huffman
Sienna 12 huffmanSienna 12 huffman
Sienna 12 huffmanchidabdu
 
Cs6402 design and analysis of algorithms impartant part b questions appasami
Cs6402 design and analysis of algorithms  impartant part b questions appasamiCs6402 design and analysis of algorithms  impartant part b questions appasami
Cs6402 design and analysis of algorithms impartant part b questions appasamiappasami
 
CS 6402 – DESIGN AND ANALYSIS OF ALGORITHMS questio
CS 6402 – DESIGN AND ANALYSIS OF ALGORITHMS questioCS 6402 – DESIGN AND ANALYSIS OF ALGORITHMS questio
CS 6402 – DESIGN AND ANALYSIS OF ALGORITHMS questioKarthik Venkatachalam
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyappasami
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unitchidabdu
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsSwapnil Agrawal
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsArvind Krishnaa
 

Destacado (20)

Algorithm Analysis and Design Class Notes
Algorithm Analysis and Design Class NotesAlgorithm Analysis and Design Class Notes
Algorithm Analysis and Design Class Notes
 
ADA complete notes
ADA complete notesADA complete notes
ADA complete notes
 
Analysis of Algorithm
Analysis of AlgorithmAnalysis of Algorithm
Analysis of Algorithm
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
 
Mea notes
Mea notesMea notes
Mea notes
 
04 brute force
04 brute force04 brute force
04 brute force
 
Algorithm chapter 5
Algorithm chapter 5Algorithm chapter 5
Algorithm chapter 5
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction
 
Sienna 12 huffman
Sienna 12 huffmanSienna 12 huffman
Sienna 12 huffman
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
Cs6402 design and analysis of algorithms impartant part b questions appasami
Cs6402 design and analysis of algorithms  impartant part b questions appasamiCs6402 design and analysis of algorithms  impartant part b questions appasami
Cs6402 design and analysis of algorithms impartant part b questions appasami
 
CS 6402 – DESIGN AND ANALYSIS OF ALGORITHMS questio
CS 6402 – DESIGN AND ANALYSIS OF ALGORITHMS questioCS 6402 – DESIGN AND ANALYSIS OF ALGORITHMS questio
CS 6402 – DESIGN AND ANALYSIS OF ALGORITHMS questio
 
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
DESIGN AND ANALYSIS OF ALGORITHM (DAA)DESIGN AND ANALYSIS OF ALGORITHM (DAA)
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unit
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 

Similar a Algorithm chapter 2

CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
02 order of growth
02 order of growth02 order of growth
02 order of growthHira Gul
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsAbdullah Al-hazmy
 
Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1Vai Jayanthi
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Deepak John
 
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 ...TechVision8
 
lecture 1
lecture 1lecture 1
lecture 1sajinsc
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfMemMem25
 

Similar a Algorithm chapter 2 (20)

CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Lec1
Lec1Lec1
Lec1
 
Big Oh.ppt
Big Oh.pptBig Oh.ppt
Big Oh.ppt
 
Chapter two
Chapter twoChapter two
Chapter two
 
02 order of growth
02 order of growth02 order of growth
02 order of growth
 
Algorithms
Algorithms Algorithms
Algorithms
 
Slide2
Slide2Slide2
Slide2
 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpall...
Algorithm Class at KPHB  (C, C++ Course Training Institute in KPHB, Kukatpall...Algorithm Class at KPHB  (C, C++ Course Training Institute in KPHB, Kukatpall...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpall...
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
 
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 ...
 
lecture 1
lecture 1lecture 1
lecture 1
 
Lec1
Lec1Lec1
Lec1
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 

Más de chidabdu

Sienna 11 graphs
Sienna 11 graphsSienna 11 graphs
Sienna 11 graphschidabdu
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamicchidabdu
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsortschidabdu
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heapschidabdu
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bstchidabdu
 
Sienna 5 decreaseandconquer
Sienna 5 decreaseandconquerSienna 5 decreaseandconquer
Sienna 5 decreaseandconquerchidabdu
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforcechidabdu
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysischidabdu
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 introchidabdu
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitationschidabdu
 
Unit 5 I/O organization
Unit 5   I/O organizationUnit 5   I/O organization
Unit 5 I/O organizationchidabdu
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1chidabdu
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11chidabdu
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10chidabdu
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 
Algorithm chapter 8
Algorithm chapter 8Algorithm chapter 8
Algorithm chapter 8chidabdu
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7chidabdu
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6chidabdu
 

Más de chidabdu (20)

Sienna 11 graphs
Sienna 11 graphsSienna 11 graphs
Sienna 11 graphs
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsorts
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bst
 
Sienna 5 decreaseandconquer
Sienna 5 decreaseandconquerSienna 5 decreaseandconquer
Sienna 5 decreaseandconquer
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforce
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysis
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
 
Unit 5 I/O organization
Unit 5   I/O organizationUnit 5   I/O organization
Unit 5 I/O organization
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
Algorithm chapter 8
Algorithm chapter 8Algorithm chapter 8
Algorithm chapter 8
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 

Último

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 

Último (20)

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 

Algorithm chapter 2

  • 1. Fundamentals of the Analysis of Algorithm Efficiency
  • 2. Analysis of Algorithms Analysis of algorithms means to investigate an algorithm’s efficiency with respect to resources: running time and memory space. Time efficiency: how fast an algorithm runs. Space efficiency: the space an algorithm requires. 2
  • 3. Analysis Framework Measuring an input’s size Measuring running time Orders of growth (of the algorithm’s efficiency function) Worst-base, best-case and average efficiency 3
  • 4. Measuring Input Sizes Efficiency is define as a function of input size. Input size depends on the problem. Example 1, what is the input size of the problem of sorting n numbers? Example 2, what is the input size of adding two n by n matrices? 4
  • 5. Units for Measuring Running Time Measure the running time using standard unit of time measurements, such as seconds, minutes? Depends on the speed of the computer. count the number of times each of an algorithm’s operations is executed. Difficult and unnecessary count the number of times an algorithm’s basic operation is executed. Basic operation: the most important operation of the algorithm, the operation contributing the most to the total running time. For example, the basic operation is usually the most time- consuming operation in the algorithm’s innermost loop. 5
  • 6. Input Size and Basic Operation Examples Input size Problem Basic operation measure Search for a key in a Number of items in Key comparison list of n items list, n Add two n by n Dimensions of addition matrices matrices, n Polynomial Order of the multiplication Evaluation polynomial 6
  • 7. Theoretical Analysis of Time Efficiency Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size. Assuming C(n) = (1/2)n(n-1), how much longer will the algorithm run if we double the input size? input size The efficiency analysis framework ignores the multiplicative constants T(n) ≈ copC (n) of C(n) and focuses on the orders of growth of the C(n). running time execution time Number of times the for the basic operation basic operation is executed 7
  • 8. Why do we care about the order of growth of an algorithm’s efficiency function, i.e., the total number of basic operations? Examples GCD( 60, 24): Euclid’s algorithm? Consecutive Integer Counting? GCD( 31415, 14142): Euclid’s algorithm? Consecutive Integer Counting? We care about how fast the efficiency function grows as n gets greater. 8
  • 9. Orders of Growth Exponential-growth functions Orders of growth: • consider only the leading term of a formula • ignore the constant coefficient. 9
  • 10. 700 600 500 n*n*n 400 n*n n log(n) 300 n log(n) 200 100 0 1 2 3 4 5 6 7 8 10
  • 11. Worst-Case, Best-Case, and Average- Case Efficiency Algorithm efficiency depends on the input size n For some algorithms efficiency depends on type of input. Example: Sequential Search Problem: Given a list of n elements and a search key K, find an element equal to K, if any. Algorithm: Scan the list and compare its successive elements with K until either a matching element is found (successful search) of the list is exhausted (unsuccessful search) Given a sequential search problem of an input size of n, what kind of input would make the running time the longest? How many key comparisons? 11
  • 12. Sequential Search Algorithm ALGORITHM SequentialSearch(A[0..n-1], K) //Searches for a given value in a given array by sequential search //Input: An array A[0..n-1] and a search key K //Output: Returns the index of the first element of A that matches K or –1 if there are no matching elements i 0 while i < n and A[i] ‡ K do i i+1 if i < n //A[I] = K return i else return -1 12
  • 13. Worst-Case, Best-Case, and Average-Case Efficiency Worst case Efficiency Efficiency (# of times the basic operation will be executed) for the worst case input of size n. The algorithm runs the longest among all possible inputs of size n. Best case Efficiency (# of times the basic operation will be executed) for the best case input of size n. The algorithm runs the fastest among all possible inputs of size n. Average case: Efficiency (#of times the basic operation will be executed) for a typical/random input of size n. NOT the average of worst and best case How to find the average case efficiency? 13
  • 14. Summary of the Analysis Framework Both time and space efficiencies are measured as functions of input size. Time efficiency is measured by counting the number of basic operations executed in the algorithm. The space efficiency is measured by the number of extra memory units consumed. The framework’s primary interest lies in the order of growth of the algorithm’s running time (space) as its input size goes infinity. The efficiencies of some algorithms may differ significantly for inputs of the same size. For these algorithms, we need to distinguish between the worst-case, best-case and average case efficiencies. 14
  • 15. Asymptotic Growth Rate Three notations used to compare orders of growth of an algorithm’s basic operation count O(g(n)): class of functions f(n) that grow no faster than g(n) Ω(g(n)): class of functions f(n) that grow at least as fast as g(n) Θ (g(n)): class of functions f(n) that grow at same rate as g(n) 15
  • 16. O-notation back 16
  • 17. O-notation Formal definition A function t(n) is said to be in O(g(n)), denoted t(n) ∈O(g(n)), if t(n) is bounded above by some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such that t(n) ≤ cg(n) for all n ≥ n0 Exercises: prove the following using the above definition 10n2 ∈ O(n2) 10n2 + 2n ∈ O(n2) 100n + 5 ∈ O(n2) 5n+20 ∈ O(n) 17
  • 18. Ω-notation back 18
  • 19. Ω-notation Formal definition A function t(n) is said to be in Ω(g(n)), denoted t(n) ∈ Ω(g(n)), if t(n) is bounded below by some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such that t(n) ≥ cg(n) for all n ≥ n0 Exercises: prove the following using the above definition 10n2 ∈ Ω(n2) 10n2 + 2n ∈ Ω(n2) 10n3 ∈ Ω(n2) 19
  • 20. Θ-notation back 20
  • 21. Θ-notation Formal definition A function t(n) is said to be in Θ(g(n)), denoted t(n) ∈ Θ(g(n)), if t(n) is bounded both above and below by some positive constant multiples of g(n) for all large n, i.e., if there exist some positive constant c1 and c2 and some nonnegative integer n0 such that c2 g(n) ≤ t(n) ≤ c1 g(n) for all n ≥ n0 Exercises: prove the following using the above definition 10n2 ∈ Θ(n2) 10n2 + 2n ∈ Θ(n2) (1/2)n(n-1) ∈ Θ(n2) 21
  • 22. >= Ω(g(n)), functions that grow at least as fast as g(n) = Θ(g(n)), functions that grow at the same rate as g(n) g(n) <= O(g(n)), functions that grow no faster than g(n) 22
  • 23. Theorem If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}). The analogous assertions are true for the Ω-notation and Θ-notation. Implication: The algorithm’s overall efficiency will be determined by the part with a larger order of growth, I.e., its least efficient part. For example, 5n2 + 3nlogn ∈ O(n2) 23
  • 24. Using Limits for Comparing Orders of Growth 0 order of growth of T(n) < order of growth of g(n) c>0 order of growth of T(n) = order of growth of g(n) limn→∞ T(n)/g(n) = ∞ order of growth of T(n) > order of growth of g(n) Examples: • 10n vs. 2n2 • n(n+1)/2 vs. n2 • logb n vs. logc n 24
  • 25. L’Hôpital’s rule If limn→∞ f(n) = limn→∞ g(n) = ∞ The derivatives f ´, g ´ exist, f(n) f ´(n) lim lim Then n→∞ g(n) = n→∞ g ´(n) • Example: log2n vs. n 25
  • 26. Summary of How to Establish Orders of Growth of an Algorithm’s Basic Operation Count Method 1: Using limits. L’Hôpital’s rule Method 2: Using the theorem. Method 3: Using the definitions of O-, Ω-, and Θ-notation. 26
  • 27. The time efficiencies of a large number Basic Efficiency classes of algorithms fall into only a few classes. fast 1 constant High time efficiency log n logarithmic n linear n log n n log n n2 quadratic n3 cubic 2n exponential slow n! factorial low time efficiency 27
  • 28. Homework 1 Problem 2, 4.b, 5, and 6.a of Exercises 2.2, Page 60. Due date: Jan. 27 (Thu). 28
  • 29. Time Efficiency of Nonrecursive Algorithms Example: Finding the largest element in a given array Algorithm MaxElement (A[0..n-1]) //Determines the value of the largest element in a given array //Input: An array A[0..n-1] of real numbers //Output: The value of the largest element in A maxval A[0] for i 1 to n-1 do if A[i] > maxval maxval A[i] return maxval 29
  • 30. Time Efficiency of Nonrecursive Algorithms Steps in mathematical analysis of nonrecursive algorithms: Decide on parameter n indicating input size Identify algorithm’s basic operation Check whether the number of times the basic operation is executed depends only on the input size n. If it also depends on the type of input, investigate worst, average, and best case efficiency separately. Set up summation for C(n) reflecting the number of times the algorithm’s basic operation is executed. Simplify summation using standard formulas (see Appendix A) 30
  • 31. Example: Element uniqueness problem Algorithm UniqueElements (A[0..n-1]) //Checks whether all the elements in a given array are distinct //Input: An array A[0..n-1] //Output: Returns true if all the elements in A are distinct and false otherwise for i 0 to n - 2 do for j i + 1 to n – 1 do if A[i] = A[j] return false return true 31
  • 32. Example: Matrix multiplication Algorithm MatrixMultiplication(A[0..n-1, 0..n-1], B[0..n-1, 0..n-1] ) //Multiplies two square matrices of order n by the definition-based algorithm //Input: two n-by-n matrices A and B //Output: Matrix C = AB for i 0 to n - 1 do for j 0 to n – 1 do M(n) ∈ Θ (n3) C[i, j] 0.0 for k 0 to n – 1 do C[i, j] C[i, j] + A[i, k] * B[k, j] return C 32
  • 33. Example: Find the number of binary digits in the binary representation of a positive decimal integer Algorithm Binary(n) //Input: A positive decimal integer n (stored in binary form in the computer) //Output: The number of binary digits in n’s binary representation count 0 while n >= 1 do //or while n > 0 do count count + 1 n ⎣n/2⎦ return count C(n) ∈ Θ (⎣ log2n ⎦ +1) 33
  • 34. Example: Find the number of binary digits in the binary representation of a positive decimal integer Algorithm Binary(n) //Input: A positive decimal integer n (stored in binary form in the computer) //Output: The number of binary digits in n’s binary representation count 1 while n > 1 do count count + 1 n ⎣n/2⎦ return count 34
  • 35. Mathematical Analysis of Recursive Algorithms Recursive evaluation of n! Recursive solution to the Towers of Hanoi puzzle Recursive solution to the number of binary digits problem 35
  • 36. Example Recursive evaluation of n ! (1) Iterative Definition F(n) = 1 if n = 0 n * (n-1) * (n-2)… 3 * 2 * 1 if n > 0 Recursive definition F(n) = 1 if n = 0 n * F(n-1) if n > 0 Algorithm F(n) if n=0 return 1 //base case else return F (n -1) * n //general case 36
  • 37. Example Recursive evaluation of n ! (2) Two Recurrences The one for the factorial function value: F(n) F(n) = F(n – 1) * n for every n > 0 F(0) = 1 The one for number of multiplications to compute n!, M(n) M(n) = M(n – 1) + 1 for every n > 0 M(0) = 0 M(n) ∈ Θ (n) 37
  • 38. Steps in Mathematical Analysis of Recursive Algorithms Decide on parameter n indicating input size Identify algorithm’s basic operation Determine worst, average, and best case for input of size n Set up a recurrence relation and initial condition(s) for C(n)-the number of times the basic operation will be executed for an input of size n (alternatively count recursive calls). Solve the recurrence or estimate the order of magnitude of the solution (see Appendix B) 38
  • 39. The Towers of Hanoi Puzzle Recurrence Relations Denote the total number of moving as M(n) M(n) = 2M(n – 1) + 1 for every n > 0 M(1) = 1 M(n) ∈ Θ (2n) Succinctness vs. efficiency Be careful with recursive algorithms because their succinctness mask their inefficiency. 39
  • 40. Example: Find the number of binary digits in the binary representation of a positive decimal integer (A recursive solution) Algorithm Binary(n) //Input: A positive decimal integer n (stored in binary form in the computer) //Output: The number of binary digits in n’s binary representation if n = 1 //The binary representation of n contains only one bit. return 1 else //The binary representation of n contains more than one bit. return BinRec( ⎣n/2⎦) + 1 40
  • 41. Smoothness Rule Let f(n) be a nonnegative function defined on the set of natural numbers. f(n) is call smooth if it is eventually nondecreasing and f(2n) ∈ Θ (f(n)) Functions that do not grow too fast, including logn, n, nlogn, and nα where α>=0 are smooth. Smoothness rule let T(n) be an eventually nondecreasing function and f(n) be a smooth function. If T(n) ∈ Θ (f(n)) for values of n that are powers of b, where b>=2, then T(n) ∈ Θ (f(n)) for any n. 41
  • 42. Important Recurrence Types Decrease-by-one recurrences A decrease-by-one algorithm solves a problem by exploiting a relationship between a given instance of size n and a smaller size n – 1. Example: n! The recurrence equation for investigating the time efficiency of such algorithms typically has the form T(n) = T(n-1) + f(n) Decrease-by-a-constant-factor recurrences A decrease-by-a-constant algorithm solves a problem by dividing its given instance of size n into several smaller instances of size n/b, solving each of them recursively, and then, if necessary, combining the solutions to the smaller instances into a solution to the given instance. Example: binary search. The recurrence equation for investigating the time efficiency of such algorithms typically has the form T(n) = aT(n/b) + f (n) 42
  • 43. Decrease-by-one Recurrences One (constant) operation reduces problem size by one. T(n) = T(n-1) + c T(1) = d Solution: T(n) = (n-1)c + d linear A pass through input reduces problem size by one. T(n) = T(n-1) + c n T(1) = d Solution: T(n) = [n(n+1)/2 – 1] c + d quadratic 43
  • 44. Decrease-by-a-constant-factor recurrences – The Master Theorem T(n) = aT(n/b) + f (n), where f (n) ∈ Θ(nk) , k>=0 1. a < bk T(n) ∈ Θ(nk) 2. a = bk T(n) ∈ Θ(nk log n ) 3. a > bk T(n) ∈ Θ(nlog b a) Examples: T(n) = T(n/2) + 1 Θ(logn) T(n) = 2T(n/2) + n Θ(nlogn) T(n) = 3 T(n/2) + n Θ(nlog23) 44
  • 45. Homework 2 Exercise 2.3, P68 Problem 6 Exercise 2.4, P76-77 Problem 1, part b., c., e. Problem 7 Due: Feb. 3 (Thu) 45