Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js

Solving Recurrences

Nathan Sprague

$ $

Binary Search Recurrence

Example: Recursive Binary Search

Example: Recursive Binary Search

Step 2: Backward Substitution

W(1)=1

W(x)=1+W(x/2)

W(n) =1+W(n/2)
=1+1+W((n/2)/2)     (substitute)
=2+W(n/4) (simplify)
=2+1+W((n/4)/2) (substitute)
=3+W(n/8) (simplify)
...
=i+W(n2i) (generalize)

Solve for i that results in initial condition:

n2i=1

n=2i

i=log2n

Substitute log2n for i:

W(n)=log2n+1

Step 3: Check the Answer

Applying recurrence:

W(1)=1

W(2)=1+W(1)=1+1=2

W(4)=1+W(2)=1+2=3

Applying solution:

W(1)=log21+1=0+1=1

W(2)=log22+1=1+1=2

W(4)=log44+1=2+1=3

Steps to Analyzing a Recursive Algorithm

Another Example:

count the comparison values[start] == values[end]

    public static boolean unique(int[] values, int start, int end) {
        if (start >= end) {
            return true;
        } else if (values[start] == values[end]) {
            return false;
        } else {
            return unique(values, start, end - 1) 
                    && unique(values, start + 1, end);
        }

    }

Part 1:

    public static boolean unique(int[] values, int start, int end) {
        if (start >= end) {
            return true;
        } else if (values[start] == values[end]) {
            return false;
        } else {
            return unique(values, start, end - 1) 
                    && unique(values, start + 1, end);
        }

    }

W(1)=0

W(n)=1+2W(n1)

Part 2:

W(1)=0

W(x)=1+2W(x1)

W(n) =1+2W(n1)
=1+2(1+2W((n1)1)     (substitute)
=1+2+4W(n2) (simplify)
=1+2+4(1+2W((n2)1) (substitute)
=1+2+4+8W(n3) (simplify)
...
=i1j2j+2iW(ni) (generalize)
=2i1+2iW(ni) (generalize)

Solve for i that results in initial condition:

ni=1

i=n1

Substitute n1 for i:

W(n)=2n11+2n1×0=2n11

Part 3

Applying recurrence:

W(1)=0

W(2)=1+2W(0)=1+0=1

W(3)=1+2W(1)=1+1=2

W(4)=1+2W(2)=1+2=3

Applying solution:

W(1)=2111=0

W(2)=2211=1

W(3)=2311=3