question of the day move one matchstick to produce a square

Post on 18-Jan-2018

234 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Question of the Day

Move one matchstick to produce a square

Question of the Day

Move one matchstick to produce a square

LECTURE 16:BIG-OH NOTATION

“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

Algorithmic Analysis

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)

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

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)

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)

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

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

Proving Your Answer

Proving Your Answer

Proving Your Answer

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

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

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

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

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

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?

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

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

Your Turn

Get into your groups and complete activity

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

top related