![Page 1: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/1.jpg)
CSE 326: Data StructuresLecture #2
Analysis of Algorithms
Alon HalevyFall Quarter 2000
![Page 2: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/2.jpg)
Analysis of Algorithms• Analysis of an algorithm gives insight into
how long the program runs and how much memory it uses– time complexity– space complexity
• Why useful?• Input size is indicated by a number n
– sometimes have multiple inputs, e.g. m and n• Running time is a function of n
n, n2, n log n, 18 + 3n(log n2) + 5n3
![Page 3: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/3.jpg)
Simplifying the Analysis
• Eliminate low order terms4n + 5 4n0.5 n log n - 2n + 7 0.5 n log n2n + n3 + 3n 2n
• Eliminate constant coefficients4n n0.5 n log n n log nlog n2 = 2 log n log nlog3 n = (log3 2) log n log n
![Page 4: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/4.jpg)
Order Notation• BIG-O T(n) = O(f(n))
– Upper bound– Exist constants c and n0 such that
T(n) c f(n) for all n n0
• OMEGA T(n) = (f(n))– Lower bound – Exist constants c and n0 such that
T(n) c f(n) for all n n0
• THETA T(n) = θ (f(n))– Tight bound – θ(n) = O(n) = (n)
![Page 5: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/5.jpg)
Examples
n2 + 100 n = O(n2) = (n2) = (n2)( n2 + 100 n ) 2 n2 for n 10
( n2 + 100 n ) 1 n2 for n 0
n log n = O(n2)n log n = (n log n)n log n = (n)
![Page 6: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/6.jpg)
More on Order Notation
• Order notation is not symmetric; write2n2 + 4n = O(n2)
but neverO(n2) = 2n2 + 4n
right hand side is a crudification of the left
LikewiseO(n2) = O(n3) (n3) = (n2)
![Page 7: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/7.jpg)
A Few ComparisonsFunction #1
n3 + 2n2
n0.1
n + 100n0.1
5n5
n-152n/100
82log n
Function #2
100n2 + 1000
log n
2n + 10 log n
n!
1000n15
3n7 + 7n
![Page 8: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/8.jpg)
Race In3 + 2n2 100n2 + 1000vs.
![Page 9: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/9.jpg)
Race IIn0.1 log nvs.
![Page 10: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/10.jpg)
Race IIIn + 100n0.1 2n + 10 log nvs.
![Page 11: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/11.jpg)
Race IV5n5 n!vs.
![Page 12: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/12.jpg)
Race Vn-152n/100 1000n15vs.
![Page 13: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/13.jpg)
Race VI82log(n) 3n7 + 7nvs.
![Page 14: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/14.jpg)
The Losers WinFunction #1
n3 + 2n2
n0.1
n + 100n0.1
5n5
n-152n/100
82log n
Function #2
100n2 + 1000
log n
2n + 10 log n
n!
1000n15
3n7 + 7n
Better algorithm!
O(n2)
O(log n)
TIE O(n)
O(n5)
O(n15)
O(n6)
![Page 15: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/15.jpg)
Common Names
constant: O(1)logarithmic: O(log n) linear: O(n)log-linear: O(n log n)superlinear: O(n1+c) (c is a constant > 0)quadratic: O(n2)polynomial: O(nk) (k is a constant)exponential: O(cn) (c is a constant > 1)
![Page 16: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/16.jpg)
Kinds of Analysis• Running time may depend on actual data input, not
just length of input• Distinguish
– worst case• your worst enemy is choosing input
– best case– average case
• assumes some probabilistic distribution of inputs– amortized
• average time over many operations
![Page 17: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/17.jpg)
Analyzing Code
• C++ operations - constant time• consecutive stmts - sum of times• conditionals - sum of branches, condition• loops - sum of iterations• function calls - cost of function body• recursive functions- solve recursive equation
Above all, use your head!
![Page 18: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/18.jpg)
Nested Loopsfor i = 1 to n do for j = 1 to n do sum = sum + 1
2
11
1
1 nnn
i
n
j
n
i
![Page 19: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/19.jpg)
Nested Dependent Loopsfor i = 1 to n do for j = i to n do sum = sum + 1
ininn
i
n
i
n
i
n
j
n
i 1111
)1()1(1
2
2)1(
2)1()1( nnnnnnn
![Page 20: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/20.jpg)
Conditionals
• Conditionalif C then S1 else S2
time time(C) + Max( time(S1), time(S2) )
![Page 21: CSE 326: Data Structures Lecture #2 Analysis of Algorithms](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815acb550346895dc89bfa/html5/thumbnails/21.jpg)
Coming Up
• Thursday– Unix tutorial– First programming project!
• Friday– Finishing up analysis– A little on Stacks and Lists– Homework #1 goes out