Question

In: Computer Science

There are two algorithms that perform a particular task. Algorithm 1 has a complexity function: f(n)...

There are two algorithms that perform a particular task. Algorithm 1 has a complexity function: f(n) = 5n + 50. Algorithm 2 has a complexity function g(n) = n^2 + 10g . (Show your answer)

a)Which algorithm is more efficient when n = 5?

b) Which algorithm is more efficient when n = 20?

c) what are complexity of f(n) and g(n)

Solutions

Expert Solution

Note:

According to the question g(n) = n^2 + 10g, but g is not defined. So, it is either n or misprinted.

I'll be solving both the solution:

f(n) = 5n + 50

g(n) = n2 + 10n

Ans a) We are given n=5, so we'll calculate the value of f(5) and g(5), and the function which will return the minimum value will be more efficient.

f(5) = 5*5 + 50 = 75

g(5) = 52 + 10*5 = 25 + 50 = 75

Both the function is returning the same value hence, for n=5 both the algorithms are efficient.

Ans b)

We are given n=5, so we'll calculate the value of f(20) and g(20), and the function which will return the minimum value will be more efficient.

f(20) = 5*20 + 50 = 150

g(20) = 202 + 10*20 = 400 + 200 = 600

The value returned by f(20) is less than the value returned by g(20). Hence, f(n) is more efficient when n=20.

Ans c) Complexity of f(n)

f(n) = 5n + 50

We can see that the Highest factor of n in the function is 1. So the complexity of f(n) is O(n) or more precisely (n).

g(n) = n2 + 10n

We can see that the Highest factor of n in the function is 2. So the complexity of g(n) is O(n2) or more precisely (n2).

Note the complexity of g(n) is greater than the f(n). Hence for large values g(n) is much better than f(n).

***************************************************************************************************

If g(n) = n2 + 10

f(n) = 5n + 50

g(n) = n2 + 10

Ans a) We are given n=5, so we'll calculate the value of f(5) and g(5), and the function which will return the minimum value will be more efficient.

f(5) = 5*5 + 50 = 75

g(5) = 52 + 10 = 25 + 10 = 35

The value returned by g(5) is less than the value returned by f(5). Hence, g(n) is more efficient when n=5.

Ans b)

We are given n=5, so we'll calculate the value of f(20) and g(20), and the function which will return the minimum value will be more efficient.

f(20) = 5*20 + 50 = 150

g(20) = 202 + 10 = 400 + 10 = 410

The value returned by f(20) is less than the value returned by g(20). Hence, f(n) is more efficient when n=20.

Ans c) Complexity of f(n)

f(n) = 5n + 50

We can see that the Highest factor of n in the function is 1. So the complexity of f(n) is O(n) or more precisely (n).

g(n) = n2 + 10

We can see that the Highest factor of n in the function is 2. So the complexity of g(n) is O(n2) or more precisely (n2).

Note the complexity of g(n) is greater than the f(n). Hence for large values g(n) is much better than f(n).

Leave a comment if face any doubt!!!!


Related Solutions

Sorting algorithm for arrays: understand how to perform the following algorithms. (a). Simple sorting Bubblesort:T(n)ÎO(n2) Selection...
Sorting algorithm for arrays: understand how to perform the following algorithms. (a). Simple sorting Bubblesort:T(n)ÎO(n2) Selection sort : T(n) Î O(n2) Insertion sort: T(n) Î O(n2) (b).Advanced sorting i. Shell sort: O(n2) < O(n1.25) < O(nlogn), O(n1.25) is empirical result for shell sort. Merge sort: T(n) Î O(nlogn), need one temporary array with the same length as the array needed to be sorted. Quick sort: -average case: T(n) Î O(nlogn), -worst case(rare occurrence): T(n) Î O(n2) 5. Searching algorithm for...
Suppose you are choosing between the following two algorithms: Algorithm A: solves problems of size n...
Suppose you are choosing between the following two algorithms: Algorithm A: solves problems of size n by recursively solving two subproblems each of size (n-1) and then combining the solutions in constant time (take T(0) = 0). Algorithm B: solves problems of size n by recursively solving one subproblem of size (n-1) and then combining the solution in (log n) time (take T(1) = 0). Write down the recurrence relations for the above algorithms.                           Solve the recurrence relations...
Given a monotonically increasing function f(n) (where ‘n’ is an integer), use a binary search algorithm...
Given a monotonically increasing function f(n) (where ‘n’ is an integer), use a binary search algorithm to find the largest value of ‘n’ for which f(n) is less than a target. Show all the work (including the values for the left index, right index, middle index and the function value at the middle index) for each iteration as well as write down the initial values of the left index and right index and the corresponding function values. f(n) = 3n^3...
Description: The goal of this assignment is to compare the empirical complexity of two sorting algorithms:...
Description: The goal of this assignment is to compare the empirical complexity of two sorting algorithms: a) Heap sort and b) Radix sort. Instructions: - Implement the above two sorting algorithms using Java or any other programming language. - Repeatedly generate random input instances containing 10, 50, 100, 500, 1000, 5000, 10000, 15000, … 50 000. The generated numbers must be between 0 and 100. - Execute both algorithms to sort the randomly generated arrays. - Compare the running time...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java program to reverse a stack of characters. Make your own assumption and your own design on the methods signature, inputs, outputs, and the full main program.
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
Let function F(n, m) outputs n if m = 0 and F(n, m − 1) +...
Let function F(n, m) outputs n if m = 0 and F(n, m − 1) + 1 otherwise. 1. Evaluate F(10, 6). 2. Write a recursion of the running time and solve it . 3. What does F(n, m) compute? Express it in terms of n and m.
This function uses a curious mix of iteration and recursion: function F(n) if n < 1...
This function uses a curious mix of iteration and recursion: function F(n) if n < 1 return 1 t <- 0 for i <- 0 to n for j <- i to n t <- t + j return t + F(n-1) What is the number of basic operations (additions and subtractions) performed? Answer: Θ(n³) Could you tell me how I can solve this problem??
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;...
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;   f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 b) Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
1) Discuss the complexity of building efficient algorithms in Game Theory (+150 words)
1) Discuss the complexity of building efficient algorithms in Game Theory (+150 words)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT