Question

In: Computer Science

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 to find the exact number of operations for each of these two algorithms in terms of (n). Arrange the algorithms in increasing complexity.                

Solutions

Expert Solution

Solution:

Algorithm A:

Given,

=>Solves the problem of size n recuursively solving two subproblems each of size (n-1) and combining the solutions in constant time.

=>T(0) = 0

Explanation:

Recurrence relation:

=>We can write recurrence relation as: T(n) = 2T(n-1) + c where c is some constant.

Solving recurrence relation:

=>T(n) = 2T(n-1) + c....(1)

=>T(n-1) = 2T(n-2) + c..(2)

From (1) and (2)

=>T(n) = 2^2T(n-2) + 2c + c

and so on.

=>T(n) = 2^kT(n-k) + c + 2c + ... + 2^(k-1)c

=>T(n) = 2^kT(n-k) + c*(1 + 2 + 2^2 + .. + 2^(k-1))

Summation of geometric progression.

=>T(n) = 2^kT(n-k) + c*(2^k - 1)...(3)

We know that T(0) = 0

=>n - k = 1

=>k = n...(4)

From (3) and (4)

=>T(n) = 2^n*0 + c*(2^n - 1)

=>T(n) = c*(2^n - 1)

=>Hence T(n) = (2^n)

Algorithm B:

Given,

=>Solves problems of size n by recursively solving one subproblem of size n-1 and then combining solutions in logn time.

=>T(1) = 0

Explanation:

Recurrence relation:

=>We can write the recurrence relation as: T(n) = T(n-1) + logn

Solving recurrence relation:

=>T(n) = T(n-1) + logn....(1)

=>T(n-1) = T(n-2) + log(n-1)...(2)

From (1) and (2)

=>T(n) = T(n-2) + logn + log(n-1)

and so on.

=>T(n) = T(n-k) + logn + log(n-1) + .. + log(n-(k-1))

=>T(n) = T(n-k) + log(n*(n-1)*(n-2)*...*(n-(k-1))....(3)

We know that T(1) = 0

=>n - k = 1

=>k = n-1...(4)

From (3) and (4)

=>T(n) = 0 + log(n*(n-1)*(n-2)*..*(n-(n-1))

=>T(n) = logn!

=>T(n) = nlogn asymptotically

=>Hence T(n) = (nlogn)

Finding order:

=>Algorithm A > Algorithm B because growth rate of algorithm A is exponential and growth rate of algorithm B is combination of polynomial and logarithmic functions.

I have explained each and every part with the help of statements attached to the answer above.


Related Solutions

Suppose n people are choosing between two activities, hiking or fishing, where n is an even...
Suppose n people are choosing between two activities, hiking or fishing, where n is an even number. The payoff from going to hike is 1 if more than half of the people go hiking, and 0 otherwise. The payoff from going to fish is 1 if more than half of the people go fishing, and 0 otherwise. A.Find all the Nash Equilibria of this game, if any. B.Suppose instead that the payoffs are such that the payoff to hiking is...
There are two algorithms: –Algorithm A requires 5*n2 time units to solve a problem of size...
There are two algorithms: –Algorithm A requires 5*n2 time units to solve a problem of size n. –Algorithm B requires 7*n time units to solve a problem of size n. Draw a chart (graph) to show the performance of the programs.
Algorithm questions. 1.When choosing an algorithm for the shortest path between two nodes in an unweighted...
Algorithm questions. 1.When choosing an algorithm for the shortest path between two nodes in an unweighted graph, should we use Breadth First Search or Depth First Search ? 2. When choosing a data structure to store a graph for an algorithm, how should we store the graph knowing that the graph is dense and this algorithm needs to determine many times if two nodes are directly connected ? Should it be an Adjacency Matrix or an Adjacency List ?
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)
Suppose we have a substring of length m and text of size n. Write an algorithm...
Suppose we have a substring of length m and text of size n. Write an algorithm to find out if the substring is present in the text or not. What is the complexity of your algorithm in terms of m and n.
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
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...
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a...
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a set of integers. MUST USE JAVA AND QUICKSELECT. Algorithm 1: Procedure SELECT( k,S) { if ISI =1 then return the single element in S    else { choose an element randomly from S;               let S1, S2, and S3 be the sequences of elements in S                 less than, equal to, and greater than m, respectively;              if IS1I >=k then return SELECT(k,S1)              ...
Provide an algorithm that solves the following problem: input: a binary heap B, an integer value...
Provide an algorithm that solves the following problem: input: a binary heap B, an integer value x output: all keys in B that are smaller than x Constraint: your algorithm must run in time O(K), where K is the number of keys output. Explain why your algorithm has the required runtime behaviour. (Use pseudocode or C++, or any informal (but clear) description. You are free to choose any kind of representation of binary heaps, as long as it was mentioned...
Introduction to Algorithms - Analysis of Algorithms Solve the following recurrence relation: T(n) = T(an) +...
Introduction to Algorithms - Analysis of Algorithms Solve the following recurrence relation: T(n) = T(an) + T((1 - a)n) + n
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT