algorithm analysis - washington state university
TRANSCRIPT
![Page 1: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/1.jpg)
11
Algorithm Analysis
CptS 223 – Advanced Data Structures
Larry HolderSchool of Electrical Engineering and Computer Science
Washington State University
![Page 2: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/2.jpg)
2
Purpose
Why bother analyzing code; isn’t getting it to work enough?
Estimate time and memory in the average case and worst caseIdentify bottlenecks, i.e., where to reduce timeSpeed up critical algorithms
![Page 3: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/3.jpg)
3
Algorithm
AlgorithmWell-defined computational procedure for transforming inputs to outputs
ProblemSpecifies the desired input-output relationship
Correct algorithmProduces the correct output for every possible input in finite timeSolves the problem
![Page 4: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/4.jpg)
4
Algorithm AnalysisPredict resource utilization of an algorithm
Running timeMemory
Dependent on architectureSerialParallelQuantum
![Page 5: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/5.jpg)
5
What to AnalyzeMain focus is on running time
Memory/time tradeoffMemory is cheap ($20 per GB)
Simple serial computing modelSingle processor, infinite memory
Cray XMT Supercomputer•Up to 64 TB (65,536 GB) shared memory•Up to 8000 processors•128 independent threads per processor•$150M
![Page 6: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/6.jpg)
6
What to AnalyzeRunning time T(N)
N is typically the size of the inputSorting?Multiplying two integers?Multiplying two matrices?Traversing a graph?
T(N) measures number of primitive operations performed
E.g., addition, multiplication, comparison, assignment
![Page 7: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/7.jpg)
7
Example
Costint sum (int n) 0
int partialSum; 0
partialSum = 0; 1for (int i = 1; i <= n; i++) 1+(N+1)+N
partialSum += i * i * i; N*(1+1+2)return partialSum; 1
T(N) = 6N+4
![Page 8: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/8.jpg)
8
What to Analyze
Worst-case running time Tworst(N)Average-case running time Tavg(N)Tavg(N) <= Tworst(N)Typically analyze worst-case behavior
Average case hard to computeWorst-case gives guaranteed upper bound
![Page 9: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/9.jpg)
9
Rate of Growth
Exact expressions for T(N) meaningless and hard to compareRate of growth
Asymptotic behavior of T(N) as N gets bigUsually expressed as fastest growing term in T(N), dropping constant coefficientsE.g., T(N) = 3N2 + N + 1 Θ(N2)
![Page 10: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/10.jpg)
10
Rate of Growth
T(N) = O(f(N)) if there are positive constants c and n0 such that T(N) ≤ cf(N) when N ≥ n0
Asymptotic upper bound“Big-Oh” notation
T(N) = Ω(g(N)) if there are positive constants c and n0 such that T(N) ≥ cg(N) when N ≥ n0
Asymptotic lower bound“Big-Omega” notation
![Page 11: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/11.jpg)
Rate of Growth
11
n0
f(N)g(N)
N
T(N)
g(N) = O(f(N))f(N) = Ω(g(N))
![Page 12: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/12.jpg)
12
Rate of Growth
T(N) = Θ(h(N)) if and only if T(N) = O(h(N)) and T(N) = Ω(h(N))
Asymptotic tight bound
T(N) = o(p(N)) if for all constants c there exists an n0 such that T(N) < cp(N) when N>n0
I.e., T(N) = o(p(N)) if T(N) = O(p(N)) and T(N) ≠Θ(p(N))“Little-oh” notation
![Page 13: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/13.jpg)
13
Rate of Growth
N2 = O(N2) = O(N3) = O(2N)N2 = Ω(1) = Ω(N) = Ω(N2)N2 = Θ(N2)N2 = o(N3)2N2 + 1 = Θ(?)N2 + N = Θ(?)
![Page 14: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/14.jpg)
14
Rate of Growth
Rule 1: If T1(N) = O(f(N)) and T2(N) = O(g(N)), then
T1(N) + T2(N) = O(f(N) + g(N))T1(N) * T2(N) = O(f(N) * g(N))
Rule 2: If T(N) is a polynomial of degree k, then T(N) = Θ(Nk)Rule 3: logk N = O(N) for any constant k
![Page 15: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/15.jpg)
15
Rate of Growth
![Page 16: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/16.jpg)
16
Example
Maximum subsequence sum problemGiven N integers A1, A2, …, AN, find the maximum value (≥ 0) of:
Don’t need the actual sequence (i,j)E.g., <1, -4, 4, 2, -3, 5, 8, -2> 16
∑ =
j
ik kA
![Page 17: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/17.jpg)
17
MaxSubSum: Solution 1
Compute each possible subsequence independently
MaxSubSum1 (A)maxSum = 0for i = 1 to N
for j = i to Nsum = 0for k = i to j
sum = sum + A[k]if (sum > maxSum)then maxSum = sum
return maxSum
![Page 18: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/18.jpg)
18
![Page 19: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/19.jpg)
19
Solution 1: Analysis
Three nested for loops, each iterating at most N timesOperations inside for loops take constant timeThus, T(N) = ?But, for loops don’t always iterate N times
![Page 20: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/20.jpg)
20
Solution 1: Analysis
More precisely
∑∑∑−
=
−
= =
Θ=1
0
1
)1()(N
i
N
ij
j
ikNT
![Page 21: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/21.jpg)
21
MaxSubSum: Solution 2
Note thatSo, no reason to re-compute sum each time MaxSubSum2 (A)
maxSum = 0for i = 1 to N
sum = 0for j = i to N
sum = sum + A[j]if (sum > maxSum)then maxSum = sum
return maxSum
∑∑ −
==+=
1j
ik kjj
ik k AAA
![Page 22: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/22.jpg)
22
![Page 23: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/23.jpg)
23
Solution 2: Analysis
)()(
)1()(
2
1
0
1
NNT
NTN
i
N
ij
Θ=
Θ=∑∑−
=
−
=
![Page 24: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/24.jpg)
24
MaxSubSum: Solution 3
Recursive, divide and conquerDivide sequence in half
A1..center and A(center+1)..N
Recursively compute MaxSubSum of left halfRecursively compute MaxSubSum of right halfCompute MaxSubSum of sequence constrained to use Acenter and A(center+1)
E.g., <1, -4, 4, 2, -3, 5, 8, -2>
![Page 25: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/25.jpg)
25
MaxSubSum: Solution 3MaxSubSum3 (A, i, j)
maxSum = 0if (i = j)then if A[i] > 0
then maxSum = A[i]else k = floor((i+j)/2)
maxSumLeft = MaxSubSum3(A,i,k)maxSumRight = MaxSubSum3(A,k+1,j)// compute maxSumThruCentermaxSum = Maximum (maxSumLeft,
maxSumRight,maxSumThruCenter)
return maxSum
![Page 26: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/26.jpg)
26
![Page 27: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/27.jpg)
27
![Page 28: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/28.jpg)
28
![Page 29: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/29.jpg)
29
Solution 3: Analysis
T(1) = Θ(1)T(N) = 2T(N/2) + Θ(N)T(N) = Θ(?)
![Page 30: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/30.jpg)
30
MaxSubSum: Solution 4
ObservationAny negative subsequence cannot be a prefix to the maximum sequenceOr, a positive, contiguous subsequence is always worth adding
T(N) = ?
MaxSubSum4 (A)maxSum = 0sum = 0for j = 1 to N
sum = sum + A[j]if (sum > maxSum)then maxSum = sumelse if (sum < 0)
then sum = 0return maxSum
![Page 31: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/31.jpg)
31
![Page 32: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/32.jpg)
32
MaxSubSum Running Times
Time in seconds.Does not include time to read array.38 min
26 days
![Page 33: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/33.jpg)
33
MaxSubSum Running Times
![Page 34: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/34.jpg)
34
MaxSubSum Running Times
![Page 35: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/35.jpg)
35
Logarithmic Behavior
T(N) = O(log2 N)Usually occurs when
Problem can be halved in constant timeSolutions to sub-problems combined in constant time
ExamplesBinary searchEuclid’s algorithmExponentiation
![Page 36: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/36.jpg)
36
Binary Search
Given an integer X and integers A0,A1,…,AN-1, which are presorted and already in memory, find i such that Ai=X, or return i = -1 if X is not in the input.T(N) = O(log2 N)T(N) = Θ(log2 N) ?
![Page 37: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/37.jpg)
37
![Page 38: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/38.jpg)
38
Euclid’s Algorithm
Compute the greatest common divisor gcd(M,N) between the integers M and N
I.e., largest integer that divides bothUsed in encryption
![Page 39: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/39.jpg)
Euclid’s Algorithm
39
Example: gcd(3360,225)•m = 3360, n = 225•m = 225, n = 210•m = 210, n = 15•m = 15, n = 0
![Page 40: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/40.jpg)
Euclid’s Algorithm: Analysis
Note: After two iterations, remainder is at most half its original value
Thm. 2.1: If M > N, then M mod N < M/2
T(N) = 2 log2 N = O(log2 N)log2225 = 7.8, T(225) = 16 (?)
Better worst case: T(N) = 1.44 log2 NT(225) = 11
Average case:T(225) = 6
40
47.1/)ln2ln12()( 2 += πNNT
![Page 41: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/41.jpg)
Exponentiation
Compute XN
Obvious algorithm:Observation
XN = XN/2 * XN/2 (for even N)XN = X(N-1)/2 * X(N-1)/2 * X (for odd N)
Minimize multiplications T(N)T(N) = 2 log2 N = O(log2 N)
41
pow(x,n)result = 1for i = 1 to n
result = result * xreturn result
![Page 42: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/42.jpg)
Exponentiation
42
T(N) = Θ(1), N ≤ 1T(N) = T(N/2) + Θ(1), N > 1
T(N) = O(log2 N)T(N) = Θ(log2 N) ?
![Page 43: Algorithm Analysis - Washington State University](https://reader031.vdocuments.site/reader031/viewer/2022020704/61fb67e72e268c58cd5dc9d1/html5/thumbnails/43.jpg)
Summary
Algorithm analysisBound running time as input gets bigRate of growth: O() and Θ()Compare algorithmsRecursion and logarithmic behavior
43