Docsity

Algorithm Analysis: Finding the Maximum Element and Primality Testing - Prof. Eric Torng, Study notes of Computer Science

Toggle sidebar
bg1
Lecture 2
Example Algorithm Analysis: Finding the maximum element in an array
Lessons to learn
- Average-case versus worst-case analysis
- Average case analysis requires assumptions about inputs
- Recurrence relations and their importance on the analysis of algorithms
- Why focusing on order of growth rather than exact analysis is preferable
Maximum Element Problem Specification
Input: n integers in X[1], ..., X[n]
Output: Integers m and j such that m = X[j] = max (X[1], ..., X[n]) with j as large as possible
Proposed algorithm
1. [initialize] m = X[n]; j = n; k = n-1;
2. [All tested?] If k = 0, the algorithm terminates
3. [Compare] If X[k] <= m, goto step 5
4. [Update m] m = X[k]; j = k;
5. [Decrease k] k--; Goto step 2
Exact Analysis of Algorithm
Step Number Number of times executed
1 1
2 n
3 n-1
4 A ?
5 n-1
What can we say about A?
best-case: A = 0 (X[n] is the max element)
worst-case: A = n-1 (array X is sorted in decreasing order)
Average-case?
Assumption 1: Lets assume all items are distinct
Assumption 2: Lets assume that all permutations are equally likely
Brute force analysis for specific value of n = 3:
6 permutations equally likely
X[1] < X[2] < X[3] A = 0
X[1] < X[3] < X[2] A = 1
X[2] < X[1] < X[3] A = 0
X[2] < X[3] < X[1] A = 1
X[3] < X[1] < X[2] A = 1
X[3] < X[2] < X[1] A = 2
Average cost: (0+1+0+1+1+2)/6 = 5/6
pf2

Partial preview of the text

Download Algorithm Analysis: Finding the Maximum Element and Primality Testing - Prof. Eric Torng and more Study notes Computer Science in PDF only on Docsity!

Lecture 2 Example Algorithm Analysis: Finding the maximum element in an array Lessons to learn

  • Average-case versus worst-case analysis
    • Average case analysis requires assumptions about inputs
  • Recurrence relations and their importance on the analysis of algorithms
  • Why focusing on order of growth rather than exact analysis is preferable Maximum Element Problem Specification Input: n integers in X[1], ..., X[n] Output: Integers m and j such that m = X[j] = max (X[1], ..., X[n]) with j as large as possible Proposed algorithm
  1. [initialize] m = X[n]; j = n; k = n-1;
  2. [All tested?] If k = 0, the algorithm terminates
  3. [Compare] If X[k] <= m, goto step 5
  4. [Update m] m = X[k]; j = k;
  5. [Decrease k] k--; Goto step 2 Exact Analysis of Algorithm Step Number Number of times executed 1 1 2 n 3 n- 4 A? 5 n- What can we say about A? best-case: A = 0 (X[n] is the max element) worst-case: A = n-1 (array X is sorted in decreasing order) Average-case? Assumption 1: Lets assume all items are distinct Assumption 2: Lets assume that all permutations are equally likely Brute force analysis for specific value of n = 3: 6 permutations equally likely X[1] < X[2] < X[3] A = 0 X[1] < X[3] < X[2] A = 1 X[2] < X[1] < X[3] A = 0 X[2] < X[3] < X[1] A = 1 X[3] < X[1] < X[2] A = 1 X[3] < X[2] < X[1] A = 2 Average cost: (0+1+0+1+1+2)/6 = 5/

Recursive analysis for general n and specific values of A P(n,k) = the number of permutations of n objects for which A = k Previous example: P(3,0) = 2, P(3,1) = 3, P(3,2) = 1 Then desired E[A] = (1/n!) k k P(n,k) Analyze first element X[1] (divide and conquer analysis) Two possibilities: first element is max element (1 possible value) will increment A by 1 first element is NOT max element (n-1 possible values) will NOT increment A by 1 P(n,k) = P(n-1,k-1) + (n-1) P(n-1,k) Base Cases: P(n,0) = (n-1)! P(0,k) = 0 for k > 0 P(0,0) =? COMMENT: In most cases, we hopefully can develop recurrence relations with only 1 parameter! What happens if we start with element X[n] rather than X[1]? Example Algorithm Analysis: Testing primality Lesson to learn

  • Be careful about input size when dealing with numbers Primality problem specification Input: Positive integer n > 1 Y/N Question: Is n prime? Algorithm
  1. For i=2 to n1/2, do a) r = n mod i; b) If r = 0, terminate and return (no);
  2. Return (yes); Analysis: How many times is step 1 executed? Worst-case: n1/2^ (n is prime) Best-case: 1 (n is even) Average case:? Is this an efficient algorithm? Input size: B = log n Worst-case running time: cn1/ cn1/2^ = c2B/2^ which is not polynomial in the input size