Question

In: Computer Science

1. Mathematically analyze the given Recurrence Relation and find out the time complexity of the algorithm....

1. Mathematically analyze the given Recurrence Relation and find out the time complexity of the algorithm.

T(n) = T(n-1)+1 , if n> 0 1 if n = 0

Solutions

Expert Solution

Mathematically:

  • Recurrence: T(n) = T(n-1) + 1, here we take initial condition t(1) = 2
  • Now going towards the problem:(Solving it)
    • T(1) = 2, Initial condition
    • T(2) = T(1) + 1 = 2+1 = 3
    • T(3) = T(2) + 1 = 3+1 = 4
    • T(4) = T(3) + 1 = 4+1 = 5
    • T(5) = T(4) + 1 = 5+1 = 6
  • Initially what we had guessed
    • T(n) = n + 1

  • Checking informally so that are answer is mathematically correct:
    • T(n) = T(n-1)+1 = [(n-1)+1] + 1 = n+1
    • T(1) = 1+1 = 2

  • Therefore,mathematically proven.

Time complexity we have check by backward substitution:

Time complexity:

  • Now we search for a pattern:
    • T(n) = T(n-1) + 1
    • T(n) = [T(n-2)+1] + 1
    • T(n) = [[T(n-3)+1]+1] + 1
    • T(n) = [[[T(n-4)+1]+1]+1] + 1
    • ...
    • T(n) = [...[[T(n-k)+1]+1] ... +1] + 1 [has k ones]
    • ... [Let k = n-1]
    • T(n) = [...[[T(n-(n-1))+1]+1] ... +1] + 1 [has k=n-1 ones]
    • T(n) = T(n-(n-1)) + (n - 1)
    • T(n) = T(1) + (n-1)
    • T(n) = 2 + (n - 1)
    • T(n) = n + 1
  • Check the guess, by substituting, as in mathematically solved example
    • T(n) = T(n-1)+1 = [(n-1)+1] + 1 = n+1
    • T(1) = 1+1 = 2

Therefore time complexity:  T(n) = Θ(n)

Hope this may help you............Have a nice day ahead..........


Related Solutions

) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int...
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int n, char from_rod,                     char to_rod, char aux_rod) {     if (n == 1)     {         cout << "Move disk 1 from " << from_rod << " to " << to_rod<<endl;         return;     }     towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);     cout << "Move disk " << n << " from " << from_rod << " to " << to_rod << endl;     towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); }
What is the auxiliary space and space complexity for a dynamic programming algorithm with time complexity...
What is the auxiliary space and space complexity for a dynamic programming algorithm with time complexity of O(n^2)? Justify.
Analyze the time complexity of the following variant of merge sort: given a constant k, divide...
Analyze the time complexity of the following variant of merge sort: given a constant k, divide the array into k parts, sort each part recursively, and merge the results.
a) Find the recurrence relation for the number of ways to arrange flags on an n...
a) Find the recurrence relation for the number of ways to arrange flags on an n foot flagpole with 1 foot high red flags, 2 feet high white flags and 1 foot high blue flags. b) solve the recurrence relation of part a
Find and solve a recurrence relation for the number of ways to stack n poker
Find and solve a recurrence relation for the number of ways to stack n poker chips using red, white and blue chips such that no two red chips are together. Use your solution to compute the number of ways to stack 15 poker chips.
find a recurrence relation for the number of bit strings of length n that contain the...
find a recurrence relation for the number of bit strings of length n that contain the string 10. What are the initial conditions? How many bit strings of length eight contain the string 10
What is the Average time complexity of sequential search algorithm in a linked list?
What is the Average time complexity of sequential search algorithm in a linked list?
What is the time complexity of my algorithm? It uses an vector E that contains and...
What is the time complexity of my algorithm? It uses an vector E that contains and object made of a string and an integer. It takes an empty vector as parameter and returns the vector with the top three integers from the objects in E . void Trendtracker::top_three_trends(vector<string> &T) {    T.clear();    if (E.size() == 0) {        return;    }    if (E.size() == 1) {        T.push_back(E[0].hashtag);    }    else if (E.size() == 2)...
In all algorithm, always explain how and why they work. ALWAYS, analyze the complexity of your...
In all algorithm, always explain how and why they work. ALWAYS, analyze the complexity of your algorithms. In all algorithms, always try to get the fastest possible. A correct algorithm with slow running time may not get full credit. In all data structures, try to minimize as much as possible the running time of any operation. 1.Show that if a binary tree has a vertex with a single child, then this can not be an optimal Huffman tree. 2. Question...
Find a recurrence relation for the number of binary strings of length n which do not...
Find a recurrence relation for the number of binary strings of length n which do not contain the substring 010
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT