Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
17
Mathematical Analysis of Recursive Algorithms Plan for Analyzing Recursive Algorithms . . . . . . . Factorial Function . . . . . . . . . . . . . . . . . . . . . . Recurrence for Factorial Function . . . . . . . . . . . . Solving the Factorial Recurrence . . . . . . . . . . . . . Towers of Hanoi Algorithm . . . . . . . . . . . . . . . . Recurrence for Towers of Hanoi . . . . . . . . . . . . . Solving the Towers of Hanoi Recurrence . . . . . . . Number of Bits in an Integer . . . . . . . . . . . . . . . Recurrence for BitCount Function . . . . . . . . . . . . Solving the BitCount Recurrence . . . . . . . . . . . . Fibonacci Function . . . . . . . . . . . . . . . . . . . . . . Recurrence for Fibonacci Function . . . . . . . . . . . Approximating the Fibonacci Recurrence . . . . . . . Empirical Analysis of Algorithms Plan for Empirical Analysis of Algorithms. . Empirical Analysis of UniqueElements . . . . Results of Empirical Analysis: Comparisons Results of Empirical Analysis: Timings. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
18 18 19 20 21 22 23 24 25 26 27 28 29 30 31 31 32 33 34
Analysis Framework Analysis of Algorithms. . . . . . . . . . . Eciency as a Function of Input Size Measuring Running Time. . . . . . . . . worst/best/average. . . . . . . . . . . . . Asymptotic Notation Order of Growth. . . . . . . . . . . . . . Asymptotic Order of Growth . . . . . Illustration of Asymptotic Order . . . Examples of Asymptotic Order . . . . Properties of Order of Growth . . . . Basic Asymptotic Eciency Classes. . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . .
2 2 3 4 5
6 . 6 . 7 . 8 . 9 10 11 12 12 13 14 15 2
Mathematical Analysis of Nonrecursive Algorithms Plan for Analyzing Nonrecursive Algorithms . . . . . . . . Useful Summation Formulas and Rules . . . . . . . . . . . Finding the Maximum . . . . . . . . . . . . . . . . . . . . . . . Element Uniqueness . . . . . . . . . . . . . . . . . . . . . . . . 1
Analysis Framework
Analysis of Algorithms
Issues:
Correctness. Is the algorithm correct? Time Eciency. How much time does the algorithm use? Space Eciency. How much extra space (in addition to the space needed for the input and output) does the algorithm use?
Approaches: Theoretical Analysis. Proof of correctness and big-Oh and related notations. Empirical Analysis. Testing and measurement over a range of instances.
Asymptotic Notation
Order of Growth
Typically, the basic operation count can be approximated as c g(n), where g(n) is the order of growth, often some simple function such as:
10 n
15
20
Chapter 2: Slide 6
Graph suggests (and we can prove) n + 3 sin n is: O(n): n + 3 sin n 2n when n 3. (n): n + 3 sin n n/2 when n 6.
CS 3343 Analysis of Algorithms Chapter 2: Slide 8
12
Chapter 2: Slide 12
1 = 1 + 1 + + 1 = n (n)
i=1 n
i = 1 + 2 + + n =
i=1 n i=1 n
ik = 1 + 2 k + + n k ai = 1 + a + + an =
i=1
n n n
an+1 1 (an ) a1
n n
(ai bi ) =
i=1 i=1 CS 3343 Analysis of Algorithms
ai
i=1
bi
i=1
c ai = c
i=1
ai
Chapter 2: Slide 13
Matrix Multiplication
algorithm MatrixMultiply(A, B : n nmatrices) // Multiplies matrix A times matrix B // Input: Two n n matrices A and B // Output: Matrix C = AB for i 0 to n 1 do for j 0 to n 1 do C[i, j] 0 for k 0 to n 1 do C[i, j] C[i, j] + A[i, k] B[k, j] return C Basic Operation: inner loop multiplications
n1 n1 n1
Performs
i=1
1 = n 1 comparisons
Chapter 2: Slide 14
Performs
i=0 j=0 k=0 CS 3343 Analysis of Algorithms
= n3 multiplications
Chapter 2: Slide 16
Element Uniqueness
algorithm UniqueElements(A[0..n 1]) // Returns true if all elements are dierent // Input: An array A // Output: Returns true if there are no duplicates for i 0 to n 2 do for j i + 1 to n 1 do if A[i] = A[j] then return false return true Basic Operation: inner loop comparison Best/Worst-Case: from 1 to n(n 1)/2
n2 n1
n(i+1) = (n1)+(n2)+ +1 =
i=0 CS 3343 Analysis of Algorithms k=1
k = n(n1)/2
10
18
Chapter 2: Slide 18
Factorial Function
algorithm Factorial(n) // Computes n! recursively // Input: A nonnegative integer n // Output: The value of n! if n = 0 then return 1 else return Factorial(n 1) n Input Size: Use number n (actually n has about log2 n bits) Basic Operation: multiplication
CS 3343 Analysis of Algorithms Chapter 2: Slide 19
to multiply Factorial(n 1) by n
Chapter 2: Slide 20
11
12
to move disk n
to compute n/2
Chapter 2: Slide 26
13
14
to compute F (n 2)
to add F (n 1) and F (n 2)
Chapter 2: Slide 29
Fibonacci Function
algorithm F (n) // Computes nth Fibonacci number recursively // Input: A nonnegative integer n // Output: The nth Fibonacci number if n = 0 or n = 1 then return n else return F (n 1) + F (n 2) Input Size: Use number n (actually n has about log2 n bits) Basic Operation: addition in else statement
CS 3343 Analysis of Algorithms Chapter 2: Slide 28
15
16
31
Prepare program implementing algorithm(s) and generating a sample of inputs. Run the algorithm(s) on the sample and record the data. Analyze the data.
CS 3343 Analysis of Algorithms Chapter 2: Slide 31
No duplicates line is n(n 1)/2. One duplicate line is least squares t, resulting in 0.24 n2 + 17 n 2042 n2/4. Predict: Half the time with one duplicate.
CS 3343 Analysis of Algorithms Chapter 2: Slide 33
800 1000
No duplicates and one duplicate lines are: 0.037 + 0.0016 n + 8.4 106 n2 . 0.073 + 0.00047 n + 5.2 106 n2.
CS 3343 Analysis of Algorithms Chapter 2: Slide 34
17
18
Algorithm Visualization
Visualization of Sorting Algorithms
35
http://homepages.dcc.ufmg.br/dorgival/applets/SortingPoints/SortingPoints.h http://maven.smith.edu/thiebaut/java/sort/demo.html
CS 3343 Analysis of Algorithms Chapter 2: Slide 35
19