comp2230 tutorial 2

16
COMP2230 Tutorial 2

Upload: violet-mcintosh

Post on 01-Jan-2016

25 views

Category:

Documents


0 download

DESCRIPTION

COMP2230 Tutorial 2. Mathematical Induction. A tool for proving the truth of a statement for some integer “n”. “n” is usually a +ve integer from 1 to infinity. Why using M.I.? Usual Procedure Prove the statement is true for n=k 0 (e.g. n=1, n=10,…) Assume it is true for n - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COMP2230 Tutorial 2

COMP2230 Tutorial 2

Page 2: COMP2230 Tutorial 2

Mathematical Induction

A tool for proving the truth of a statement for some integer “n”.“n” is usually a +ve integer from 1 to infinity.Why using M.I.?

Usual Procedure• Prove the statement is true for n=k0 (e.g. n=1, n=10,…)• Assume it is true for n• Prove it is true for (n+1)

e.g. Prove 1+2+3+…..+ n =n(n+1)/2……………..eqn.1

n=1,

l.h.s of eqn 1 = 1,

r.h.s of eqn 1 = 1(1+1)/2=1. (i.e. eqn. 1 is true for n=1)

Page 3: COMP2230 Tutorial 2

Mathenatical Induction II

Assume eqn. 1. is true for n

i.e. 1+2+…..+n = n(n+1)/2

For (n+1),

l.h.s. = 1+2+….+n +(n+1) = n(n+1)/2 +(n+1) =(n+1)(n+2)/2

r.h.s = (n+1)(n+2)/2=l.h.s.

Page 4: COMP2230 Tutorial 2

Mathematical Induction III

How does M.I. work?

We have proved: 1. The statement/equation is true for n=1

2. If it is true for n, then it is true for (n+1)

The Equation 1+2+3+…….+n = n(n+1)/2

As The Equation is true for n=1, The Equation is true for n=2

As The Equation is true for n=2, The Equation is true for n=3

…………

The Equation is true for any +ve integer n.

Page 5: COMP2230 Tutorial 2

Useful assumptions

•For simplifying analysis

•Do not affect the asymptotic results

•E.g (ceiling, flooring) Why ceiling and flooring?

ceiling(2.3)=3, flooring(2.3)=2, round(2.3)=2.

ceiling(2.7)=3, flooring(2.7)=2, round(2.7)=3.

• For asymptotic analysis, T(1) can be regarded as the smallest T(n).

•If there is a recurrence T(n)=2T(n/4)+n, then T(1), T(2), T(3) can be treated as the same constant.

Page 6: COMP2230 Tutorial 2

Computing Complexity

1.Direct Counting. (for simple algorithms, non-recurrent algorithms)

You can refer to Tutorial 1.

2.Subsitution method (for recurrent algorithms).

Guess the form of the solution

Use M.I. to prove your guess.

3. Recursion tree

4. Master Theorems

Page 7: COMP2230 Tutorial 2
Page 8: COMP2230 Tutorial 2

If both algorithms can be used to solve a problem, which one is more efficient, in time?

Page 9: COMP2230 Tutorial 2

16

Page 10: COMP2230 Tutorial 2
Page 11: COMP2230 Tutorial 2

Another Example

T(n)=Θ(1) =1for 1≤n ≤ 3T(n)=3T(n/4)+Θ (n2), for n4

We make a simplifying assumption: n is a power of 4.

Page 12: COMP2230 Tutorial 2

Without expansion: T(n)First expansion

Cn2

T(n/4)

T(n/4) T(n/4)Second expansion C(n/4)2

C(n/4)2 C(n/4)2

…………………………………..T(n/16)……………………………….C(n/42)2

Page 13: COMP2230 Tutorial 2

L0: Cn2

L1: 3C(n/4)2

L2: 32C(n/42)2

L3: 33C(n/43)2

…..

Lk: 3kC(n/4k)2

If (n/4k) = 1, k=log4n

Total:k

k

i

iCn 3)16/3(1

0

2

Page 14: COMP2230 Tutorial 2

nk

i

iCn 4log1

0

2 3)16/3(

3log1

0

2 4)16/3( nCnk

i

i

Ans: O(n2) , Can it be Θ(n2)?

……. (what should we do to do to get the answer below)

Page 15: COMP2230 Tutorial 2

Exercise

Use substitution method, andMaster method to verify the

above result.

Page 16: COMP2230 Tutorial 2

note

Big-O concepts can’t be used within M.I. process.

A physical function, (e.g. cn2) which is a member of Big-O should be defined before M.I.

At the end of M.I. if it comes up with

T(n) ≤ guess(n) – (some +ve lower order terms), just ignore the lower order terms.

If there is a +ve lower order term left during M.I., add a lower order term to the physical function, guess(n).