Algorithm Analysis (CSC 301 2015F) : EBoards

CSC301.01 2015F, Class 07: Pause for Breath


Overview

Preliminaries

Admin

Upcoming Work

Extra Credit

Academic

Peer

Questions

Using assert

    #include <assert.h>

    main ()
    {
      assert(square(x) == x);
    }

Proving closed forms of recurrence relations

    T(n) = 2*T(n/2) + dn
    T(1) = 1

    Conclusion T(N) is in O(nlogn)

Let's prove it. What techniques do you know? induction, contradiction, strong induction, construction, prove the inverse of the contrapositive

Goal is to prove T(n) is in O(nlogn). We need to prove that there exist an n0 and a c s.t. for all n > n0 T(n) <= c*nlogn.

The (strong) inductive hypothesis says ...

With your group

Base case: Exists c s.t. T(1) <= c*1log1. Whoops. log(1) = 0. So ... Choose a different base case (or base cases)

Does there exist a c s.t. T(2) <= c*2log2

Inductive hypothesis: For n > n0 T(n/2) <= c(n/2)*log(n/2)

General indicutive hypothesis. We are trying to prove CLAIM(n). We assume for all k < n, CLAIM(k) holds.

Inductive hypothesis: For n0 < k < n, T(k) <= cklogk

    T(n) = 2*T(n/2) + dn
    T(n) <= 2*c*n/2*log(n/2) + dn 
            ; because n/2 < n and 
            ; T(n/2) = c * n/2 * log(n/2)
    T(n) <= c*n*log(n/2) + dn
            ; Cancel the 2 and 1/2
    T(n) <= c*n*(log(n)+log(1/2)) + dn
            ; log(ab) = log(a) + log(b)
    T(n) <= c*n*(log(n)+log(2^-1)) + dn
    T(n) <= c*n*(log(n)-1) + dn
    T(n) <= c*n*log(n) - cn + dn
    T(n) <= c*n*log(n) + (d-c)n
            ; If c > d, (d-c)*n is negative
    T(n) <= c*n*log(n) + (d-c)n <= c*n*log(n)
    T(n) <= c*n*log(n) Q.E.D.

Revised hypothesis n0 and a c > d s.t. for all n > n0 T(n) <= c*nlogn.

Finding the kth-largest element of an unsorted array/list