Title: Chapter 6 Algorithm Analysis
1Chapter 6 Algorithm Analysis
26.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.
3Big 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)
-
4Functions in order of increasing growth rate
56.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)
66.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
7Brute 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
8Brute Force Cont.
if( thisSum gt maxSum) maxSum
thisSum seqStart i seqEnd j
return maxSum Figure 6.4
9O(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
10O(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
116.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))
12Figure 6.9
13Various growth rates
146.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)
156.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).
16Binary 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
17Binary 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
186.7 Checking an Algorithm Analysis
- If it is possible, write codes to test your
algorithm for various large n.
196.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
20Summary
- Introduced some estimate tools for algorithm
analysis. - Introduced binary search.