Can not figure out complexity of this recurrence

前端 未结 4 692
借酒劲吻你
借酒劲吻你 2021-01-21 07:21

I am refreshing on Master Theorem a bit and I am trying to figure out the running time of an algorithm that solves a problem of size n by recursively solving 2 subp

4条回答
  •  天涯浪人
    2021-01-21 07:48

    ah, enough with the hints. the solution is actually quite simple. z-transform both sides, group the terms, and then inverse z transform to get the solution.

    first, look at the problem as

        x[n] = a x[n-1] + c
    

    apply z transform to both sides (there are some technicalities with respect to the ROC, but let's ignore that for now)

        X(z) = (a X(z) / z) + (c z / (z-1))
    

    solve for X(z) to get

        X(z) = c z^2 / [(z - 1) * (z-a)]
    

    now observe that this formula can be re-written as:

        X(z) = r z / (z-1) + s z / (z-a)
    

    where r = c/(1-a) and s = - a c / (1-a)

    Furthermore, observe that

        X(z) = P(z) + Q(z)
    

    where P(z) = r z / (z-1) = r / (1 - (1/z)), and Q(z) = s z / (z-a) = s / (1 - a (1/z))

    apply inverse z-transform to get that:

        p[n] = r u[n] 
    

    and

        q[n] = s exp(log(a)n) u[n]
    

    where log denotes the natural log and u[n] is the unit (Heaviside) step function (i.e. u[n]=1 for n>=0 and u[n]=0 for n<0).

    Finally, by linearity of z-transform:

        x[n] = (r + s exp(log(a) n))u[n]
    

    where r and s are as defined above.

    so relabeling back to your original problem,

        T(n) = a T(n-1) + c
    

    then

        T(n) = (c/(a-1))(-1+a exp(log(a) n))u[n]
    

    where exp(x) = e^x, log(x) is the natural log of x, and u[n] is the unit step function.

    What does this tell you?

    Unless I made a mistake, T grows exponentially with n. This is effectively an exponentially increasing function under the reasonable assumption that a > 1. The exponent is govern by a (more specifically, the natural log of a).

    One more simplification, note that exp(log(a) n) = exp(log(a))^n = a^n:

        T(n) = (c/(a-1))(-1+a^(n+1))u[n]
    

    so O(a^n) in big O notation.

    And now here is the easy way:

    put T(0) = 1

        T(n) = a T(n-1) + c
    
        T(1) = a * T(0) + c = a + c
        T(2) = a * T(1) + c = a*a + a * c + c
        T(3) = a * T(2) + c = a*a*a + a * a * c + a * c + c
        ....
    

    note that this creates a pattern. specifically:

        T(n) = sum(a^j c^(n-j), j=0,...,n)
    

    put c = 1 gives

        T(n) = sum(a^j, j=0,...,n)
    

    this is geometric series, which evaluates to:

        T(n) = (1-a^(n+1))/(1-a)
             = (1/(1-a)) - (1/(1-a)) a^n
             = (1/(a-1))(-1 + a^(n+1))
    

    for n>=0.

    Note that this formula is the same as given above for c=1 using the z-transform method. Again, O(a^n).

提交回复
热议问题