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); }
Derive the recurrence for the average time complexity of Quick Sort.
Derive the recurrence for the average time complexity of Quick Sort.
What is recurrence for worst case of QuickSort and what is the time complexity in Worst...
What is recurrence for worst case of QuickSort and what is the time complexity in Worst case? algorithms coures
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.
ALGORITHM ANALYSIS AND DESIGN Analyze the given questions and answer accordingly 1.       Given the algorithm to...
ALGORITHM ANALYSIS AND DESIGN Analyze the given questions and answer accordingly 1.       Given the algorithm to calculate the factorial of ‘n’. Mathematically analyze the given recursive algorithm to find out the time complexity of the algorithm.    Algorithm F(n) If n = 0 return 1 Else Return f(n-1) * n
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
Discuss the Hamiltion circuit 1) sequential algorithm; 2) parallel algorithm; 3) discuss its time complexity.
Discuss the Hamiltion circuit 1) sequential algorithm; 2) parallel algorithm; 3) discuss its time complexity.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT