CE100 Algorithms and Programming II

CE100 Algorithms and Programming II

Week-2 (Solving Recurrences / The Divide-and-Conquer)

Spring Semester, 2021-2022

Download DOC-PDF, DOC-DOCX, SLIDE, PPTX

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Solving Recurrences

Outline (1)

  • Solving Recurrences

    • Recursion Tree

    • Master Method

    • Back-Substitution

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Outline (2)

  • Divide-and-Conquer Analysis

    • Merge Sort

    • Binary Search

    • Merge Sort Analysis

    • Complexity

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Outline (3)

  • Recurrence Solution
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Solving Recurrences (1)

  • Reminder: Runtime (T(n))(T(n)) of MergeSort was expressed as a recurrence

T(n)={Θ(1)if n=12T(n/2)+Θ(n)otherwiseT(n)=\begin{cases} \Theta(1)&\text{if n=1} \\ 2T(n/2)+\Theta(n)&otherwise \end{cases}

  • Solving recurrences is like solving differential equations, integrals, etc.

    • Need to learn a few tricks
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Solving Recurrences (2)

Recurrence: An equation or inequality that describes a function in terms of its value on smaller inputs.

Example :

T(n)={1if n=1T(n/2)+1if n>1T(n)= \begin{cases} 1 &\text{if n=1} \\ T(\lceil{n/2}\rceil)+ 1 &\text{if n>1} \end{cases}

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Recurrence Example

T(n)={1if n=1T(n/2)+1if n>1T(n)= \begin{cases} 1 &\text{if n=1} \\ T(\lceil{n/2}\rceil)+ 1 &\text{if n>1} \end{cases}

  • Simplification: Assume n=2kn=2^k

  • Claimed answer : T(n)=lgn+1T(n)=lgn+1

  • Substitute claimed answer in the recurrence:

lgn+1={1if n=1lg(n/2)+2if n>1lgn+1= \begin{cases} 1 &\text{if n=1} \\ lg(\lceil{n/2}\rceil)+ 2 &\text{if n>1} \end{cases}

  • True when n=2kn=2^k
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Technicalities: Floor / Ceiling

Technically, should be careful about the floor and ceiling functions (as in the book).

e.g. For merge sort, the recurrence should in fact be:,

T(n)={Θ(1)if n=1T(n/2)+T(n/2)+Θ(n)if n>1T(n)= \begin{cases} \Theta(1) &\text{if n=1} \\ T(\lceil{n/2}\rceil)+ T(\lfloor{n/2}\rfloor) +\Theta(n) &\text{if n>1} \end{cases}

But, it's usually ok to:

  • ignore floor/ceiling

  • solve for the exact power of 2 (or another number)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Technicalities: Boundary Conditions

  • Usually assume: T(n)=Θ(1)T(n) = \Theta(1) for sufficiently small nn
    • Changes the exact solution, but usually the asymptotic solution is not affected (e.g. if polynomially bounded)
  • For convenience, the boundary conditions generally implicitly stated in a recurrence
    • T(n)=2T(n/2)+Θ(n)T(n) = 2T(n/2) + \Theta(n) assuming that
    • T(n)=Θ(1)T(n)=\Theta(1) for sufficiently small nn
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Example: When Boundary Conditions Matter

Exponential function: T(n)=(T(n/2))2T(n) = (T(n/2))2
Assume
T(1)=c (where c is a positive constant)T(1) = c \text{ (where c is a positive constant)}
T(2)=(T(1))2=c2T(2) = (T(1))^2 = c^2
T(4)=(T(2))2=c4T(4) = (T(2))^2 = c^4
T(n)=Θ(cn)T(n) = \Theta(c^n)
e.g.

 However Θ(2n)Θ(3n){T(1)=2T(n)=Θ(2n)T(1)=3T(n)=Θ(3n)\text{ However } \Theta(2^n) \neq \Theta(3^n) \begin{cases} T(1)= 2 &\Rightarrow & T(n)= \Theta(2^n) \\ T(1)= 3 &\Rightarrow & T(n)= \Theta(3^n) \end{cases}

The difference in solution more dramatic when:

T(1)=1T(n)=Θ(1n)=Θ(1)T(1) = 1 \Rightarrow T(n) = \Theta(1^n) = \Theta(1)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Solving Recurrences Methods

We will focus on 3 techniques

  • Substitution method

  • Recursion tree approach

  • Master method

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Substitution Method

The most general method:

  • Guess

  • Prove by induction

  • Solve for constants

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Substitution Method: Example (1)

Solve T(n)=4T(n/2)+nT(n)=4T(n/2)+n (assume T(1)=Θ(1)T(1)= \Theta(1))

  1. Guess T(n)=O(n3)T(n) = O(n^3) (need to prove OO and Ω\Omega separately)
  2. Prove by induction that T(n)cn3T(n) \leq cn^3 for large nn (i.e. nn0n \geq n_0)
    • Inductive hypothesis: T(k)ck3T(k) \leq ck^3 for any k<nk < n
    • Assuming ind. hyp. holds, prove T(n)cn3T(n) \leq cn^3
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Substitution Method: Example (2)

Original recurrence: T(n)=4T(n/2)+nT(n) = 4T(n/2) + n

From inductive hypothesis: T(n/2)c(n/2)3T(n/2) \leq c(n/2)^3

Substitute this into the original recurrence:

  • T(n)4c(n/2)3+nT(n) \leq 4c(n/2)^3 + n
  • =(c/2)n3+n= (c/2)n^3 + n
  • =cn3((c/2)n3n)= cn^3 – ((c/2)n^3 – n) \Longleftarrow desired - residual
  • cn3\leq cn^3
    when ((c/2)n3n)0((c/2)n^3 – n) \geq 0
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Substitution Method: Example (3)

So far, we have shown:

T(n)cn3 when ((c/2)n3n)0T(n) \leq cn^3 \text{ when } ((c/2)n^3 – n) \geq 0

We can choose c2c \geq 2 and n01n_0 \geq 1

But, the proof is not complete yet.

Reminder: Proof by induction:
1.Prove the base cases \Longleftarrow haven’t proved the base cases yet
2.Inductive hypothesis for smaller sizes
3.Prove the general case

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Substitution Method: Example (4)

  • We need to prove the base cases
    • Base: T(n)=Θ(1)T(n) = \Theta(1) for small nn (e.g. for n=n0n = n_0)
  • We should show that:
    • Θ(1)cn3\Theta(1) \leq cn^3 for n=n0n = n_0 , This holds if we pick cc big enough
  • So, the proof of T(n)=O(n3)T(n) = O(n^3) is complete
  • But, is this a tight bound?
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Example: A tighter upper bound? (1)

  • Original recurrence: T(n)=4T(n/2)+nT(n) = 4T(n/2) + n

  • Try to prove that T(n)=O(n2)T(n) = O(n^2),

    • i.e. T(n)cn2T(n) \leq cn^2 for all nn0n \geq n_0
  • Ind. hyp: Assume that T(k)ck2T(k) \leq ck^2 for k<nk < n

  • Prove the general case: T(n)cn2T(n) \leq cn^2

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Example: A tighter upper bound? (2)

Original recurrence: T(n)=4T(n/2)+nT(n) = 4T(n/2) + n
Ind. hyp: Assume that T(k)ck2T(k) \leq ck^2 for k<nk < n
Prove the general case: T(n)cn2T(n) \leq cn^2

T(n)=4T(n/2)+n4c(n/2)2+n=cn2+n=O(n2) Wrong! We must prove exactly\begin{align*} T(n) & = 4T(n/2) + n \\ & \leq 4c(n/2)^2 + n \\ & = cn^2 + n \\ & = O(n2) \Longleftarrow \text{ Wrong! We must prove exactly} \end{align*}

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Example: A tighter upper bound? (3)

Original recurrence: T(n)=4T(n/2)+nT(n) = 4T(n/2) + n
Ind. hyp: Assume that T(k)ck2T(k) \leq ck^2 for k<nk < n
Prove the general case: T(n)cn2T(n) \leq cn^2

  • So far, we have:
    T(n)cn2+nT(n) \leq cn^2 + n
  • No matter which positive c value we choose, this does not show that T(n)cn2T(n) \leq cn^2
  • Proof failed?
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Example: A tighter upper bound? (4)

  • What was the problem?

    • The inductive hypothesis was not strong enough
  • Idea: Start with a stronger inductive hypothesis

    • Subtract a low-order term
  • Inductive hypothesis: T(k)c1k2c2kT(k) \leq c_1k^2 – c_2k for k<nk < n

  • Prove the general case: T(n)c1n2c2nT(n) \leq c_1n^2 - c_2n

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Example: A tighter upper bound? (5)

Original recurrence: T(n)=4T(n/2)+nT(n) = 4T(n/2) + n

Ind. hyp: Assume that T(k)c1k2c2kT(k) \leq c_1k^2 – c_2k for k<nk < n

Prove the general case: T(n)c1n2c2nT(n) ≤ c_1n^2 – c_2n

T(n)=4T(n/2)+n4(c1(n/2)2c2(n/2))+n=c1n22c2n+n=c1n2c2n(c2nn)c1n2c2n for n(c21)0choose c21\begin{align*} T(n) & = 4T(n/2) + n \\ & \leq 4 (c_1(n/2)^2 – c_2(n/2)) + n \\ & = c_1n^2 – 2c_2n + n \\ & = c_1n^2 – c_2n – (c_2n – n) \\ & \leq c_1n^2 – c_2n \text{ for } n(c_2 – 1) \geq 0 \\ & \text{choose } c2 \geq 1 \end{align*}

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Example: A tighter upper bound? (6)

We now need to prove

T(n)c1n2c2nT(n) \leq c_1n^2 – c_2n

for the base cases.

T(n)=Θ(1) for 1nn0T(n) = \Theta(1) \text{ for } 1 \leq n \leq n_0 (implicit assumption)

Θ(1)c1n2c2n\Theta(1) \leq c_1n^2 – c_2n for nn small enough (e.g. n=n0n = n_0)

  • We can choose c1 large enough to make this hold

We have proved that T(n)=O(n2)T(n) = O(n^2)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Substitution Method: Example 2 (1)

For the recurrence T(n)=4T(n/2)+nT(n) = 4T(n/2) + n,

prove that T(n)=Ω(n2)T(n) = \Omega(n^2)

i.e. T(n)cn2T(n) ≥ cn^2 for any nn0n \geq n_0

Ind. hyp: T(k)ck2T(k) \geq ck^2 for any k<nk < n

Prove general case: T(n)cn2T(n) \geq cn^2

T(n)=4T(n/2)+nT(n) = 4T(n/2) + n
4c(n/2)2+n\geq 4c (n/2)^2 + n
=cn2+n= cn^2 + n
cn2\geq cn^2 since n>0n > 0

Proof succeeded – no need to strengthen the ind. hyp as in the last example

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Substitution Method: Example 2 (2)

We now need to prove that
T(n)cn2T(n) ≥ cn^2
for the base cases
T(n)=Θ(1)T(n) = \Theta(1) for 1nn01 \leq n \leq n_0 (implicit assumption)
Θ(1)cn2\Theta(1) \geq cn^2 for n=n0n = n_0

n0n_0 is sufficiently small (i.e. constant)

We can choose cc small enough for this to hold

We have proved that T(n)=Ω(n2)T(n) = \Omega(n^2)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Substitution Method - Summary

  • Guess the asymptotic complexity

  • Prove your guess using induction

    • Assume inductive hypothesis holds for k<nk < n

    • Try to prove the general case for nn

      • Note: MUSTMUST prove the EXACTEXACT inequality CANNOTCANNOT ignore lower order terms, If the proof fails, strengthen the ind. hyp. and try again
  • Prove the base cases (usually straightforward)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Recursion Tree Method

  • A recursion tree models the runtime costs of a recursive execution of an algorithm.
  • The recursion tree method is good for generating guesses for the substitution method.
  • The recursion-tree method can be unreliable.
    • Not suitable for formal proofs
  • The recursion-tree method promotes intuition, however.
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Solve Recurrence (1) : T(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n)

alt:"alt" center

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Solve Recurrence (2) : T(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n)

alt:"alt" center

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Solve Recurrence (3) : T(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n)

alt:"alt" center

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Example of Recursion Tree (1)

Solve T(n)=T(n/4)+T(n/2)+n2T(n) = T(n/4) + T(n/2) + n^2

alt:"alt" center

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Example of Recursion Tree (2)

Solve T(n)=T(n/4)+T(n/2)+n2T(n) = T(n/4) + T(n/2) + n^2
alt:"alt" center

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Example of Recursion Tree (3)

Solve T(n)=T(n/4)+T(n/2)+n2T(n) = T(n/4) + T(n/2) + n^2
alt:"alt" center

RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Master Method

  • A powerful black-box method to solve recurrences.

  • The master method applies to recurrences of the form

    • T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f (n)
  • where a1,b>1a \geq 1, b > 1, and ff is asymptotically positive.

RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Master Method: 3 Cases

(TODO : Add Notes )

  • Recurrence: T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)
  • Compare f(n)f(n) with nlogban^{log_b^a}
  • Intuitively:
    • Case 1: f(n)f(n) grows polynomially slower than nlogban^{log_b^a}
    • Case 2: f(n)f(n) grows at the same rate as nlogban^{log_b^a}
    • Case 3: f(n)f(n) grows polynomially faster than nlogban^{log_b^a}
RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Master Method: Case 1 (Bigger)

  • Recurrence: T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)

  • Case 1: nlogbaf(n)=Ω(nε)\frac{n^{log_b^a}}{f(n)}=\Omega(n^{\varepsilon}) for some constant ε>0\varepsilon>0

  • i.e., f(n)f(n) grows polynomialy slower than nlogban^{log_b^a} (by an nεn^{\varepsilon} factor)

  • Solution: T(n)=Θ(nlogba)T(n)=\Theta(n^{log_b^a})

RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Master Method: Case 2 (Simple Version) (Equal)

  • Recurrence: T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)

  • Case 2: f(n)nlogba=Θ(1)\frac{f(n)}{n^{log_b^a}}=\Theta(1)

  • i.e., f(n)f(n) and nlogban^{log_b^a} grow at similar rates

  • Solution: T(n)=Θ(nlogbalgn)T(n)=\Theta(n^{log_b^a}lgn)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Master Method: Case 3 (Smaller)

  • Case 3: f(n)nlogba=Ω(nε)\frac{f(n)}{n^{log_b^a}}=\Omega(n^{\varepsilon}) for some constant ε>0\varepsilon > 0

  • i.e., f(n)f(n) grows polynomialy faster than nlogban^{log_b^a} (by an nεn^{\varepsilon} factor)

  • and the following regularity condition holds:

    • af(n/b)cf(n)af(n/b) \leq cf(n) for some constant c<1c<1
  • Solution: T(n)=Θ(f(n))T(n)=\Theta(f(n))

RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Master Method Example (case-1) : T(n)=4T(n/2)+nT(n)=4T(n/2)+n

  • a=4a=4
  • b=2b=2
  • f(n)=nf(n)=n
  • nlogba=nlog24=nlog222=n2log22=n2n^{log_b^a}=n^{log_2^4}=n^{log_2^{2^2}}=n^{2log_2^2}=n^2
  • f(n)=nf(n)=n grows polynomially slower than nlogba=n2n^{log_b^a}=n^2
    • nlogbaf(n)=n2n=n=Ω(nε)\frac{n^{log_b^a}}{f(n)}=\frac{n^2}{n}=n=\Omega(n^{\varepsilon})
  • CASE-1:
    • T(n)=Θ(nlogba)=Θ(nlog24)=Θ(n2)T(n)=\Theta(n^{log_b^a})=\Theta(n^{log_2^4})=\Theta(n^2)
RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Master Method Example (case-2) : T(n)=4T(n/2)+n2T(n)=4T(n/2)+n^2

  • a=4a=4

  • b=2b=2

  • f(n)=n2f(n)=n^2

  • nlogba=nlog24=nlog222=n2log22=n2n^{log_b^a}=n^{log_2^4}=n^{log_2^{2^2}}=n^{2log_2^2}=n^2

  • f(n)=n2f(n)=n^2 grows at similar rate as nlogba=n2n^{log_b^a}=n^2

    • f(n)=Θ(nlogba)=n2f(n)=\Theta(n^{log_b^a})=n^2
  • CASE-2:

    • T(n)=Θ(nlogbalgn)=Θ(nlog24lgn)=Θ(n2lgn)T(n)=\Theta(n^{log_b^a}lgn)=\Theta(n^{log_2^4}lgn)=\Theta(n^2lgn)
RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Master Method Example (case-3) (1) : T(n)=4T(n/2)+n3T(n)=4T(n/2)+n^3

  • a=4a=4
  • b=2b=2
  • f(n)=n3f(n)=n^3
  • nlogba=nlog24=nlog222=n2log22=n2n^{log_b^a}=n^{log_2^4}=n^{log_2^{2^2}}=n^{2log_2^2}=n^2
  • f(n)=n3f(n)=n^3 grows polynomially faster than nlogba=n2n^{log_b^a}=n^2
    • f(n)nlogba=n3n2=n=Ω(nε)\frac{f(n)}{n^{log_b^a}}=\frac{n^3}{n^2}=n=\Omega(n^{\varepsilon})
RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Master Method Example (case-3) (2) : T(n)=4T(n/2)+n3T(n)=4T(n/2)+n^3 (con't)

  • Seems like CASE 3, but need to check the regularity condition
  • Regularity condition af(n/b)cf(n)af(n/b) \leq cf(n) for some constant c<1c<1
  • 4(n/2)3cn34(n/2)^3 \leq cn^3 for c=1/2c=1/2
  • CASE-3:
    • T(n)=Θ(f(n))T(n)=\Theta(f(n)) \Longrightarrow T(n)=Θ(n3)T(n)=\Theta(n^3)
RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Master Method Example (N/A case) : T(n)=4T(n/2)+n2lgnT(n)=4T(n/2)+n^2lgn

  • a=4a=4
  • b=2b=2
  • f(n)=n2lgnf(n)=n^2lgn
  • nlogba=nlog24=nlog222=n2log22=n2n^{log_b^a}=n^{log_2^4}=n^{log_2^{2^2}}=n^{2log_2^2}=n^2
  • f(n)=n2lgnf(n)=n^2lgn grows slower than nlogba=n2n^{log_b^a}=n^2
    • but is it polynomially slower?
    • nlogbaf(n)=n2n2lgn=lgnΩ(nε)\frac{n^{log_b^a}{f(n)}}=\frac{n^2}{\frac{n^2}{lgn}}=lgn \neq \Omega(n^{\varepsilon}) for any ε>0\varepsilon>0
      • is not CASE-1
      • Master Method does not apply!
RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Master Method : Case 2 (General Version)

  • Recurrence : T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)
  • Case 2: f(n)nlogba=Θ(lgkn)\frac{f(n)}{n^{log_b^a}}=\Theta(lg^kn) for some constant k0k \geq 0
  • Solution : T(n)=Θ(nlogbalgk+1n)T(n)=\Theta(n^{log_b^a}lg^{k+1}n)
RTEU CE100 Week-2
CE100 Algorithms and Programming II

General Method (Akra-Bazzi)

T(n)=i=1kaiT(n/bi)+f(n)T(n)=\sum \limits_{i=1}^k{a_iT(n/b_i)}+f(n)

Let pp be the unique solution to

i=1k(ai/bip)=1\sum \limits_{i=1}^k{(a_i/b^p_i)}=1

Then, the answers are the same as for the master method, but with npn^p instead of nlogban^{log_b^a}
(Akra and Bazzi also prove an even more general result.)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Idea of Master Theorem (1)

Recursion Tree:
alt:"alt" center

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Idea of Master Theorem (2)

CASE 1 : The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight.

nlogbaT(1)=Θ(nlogba)n^{log_b^a}T(1)=\Theta(n^{log_b^a})

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Idea of Master Theorem (3)

CASE 2 : (k=0)(k = 0) The weight is approximately the same on each of the logbnlog_bn levels.

nlogbaT(1)=Θ(nlogbalgn)n^{log_b^a}T(1)=\Theta(n^{log_b^a}lgn)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Idea of Master Theorem (4)

CASE 3 : The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight.

nlogbaT(1)=Θ(f(n))n^{log_b^a}T(1)=\Theta(f(n))

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Proof of Master Theorem: Case 1 and Case 2

  • Recall from the recursion tree (note h=lgbn=tree heighth = lg_bn =\text{tree height})

Leaf Cost=Θ(nlogba)\text{Leaf Cost}=\Theta(n^{log_b^a})
Non-leaf Cost=g(n)=i=0h1aif(n/bi)\text{Non-leaf Cost}=g(n)=\sum \limits_{i=0}^{h-1}a^if(n/{b^i})

T(n)=Leaf Cost+Non-leaf CostT(n)=\text{Leaf Cost} + \text{Non-leaf Cost}

T(n)=Θ(nlogba)+i=0h1aif(n/bi)T(n)=\Theta(n^{log_b^a}) + \sum \limits_{i=0}^{h-1}a^if(n/{b^i})

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Proof of Master Theorem Case 1 (1)

  • nlogbaf(n)=Ω(nε)\frac{n^{log_b^a}}{f(n)}=\Omega(n^{\varepsilon}) for some ε>0\varepsilon>0

  • nlogbaf(n)=Ω(nε)O(nε)f(n)=O(nlogbaε)\frac{n^{log_b^a}}{f(n)}=\Omega(n^{\varepsilon}) \Longrightarrow O(n^{-\varepsilon}) \Longrightarrow f(n) = O(n^{log_b^{a-\varepsilon}})

  • g(n)=i=0h1aiO((n/bi)logbaε)=O(i=0h1ai(n/bi)logbaε)g(n)=\sum \limits_{i=0}^{h-1}a^iO((n/{b^i})^{log_b^{a-\varepsilon}})=O(\sum \limits_{i=0}^{h-1}a^i(n/{b^i})^{log_b^{a-\varepsilon}})

  • O(nlogbaεi=0h1aibiε/bilogbaε)O(n^{log_b^{a-\varepsilon}}\sum \limits_{i=0}^{h-1}a^ib^{i\varepsilon}/b^{ilog_b^{a-\varepsilon}})

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Proof of Master Theorem Case 1 (2)

  • i=0h1aibiεbilogba=i=0h1ai(bε)i(blogba)i=aibiεai=i=0h1(bε)i\sum \limits_{i=0}^{h-1} \frac{a^ib^{i\varepsilon}}{b^{ilog_b^a}} =\sum \limits_{i=0}^{h-1} a^i\frac{(b^\varepsilon)^i}{(b^{log_b^a})^i} =\sum a^i\frac{b^{i\varepsilon}}{a^i}=\sum \limits_{i=0}^{h-1}(b^{\varepsilon})^i

= An increasing geometric series since b>1b > 1

bhε1bε1=(bh)ε1bε1=(blogbn)ε1bε1=nε1bε1=O(nε)\frac{b^{h\varepsilon}-1}{b^{\varepsilon}-1}=\frac{(b^h)^{\varepsilon}-1}{b^{\varepsilon}-1} = \frac{(b^{log_b^n})^{\varepsilon}-1}{b^{\varepsilon}-1}=\frac{n^{\varepsilon}-1}{b^{\varepsilon}-1} = O(n^{\varepsilon})

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Proof of Master Theorem Case 1 (3)

  • g(n)=O(nlogbaεO(nε))=O(nlogbanεO(nε))=O(nlogba)g(n)=O(n^{log_b{a-\varepsilon}}O(n^{\varepsilon}))=O(\frac{n^{log_b^a}}{n^{\varepsilon}}O(n^{\varepsilon}))=O(n^{log_b^a})
  • T(n)=Θ(nlogba)+g(n)=Θ(nlogba)+O(nlogba)=Θ(nlogba)T(n)=\Theta(n^{log_b^a})+g(n)=\Theta(n^{log_b^a})+O(n^{log_b^a})=\Theta(n^{log_b^a})

Q.E.D.
(Quod Erat Demonstrandum)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Proof of Master Theorem Case 2 (limited to k=0)

  • f(n)nlogba=Θ(lg0n)=Θ(1)f(n)=Θ(nlogba)f(n/bi)=Θ((n/bi)logba)\frac{f(n)}{n^log_b^a}=\Theta(lg^0n)=\Theta(1) \Longrightarrow f(n)=\Theta(n^{log_b^a}) \Longrightarrow f(n/b^i)=\Theta((n/b^i)^{log_b^a})
  • g(n)=i=0h1aiΘ((n/bi)logba)g(n)=\sum \limits_{i=0}^{h-1}a^i\Theta((n/b^i)^{log_b^a})
  • =Θ(i=0h1ainlogbabilogba)= \Theta(\sum \limits_{i=0}^{h-1}a^i\frac{n^{log_b^a}}{b^{ilog_b^a}})
  • =Θ(nlogbai=0h1ai1(blogba)i)=\Theta(n^{log_b^a}\sum \limits_{i=0}^{h-1}a^i\frac{1}{(b^{log_b^a})^i})
  • =Θ(nlogbai=0h1ai1ai)=\Theta(n^{log_b^a}\sum \limits_{i=0}^{h-1}a^i\frac{1}{a^i})
  • =Θ(nlogbai=0logbn11)=Θ(nlogbalogbn)=Θ(nlogbalgn)=\Theta(n^{log_b^a}\sum \limits_{i=0}^{log_b^{n-1}}1) = \Theta(n^{log_b^a}log_bn)=\Theta(n^{log_b^a}lgn)
  • T(n)=nlogba+Θ(nlogbalgn)T(n)=n^{log_b^a}+\Theta(n^{log_b^a}lgn)
  • =Θ(nlogbalgn)=\Theta(n^{log_b^a}lgn)

Q.E.D.

RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Divide-and-Conquer Design Paradigm (1)

alt:"alt" center

RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Divide-and-Conquer Design Paradigm (2)

  1. Divide we divide the problem into a number of subproblems.
  2. Conquer we solve the subproblems recursively.
  3. BaseCase solve by Brute-Force
  4. Combine subproblem solutions to the original problem.
RTEU CE100 Week-2
CE100 Algorithms and Programming II

The Divide-and-Conquer Design Paradigm (3)

  • a=subproblema=\text{subproblem}
  • 1/b=each size of the problem1/b=\text{each size of the problem}

T(n)={Θ(1)ifnc(basecase)aT(n/b)+D(n)+C(n)otherwiseT(n)=\begin{cases} \Theta(1) & \text{if} & n \leq c & (basecase) \\ aT(n/b)+D(n)+C(n) & \text{otherwise} \end{cases}

Merge-Sort

T(n)={Θ(1)n=12T(n/2)+Θ(n)ifn>1T(n)=\begin{cases} \Theta(1) & & n = 1 \\ 2T(n/2)+\Theta(n) & \text{if} & n>1 \end{cases}

T(n)=Θ(nlgn)T(n)=\Theta(nlgn)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Selection Sort Algorithm

SELECTION-SORT(A)
    n = A.length;
    for j=1 to n-1
        smallest=j;
        for i= j+1 to n
            if A[i]<A[smallest]
                smallest=i;
        endfor
        exchange A[j] with A[smallest]
    endfor
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Selection Sort Algorithm

T(n)={Θ(1)n=1T(n1)+Θ(n)ifn>1T(n)=\begin{cases} \Theta(1) & & n = 1 \\ T(n-1)+\Theta(n) & \text{if} & n>1 \end{cases}

  • Sequential Series

cost=n(n+1)/2=1/2n2+1/2ncost = n(n+1)/2 = {1/2}n^2 +{1/2}n

  • Drop low-order terms
  • Ignore the constant coefficient in the leading term

T(n)=Θ(n2)T(n)=\Theta(n^2)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Merge Sort Algorithm (initial setup)

Merge Sort is a recursive sorting algorithm, for initial case we need to call Merge-Sort(A,1,n) for sorting A[1..n]A[1..n]

initial case

A : Array
p : 1 (offset)
r : n (length)
Merge-Sort(A,1,n)
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Merge Sort Algorithm (internal iterations)

internal iterations

p=startpointp = start-point

q=midpointq = mid-point

r=endpointr = end-point

A : Array
p : offset
r : length
Merge-Sort(A,p,r)
    if p=r then                (CHECK FOR BASE-CASE)
        return
    else
        q = floor((p+r)/2)    (DIVIDE)
        Merge-Sort(A,p,q)     (CONQUER)
        Merge-Sort(A,q+1,r)   (CONQUER)
        Merge(A,p,q,r)        (COMBINE)
    endif
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Merge Sort Combine Algorithm (1)

Merge(A,p,q,r)
    n1 = q-p+1
    n2 = r-q

    //allocate left and right arrays 
    //increment will be from left to right 
    //left part will be bigger than right part

    L[1...n1+1] //left array
    R[1...n2+1] //right array

    //copy left part of array
    for i=1 to n1
        L[i]=A[p+i-1]

    //copy right part of array
    for j=1 to n2
        R[j]=A[q+j]

    //put end items maximum values for termination
    L[n1+1]=inf
    R[n2+1]=inf

    i=1,j=1
    for k=p to r
        if L[i]<=R[j]
            A[k]=L[i]
            i=i+1
        else
            A[k]=R[j]
            j=j+1
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Example : Merge Sort

  1. Divide: Trivial.

  2. Conquer: Recursively sort 2 subarrays.

  3. Combine: Linear- time merge.

  • T(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n)
    • Subproblems 2\Longrightarrow 2
    • Subproblemsize n/2\Longrightarrow n/2
    • Work dividing and combining Θ(n)\Longrightarrow\Theta(n)
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Master Theorem: Reminder

  • T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)
    • Case 1: nlogbaf(n)=Ω(nε)T(n)=Θ(nlogba)\frac{n^{log_b^a}}{f(n)}=\Omega(n^{\varepsilon}) \Longrightarrow T(n)=\Theta(n^{log_b^a})
    • Case 2: f(n)nlogba=Θ(lgkn)T(n)=Θ(nlogbalgk+1n)\frac{f(n)}{n^{log_b^a}}=\Theta(lg^kn) \Longrightarrow T(n)=\Theta(n^{log_b^a}lg^{k+1}n)
    • Case 3: nlogbaf(n)=Ω(nε)T(n)=Θ(f(n))\frac{n^{log_b^a}}{f(n)}=\Omega(n^{\varepsilon}) \Longrightarrow T(n)=\Theta(f(n)) and af(n/b)cf(n)af(n/b) \leq cf(n) for c<1c<1
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Merge Sort: Solving the Recurrence

T(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n)
a=2,b=2,f(n)=Θ(n),nlogba=na=2,b=2,f(n)=\Theta(n),n^{log_b^a}=n

Case-2: f(n)nlogba=Θ(lgkn)T(n)=Θ(nlogbalgk+1n)\frac{f(n)}{n^{log_b^a}}=\Theta(lg^kn) \Longrightarrow T(n)=\Theta(n^{log_b^a}lg^{k+1}n) holds for k=0k=0

T(n)=Θ(nlgn)T(n)=\Theta(nlgn)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Binary Search (1)

Find an element in a sorted array:

1. Divide: Check middle element.
2. Conquer: Recursively search 1 subarray.
3. Combine: Trivial.

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Binary Search (2)

PARENT=i/2\text{PARENT} = \lfloor i/2 \rfloor

LEFT-CHILD=2i, 2i>n\text{LEFT-CHILD} = 2i, \text{ 2i>n}

RIGHT-CHILD=2i+1, 2i>n\text{RIGHT-CHILD} = 2i+1, \text{ 2i>n}

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Binary Search (3) : Iterative

ITERATIVE-BINARY-SEARCH(A,V,low,high)
    while low<=high
        mid=floor((low+high)/2);
        if v == A[mid]
            return mid;
        elseif v > A[mid]
            low = mid + 1;
        else
            high = mid - 1;
    endwhile
    return NIL
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Binary Search (4): Recursive

RECURSIVE-BINARY-SEARCH(A,V,low,high)
    if low>high
        return NIL;
    endif

    mid = floor((low+high)/2);

    if v == A[mid]
        return mid;
    elseif v > A[mid]
        return RECURSIVE-BINARY-SEARCH(A,V,mid+1,high);
    else
        return RECURSIVE-BINARY-SEARCH(A,V,low,mid-1);
    endif
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Binary Search (5): Recursive

T(n)=T(n/2)+Θ(1)T(n)=Θ(lgn)T(n)=T(n/2)+\Theta(1) \Longrightarrow T(n)=\Theta(lgn)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Binary Search (6): Example (Find 9)

alt:"alt" center

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Recurrence for Binary Search (7)

T(n)=1T(n/2)+Θ(1)T(n)=1T(n/2)+\Theta(1)

  • Subproblems 1\Longrightarrow 1
  • Subproblemsize n/2\Longrightarrow n/2
  • Work dividing and combining Θ(1)\Longrightarrow\Theta(1)
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Binary Search: Solving the Recurrence (8)

  • T(n)=T(n/2)+Θ(1)T(n) = T(n/2) + \Theta(1)

  • a=1,b=2,f(n)=Θ(1)nlogba=n0=1a = 1,b = 2,f(n) = \Theta(1) \Longrightarrow n^{log_b^a} = n^0=1

  • Case 2: f(n)nlogba=Θ(lgkn)T(n)=Θ(nlogbalgk+1n)\frac{f(n)}{n^{log_b^a}}=\Theta(lg^kn) \Longrightarrow T(n)=\Theta(n^{log_b^a}lg^{k+1}n) holds for k=0k=0

  • T(n)=Θ(lgn)T(n)=\Theta(lgn)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Powering a Number: Divide & Conquer (1)

Problem: Compute an, where n is a natural number

NAIVE-POWER(a, n)
    powerVal = 1;
    for i = 1 to n
        powerVal = powerVal * a;
    endfor
return powerVal;
  • What is the complexity? T(n)=Θ(n)\Longrightarrow T(n)=\Theta(n)
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Powering a Number: Divide & Conquer (2)

  • Basic Idea:

an={an/2an/2if n is evena(n1)/2a(n1)/2aif n is odda^n=\begin{cases} a^{n/2}*a^{n/2} & \text{if n is even} \\ a^{(n-1)/2}*a^{(n-1)/2}*a & \text{if n is odd} \end{cases}

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Powering a Number: Divide & Conquer (3)

POWER(a, n)
    if n = 0 then 
        return 1;
    else if n is even then
        val = POWER(a, n/2);
        return val * val;
    else if n is odd then
        val = POWER(a,(n-1)/2)
        return val * val * a;
    endif
RTEU CE100 Week-2
CE100 Algorithms and Programming II

Powering a Number: Solving the Recurrence (4)

  • T(n)=T(n/2)+Θ(1)T(n) = T(n/2) + \Theta(1)

  • a=1,b=2,f(n)=Θ(1)nlogba=n0=1a = 1,b = 2,f(n) = \Theta(1) \Longrightarrow n^{log_b^a} = n^0=1

  • Case 2: f(n)nlogba=Θ(lgkn)T(n)=Θ(nlogbalgk+1n)\frac{f(n)}{n^{log_b^a}}=\Theta(lg^kn) \Longrightarrow T(n)=\Theta(n^{log_b^a}lg^{k+1}n) holds for k=0k=0

  • T(n)=Θ(lgn)T(n)=\Theta(lgn)

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Correctness Proofs for Divide and Conquer Algorithms

  • Proof by induction commonly used for Divide and Conquer Algorithms

  • Base case: Show that the algorithm is correct when the recursion bottoms out (i.e., for sufficiently small n)

  • Inductive hypothesis: Assume the alg. is correct for any recursive call on any smaller subproblem of size kk, (k<n)(k < n)

  • General case: Based on the inductive hypothesis, prove that the alg. is correct for any input of size n

RTEU CE100 Week-2
CE100 Algorithms and Programming II

Example Correctness Proof: Powering a Number

  • Base Case: POWER(a,0)POWER(a, 0) is correct, because it returns 11
  • Ind. Hyp: Assume POWER(a,k)POWER(a, k) is correct for any k<nk<n
  • General Case:
    • In POWER(a,n)POWER(a,n) function:
      • If nn is eveneven:
        • val=an/2val = a^{n/2} (due to ind. hyp.)
        • it returns valval=anval*val = a^n
      • If nn is oddodd:
        • val=a(n1)/2val = a^{(n-1)/2} (due to ind. hyp.)
        • it returns valvala=anval*val*a = a^n
  • The correctness proof is complete
RTEU CE100 Week-2
CE100 Algorithms and Programming II

References

RTEU CE100 Week-2
CE100 Algorithms and Programming II

EndOfWeek2CourseModule-End-Of-Week-2-Course-Module-

RTEU CE100 Week-2