9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis...
Transcript of 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis...
![Page 1: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/1.jpg)
9/10/2018
Algorithms & Data Structures
Siyuan Jiang, Sept. 2018 1
Analysis of Algorithms
![Page 2: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/2.jpg)
Siyuan Jiang, Sept. 2018 2
Email me if the office door is closed
![Page 3: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/3.jpg)
Grades have been emailed
github.com/COSC311/assignment01-userid
Check out the my latest commit for the comments and changes.
Siyuan Jiang, Sept. 2018 3
Click on the latest commit, and you can
![Page 4: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/4.jpg)
This class meeting
•Algorithm Analysis•Big O, Big Omega, Big Theta
•Recursion
•Useful data structure: ArrayList
Siyuan Jiang, Sept. 2018 4
![Page 5: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/5.jpg)
Algorithm: a step by step procedure
Siyuan Jiang, Sept. 2018 5
Data structure: a systematic way to organize the data
![Page 6: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/6.jpg)
Algorithms
Siyuan Jiang, Sept. 2018 6
![Page 7: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/7.jpg)
Algorithm Analysis
Analyze what?
Siyuan Jiang, Sept. 2018 7
Time and space
Analogy: ways of transportation
![Page 8: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/8.jpg)
Different algorithms
Siyuan Jiang, Sept. 2018 8
Examples of algorithms:Ways of sorting
elementary sorts, mergesort, quick sort, …
![Page 9: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/9.jpg)
Different algorithms
Siyuan Jiang, Sept. 2018 9
Examples of algorithms:Ways of sorting
elementary sorts, mergesort, quick sort, priority queue sorting
26 year-oldIn 1959
![Page 10: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/10.jpg)
Why is the running time important?
Siyuan Jiang, Sept. 2018 10
30 trillion web pages
![Page 11: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/11.jpg)
Why is the running time important?
Siyuan Jiang, Sept. 2018 11
30 trillion web pages30,000,000,000,000 web pages
![Page 12: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/12.jpg)
Why is the running time important?
Siyuan Jiang, Sept. 2018 12
30 trillion web pages
Suppose we have an algorithm to search these web pagesThe speed of the algorithm is:1000 web pages in 1/1000 second
How long will the algorithm take to find a web page?
30,000,000,000,000 web pages
![Page 13: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/13.jpg)
Why is the running time important?
Siyuan Jiang, Sept. 2018 13
30 trillion web pages
Suppose we have an algorithm to search these web pagesThe speed of the algorithm is:1000 web pages in 1/1000 second
How long will the algorithm take to find a web page?
30,000,000,000,000 web pages
~ 347 days
![Page 14: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/14.jpg)
Analysis of Algorithm
Siyuan Jiang, Sept. 2018 14
• Most algorithms transform input objects into output objects.• The running time of an algorithm typically grows with the input
size.• Average case time is often difficult to determine.• We focus on the worst case running time.• Easier to analyze• Crucial to applications such as games,
finance and robotics
0
20
40
60
80
100
120
Ru
nn
ing
Tim
e
1000 2000 3000 4000
Input Size
best case
average case
worst case
![Page 15: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/15.jpg)
Siyuan Jiang, Sept. 2018 15
https://www.youtube.com/watch?v=eTosD9ZxPTU
![Page 16: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/16.jpg)
How do we measure?
Siyuan Jiang, Sept. 2018 16
Experimental Studies
![Page 17: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/17.jpg)
1st step: implementation
Siyuan Jiang, Sept. 2018 17
Experimental Process
![Page 18: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/18.jpg)
Siyuan Jiang, Sept. 2018 18
![Page 19: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/19.jpg)
2nd step: set up monitoring method
Siyuan Jiang, Sept. 2018 19
Experimental Process
![Page 20: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/20.jpg)
Siyuan Jiang, Sept. 2018 20
repeat1(‘*’, 50000)
repeat2(‘*’, 50000)
![Page 21: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/21.jpg)
3rd step: run the implementation with different inputs
Siyuan Jiang, Sept. 2018 21
Experimental Process
![Page 22: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/22.jpg)
Siyuan Jiang, Sept. 2018 22
![Page 23: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/23.jpg)
Siyuan Jiang, Sept. 2018 23
![Page 24: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/24.jpg)
Assignment (part 1/3) – P.187, P-4.60 –deadline Friday 10 pm
Siyuan Jiang, Sept. 2018 24
![Page 25: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/25.jpg)
Assignment (part 2/3) – P.187, P-4.60 –deadline Friday 10 pm
Siyuan Jiang, Sept. 2018 25
![Page 26: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/26.jpg)
Assignment (part 3/3) – deadline Friday 10 pm
Siyuan Jiang, Sept. 2018 26
Perform an experimental analysis of the two algorithms prefixAverage1 and prefixAverage2, from Section 4.3.4.Visualize their running times as a function of the input size with a log-log chart (using Excel or other software tools. You don’t have to use Java to draw the chart).
![Page 27: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/27.jpg)
break
Siyuan Jiang, Sept. 2018 27
![Page 28: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/28.jpg)
Limitations of Experiments
• It is necessary to implement the algorithm, which may be difficult
• Results may not be indicative of the running time on other inputs not included in the experiment.
• In order to compare two algorithms, the same hardware and software environments must be used
Siyuan Jiang, Sept. 2018 28
![Page 29: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/29.jpg)
Theoretical Analysis
• Uses a high-level description of the algorithm instead of an implementation
• Characterizes running time as a function of the input size, n
• Takes into account all possible inputs
• Allows us to evaluate the speed of an algorithm independent of the hardware/software environment
Siyuan Jiang, Sept. 2018 29
![Page 30: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/30.jpg)
Assumption: Primitive Operations
• Basic computations performed by an algorithm
• Identifiable in pseudocode (high-level language, that is irrelevant to programming languages, running platforms, etc.)
• Largely independent from the programming language
• Exact definition not important (we will see why later, when we introduce big-Oh)
• Assumed to take a constant amount of time
Siyuan Jiang, Sept. 2018 30
![Page 31: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/31.jpg)
Primitive Operations
• Examples:• Evaluating an expression
• Assigning a value to a variable
• Indexing into an array
• Calling a method
• Returning from a method
Siyuan Jiang, Sept. 2018 31
![Page 32: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/32.jpg)
Counting Primitive Operations
Siyuan Jiang, Sept. 2018 32
![Page 33: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/33.jpg)
Counting Primitive Operations
• Step 3: 2 ops, 4: 2 ops, 5: 2n ops, 6: 2n ops, 7: 0 to n ops, 8: 1 op
Siyuan Jiang, Sept. 2018 33
![Page 34: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/34.jpg)
Estimating Running Time
• Algorithm arrayMax executes 5n + 5 primitive operations in the worst case, 4n + 5 in the best case. Define:
a = Time taken by the fastest primitive operation
b = Time taken by the slowest primitive operation
• Let T(n) be worst-case time of arrayMax. Thena (4n + 5) T(n) b(5n + 5)
• Hence, the running time T(n) is bounded by two linear functions
Siyuan Jiang, Sept. 2018 34
![Page 35: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/35.jpg)
Functions Graphed Using “Normal” Scale
Siyuan Jiang, Sept. 2018 35
g(n) = 2ng(n) = 1
g(n) = lg n
g(n) = n
g(n) = n2
g(n) = n3
Slide by Matt Stallmann included with permission.
![Page 36: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/36.jpg)
Seven Important Functions
Seven functions that often appear in algorithm analysis:
Constant 1
Logarithmic log n
Linear n
N-Log-N n log n
Quadratic n2
Cubic n3
Exponential 2n
In a log-log chart, the slope of the line corresponds to
the growth rate
Siyuan Jiang, Sept. 2018 36
1E+0
1E+2
1E+4
1E+6
1E+8
1E+10
1E+12
1E+14
1E+16
1E+18
1E+20
1E+22
1E+24
1E+26
1E+28
1E+30
1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
n
T(n
)
Cubic
Quadratic
Linear
![Page 37: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/37.jpg)
Big-Oh High-level idea
Siyuan Jiang, Sept. 2018 37
Limitations of Experiments
• Need implementation
• Results depend on inputs not included in the experiment.
• In order to compare two algorithms, the same hardware and software environments must be used
![Page 38: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/38.jpg)
Big-Oh Notation
• Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constantsc and n0 such that
f(n) cg(n) for n n0
• Example: 2n + 10 is O(n)
• 2n + 10 cn
• (c 2) n 10
• n 10/(c 2)
• Pick c = 3 and n0 = 10
Siyuan Jiang, Sept. 2018 38
1
10
100
1,000
10,000
1 10 100 1,000
n
3n
2n+10
n
![Page 39: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/39.jpg)
arrayMax: T(n) is O(n)
Siyuan Jiang, Sept. 2018 39
Let T(n) be worst-case time of arrayMax. Thena (4n + 5) T(n) b(5n + 5)
![Page 40: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/40.jpg)
Big-Oh Example
• Example: the function n2 is not O(n)
• n2 cn
• n c
• The above inequality cannot be satisfied since c must be a constant
Siyuan Jiang, Sept. 2018 40
1
10
100
1,000
10,000
100,000
1,000,000
1 10 100 1,000n
n^2
100n
10n
n
![Page 41: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/41.jpg)
More Big-Oh Examples
Siyuan Jiang, Sept. 2018 41
7n - 27n-2 is O(n)
need c > 0 and n0 1 such that 7 n - 2 c n for n n0
this is true for c = 7 and n0 = 1
3 n3 + 20 n2 + 53 n3 + 20 n2 + 5 is O(n3)
need c > 0 and n0 1 such that 3 n3 + 20 n2 + 5 c n3 for n n0
this is true for c = 4 and n0 = 21
3 log n + 5
3 log n + 5 is O(log n)
need c > 0 and n0 1 such that 3 log n + 5 c log n for n n0
this is true for c = 8 and n0 = 2
![Page 42: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/42.jpg)
Big-Oh Rules
• If is f(n) a polynomial of degree d, then f(n) is O(nd), i.e.,1.Drop lower-order terms
2.Drop constant factors
• Use the smallest possible class of functions• Say “2n is O(n)” instead of “2n is O(n2)”
• Is “2n is O(n2)” correct?
• Use the simplest expression of the class• Say “3n + 5 is O(n)” instead of “3n + 5 is O(3n)”
Siyuan Jiang, Sept. 2018 42
![Page 43: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/43.jpg)
Asymptotic Algorithm Analysis
• The asymptotic analysis of an algorithm determines the running time in big-Oh notation
• To perform the asymptotic analysis• We find the worst-case number of primitive operations executed as a function of
the input size
• We express this function with big-Oh notation
• Example:• We say that algorithm arrayMax “runs in O(n) time”
• Since constant factors and lower-order terms are eventually dropped anyhow, we can disregard them when counting primitive operations
Siyuan Jiang, Sept. 2018 43
![Page 44: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/44.jpg)
Big-Oh High-level idea
Siyuan Jiang, Sept. 2018 44
Suppress constant factors, lower-order terms
Too system-dependent Irrelevant for large inputs
![Page 45: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/45.jpg)
Comparison of Two Algorithms
Siyuan Jiang, Sept. 2018 45
Broadly
![Page 46: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/46.jpg)
break
Siyuan Jiang, Sept. 2018 46
![Page 47: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/47.jpg)
Exercise: Computing Prefix Averages
The ith prefix average of an array X is average of the first (i + 1) elements of X:
A[i] = (X[0] + X[1] + … + X[i])/(i+1)
Siyuan Jiang, Sept. 2018 47
![Page 48: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/48.jpg)
Exercise: Prefix Averages
Siyuan Jiang, Sept. 2018 48
![Page 49: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/49.jpg)
Arithmetic Progression
• The running time of prefixAverage1 isO(1 + 2 + …+ n)
• The sum of the first n integers is n(n + 1) / 2
Siyuan Jiang, Sept. 2018 49
![Page 50: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/50.jpg)
Improvement on this algorithm?
Siyuan Jiang, Sept. 2018 50
![Page 51: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/51.jpg)
Prefix Averages 2
Siyuan Jiang, Sept. 2018 51
Algorithm prefixAverage2 runs in O(n) time!
![Page 52: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/52.jpg)
Math you need to Review
• Summations
• Powers
• Logarithms
• Proof techniques
• Basic probability
Siyuan Jiang, Sept. 2018 52
Properties of powers:a(b+c) = aba c
abc = (ab)c
ab /ac = a(b-c)
b = a logab
bc = a c*logab
Properties of logarithms:
logb(xy) = logbx + logby
logb (x/y) = logbx - logby
logbxa = alogbx
logba = logxa/logxb
![Page 53: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/53.jpg)
Relatives of Big-Oh
Siyuan Jiang, Sept. 2018 53
big-Omega
f(n) is (g(n)) if there is a constant c > 0
and an integer constant n0 1 such that
f(n) c g(n) for n n0
big-Theta
f(n) is (g(n)) if there are constants c’ > 0 and
c’’ > 0 and an integer constant n0 1 such that
c’g(n) f(n) c’’g(n) for n n0
![Page 54: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/54.jpg)
Intuition for Asymptotic Notation
Siyuan Jiang, Sept. 2018 54
big-Oh
f(n) is O(g(n)) if f(n) is asymptotically less than or equal to g(n)
big-Omega
f(n) is (g(n)) if f(n) is asymptotically greater than or equal to g(n)
big-Theta
f(n) is (g(n)) if f(n) is asymptotically equal to g(n)
![Page 55: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/55.jpg)
Some catch up
• Recursion
• ArrayList<Type>
Siyuan Jiang, Sept. 2018 55
![Page 56: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/56.jpg)
Recursion
Siyuan Jiang, Sept. 2018 56
Factorial: n! = n*(n-1)*(n-2)*…3*2*1
4! = 4*3*2*1
2! = 2*1
1! = 1
0! = 1
![Page 57: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/57.jpg)
Sum
Siyuan Jiang, Sept. 2018 57
![Page 58: 9/10/2018 Algorithms & Data Structures Analysis of Algorithms · Asymptotic Algorithm Analysis •The asymptotic analysis of an algorithm determines the running time in big-Oh notation](https://reader030.fdocuments.in/reader030/viewer/2022021711/5e5cd5bc9df8a26d2607dc52/html5/thumbnails/58.jpg)
Assignment- Deadline Friday 10 pmChapter 2 – Sorting an Array (P.110 InsertionSort)
Siyuan Jiang, Sept. 2018 58
Implement Insertion(A):
Input: An array A of n comparable elementsOutput: The array A with elements rearranged in non-decreasing order
for k from 1 to n-1 doInsert A[k] at its proper location within A[0], A[1], …, A[k]