Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency

Description:

The Design and Analysis of Algorithms Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency Mathematical Analysis of Non-recursive and Recursive Algorithms – PowerPoint PPT presentation

Number of Views:408
Avg rating:3.0/5.0
Slides: 10
Provided by: ValuedGa597
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency


1
Chapter 2 Fundamentals of the Analysis of
Algorithm Efficiency
The Design and Analysis of Algorithms
  • Mathematical Analysis of Non-recursive and
    Recursive Algorithms

2
Section 2.3. Mathematical Analysis of
Non-recursive Algorithms
  • Steps in mathematical analysis of non-recursive
    algorithms
  • Decide on parameter n indicating input size
  • Identify algorithms basic operation
  • Determine worst, average, and best case for input
    of size n
  • Set up summation for C(n) reflecting algorithms
    loop structure
  • Simplify summation using standard formulas (see
    Appendix A)

3
Example Selection sort 1
  • Input An array A0..n-1
  • Output Array A0..n-1 sorted in ascending order
  • for i ? 0 to n-2 do
  • min ? i
  • for j i 1 to n 1 do
  • if Aj lt Amin
  • min ? j
  • swap Ai and Amin

4
Example Selection sort 2
  • Basic operation comparison
  • Inner loop
  • n-1
  • S(i) ? 1 (n-1) (i 1) 1 n 1 i
  • j i1
  • Outer loop
  • n-2 n-2
    n-2 n-2
  • C(n) ? S(i) ? (n 1 i) ? (n 1)
    ? i
  • i 0 i 0
    i 0 i 0
  • n
  • Basic formula ? i n(n1) / 2
  • i 0
  • C(n) (n 1 )(n -1 ) (n-2)(n-1)/2 (n 1)
    2(n 1) (n 2) / 2
  • (n 1) n / 2 O(n2)

5
Section 2.4. Mathematical Analysis of Recursive
Algorithms
  • Steps in mathematical analysis of non-recursive
    algorithms
  • Decide on parameter n indicating input size
  • Identify algorithms basic operation
  • Determine worst, average, and best case for input
    of size n
  • Set up a recurrence relation and initial
    condition(s)
  • Solve the recurrence to obtain a closed form or
    estimate the order of magnitude of the solution
    (see Appendix B)

6
Important Recurrence Types
  • 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) cn T(1) d
  • Solution T(n) n(n1)/2 1 c d
    quadratic
  • One (constant) operation reduces problem size by
    half.
  • T(n) T(n/2) c T(1) d
  • Solution T(n) c log n d
    logarithmic
  • A pass through input reduces problem size by
    half.
  • T(n) 2T(n/2) cn T(1) d
  • Solution T(n) cn log n d n
    n log n

7
Example 1 Factorial
  • n! n(n-1)!
  • 0! 1
  • Recurrence relation
  • T(n) T(n-1) 1
  • T(1) 1
  • Telescoping
  • T(n) T(n-1) 1
  • T(n-1) T(n-2) 1
  • T(n-2) T(n-3) 1
  • T(2) T(1 ) 1
  • Add the equations and cross equal terms on
    opposite sides
  • T(n) T(1) (n-1)
  • n

8
Example 2 Binary Search
  • Recurrence Relation
  • T(n) T(n/2) 1, T(1) 1
  • Telescoping
  • T(n/2) T(n/4) 1
  • T(2) T(1) 1
  • Add the equations and cross equal terms on
    opposite sides
  • T(n) T(1) log(n) O(log(n))

9
Master Theorem A general divide-and-conquer
recurrence
T(n) aT(n/b) f (n) where f (n) ? T(nk)
a lt bk T(n) ? T(nk) a bk
T(n) ? T(nk log n ) a gt bk
T(n) ? T(n to the power of (logba)) Note the
same results hold with O instead of T.
Write a Comment
User Comments (0)
About PowerShow.com