Chengwei LEI, Ph.D.    Associate Professor

Department of Computer and Electrical Engineering and Computer Science
California State University, Bakersfield

CMPS 3140 Complexity Review

 

 




Example Algorithm Explanation
Pseudocode

MERGE-SORT  A[1 . . n]

1.If n = 1, done.
2.Recursively sort A[ 1 . . én/2ù ] and A[ én/2ù+1 . . n ] .
3.Merge the 2 sorted lists.

 

White the pseudocode to "high-level" discribe the algorithm
Prove the Correctness

Proof by induction:

1.Base case: if n = 1, the algorithm will return the correct answer because A[1..1] is already sorted.
2.Inductive hypothesis: assume that the algorithm correctly sorts
A[1.. én/2ù ] and A[én/2ù+1..n].
3.Step: if A[1.. én/2ù ] and A[én/2ù+1..n] are both correctly sorted, the whole array A[1.. én/2ù ] and A[én/2ù+1..n] is sorted after merging.
Mathematical proof: for each input it produces the expected output
Time Efficiency Analysis

T(n) =Q(1)  if n = 1;
T(n) =2T(n/2) + Q(n)  if n > 1.
......
Q(n log n)

A measure of amount of time(how do you define "time"? ) for an algorithm to execute
Space Efficiency Analysis ...... ......











~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~






Two types of algorithm (Iterative vs Recursive)


Compute the Power of an integer: how can we compute bn with recursive algorithm? What's performance difference between following two algorithms?

def power(b, n):
    pow = 1
    for i in range(n):
        pow = pow * b
    return pow
def power(b, n):
    if n == 0:
        return 1
    pow = power(b, n // 2)
    if n & 1: #odd
        return b * pow * pow
    return pow * pow #even

 

 




How to formally calculate algorithm complexity



Examples

Examples

 

  • Calculate Complexity Example (Insertion Sort)

Examples (PDF)

 







~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~






How to find out the order of growth




Examples


Examples