1、Asymptotic Behavior,Algorithm : Design & Analysis 2,In the last class,Goal of the Course Algorithm: the Concept Algorithm Analysis: the Contents Average and Worst-Case Analysis Lower Bounds and the Complexity of Problems,Asymptotic Behavior,Asymptotic growth rate The Sets , and Complexity Class An E
2、xample: Searching an Ordered Array Improved Sequential Search Binary Search Binary Search Is Optimal,How to Compare Two Algorithm?,Simplifying the analysis assumption that the total number of steps is roughly proportional to the number of basic operations counted (a constant coefficient) only the le
3、ading term in the formula is considered constant coefficient is ignoredAsymptotic growth rate large n vs. smaller n,Relative Growth Rate,g,(g):functions that grow at least as fast as g,(g):functions that grow at the same rate as g,(g):functions that grow no faster as g,The Set “Big Oh”,Definition Gi
4、ving g:NR+, then (g) is the set of f:NR+, such that for some cR+ and some n0N, f(n)cg(n) for all nn0. A function f(g) if limnf(n)/g(n)=c Note: c may be zero. In that case, f(g), “little Oh”,Example,Let f(n)=n2, g(n)=nlgn, then: f(g), since limnf(n)/g(n)= limnn2/nlgn= limnn/lgn= limn1/(1/nln2)= g(f),
5、 since limng(n)/f(n)= limnnlogn / n2= 0,Logarithmic Functions and Powers,So, log2nO(n),The Result Generalized,The log function grows more slowly than any positive power of n lgn o(n) for any 0,Factorials and Exponential Functions,n! grows faster than 2n for any positive integer n.,The Sets and ,Defi
6、nition Giving g:NR+, then (g) is the set of f:NR+, such that for some cR+ and some n0N, f(n)cg(n) for all nn0. A function f(g) if limnf(n)/g(n)0 Note: the limit may be infinity Definition Giving g:NR+, then (g) = (g) (g) A function f(g) if limnf(n)/g(n)=c,0c,How Functions Grow,Increasing Computer Sp
7、eed,Sorting a Array of 1 Million Numbers,Computer A 1000 Mips,Computer B 10 Mips,Using insertion sort, taking time 2n2: 2000 seconds!,Using merge sort, taking time 50nlogn: 100 seconds!,Properties of O, and ,Transitive property: If fO(g) and gO(h), then fO(h) Symmetric propertiesfO(g) if and only if
8、 g(f) f(g) if and only if g(f) Order of sum function O(f+g)=O(max(f,g),Complexity Class,Let S be a set of f:NR* under consideration, define the relation on S as following: fg iff. f(g)then, is an equivalence. Each set (g) is an equivalence class, called complexity class. We usually use the simplest
9、element as possible as the representative, so, (n), (n2), etc.,Searching an Ordered Array,The Problem: Specification Input: an array E containing n entries of numeric type sorted in non-decreasing order a value K Output: index for which K=Eindex, if K is in E, or, -1, if K is not in E,Sequential Sea
10、rch: the Procedure,The Procedure Int seqSearch(int E, int n, int K) 1. Int ans, index; 2. Ans=-1; / Assume failure 3. For (index=0; indexn; index+) 4. If (K=Eindex) ans=index;/success! 5. break; 6. return ans,Searching a Sequence,For a given K, there are two possibilities K in E (say, with probabili
11、ty q), then K may be any one of Ei (say, with equal probability, that is 1/n) K not in E (with probability 1-q), then K may be located in any one of gap(i) (say, with equal probability, that is 1/(n+1),Improved Sequential Search,Since E is sorted, when an entrie larger than K is met, no nore compari
12、son is needed Worst-case complexity: n, unchanged Average complexityNote: A(n)(n),Roughly n/2,Divide and Conquer,If we compare K to every jth entry, we can locate the small section of E that may contain K. To locate a section, roughly n/j steps at most To search in a section, j steps at most So, the
13、 worst-case complexity: (n/j)+j, with j selected properly, (n/j)+j(n) However, we can use the same strategy in the small sections recursively,Choose j = n,Binary Search,int binarySearch(int E, int first, int last, int K) if (lastfirst) index=-1; else int mid=(first+last)/2 if (K=Emid) index=mid; els
14、e if (KEmid) index=binarySearch(E, first, mid-1, K) else if (KEmid) index=binarySearch(E, mid+1, last, K) return index;,Worst-case Complexity of Binary Search,Observation: with each call of the recursive procedure, only at most half entries left for further consideration. At most lg n calls can be m
15、ade if we want to keep the range of the section left not less than 1. So, the worst-case complexity of binary search is lg n+1=lg(n+1),Average Complexity of Binary Search,Observation: for most cases, the number of comparison is or is very close to that of worst-case particularly, if n=2k-1, all fail
16、ure position need exactly k comparisons Assumption: all success position are equally likely (1/n) n=2k-1,Average Complexity of Binary Search,Average complexity Note: We count the sum of st, which is the number of inputs for which the algorithm does t comparisons, and if n=2k-1, st=2t-1,Decision Tree
17、,A decision tree for A and a given input of size n is a binary tree whose nodes are labeled with numbers between 0 and n-1 Root: labeled with the index first compared If the label on a particular node is i, then the left child is labeled the index next compared if KEi, and no branch for the case of
18、K=Ei.,Binary Search Is Optimal,If the number of comparison in the worst case is p, then the longest path from root to a leaf is p-1, so there are at most 2p-1 node in the tree. There are at least n node in the tree. (We can prove that For all i0,1,n-1, exist a node with the label i.) Conclusion: n 2
19、p-1, that is plg(n+1),Binary Search Is Optimal,For all i0,1,n-1, exist a node with the label i. Proof: if otherwise, suppose that i doesnt appear in the tree, make up two inputs E1 and E2, with E1i=K, E2i=K, KK, for any ji, (0jn-1), E1j=E2j. (Arrange the values to keeping the order in both arrays).
20、Since i doesnt appear in the tree, for both K and K, the algorithm behave alike exactly, and give the same outputs, of which at least one is wrong, so A is not a right algorithm.,Finding the Fake Coin,Situation You have 70 coins that are all supposed to be gold coins of the same weight, but you know
21、 that one coin is fake and weighs less than the others. You have a balance scale; you can put any number of coins on each side of the scale at one time, and it will tell you if the two sides weigh the same, or which side is lighter if they dont weigh the same. Problem Outline an algorithm for finding the fake coin. How many weighings will you do?,Outline of an Algorithm,123:2446,18:916,4754:5562,2431:3240,=,4749:5052,6365:6668,5557:5860,47:48,53:54,50:51,Done!,Is it optimal?,How about unknown weighing?,Home Assignment,pp.63 1.23 1.27 1.31 1.34 1.37 1.45,