30
COMP26120: Revision Lecture (2018/19) Lucas Cordeiro [email protected]

COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

COMP26120: Revision Lecture (2018/19)

Lucas Cordeiro [email protected]

Page 2: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

Revision Lecture

•  Changing variables and apply master method (Model Answer)

Page 3: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

Revision Lecture

•  Changing variables and apply master method (Model Answer)

•  Recurrence relation of binary search

Page 4: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

Revision Lecture

•  Changing variables and apply master method (Model Answer)

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

Page 5: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

Model Answer •  Solve the following recurrence by changing

variables and then applying the master method

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

Page 6: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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

Page 7: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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 )

Page 8: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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

Page 9: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

Model Answer (Applying Master Method)

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

a=2, b=2, mlogb

a = mlog2

2 = m

Page 10: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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)

Page 11: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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)

Page 12: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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 )

Page 13: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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 )

Page 14: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

•  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)

Page 15: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

•  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

Page 16: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

•  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

Page 17: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

Exercise: Recursive Binary Search

•  Given the recurrence equation of the binary search:

Page 18: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

Exercise: Recursive Binary Search

•  Given the recurrence equation of the binary search:

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

Page 19: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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

Page 20: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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

Page 21: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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

Page 22: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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

Page 23: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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)

Page 24: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

Induction Example: Gaussian Closed Form

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

Page 25: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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

Page 26: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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

Page 27: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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.

Page 28: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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).

Page 29: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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

Page 30: COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... · Exercise: Recursive Binary Search • Step 1: Prove that the base case holds § 1a: If it

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