Question

In: Computer Science

Problem 3 The function distance returns the distance between two strings X and Y. Its running...

Problem 3

The function distance returns the distance between two strings X and Y. Its running time is exponential. Explain why.

(Note: make sure you understand what X[0: -1] is.)

#
# Input: X, Y, which are strings
# Output: a number, which is the distance between the two strings
#
def distance(X, Y):
    if len(X)==0:
        return len(Y)
    if len(Y)==0:
        return len(X)
    if X[-1]==Y[-1]:
        return distance(X[0:-1],  Y[0:-1])
    else:
        a = distance(X[0:-1], Y[0:-1])
        b = distance(X, Y[0:-1])
        c = distance(X[0:-1], Y)
        return 1 + min(a,b,c)

Solutions

Expert Solution

Lets Suppose Length of String argument given to function intially are

For String x = n

String y = m

X[-1] = last character of string X = X[n-1]

X[0:-1] = String after removing X[n-1](X[-1] or last character of string) from String X , so length of X[0:-1] will be (n-1)

distance(x,y)

Base case (termination) for function is when length of one of the argumen is 0;

else

/*

 else:
        a = distance(X[0:-1], Y[0:-1])
        b = distance(X, Y[0:-1])
        c = distance(X[0:-1], Y)
        return 1 + min(a,b,c)

It will be calling 3 more function with aregument (n-1,m-1) (n,m-1) (n-1,m) respectively

*/

if (X[-1] == Y[-1])

then function is making call to one function , we are ignoring matching of last character of string to calculate worst case time complexity of function

following is tree to calculate total number of calls made in one execution of program

So programs complexity will O(3n) which is exponential.


Related Solutions

True or False. The production function f(x,y)=x^(2/3)+y^(2/3) has increasing returns to scale
True or False. The production function f(x,y)=x^(2/3)+y^(2/3) has increasing returns to scale
A function f : X ------> Y between two topological spaces ( X , TX )...
A function f : X ------> Y between two topological spaces ( X , TX ) and ( Y , TY ) is called a homeomorphism it has the following properties: a) f is a bijection (one - to- one and onto ) b) f is continuous c) the inverse fucntion f  -1 is continuous ( f is open mapping) A function with these three properties is sometimes called bicontinuous . if such a function exists, we say X and Y...
Compute the correlation between X and the transformed Y values. Distance jumped in inches (X) Distance...
Compute the correlation between X and the transformed Y values. Distance jumped in inches (X) Distance of discus throw in inches (Y) 3 12 1 36 4 60 1 12 3 48 5 12 1 24
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...
The Lennard-Jones potential energy, U(x), is a function of "x" the distance between a pair of...
The Lennard-Jones potential energy, U(x), is a function of "x" the distance between a pair of molecules. It is frequently called the van der Walls potential between molecules for dipole-dipole interactions. The potential is U(x) = C12/ x^12 - C6/x^6             [1] Here we assume for water molecules: C12 = 9.5 x 104" J•m12 C6 = 1 x 10-76 J•m6. (a) What is the separation distance (Xs) between two water molecules where the applied force (F) to pull the molecules apart...
in the problem given (3 - x)y" + x2y' + (1 + x)y = 0 ,...
in the problem given (3 - x)y" + x2y' + (1 + x)y = 0 , x0=0. find the recurrence relation and the series solution given y(0) = -6 and y'(0)= 2
Given the utility function U ( X , Y ) = X 1 3 Y 2...
Given the utility function U ( X , Y ) = X 1 3 Y 2 3, find the absolute value of the MRS when X=10 and Y=24. Round your answer to 4 decimal places.
The Lennard-Jones potential energy, U(x), is a function of “x” the separation distance between a pair...
The Lennard-Jones potential energy, U(x), is a function of “x” the separation distance between a pair of atoms in a molecule. It is frequently called the van der Walls potential between atoms for dipole-dipole interactions. The potential is U(x) = C12/ x12 - C6/x6,                                                                    [1] Where C12 = 1.51 x 10-134 J∙m12 C6 = 1.01 x 10-77 J∙m6. (a) Determine the shape of the potential energy curve vs. distance of separation (x) between the atoms using Equation [1] by plotting...
Use the following returns for X and Y. Returns Year X Y 1 21.9 % 26.7...
Use the following returns for X and Y. Returns Year X Y 1 21.9 % 26.7 % 2 – 16.9 –3.9 3 9.9 28.7 4 19.8 –14.8 5 4.9 32.7 Calculate the average returns for X and Y. (Do not round intermediate calculations and enter your answers as a percent rounded to 2 decimal places, e.g., 32.16.) Average returns X % Y %    Calculate the variances for X and Y. (Do not round intermediate calculations and round your answers...
Use the following returns for X and Y. Returns Year X Y 1 22.0 % 27.0...
Use the following returns for X and Y. Returns Year X Y 1 22.0 % 27.0 % 2 – 17.0 – 4.0 3 10.0 29.0 4 20.0 – 15.0 5 5.0 33.0 Requirement 1: Calculate the average returns for X and Y. (Do not round intermediate calculations. Enter your answers as a percentage rounded to 2 decimal places (e.g., 32.16).) Average returns X % Y % Requirement 2: Calculate the variances for X and Y. (Do not round intermediate calculations....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT