Algorithm Analysis

Measuring Complexity

Nathan Tenney

WSU Tri-Cities

Recommended Reading

Algorithm Analysis

Algorithms

Analysis steps

Data Structures

Review

Properties of Algorithms

  1. Must be correct
  2. Steps must be well understood
  3. No ambiguity at any point in the process
  4. Fixed number of steps
  5. Must end (no infinite loops)

Math Review: Sets

Math Review: Sets

Math Review: Sequences

Math Review: Relations

Math Review: Relations

Math Review: Logarithms

Definitions

Definitions

Things to note

Proof

Claim: if \(T\left(n\right) = a_{k}N^{k} + … + a_{1}N + a_{0}\) then \(T\left(N\right) = O\left(N^{k}\right)\)

Proof: choose \(n_0 = 1\) and \( c = |a_{k}| + |a_{k-1}| + … + |a_{1}| + |a_{0}| \)

Need to show that \(\forall N \ge 1\), \(T\left(N\right) \le c \cdot N^{k}\)

Properties

Typical Growth Rates

Function Name
c Constant
\(\log N\) Logarithmic
\(\log^2 N\) Log-squared
N Linear
\(N \log N\)  
\(N^{2}\) Quadratic
\(N^{3}\) Cubic
\(2^N\) Exponential

The Model

Analysis of a Program

Rules

  1. For Loops
    • The running time of the items in the loop times the number of iterations
  2. Nested loops
    • Analyze inside out
  3. Consecutive statements
    • Sum of the consecutive statements
  4. if/else
    • Running time of the test plus the maximum of the two statements.

Simple Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int sum( int n )
{
    int partialSum;
    
    partialSum = 0;
    for( int i = 1; i <= n; i++ )
        partialSum += i * i * i;
        
    return partialSum;
}

Maximum Subsequence Sum Problem

Maximum Subsequence Sum

Input Size $$ O(N^3) $$ $$ O(N^2) $$ $$ O(N log N) $$ $$O(N)$$
N=10 0.000009 0.000004 0.000006 0.000003
N=100 0.002580 0.000109 0.000045 0.000006
N=1000 2.281013 0.010203 0.000485 0.000031
N=10000 NA 1.2329 0.005712 0.000317
N=100000 NA 135 0.064618 0.003206

Cubic Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
/	*
 * Cubic maximum contiguous subsequence sum algorithm.
 */
int maxSubSum1( const vector<int> & a )
{
    int maxSum = 0;
    for( int i = 0; i < a.size( ); i++ )
        for( int j = i; j < a.size( ); j++ )
        {
            int thisSum = 0;
            for( int k = i; k <= j; k++ )
                thisSum += a[ k ];
            if( thisSum > maxSum )
                maxSum   = thisSum;
        }
    return maxSum;
}

Mathematical Evaluation

Useful Formulas

Mathematical Evaluation

Mathematical Evaluation

\[\begin{eqnarray}\displaystyle\sum_{j=i}^{N-1} j - i + 1 &\rightarrow& \frac{\left(N-M+1\right)\left(N+M\right)}{2} \\ \left(n+m\right) &=& \left(N-1-i+1\right) + \left(i - i + 1\right) = \left(N-i+1\right) \\ \left(n-m+1\right) &=& \left(N - 1 - i + 1\right) - \left(i - i + 1\right) + 1 \\ &=& N - i - 1 + 1 = N - i \\ \displaystyle\sum_{j=i}^{N-1} j - i + 1 &=& \frac{\left(N-i+1\right)\left(N-i\right)}{2}\end{eqnarray}\]

Mathematical Evaluation

Mathematical Evaluation

\[\displaystyle\sum_{i=0}^{N-1} \frac{\left(N-i+1\right)\left(N-i\right)}{2}\] \[\begin{eqnarray}&=&\sum_{i=1}^N \frac{\left(N-i+1\right)\left(N-i+2\right)}{2}\\ &=&\sum_{i=1}^N \frac{N^2-Ni+2N-Ni+i^2-2i+n-i+2}{2} \\ &=&\sum_{i=1}^N \frac{i^2-2Ni-3i+N^2+3N+2}{2}\end{eqnarray}\]

Mathematical Evaluation

\[\begin{eqnarray}&=&\sum_{i=1}^N \frac{i^2-\left(2Ni+3i\right)+\left(N^2+3N+2\right)}{2}\\ &=&\frac{1}{2}\sum_{i=1}^N i^2 - \left( N + \frac{3}{2} \right) \sum_{i=1}^N i + \frac{1}{2} \left(N^2+3N+2\right) \sum_{i=1}^N 1 \\ &=&\left(\frac{1}{2}\frac{N\left(N+1\right)\left(2N+1\right)}{6}\right) - \left(N+\frac{3}{2}\right)\frac{N\left(N+1\right)}{2} + \frac{N^2+3N+2}{2}N \end{eqnarray}\]

Mathematical Evaluation

\[\begin{eqnarray}&=&\left(\frac{1}{2}\frac{N\left(N+1\right)\left(2N+1\right)}{6}\right) - \left(N+\frac{3}{2}\right)\frac{N\left(N+1\right)}{2} + \frac{N^2+3N+2}{2}N \\ &=&\frac{N^3 + 3N^2 + 2N}{6} \end{eqnarray} \]

Quadratic Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
int maxSubSum2( const vector<int> & a )
{
    int maxSum = 0;
    for( int i = 0; i < a.size( ); i++ )
    {
        int thisSum = 0;
        for( int j = i; j < a.size( ); j++ )
        {
            thisSum += a[ j ];
            if( thisSum > maxSum )
                maxSum = thisSum;
        }
    }
    return maxSum;
}

Quadratic Solution

Recursive Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int maxSumRec( const vector<int> & a, int left, int right )
{
    if( left == right )  // Base case
        if( a[ left ] > 0 )
            return a[ left ];
        else
            return 0;

    int center = ( left + right ) / 2;
    int maxLeftSum  = maxSumRec( a, left, center );
    int maxRightSum = maxSumRec( a, center + 1, right );

    int maxLeftBorderSum = 0, leftBorderSum = 0;
    for( int i = center; i >= left; i-- )
    {
        leftBorderSum += a[ i ];
        if( leftBorderSum > maxLeftBorderSum )
            maxLeftBorderSum = leftBorderSum;
    }
    int maxRightBorderSum = 0, rightBorderSum = 0;
    for( int j = center + 1; j <= right; j++ )
    {
        rightBorderSum += a[ j ];
        if( rightBorderSum > maxRightBorderSum )
            maxRightBorderSum = rightBorderSum;
    }
    return max3( maxLeftSum, maxRightSum,
                    maxLeftBorderSum + maxRightBorderSum );
}

Recursive Solution

Linear Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
int maxSubSum4( const vector<int> & a )
{
    int maxSum = 0, thisSum = 0;
    for( int j = 0; j < a.size( ); j++ )
    {
        thisSum += a[ j ];
        if( thisSum > maxSum )
            maxSum = thisSum;
        else if( thisSum < 0 )
            thisSum = 0;
    }
    return maxSum;
}

Logarithmic Algorithms

Binary Search

Binary Search

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
int binarySearch( const vector<Comparable> & a, 
    const Comparable & x )
{
    int low = 0, high = a.size( ) - 1;
    while( low <= high )
    {
        int mid = ( low + high ) / 2;
        if( a[ mid ] < x )
            low = mid + 1;
        else if( a[ mid ] > x )
            high = mid - 1;
        else
            return mid;   // Found
    }
    return NOT_FOUND;     // NOT_FOUND is defined as -1
}

Euclid's Algorithm

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
long gcd( long m, long n )
{
    while( n != 0 )
    {
        long rem = m % n;
        m = n;
        n = rem;
    }
    return m;
}

Exponentiation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
bool isEven( int n )
{
    return n % 2 == 0;
}

long pow( long x, int n )
{
    if( n == 0 )
        return 1;
    if( n == 1 )
        return x;
    if( isEven( n ) )
        return pow( x * x, n / 2 );
    else
        return pow( x * x, n / 2 ) * x;
}

Checking Analysis