CS420 lecture three Lowerbounds wim bohm, cs CSU.

36
CS420 lecture three Lowerbounds wim bohm, cs CSU
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    224
  • download

    0

Transcript of CS420 lecture three Lowerbounds wim bohm, cs CSU.

Page 1: CS420 lecture three Lowerbounds wim bohm, cs CSU.

CS420 lecture threeLowerbounds

wim bohm, cs CSU

Page 2: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Big O, big omega, big theta• f(x) = O(g(x)) iff there are positive integers c and n0 such that

f(x) < c.g(x) for all x > n0

• f(x) = Ω(g(x)) iff there are positive integers c and n0 such that

f(x) > c.g(x) for all x > n0

• f(x) = Θ(g(x)) iff f(x) = O(g(x)) and f(x) = Ω(g(x))

Page 3: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Big O etc.

• Big O used in upper bounds, ie the worst or average case complexity of an algorithm. Big theta is a tight bound, eg stronger than upper bound.

• Big omega used in lower bounds, ie the complexity of a problem.

Page 4: CS420 lecture three Lowerbounds wim bohm, cs CSU.

lower bounds

• An easy lower bound on a problem is the size of the output it needs to produce, or the number of inputs it has to access– Generate all permutations of size n, lower bound?– Towers of Hanoi, lower bound?– Sum n input integers, lower bound? but... sum integers 1 to n?

Page 5: CS420 lecture three Lowerbounds wim bohm, cs CSU.

some Ω(n) problems

• Given an unsorted array A and a number p, rearrange A such that all A[i]<x occur before all A[i]>x

Page 6: CS420 lecture three Lowerbounds wim bohm, cs CSU.

some Ω(n) problems

• Given an unsorted array A and a number x, rearrange A such that all A[i]<=x occur before all A[i]>=x

L=1; R=n; while(L<R){ while((A[L]<=x)&&(L<R)L++ while((A[R]>=x)&&(L<R))R- - if(L<R)swap(A,L,R) }

Page 7: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Dutch National Flag

• Red, blue and white tulips, rearrange them so that all reds precede all whites precede all blues

r=1; b=n; i=1; while(i<=b){ case S[i] red: swap(S,i,r);r++;i++; white: i++; blue: swap(S,i,b); b- -; } //How does it work?

Page 8: CS420 lecture three Lowerbounds wim bohm, cs CSU.

flag

while(i<=b){ case S[i] red: swap(S,i,r);r++;i++; white: i++; blue: swap(S,i,b); b- -; }

∀j < r S[ j] = red

∀ j > b S[ j] = blue

∀ j : r < j < i S[ j] = white

Page 9: CS420 lecture three Lowerbounds wim bohm, cs CSU.

flag

while(i<=b){ case S[i] red: swap(S,i,r);r++;i++; white: i++; blue: swap(S,i,b); b- -; }

but does it terminate? what happens if S[i] = S[b] = blue

Page 10: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• We have seen search.

Page 11: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• We have seen search.

Why is it closed?

Page 12: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• We have seen search.

Why is it Ω(logn)?

Page 13: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• We have seen search.

What are the allowed operations?

Page 14: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• We have seen search.

Why is the decision tree binary?

Page 15: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• We have seen search.

At least how many leaves?

Page 16: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• We have seen search.

At least how high? Why?

Page 17: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• Let's try the same for sort we have an O(n logn) upper bound

what would be the allowed steps?

Page 18: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• Let's try the same for sort we have an O(n logn) upper bound

comparison, array element assignment, index arithmetic, establish sorted

Page 19: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• Let's try the same for sort we have an O(n logn) upper bound

is the decision tree binary again?

Page 20: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• Let's try the same for sort we have an O(n logn) upper bound

how many leaves this time?

Page 21: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• Let's try the same for sort we have an O(n logn) upper bound

every leaf represents a specific permutation of the array

Page 22: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• Let's try the same for sort we have an O(n logn) upper bound

how many different permutations?

Page 23: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• Let's try the same for sort we have an O(n logn) upper bound

n! how high will the decision tree at least be?

Page 24: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Comparison problems

• Let's try the same for sort we have an O(n logn) upper bound

the decision tree will be at least log(n!)

high log(n!) = O(nlogn) so sort is Ω(nlogn), and thus closed.

Page 25: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Tournaments• Selecting the maximum element of an array. View it

as a tournament. Comparison is a match, winning is transitive:– A>B and B>C implies A>C, so A does not need to play C, and B and C can never be the winner of the tournament

• Many possible O(n) algorithms tennis(S): if (|S|==1) S1

else max(tennis(topHalf(S)),

tennis(bottomHalf(S))) sweep(S): winner= S1

for i=2 to n winner = max(winner,Si)

Page 26: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Runner up

ab c d

e fg h

ba

dc

fe

hg

tennis

sweep

which gives more info about runner up?

Page 27: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Assumptions about initial input

• Transitive and total ordering– transitive: a>b and b>c implies a>c (no roshambo)– total: for all a and b: either a>b or b>a– ie the elements can be ranked

• Any assignment of ranks to names is feasible – in previous example we could assign any of the 8! permutations of

rankings 1 to 8 to a to h

• Because we look for the winner, anyone who loses stops playing. Therefore there will be n-1 (n is number of inputs) games / comparisons for any tournament– notice: both sweep and tennis have 7 matches

Page 28: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Simpler case: n=4

• Any of the 24 assignments of 1..4 to a..d is valid• Build a decision tree for sweep (for n = 4), which is

sometimes called "caterpillar"

Page 29: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Decision tree for sweep

• Any of the 24 assignments of 1..4 to a..d is valid• Build a decision tree for sweep (for n = 4)

a:b ba

a:c ca b:c cb

c:d

dc

b:d

db

c:d

dca:d

da

Page 30: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Decision tree for sweep

• Any of the 24 assignments of 1..4 to a..d is valid• Build a decision tree for sweep (for n = 4)

a:b ba

a:c ca b:c cb

c:d

dc

b:d

db

c:d

dca:d

da

what do we know about the runner up for this outcome?

Page 31: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Decision tree for sweep

• Any of the 24 assignments of 1..4 to a..d is valid• Build a decision tree for sweep (for n = 4)

a:b ba

a:c ca b:c cb

c:d

dc

b:d

db

c:d

dca:d

da

what do we know about the runner up for this outcome?

Page 32: CS420 lecture three Lowerbounds wim bohm, cs CSU.

tennis

• Build a decision tree for tennis (for n = 4)

Page 33: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Decision tree for tennis

• Build a decision tree for tennis (for n = 4)

a:b ba

c:d dc c:d dc

b:d

db

b:c

cb

a:d

daa:c

ca

Page 34: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Decision tree for tennis

• Build a decision tree for tennis (for n = 4)

a:b ba

c:d dc c:d dc

b:d

db

b:c

cb

a:d

daa:c

ca

what do we know about the runner up for this outcome?

Page 35: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Decision tree for tennis

• Build a decision tree for tennis (for n = 4)

a:b ba

c:d dc c:d dc

b:d

db

b:c

cb

a:d

daa:c

ca

what do we know about the runner up for this outcome?

Page 36: CS420 lecture three Lowerbounds wim bohm, cs CSU.

Runner up

• In “The Art of Computer Programming, vol 3”, Donald Knuth proved that the tennis tournament algorithm provides the lower bound to determine the runner up:

In the case of tennis we need lg n more comparisons to find the runner up