java bootcamp - mcgill university

44
CSUS Help desk is hosting a JAVA BOOTCAMP Thursday 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

Upload: others

Post on 01-May-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JAVA BOOTCAMP - McGill University

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

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

Outline

โ€ข Motivationsโ€ข The Big O notation

o Definitiono Exampleso Rules

โ€ข Big Omega and Big Thetaโ€ข Applications

Page 4: JAVA BOOTCAMP - McGill University

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

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

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

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

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

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

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

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

Asymptotic behavior

Page 13: JAVA BOOTCAMP - McGill University

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

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

๐‘“๐‘œ๐‘Ÿ ๐‘Ž๐‘™๐‘™ ๐‘› โ‰ฅ ๐‘›!, ๐‘ก(๐‘›) โ‰ค ๐‘”(๐‘›)

Page 16: JAVA BOOTCAMP - McGill University

Example

Page 17: JAVA BOOTCAMP - McGill University

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

Choosing a function and constants

(A) (B) (C)

Page 19: JAVA BOOTCAMP - McGill University

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

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

Intuitionโ€œ๐‘“ ๐‘› ๐‘–๐‘  ๐‘‚(๐‘” ๐‘› )โ€ if and only if there exists a point ๐‘›#beyond which ๐‘“ ๐‘› is less than some fixed constant times๐‘”(๐‘›).

Page 22: JAVA BOOTCAMP - McGill University

Example (1)

Claim: 5 " ๐‘› + 70 ๐‘–๐‘  ๐‘‚(๐‘›)

Page 23: JAVA BOOTCAMP - McGill University

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

Visualization

(A) (B) (C)

Page 25: JAVA BOOTCAMP - McGill University

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

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

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

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

Growth of functions

(from stackoverflow)

Tip: It is helpful to memorize the relationship between basic functions.

Page 30: JAVA BOOTCAMP - McGill University

Practical meaning of big Oโ€ฆ

If the unit is in seconds, this would make ~1011 yearsโ€ฆ

Page 31: JAVA BOOTCAMP - McGill University

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

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

Generalized Sum rule

Suppose ๐‘“$ ๐‘› ๐‘–๐‘  ๐‘‚ ๐‘”(๐‘› ) and ๐‘“! ๐‘› ๐‘–๐‘  ๐‘‚ ๐‘”(๐‘› ).

Then, ๐‘“$ ๐‘› + ๐‘“! ๐‘› ๐‘–๐‘  ๐‘‚ ๐‘”$(๐‘› + ๐‘”!(๐‘›)).

Proof: Exerciseโ€ฆ

Page 34: JAVA BOOTCAMP - McGill University

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

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

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

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

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

Intuition

Page 40: JAVA BOOTCAMP - McGill University

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

Example

Let ๐‘ก ๐‘› = 4 + 17 log! ๐‘› + 3๐‘› + 9๐‘› log! ๐‘› +"("&$)

!

Claim: ๐‘ก(๐‘›) is ฮ˜(๐‘›!)

Proof: ๐‘›!

4โ‰ค ๐‘ก ๐‘› โ‰ค (4 + 17 + 3 + 9 +

12) 2 ๐‘›!

Page 42: JAVA BOOTCAMP - McGill University

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

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

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)