question of the day move one matchstick to produce a square
TRANSCRIPT
![Page 1: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/1.jpg)
Question of the Day
Move one matchstick to produce a square
![Page 2: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/2.jpg)
Question of the Day
Move one matchstick to produce a square
![Page 3: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/3.jpg)
LECTURE 16:BIG-OH NOTATION
![Page 4: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/4.jpg)
“Anything that can go wrong…” Big-Oh will calculate algorithm’s
complexity Worst-case analysis of algorithm
performance Usually reasonably correlated with
execution time Not always right to consider only worst-
case May be situation where worst-case is very
rare Solve for other cases similarly, but almost
never done
![Page 5: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/5.jpg)
Algorithmic Analysis
![Page 6: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/6.jpg)
Primitive Statements
Basis of programming, take constant time: O(1) Fastest possible big-Oh notation
Time to run sequence of primitive statements, too But only if the input does not affect
sequence
Ignore constant multiplierO(5) = O(5 * 1) = O(1)
![Page 7: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/7.jpg)
Simple Loops
for (int i = 0; i < n.length; i++){}-or-
while (i < n) { i++; }
Each loop executed n times Primitive statements only within body
of loop Big –oh complexity of single loop iteration:
O(1) Either loop runs O(n) iterations
So loop has O(n) * O(1) = O(n) complexity total
![Page 8: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/8.jpg)
More Complicated Loops
for (int i = 0; i < n; i += 2) { }
i 0, 2, 4, 6, ..., n
In above example, loop executes n/2 iterations
Iterations takes O(1) time, so total complexity:= O(n/2) * O(1)= O(n * ½ * 1)= O(n)
![Page 9: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/9.jpg)
Really Complicated Loops
for (int i = 1; i < n; i *= 2) { }
i 1, 2, 4, 8, ..., n
In above code, loop executes log n iterations
Iterations takes O(1) time, so total complexity:= O(log n) * O(1)= O(log n * 1)= O(log n)
![Page 10: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/10.jpg)
Nested Loops
for (int i = 0; i < n; i++){for (int j = 0; j < n; j++) { }
}
Program would execute outer loop n times Inner loop run n times each iteration of
outer loop O(n) iterations doing O(n) work each
iteration So loop has O(n) * O(n) = O(n2) complexity
total Loops complexity multiplies when
nested
![Page 11: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/11.jpg)
Important to explain your answer Saying O(n) not enough to make it O(n) Methods using recursion especially hard to
determine Derive difficult answer using simple
process
Justifying an Answer
![Page 12: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/12.jpg)
Proving Your Answer
![Page 13: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/13.jpg)
Proving Your Answer
![Page 14: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/14.jpg)
Proving Your Answer
![Page 15: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/15.jpg)
Important to explain your answer Saying O(n) not enough to make it O(n) Methods using recursion especially hard to
determine Derive difficult answer using simple
process May find that you can simplify big-Oh
computation Find smaller or larger big-Oh than imagined
Convincing others need not be very formal Explaining your answer in clear way is
critical, however
Justifying an Answer
![Page 16: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/16.jpg)
Algorithm sneaky(int n)total = 0for i = 0 to n do for j = 0 to n do total += i * j return total end forend for
sneaky would take _____ time to execute O(n) iterations for each loop in the method
It’s About Time
![Page 17: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/17.jpg)
Algorithm sneaky(int n)total = 0for i = 0 to n do for j = 0 to n do total += i * j return total end forend for
sneaky would take O(1) time to execute O(n) iterations for each loop in the method But in first pass, method ends after return Always executes same number of
operations
It’s About Time
![Page 18: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/18.jpg)
Algorithm power(int a, int b ≥ 0)if a == 0 && b == 0 then return -1endifexp = 1repeat b times exp *= aend repeatreturn exp
power takes O(n) time in most cases Would only take O(1) if a & b are 0
____ algorithm overall
Big-Oh == Murphy’s Law
![Page 19: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/19.jpg)
Algorithm power(int a, int b ≥ 0)if a == 0 && b == 0 then return -1endifexp = 1repeat b times exp *= aend repeatreturn exp
power takes O(n) time in most cases Would only take O(1) if a & b are 0
O(n) algorithm overall; big-Oh uses worst-case
Big-Oh == Murphy’s Law
![Page 20: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/20.jpg)
algorithm sum(int[][] a)total = 0for i = 0 to a.length do for j = 0 to a[i].length do total += a[i][j] end forend forreturn total
Despite nested loops, this runs in O(n) time Input is doubly-subscripted array for this method For this method n is number entries in array
How Big Am I?
![Page 21: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/21.jpg)
Handling Method Calls
Method call is O(1) operation, … … but then also need to add time running
method Big-Oh counts operations executed in
total Remember: there is no such thing as free
lunch Borrowing $5 to pay does not make your
lunch free Similarly, need to include all operations
executed In which method run DOES NOT MATTER
![Page 22: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/22.jpg)
public static int sumOdds(int n) {int sum = 0; for (int i = 1; i <= n; i+=2) { sum+=i; }return sum;
}public static void oddSeries(int n) {for (int i = 1; i < n; i++) { System.out.println(i + “ ” + sumOdds(n));}
} oddSeries calls sumOdds n times
Each call does O(n) work, so takes O(n2) total time!
Methods Calling Methods
![Page 23: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/23.jpg)
Your Turn
Get into your groups and complete activity
![Page 24: Question of the Day Move one matchstick to produce a square](https://reader035.vdocuments.site/reader035/viewer/2022081802/5a4d1b307f8b9ab05999ae38/html5/thumbnails/24.jpg)
How to Prepare for Midterm
DO DON'T Make cheat sheets for the
test Review how parts of Java
work Add post-its to important
pages
Memorize Drink case of 40s before
test Use post-its as clothing