COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... ·...
Transcript of COMP26120: Revision Lecture (2018/19)syllabus.cs.manchester.ac.uk/ugt/COMP26120/2018... ·...
![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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/14.jpg)
• 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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/15.jpg)
• 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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/16.jpg)
• 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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/18.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/19.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/21.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/22.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/23.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/24.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/25.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/26.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/27.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/28.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/29.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071300/609370c049f9302b9d4052fd/html5/thumbnails/30.jpg)
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