comp26120: revision lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/comp26120/2018... ·...

Post on 02-Dec-2020

0 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

COMP26120: Revision Lecture (2018/19)

Lucas Cordeiro lucas.cordeiro@manchester.ac.uk

Revision Lecture

•  Changing variables and apply master method (Model Answer)

Revision Lecture

•  Changing variables and apply master method (Model Answer)

•  Recurrence relation of binary search

Revision Lecture

•  Changing variables and apply master method (Model Answer)

•  Recurrence relation of binary search •  Proof by induction with sums

Model Answer •  Solve the following recurrence by changing

variables and then applying the master method

T (n ) = 2T ( n )+ lgn

T (n ) = 2T ( n )+ lgn

T (2m ) = 2T (2m

2 )+mS (m ) = 2S (m / 2)+m

T (n ) =T (2m ) = S (m )=O (m log m ) =O (log n log log n )

m = log n ∴ n = 2m

Model Answer (Changing Variables)

•  Applying algebraic manipulation to the recurrence

m = log n ∴ n = 2m

S(m)=T(2m)

Model Answer (Changing Variables)

•  Applying algebraic manipulation to the recurrence

Note that log 2m = m and log 2m/2 = m/2

T (n ) = 2T ( n )+ lgn

T (2m ) = 2T (2m

2 )+mS (m ) = 2S (m / 2)+m

T (n ) =T (2m ) = S (m )=O (m log m ) =O (log n log log n )

m = log n ∴ n = 2m

S(m)=T(2m)

Model Answer (Changing Variables)

•  Applying algebraic manipulation to the recurrence

Note that log 2m = m and log 2m/2 = m/2

T (n ) = 2T ( n )+ lgn

T (2m ) = 2T (2m

2 )+mS (m ) = 2S (m / 2)+m

T (n ) =T (2m ) = S (m )=O (m log m ) =O (log n log log n )

Apply the master method

Model Answer (Applying Master Method)

S(m) = 2S(m/2)+m

a=2, b=2, mlogb

a = mlog2

2 = m

We have case 2:

f (m ) =Θ(m logb a ) T (m ) =Θ(m logb a log m )

Model Answer (Applying Master Method)

S(m) = 2S(m/2)+m

a=2, b=2, mlogb

a = mlog2

2 = m

Given f(m)=Θ(m)

We have case 2:

f (m ) =Θ(m logb a ) T (m ) =Θ(m logb a log m )

which leads to:

Model Answer (Applying Master Method)

S(m) = 2S(m/2)+m

a=2, b=2, mlogb

a = mlog2

2 = m

Given f(m)=Θ(m)

S(m)=Θ(mlogb

a log m) = Θ(m log m)

m = log n ∴ n = 2m

S(m)=O(m log m)

Model Answer (Changing Variables)

•  Applying algebraic manipulation to the recurrence

S(m)=T(2m)

T (n ) = 2T ( n )+ lgn

T (2m ) = 2T (2m

2 )+mS (m ) = 2S (m / 2)+m

T (n ) =T (2m ) = S (m )=O (m log m ) =O (log n log log n )

m = log n ∴ n = 2m

S(m)=O(m log m)

Model Answer (Changing Variables)

•  Applying algebraic manipulation to the recurrence

S(m)=T(2m)

T (n ) = 2T ( n )+ lgn

T (2m ) = 2T (2m

2 )+mS (m ) = 2S (m / 2)+m

T (n ) =T (2m ) = S (m )=O (m log m ) =O (log n log log n )

•  What is the recurrence equation for this algorithm?

Binary Search Recurrence

BinSearch(A[1…n],q) if n=1 then if A[n]=q then return n else return 0 k←(n+1)/2 if q < A[k] then BinSearch(A[1…k-1],q) else BinSearch(A[k…n],q)

•  What is the recurrence equation for this algorithm?

Binary Search Recurrence

BinSearch(A[1…n],q) if n=1 then if A[n]=q then return n else return 0 k←(n+1)/2 if q < A[k] then BinSearch(A[1…k-1],q) else BinSearch(A[k…n],q)

T(n) = 1 if n=1

•  What is the recurrence equation for this algorithm?

Binary Search Recurrence

BinSearch(A[1…n],q) if n=1 then if A[n]=q then return n else return 0 k←(n+1)/2 if q < A[k] then BinSearch(A[1…k-1],q) else BinSearch(A[k…n],q)

T(n) = 1 if n=1 T(n) = 1 + T(n/2) if n>1

Exercise: Recursive Binary Search

•  Given the recurrence equation of the binary search:

Exercise: Recursive Binary Search

•  Given the recurrence equation of the binary search:

•  Guess the solution is: T(n) = O(log n)

Exercise: Recursive Binary Search

•  Given the recurrence equation of the binary search:

•  Guess the solution is: T(n) = O(log n)

•  Prove that T(n) ≤ c log n for some c>0 using induction and for all n≥n0

Exercise: Recursive Binary Search

•  Step 1: Prove that the base case holds §  1a: If it does not hold for n=0, then change the

boundary conditions

Exercise: Recursive Binary Search

•  Step 1: Prove that the base case holds §  1a: If it does not hold for n=0, then change the

boundary conditions

•  Step 2: Prove that the inductive step holds §  2a: define the inductive hypothesis

§  2b: substitute the inductive hypothesis in the original recurrence

§  2c: manipulate the inequality to demonstrate that the inductive hypothesis holds for the given boundary conditions

Solution: Recursive Binary Search

•  Step 1: Prove that the base case holds §  n=2 à T(2) =T(1) + 2 = 3 and c log n = c * log 2 = c

Solution: Recursive Binary Search

•  Step 1: Prove that the base case holds §  n=2 à T(2) =T(1) + 2 = 3 and c log n = c * log 2 = c

•  Step 2: Prove that the inductive step holds §  2a: T(n/2) ≤ c log n/2

§  2b: T(n) = T(n/2) + 1 à T(n) ≤ c log n/2 + 1

§  2c: T(n) = c log n – c log 2 + 1

§  2c: T(n) = c log n – c + 1

§  2c: T(n) ≤ c log n (holds for c ≥ 1, upper bound analysis)

Induction Example: Gaussian Closed Form

•  Prove 0+1 + 2 + 3 + … + n = n(n+1) / 2

Induction Example: Gaussian Closed Form

•  Prove 0+1 + 2 + 3 + … + n = n(n+1) / 2 §  Basis: If n = 0, then 0 = 0(0+1) / 2

Induction Example: Gaussian Closed Form

•  Prove 0+1 + 2 + 3 + … + n = n(n+1) / 2 §  Basis: If n = 0, then 0 = 0(0+1) / 2

§  Inductive hypothesis: Assume that 0 + 1 + 2 + 3 + … + k = k(k+1) / 2

Induction Example: Gaussian Closed Form

•  Prove 0+1 + 2 + 3 + … + n = n(n+1) / 2 §  Basis: If n = 0, then 0 = 0(0+1) / 2

§  Inductive hypothesis: Assume that 0 + 1 + 2 + 3 + … + k = k(k+1) / 2

§  Inductive step: show that if P(k) holds, then also P(k+1) holds

(0+1+2+…+k)+(k+1) = (k+1)((k+1)+1) / 2.

Induction Example: Gaussian Closed Form

•  Prove 0+1 + 2 + 3 + … + n = n(n+1) / 2 §  Basis: If n = 0, then 0 = 0(0+1) / 2

§  Inductive hypothesis: Assume that 0 + 1 + 2 + 3 + … + k = k(k+1) / 2

§  Inductive step: show that if P(k) holds, then also P(k+1) holds

(0+1+2+…+k)+(k+1) = (k+1)((k+1)+1) / 2. o  Using the induction hypothesis that P(k) holds:

k(k+1)/2 + (k+1).

Induction Example: Gaussian Closed Form

•  Prove 0+1 + 2 + 3 + … + n = n(n+1) / 2 §  Basis: If n = 0, then 0 = 0(0+1) / 2

§  Inductive hypothesis: Assume that 0 + 1 + 2 + 3 + … + k = k(k+1) / 2

§  Inductive step: show that if P(k) holds, then also P(k+1) holds

(0+1+2+…+k)+(k+1) = (k+1)((k+1)+1) / 2. o  Using the induction hypothesis that P(k) holds:

k(k+1)/2 + (k+1).

k(k+1)/2 + (k+1) = [k(k+1)+2(k+1)]/2 = (k2 +3k+2)/2 = (k+1)(k+2)/2

Induction Example: Gaussian Closed Form

•  Prove 0+1 + 2 + 3 + … + n = n(n+1) / 2 §  Basis: If n = 0, then 0 = 0(0+1) / 2

§  Inductive hypothesis: Assume that 0 + 1 + 2 + 3 + … + k = k(k+1) / 2

§  Inductive step: show that if P(k) holds, then also P(k+1) holds

(0+1+2+…+k)+(k+1) = (k+1)((k+1)+1) / 2. o  Using the induction hypothesis that P(k) holds:

k(k+1)/2 + (k+1).

k(k+1)/2 + (k+1) = [k(k+1)+2(k+1)]/2 = (k2 +3k+2)/2 = (k+1)(k+2)/2

= (k+1)((k+1)+1)/2

hereby showing that indeed P(k+1) holds

top related