SUNY Geneseo Department of Computer Science


The Master Method

Tuesday, February 19

CSci 242, Spring 2013
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture

Questions?

Analyzing Execution Time and the Master Method

Quicksort

    quicksort( A, first, last )
        if first > last
            p = partition( A, first, last )     // Θ(n)
            quicksort( A, first, p-1 )          // T(n/2)
            quicksort( A, p+1, last )           // T(n/2)

Strassen’s algorithm

    strassen( A, B, n )
        if n == 1
            return A[1][1] * B[1][1]
        else
            compute S1 through S10, sums and differences of n/2-by-n/2 matrices
            compute P1 through P7, products of n/2-by-n/2 matrices
            compute C as sums and differences of certain P matrices
            return C

n-by-n matrix divides into n/2-by-n/2 quadrants

        given k-by-k matrices X and Y, with sum to go in Z
        for i = 1 to k
            for j = 1 to k
                Z[i][j] = X[i][j] + Y[i][j]

Our kth-smallest algorithm

    kth( k, A, first, last )
        if first == last
            return A[first]
        else
            p = partition( A, first, last )
            if k < p - first + 1
                return kth( k, A, first, p-1 )
            else if k > p - first + 1
                return kth( k - p + first - 1, A, p+1, last )
            else
                return A[p]

Naive version of exponentiation

    power( x, n )
        if n == 0
            return 1
        else if n is even
            return power( x, n/2 ) * power( x, n/2 )
        else
            return x * power(x,(n-1)/2) * power(x,(n-1)/2)

Hand out master method problem set

Next

Introduction to dynamic programming

Read chapter 15 introduction and section 15.1


Next Lecture