n301/Complexity n301.tplt Figure2.10 Algorithm to find the largest value in a list Figure2.10 Figure2.11 First draft of the pattern-matching algorithm Desigman "Genes" Genes: Sequence of nucleotides Human genome: over 100,000 genes => 3.5 billion nucle. A genes ~= over 10,000 nucleotides A probe: search pattern for a gene ex: Genome: ...TCAGGCTAATCG...
Probe: TAATC A pattern matching problem. Matching process
Analysis of Algorithms Sequential Search for n items Best case: 1 (first item) Worst case: n (last or missing item) Average case: n/2 Housekeeping (advancing index) Figure 3.6 Work = 2n Figure 3.7 Work = cn for various values of c Figure 3.8 Growth of Work = cn,br>for Various values of c Order of magnitude Sequential search is linear:0(n) Processing 2-D table is quadratic 0(n2) always gets bigger than 0(n) eventually Figure 3.10 Work = cn2for carious values of c Figure 3.11 A comparison of n and n2 Comparison Pentium Pro 200 runs at bout 75 megaflops (75 million floating-point operations per second Cray T3E-900 can achieve speeds of 670 gigaflops (670 billion floating-point operations per second Race between Tortoise and Hare Pentuim runs the 0(n) algorithm and the Cray runs the 0(n2) algorithm Comparison 2 timing result:
nPcCray
7500.00001 sec0.00000084 sec
7,5000.0001 sec0.000084 sec
75,0000.001 sec0.0084 sec
750,0000.01 sec0.84 sec
7,500,0000.1 sec84 sec
75,000,0001 sec8400 sec = 2.3 hr
750,000,00010sec840,000 sec = 9.7 days
Comparison 3 At beginning the Cray has the lead, but by the end the Pentium surpassed the Cray "The order of magnitude of the algorithm being executed can play a more important role than the raw speed of the computer. Figure 3.13 A Comparison of two extreme 0(n2) and 0(n) algorithms Selection Sort Input: unsorted list Output: sorted list Algorithm Selection Sort Algorithm 1. Get values for n and the n list items 2. Set the marker for the unsorted section at the end of the list 3. Repeat step 4 through 6 until the unsorted section of the list is empty 7. Stop Sort Example
     output               input
2 3 5 7 8 <--- 5 7 2 8 3

Unsorted ListSorted List
5 7 2 8 3
5 7 2 8 3
5 7 2 38
5 2 37 8
2 35 7 8
23 5 7 8
2 3 5 7 8
Explanation of example To put 8 in place in the list 5, 7, 2, 8, 3 | Compare 5 (largest so far) to 7

Compare 7 (largest so far) to 2 Compare 7 (largest so far) to 8 Compare 8 to 3 Total number of comparisons: 4 (which is 5 - 1)
Explanation of example 2 To put 7 in place in the list 5, 7, 2, 3 | 8 Compare 5 (largest so far) to 7 Compare 7 to 2 Compare 7 to 3 7 is the largest Total number of comparisons: 3 (Which is 5 - 2) Explanation of example 3 To put 5 in place in the list 5, 3, 2 | 7, 8 Compare 5 (largest so far) to 3 Compare 5 to 2 5 is the largest Total number of comparison: 2 (which is 5 - 3) Explanation of example 4 To put 3 in place in the list 2, 3 | 5, 7, 8 Compare 2(largest so far) to 3 3 is the largest Total number of comparisons: 1 (which is 5 - 4) Selection Sort Analysis Work unit = comparisons (n -1) + (n - 2) + ... + 1 Binary Search algorithm Requires sorted list Work unit = compares Compare at successive minpoints as list gets cut in half Work = number of times n is divisible by 2 = log2(n) = lg n lg n = 0(lg n) Binary Search Algorithm (list must be sorted) 1. Get values for NAME, n, N1, ..., Nn and T1, ..., Tn 2. Set the value of beginning to 1 and set the value of Found to NO 3. Set the value of end to n 4. Repeat steps 5 through 10 until either Found = YES or end is less than beginning Binary Search Algorithm (list must be sorted) 2 Binary Search Algorithm (list must be sorted) 3 11. If (Found = NO) then print the message 'I am sorry but that name is not in the directory' 12. Stop Figure 3.20 Binary Search Tree for a Seven-Element List Figure 3.21 Values for n and lg n Figure 3.22 A Comparison of n and lg n Figure 3.23 Order-of-Magnitude Time Efficiency Summary Orders of magnitude 0(n2) = walking 0(n) = driving 0(lg n) = flying Brute force algorithm Hamiltonian circuit problem Trace all paths in a graph 0(2n) Too costly, too slow, impractical Figure 3.24 Four Connected Cities Figure 3.25 Hamiltonian Circuits among all paths from A in figure 3.24 with four links Explanation For n-node city, the bottom of the tree has 2n nodes ans 2n path to examine 0(2n) ==> exponential Figure 3.26(a) Comparisons of lg n, n2, and 2n Figure 3.27 A comparison of four orders of magnitude Orders of Magnitude - 2 0(lg n) = flying 0(n) = driving 0(n2) = walking 0(n3) = crawling 0(n4) = barely moving 0(n5) = no visible progress 0(2n) = forget it, it will never happen