0 index
1 Figure2.10
2
3 Figure2.11
4 Desigman "Genes"
5 Analysis of Algorithms
6 Figure 3.6
7 Figure 3.7
8 Figure 3.8
9 Order of magnitude
10 Figure 3.10
11 Figure 3.11
12 Comparison
13 Comparison 2
14 Comparison 3
15 Figure 3.13
16 Selection Sort
17 Selection Sort Algorithm
18 Sort Example
19 Explanation of example
20 Explanation of example 2
21 Explanation of example 3
22 Explanation of example 4
23 Selection Sort Analysis
24 Binary Search algorithm
25 Binary Search Algorithm (list must be sorted)
26 Binary Search Algorithm (list must be sorted) 2
27 Binary Search Algorithm (list must be sorted) 3
28 Figure 3.20
29 Figure 3.21
30 Figure 3.22
31 Figure 3.23
32 Orders of magnitude
33 Brute force algorithm
34 Figure 3.24
35 Figure 3.25
36 Explanation
37 Figure 3.26(a)
38 Figure 3.27
39 Orders of Magnitude - 2

outline
created using slideshow.cgi by Andy Harris















CSCI N301 Fundamental CS Concepts: n301/Complexity
1. Figure2.10
  • Algorithm to find the largest value in a list



































CSCI N301 Fundamental CS Concepts: n301/Complexity
2.
  • Figure2.10



































CSCI N301 Fundamental CS Concepts: n301/Complexity
3. Figure2.11
  • First draft of the pattern-matching algorithm



































CSCI N301 Fundamental CS Concepts: n301/Complexity
4. 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
    • step 1: T1 T2 T3 T4 T5 ... -> source
      P1 P2 P3 ... -> target
    • Step 2: T1 T2 T3 T4 T5 ...
      -------->P1 P2 P3
      slide



































CSCI N301 Fundamental CS Concepts: n301/Complexity
5. Analysis of Algorithms
  • Sequential Search for n items
    • unit of work = compares
  • Best case: 1 (first item)
  • Worst case: n (last or missing item)
  • Average case: n/2
  • Housekeeping (advancing index)
    • a constant multiple



































CSCI N301 Fundamental CS Concepts: n301/Complexity
6. Figure 3.6
  • Work = 2n



































CSCI N301 Fundamental CS Concepts: n301/Complexity
7. Figure 3.7
  • Work = cn for various values of c



































CSCI N301 Fundamental CS Concepts: n301/Complexity
8. Figure 3.8
  • Growth of Work = cn,br>for Various values of c



































CSCI N301 Fundamental CS Concepts: n301/Complexity
9. Order of magnitude
  • Sequential search is linear:0(n)
    • shape of graph
    • constant does not affect shape
  • Processing 2-D table is quadratic
    • o(n2)
    • shape of graph
  • 0(n2) always gets bigger than 0(n) eventually



































CSCI N301 Fundamental CS Concepts: n301/Complexity
10. Figure 3.10
  • Work = cn2for carious values of c



































CSCI N301 Fundamental CS Concepts: n301/Complexity
11. Figure 3.11
  • A comparison of n and n2



































CSCI N301 Fundamental CS Concepts: n301/Complexity
12. 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



































CSCI N301 Fundamental CS Concepts: n301/Complexity
13. 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



































CSCI N301 Fundamental CS Concepts: n301/Complexity
14. 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.



































CSCI N301 Fundamental CS Concepts: n301/Complexity
15. Figure 3.13
  • A Comparison of two extreme 0(n2) and 0(n) algorithms



































CSCI N301 Fundamental CS Concepts: n301/Complexity
16. Selection Sort
  • Input: unsorted list
  • Output: sorted list
  • Algorithm
    • find max value in unsorted section
    • swap with last value in unsorted section



































CSCI N301 Fundamental CS Concepts: n301/Complexity
17. 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
    • 4. Select the largest number in the unsorted section of the list
    • 5. Exchange this number with the last number in the unsorted list
    • 6. Move the marker for the unsorted section forwards one position
  • 7. Stop



































CSCI N301 Fundamental CS Concepts: n301/Complexity
18. 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



































CSCI N301 Fundamental CS Concepts: n301/Complexity
19. Explanation of example
  • To put 8 in place in the list 5, 7, 2, 8, 3 |
  • Compare 5 (largest so far) to 7
    • 7 becomes largest so far
  • Compare 7 (largest so far) to 2
  • Compare 7 (largest so far) to 8
    • 8 becomes largest so far
  • Compare 8 to 3
  • Total number of comparisons: 4 (which is 5 - 1)



































CSCI N301 Fundamental CS Concepts: n301/Complexity
20. Explanation of example 2
  • To put 7 in place in the list 5, 7, 2, 3 | 8
  • Compare 5 (largest so far) to 7
    • 7 becomes largest so far
  • Compare 7 to 2
  • Compare 7 to 3
  • 7 is the largest
  • Total number of comparisons: 3 (Which is 5 - 2)



































CSCI N301 Fundamental CS Concepts: n301/Complexity
21. 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)



































CSCI N301 Fundamental CS Concepts: n301/Complexity
22. 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)



































CSCI N301 Fundamental CS Concepts: n301/Complexity
23. Selection Sort Analysis
  • Work unit = comparisons
    • done in finding maximum element
  • (n -1) + (n - 2) + ... + 1
    • (1/2)n2 - (1/2)n + swapping
    • 0(n2)



































CSCI N301 Fundamental CS Concepts: n301/Complexity
24. 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)
    • shape of curve



































CSCI N301 Fundamental CS Concepts: n301/Complexity
25. 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



































CSCI N301 Fundamental CS Concepts: n301/Complexity
26. Binary Search Algorithm (list must be sorted) 2
    • 5. Set the value of m to the middle value between beginning and end.
    • 6. If NAME is equal t Nm, then name found at the midpoint between beginning and end, then do step 7 and 8
      • 7. Print the telephone number of that person, Tm
      • 8. Set the value of Found to YES



































CSCI N301 Fundamental CS Concepts: n301/Complexity
27. Binary Search Algorithm (list must be sorted) 3
    • 9. Else if NAME precedes Nm alphabetically, then set end = m - 1
    • 10. Else (NAME foloows Nm alphabetically) set beginning = m + 1
  • 11. If (Found = NO) then print the message 'I am sorry but that name is not in the directory'
  • 12. Stop



































CSCI N301 Fundamental CS Concepts: n301/Complexity
28. Figure 3.20
  • Binary Search Tree for a Seven-Element List



































CSCI N301 Fundamental CS Concepts: n301/Complexity
29. Figure 3.21
  • Values for n and lg n



































CSCI N301 Fundamental CS Concepts: n301/Complexity
30. Figure 3.22
  • A Comparison of n and lg n



































CSCI N301 Fundamental CS Concepts: n301/Complexity
31. Figure 3.23
  • Order-of-Magnitude Time Efficiency Summary



































CSCI N301 Fundamental CS Concepts: n301/Complexity
32. Orders of magnitude
  • 0(n2) = walking
  • 0(n) = driving
  • 0(lg n) = flying



































CSCI N301 Fundamental CS Concepts: n301/Complexity
33. Brute force algorithm
  • Hamiltonian circuit problem
  • Trace all paths in a graph
    • work unit = examine path
    • suppose only 2 choices per node
    • 2n paths, exponential
  • 0(2n)
  • Too costly, too slow, impractical



































CSCI N301 Fundamental CS Concepts: n301/Complexity
34. Figure 3.24
  • Four Connected Cities



































CSCI N301 Fundamental CS Concepts: n301/Complexity
35. Figure 3.25
  • Hamiltonian Circuits among all paths from A in figure 3.24 with four links



































CSCI N301 Fundamental CS Concepts: n301/Complexity
36. Explanation
  • For n-node city, the bottom of the tree has 2n nodes ans 2n path to examine
  • 0(2n) ==> exponential



































CSCI N301 Fundamental CS Concepts: n301/Complexity
37. Figure 3.26(a)
  • Comparisons of lg n, n2, and 2n



































CSCI N301 Fundamental CS Concepts: n301/Complexity
38. Figure 3.27
  • A comparison of four orders of magnitude



































CSCI N301 Fundamental CS Concepts: n301/Complexity
39. 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



































outline

Figure2.10

Figure2.11

Desigman "Genes"

Analysis of Algorithms

Figure 3.6

Figure 3.7

Figure 3.8

Order of magnitude

Figure 3.10

Figure 3.11

Comparison

Comparison 2

Comparison 3

Figure 3.13

Selection Sort

Selection Sort Algorithm

Sort Example

Explanation of example

Explanation of example 2

Explanation of example 3

Explanation of example 4

Selection Sort Analysis

Binary Search algorithm

Binary Search Algorithm (list must be sorted)

Binary Search Algorithm (list must be sorted) 2

Binary Search Algorithm (list must be sorted) 3

Figure 3.20

Figure 3.21

Figure 3.22

Figure 3.23

Orders of magnitude

Brute force algorithm

Figure 3.24

Figure 3.25

Explanation

Figure 3.26(a)

Figure 3.27

Orders of Magnitude - 2