Chapter 6 Algorithm Analysis - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Chapter 6 Algorithm Analysis

Description:

Big Oh notation is used to capture the most dominant term in a function, and to ... X=N, how many times should X be halved to become = 1? = floor(logN) ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 21
Provided by: Ken695
Category:

less

Transcript and Presenter's Notes

Title: Chapter 6 Algorithm Analysis


1
Chapter 6 Algorithm Analysis
  • Ken Nguyen
  • Spring 2002

2
6.1 What is Algorithm Analysis?
  • Algorithm a clearly specified set of
    instructions a computer follows to solve a
    problem.
  • Algorithm analysis a process of determining the
    amount of time, resource, etc. required when
    executing an algorithm.

3
Big Oh Notation
  • Big Oh notation is used to capture the most
    dominant term in a function, and to represent the
    growth rate.
  • Ex 100n3 2n530000n gtO(n5)

4
Functions in order of increasing growth rate
5
6.2 Examples of Algorithm Running Times
  • Min element in an array O(n)
  • Closest points in the plane, ie. Smallest
    distance pairs n(n-1)/2 gt O(n2)
  • Colinear points in the plane, ie. 3 points on a
    straight line n(n-1)(n-2)/6 gt O(n3)

6
6.3 The Max. Contiguous Subsequence
  • Given (possibly negative) integers A1, A2, ..,
    An, find (and identify the sequence corresponding
    to) the max. value of sum of Ak where k i -gt j.
    The max. contiguous sequence sum is zero if all
    the integer are negative.
  • -2, 11, -4, 13, -5, 2 gt20
  • 1, -3, 4, -2, -1, 6 gt 7

7
Brute Force Algorithm O(n3)
template ltclass Comparablegt Comparable
maxSubSum(const vectorltComparablegt a, int
seqStart, int seqEnd) int n a.size()
Comparable maxSum 0 for(int i 0 i lt n
i) for(int j i j lt n j)
Comparable thisSum 0 for(int k i k lt j
k) thisSum ak
8
Brute Force Cont.
if( thisSum gt maxSum) maxSum
thisSum seqStart i seqEnd j
return maxSum Figure 6.4
9
O(n2) contiguous . . .
  • template ltclass Comparablegt
  • Comparable maxSubsequenceSum(const
    vectorltComparablegt a,
  • int seqStart, int seqEnd)
  • int n a.size()
  • Comparable maxSum 0
  • for( int i 0 i lt n i)
  • Comparable thisSum 0
  • for( int j i j lt n j)
  • thisSum aj
  • if( thisSum gt maxSum)
  • maxSum thisSum
  • seqStart I
  • seqEnd j
  • return maxSum
  • //figure 6.5

10
O(n) Contiguous
  • template ltclass Comparablegt
  • Comparable maxSubsequenceSum(const
    vectorltComparablegt a,
  • int seqStart, int seqEnd)
  • int n a.size()
  • Comparable thisSum 0, maxSum 0
  • for( int i 0, j 0 j lt n j)
  • thisSum aj
  • if( thisSum gt maxSum)
  • maxSum thisSum
  • seqStart i
  • seqEnd j
  • else if( thisSum lt 0)
  • i j 1
  • thisSum 0
  • return maxSum
  • //figure 6.8

11
6.4 General Big-Oh Rules
  • Def (Big-Oh) T(n) is O(F(n)) if there are
    positive constants c and n0 such that
  • T(n)lt cF(n) when n gt n0
  • Def (Big-Omega) T(n) is O(F(n)) if there are
    positive constant c and n0 such that
  • T(n) gt cF(n) when n gt n0
  • Def (Big-Theta) T(n) is T(F(n)) if and only if
  • T(n) O(F(n)) and T(n) O(F(n)) 
  • Def (Little-Oh) T(n) o(F(n)) if and only if
  • T(n) O(F(n)) and T(n) ! T (F(n))

12
Figure 6.9
13
Various growth rates
14
6.5 The Logarithm
  • Def for any B, N gt 0 logBN K if Bk N
  • How many bits are required to represent N
    consecutive numbers? 2Bgt N gtceil(logN)
  • X 1, how many times should X be doubled before
    it gt N? gtceil(logN)
  • XN, how many times should X be halved to become
    lt 1? gt floor(logN)

15
6.6 Static Searching problem
  • Sequential search gtO(n)
  • Binary search (sorted data) gt O(logn)
  • Interpolation search (data must be uniform
    distributed) making guesses and search gtO(n) in
    worse case, but better than binary search on
    average Big-Oh performance, (impractical in
    general).

16
Binary Search
  • template lt class Comparablegt
  • int binarySearch(const vectorltComparablegt a,
    const Comparable x)
  • int low, mid
  • int high a.size() 1
  • while(low lt high)
  • mid (low high) / 2
  • if(amid lt x)
  • low mid 1
  • else if( amid gt x)
  • high mid - 1
  • else
  • return mid
  • return NOT_FOUND // NOT_FOUND -1
  • //figure 6.11 binary search using three-ways
    comparisons

17
Binary search
  • template lt class Comparablegt
  • int binarySearch(const vectorltComparablegt a,
    const Comparable x)
  • int low, mid
  • int high a.size() 1
  • while(low lt high)
  • mid (low high) / 2
  • if(amid lt x)
  • low mid 1
  • else
  • high mid
  • return (low high alow x) ? low
    NOT_FOUND
  • //figure 6.12 binary search using two ways
    comparisons

18
6.7 Checking an Algorithm Analysis
  • If it is possible, write codes to test your
    algorithm for various large n.

19
6.8 Limitations of Big-Oh Analysis
  • Big-Oh is an estimate tool for algorithm
    analysis. It ignores the costs of memory access,
    data movements, memory allocation, etc. gt hard
    to have a precise analysis.
  • Ex 2nlogn vs. 1000n. Which is faster? gt it
    depends on n

20
Summary
  • Introduced some estimate tools for algorithm
    analysis.
  • Introduced binary search.
Write a Comment
User Comments (0)
About PowerShow.com