java bootcamp - mcgill university
TRANSCRIPT
![Page 1: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/1.jpg)
CSUS Help desk is hosting a
JAVA BOOTCAMPThursday September 17th from 5:30pm to 7h30pm
This bootcamp is aimed to programmers who donβt know the particularities of Java. There will be an overview of syntactic and semantic difference between java and other coding languages, with a focus on OOP (including polymorphism and inheritance).
The zoom link is: https://mcgill.zoom.us/j/92101531362
![Page 2: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/2.jpg)
COMP251: Running time analysis and the Big O notation
JΓ©rΓ΄me WaldispΓΌhlSchool of Computer Science
McGill UniversityBased on slides from M. Langer and M. Blanchette
![Page 3: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/3.jpg)
Outline
β’ Motivationsβ’ The Big O notation
o Definitiono Exampleso Rules
β’ Big Omega and Big Thetaβ’ Applications
![Page 4: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/4.jpg)
Measuring the running βtimeββ’ Goal: Analyze an algorithm written in
pseudocode and describe its running timeβ Without having to write codeβ In a way that is independent of the computer
usedβ’ To achieve that, we need to
β Make simplifying assumptions about the running time of each basic (primitive) operations
β Study how the number of primitive operations depends on the size of the problem solved
![Page 5: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/5.jpg)
Simple computer operation that can be performed in time that is always the same, independent of the size of the bigger problem solved (we say: constant time)
β Assigning a value to a variable: x Β¬1 Tassignβ Calling a method: Expos.addWin() Tcall
β Note: doesnβt include the time to execute the methodβ Returning from a method: return x; Treturnβ Arithmetic operations on primitive types Tarith
x + y, r*3.1416, x/y, etc.β Comparisons on primitive types: x==y Tcomp
β Conditionals: if (...) then.. else... Tcondβ Indexing into an array: A[i] Tindex
β Following object reference: Expos.losses Tref
Note: Multiplying two Large Integers is not a primitive operation, because the running time depends on the size of the numbers multiplied.
Primitive Operations
![Page 6: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/6.jpg)
FindMin analysisAlgorithm findMin(A, start, stop)Input: Array A, index start & stopOutput: Index of the smallest element of A[start:stop]
minvalue Β¬ A[start]
minindex Β¬ start
index Β¬ start + 1
while ( index <= stop ) do {
if (A[index]<minvalue)
then {
minvalue Β¬ A[index]
minindex Β¬ index
}
index = index + 1
}
return minindex
Tindex + TassignTassignTarith + TassignTcomp+ TcondTindex + Tcomp + Tcond
Tindex + TassignTassign
Tassign + TarithTcomp+ Tcond (last check of loop)Treturn
repeated
stop-start
times
Running time
![Page 7: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/7.jpg)
Worst case running time
β’ Running time depends on n = stop β start + 1β But it also depends on the content of the array!
β’ What kind of array of n elements will give the worst running time for findMin?
β’ The best running time?
5 4 3 2 1 0Example:
0 1 2 3 4 5Example:
![Page 8: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/8.jpg)
More assumptions
β’ Counting each type of primitive operations is tedious
β’ The running time of each operation is roughly comparable:
Tassign Β» Tcomp Β» Tarith Β» ...Β» Tindex=1primitive operation
β’ We are only interested in the number of primitive operations performed
Worst-case running time for findMin becomes:
T(n)=8+10*n
![Page 9: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/9.jpg)
Algorithm SelectionSort(A,n)
Input: an array A of n elements
Output: the array is sorted
i¬ 0
while (i<n) do {
minindex Β¬ findMin(A,i,n-1)
t Β¬ A[minindex]
A[minindex] Β¬ A[i]
A[i] Β¬ t
i Β¬ i + 1
}
Primitive operations(worst case) :
1
2
3 + TFindMin(n-1-i+1)=3 +(10 (n-i) - 2)
2
3
2
2
2 (last check of loop condition)
Selection Sort
![Page 10: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/10.jpg)
Selection Sort: adding it up
Total: T(n) = 1 + ( Γ₯ 12 + 10 (n - i) ) + 2
= 3 + (12 n + 10 Γ₯ (n-i) )
= 3 + 12 n + 10 ( Γ₯ n ) β 10 ( Γ₯ i)
= 3 + 12 n + 10 n * n - 10 ( (n-1)*n) / 2)
= 3 + 12 n + 10 n2 - 5 n2 + 5 n
= 5 n2 + 17 n + 3
n-1i=0
n-1i=0
n-1i=0
n-1i=0
![Page 11: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/11.jpg)
More simplifications
We have: T(n) = 5 n2 + 17 n + 3
Simplification #1:When n is large, T(n) Β» 5 n2
Simplification #2:When n is large, T(n) grows approximately like n2
We will write T(n) is O(n2)βT(n) is big O of n squaredβ
![Page 12: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/12.jpg)
Asymptotic behavior
![Page 13: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/13.jpg)
Towards a formal definition of big O
Let t(π) be a function that describes the time it takes for some algorithm on input size π.
We would like to express how t(π) grows with π, as πbecomes large i.e. asymptotic behavior.
Unlike with limits, we want to say that t(π) grows like certain simpler functions such as π, log! π , π, π!, 2"β¦
![Page 14: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/14.jpg)
Preliminary Definition
Let π‘(π) and π(π) be two functions, where π β₯ 0. We say π‘(π) is asymptotically bounded above by π(π) if there exists π#such that, for all π β₯ π#,
π‘(π) β€ π(π)
WARNING: This is not yet a formal definition!
![Page 15: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/15.jpg)
πππ πππ π β₯ π!, π‘(π) β€ π(π)
![Page 16: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/16.jpg)
Example
![Page 17: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/17.jpg)
Claim: 5π + 70 is asymptotically bounded above by 6π.
Proof:(State definition) We want to show there exists an π# such that, for all π β₯ π#, 5 2 π + 70 β€ 6 2 π.
5π + 70 β€ 6πβ 70 β€ π
Thus, we can use π# = 70
Symbol ββΊ " means βif and only ifβ i.e. logical equivalence
Example
![Page 18: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/18.jpg)
Choosing a function and constants
(A) (B) (C)
![Page 19: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/19.jpg)
Motivation
We would like to express formally how some function π‘(π) grows with π, as π becomes large.
We would like to compare the function π‘(π) with simpler functions , g(π), such as as π, log! π , π, π!, 2"β¦
![Page 20: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/20.jpg)
Formal Definition
Let π‘ π and π π be two functions, where π β₯ 0.
We say π‘(π) is π(π(π)) if there exists two positive constants π# and π such that, for all π β₯ π#,
π‘(π) β€ π 2 π(π)
Note: π(π) will be a simple function, but this is not required in the definition.
![Page 21: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/21.jpg)
Intuitionβπ π ππ π(π π )β if and only if there exists a point π#beyond which π π is less than some fixed constant timesπ(π).
![Page 22: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/22.jpg)
Example (1)
Claim: 5 " π + 70 ππ π(π)
![Page 23: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/23.jpg)
Proof(s)Claim: 5 2 π + 70 ππ π(π)
Proof 1: 5 2 π + 70 β€ 5 2 π + 70 2 π = 75 2 π, ππ π β₯ 1Thus, take π = 75 and π# = 1.
Proof 2: 5 2 π + 70 β€ 5 2 π + 6 2 π = 11 2 π, ππ π β₯ 12Thus, take π = 11 and π# = 12.
Proof 3: 5 2 π + 70 β€ 5 2 π + π = 6 2 π, ππ π β₯ 70Thus, take π = 6 and π# = 70.
All these proofs are correct and show that 5 2 π + 70 is π(π)
![Page 24: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/24.jpg)
Visualization
(A) (B) (C)
![Page 25: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/25.jpg)
Example (2)
Claim: 8 2 π! β 17 2 π + 46 is π π! .
Proof 1: 8π! β 17π + 46 β€ 8π! + 46π!, if π β₯ 1β€ 54π!
Thus, we can take π = 54 and π# = 1.
Proof 2: 8π! β 17π + 46 β€ 8π!, if π β₯ 3Thus, we can take π = 8 and π# = 3.
![Page 26: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/26.jpg)
What does O(1) mean?
We say π‘(π)is π(1), if there exist two positive constants π#and π such that, for all π β₯ π#.
π‘ π β€ π
So, it just means that π‘(π)is bounded.
![Page 27: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/27.jpg)
Tips
Never write π 3π , π 5 log! π , ππ‘π.
Instead, write π π , π log! π , ππ‘π.
Why? The point of the big O notation is to avoid dealing with constant factors. Itβs technically correct but we donβt do itβ¦
![Page 28: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/28.jpg)
Other considerations
β’ π# and π are not uniquely defined. For a given π# and πthat satisfies π(), we can increase one or both to againsatisfy the definition. There is not βbetterβ choice ofconstants.
β’ However, we generally want a βtightβ upper bound(asymptotically), so functions in the big O gives us moreinformation (Note: This is not the same as smaller π# or π).For instance, π(π) that is π π is also π π! and π 2" .But π π is more informative.
![Page 29: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/29.jpg)
Growth of functions
(from stackoverflow)
Tip: It is helpful to memorize the relationship between basic functions.
![Page 30: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/30.jpg)
Practical meaning of big Oβ¦
If the unit is in seconds, this would make ~1011 yearsβ¦
![Page 31: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/31.jpg)
Constant Factor rule
Suppose f(n)is O(g(n))and a is a positive constant.Then, π 2 π π is also π(π π )
Proof: By definition, if f(n)is O(g(n))then there exists two positive constants π# and π such that for all π β₯ π#,
π(π) β€ π 2 π(π)
Thus, a 2 π(π) β€ a 2 π 2 π(π)
We use the constant π 2 π to show that π 2 π π is π(π π ).
![Page 32: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/32.jpg)
Sum rule
Suppose π$ π ππ π π(π ) and π! π ππ π π(π ).Then, π$ π + π!(π) ππ π π(π ).
Proof: Let π$, π$ and π!, π! be constants such that
π$(π) β€ π$π(π), for all π β₯ π$π!(π) β€ π!π(π), for all π β₯ π!
So, π$ π + π! π β€ (π$+π!)π(π), for all π β₯ max(π$, π!).
We can use the constants π$ + π! and max(π$, π!) to satisfythe definition.
![Page 33: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/33.jpg)
Generalized Sum rule
Suppose π$ π ππ π π(π ) and π! π ππ π π(π ).
Then, π$ π + π! π ππ π π$(π + π!(π)).
Proof: Exerciseβ¦
![Page 34: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/34.jpg)
Product Rule
Suppose π$ π ππ π π(π ) and π! π ππ π π(π ).
Then, π$ π 2 π! π ππ π π$(π 2 π!(π)).
Proof: Let π$, π$ and π!, π! be constants such that
π$(π) β€ π$π$(π), for all π β₯ π$π!(π) β€ π!π!(π), for all π β₯ π!
So, π$ π 2 π! π β€ (π$2 π!) 2 (π$(π) 2 π!(π)) , for all π β₯max(π$, π!).We can use the constants π$ 2 π! and max(π$, π!) to satisfythe definition.
![Page 35: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/35.jpg)
Transitivity Rule
Suppose π π ππ π π(π ) and π π ππ π β(π ).
Then, π π ππ π(β(π)).
Proof: Let π$, π$ and π!, π! be constants such that
π(π) β€ π$π(π), for all π β₯ π$π(π) β€ π!β(π), for all π β₯ π!
So, π π β€ (π$2 π!)β(π), for all π β₯ max(π$, π!).
We can use the constants π$ 2 π! and max(π$, π!) to satisfythe definition.
![Page 36: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/36.jpg)
NotationsIf π(π) is π π π , we often write π(π) β π(π π ). That is a member of the functions that are π π π .
For n sufficiently large we have, 1 < log! π < π < π log! πβ¦And we write π 1 β π log! π β π π β π π log! π β¦
![Page 37: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/37.jpg)
The Big Omega notation (Ξ©)
Let π‘ π and π π be two functions with π β₯ 0.
We say π‘ π is Ξ©(π π ), if there exists two positives constants π# and π such that, for all π β₯ π#,
π‘(π) β₯ π 2 π(π)
Note: This is the opposite of the big O notation. The function π is now used as a "lower boundβ.
![Page 38: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/38.jpg)
Example
Claim: "("&$)!
is Ξ©(π!).
Proof: We show first that "("&$)!
β₯ "!
(.
β 2π(π β 1) β₯ π!
β π! β₯ 2π
β π β₯ 2
Thus, we take π = $(
and π# = 2.
(Exercise: Prove that it also works with π = $) and π# = 3.
![Page 39: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/39.jpg)
Intuition
![Page 40: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/40.jpg)
Andβ¦ big Theta!Let π‘(π) and π(π) be two functions, where π β₯ 0.
We say π‘(π) is Ξ(π(π)) if there exists three positive constants π# and π$, π! such that, for all π β₯ π#,
π$ 2 π(π) β€ π‘(π) β€ π! 2 π(π)
Note: if π‘ π is Ξ(π(π)). Then, it is also π(π(π)) and Ξ©(π(π)) .
![Page 41: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/41.jpg)
Example
Let π‘ π = 4 + 17 log! π + 3π + 9π log! π +"("&$)
!
Claim: π‘(π) is Ξ(π!)
Proof: π!
4β€ π‘ π β€ (4 + 17 + 3 + 9 +
12) 2 π!
![Page 42: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/42.jpg)
Big vs. little
(from geekforgeek.org)
The big O (resp. big Ξ©) denotes a tight upper (resp. lower) bounds, while the little o (resp. little π) denotes a lose upper (resp. lower) bounds.
![Page 43: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/43.jpg)
Back to running time analysis
The time it takes for an algorithm to run depends on:
β’ constant factors (often implementation dependent)β’ the size π of the input β’ the values of the input, including arguments if applicableβ¦
Q: What are the best and worst cases?
![Page 44: JAVA BOOTCAMP - McGill University](https://reader033.vdocuments.us/reader033/viewer/2022050214/626e6a7b296db145ad18082c/html5/thumbnails/44.jpg)
Example (Binary Search)
Best case: The value is exactly in the middle of the array.β Ξ©(1)
Worst case: You recursively search until you reach an array of size 1 (Note: It does not matter if you find the key or not).
β π(log! π)
(from Wikipedia)