How to derive the time complexity from a recurrence relation?
The recurrence relation is a model that helps in calculating time complexity of an algorithm. The process of recursively discovering the time complexity of an algorithm is known as a recurrence relation.
There are different methods to derive the time complexity of the recurrence relation. But we will use the Master theorem The master method is a formula for solving recurrence relations of the form:
Where
Let's look into a program example to derive time complexity
Program example
#include <iostream>using namespace std;// Binary Search Algorithum in C++int binarySearch(int [], int , int , int );int main() {int BinarySearchArray[] = {2, 4, 6, 11,13};int valueToFind = 13;int sizeOfArray = sizeof(BinarySearchArray) / sizeof(BinarySearchArray[0]);int valueFound = binarySearch(BinarySearchArray, valueToFind, 0, sizeOfArray - 1);if (valueFound == -1)cout<< "Not exist"<<endl;elsecout<< "Element is exist at index = "<< valueFound<<endl;}int binarySearch(int BinarySearchArray[], int x, int lowestValue, int highestValue) {while (lowestValue <= highestValue) {int middleValue = lowestValue + (highestValue - lowestValue) / 2;if (BinarySearchArray[middleValue] == x)return middleValue;if (BinarySearchArray[middleValue] < x)lowestValue = middleValue + 1;elsehighestValue = middleValue - 1;}return -1;}
Now we will derive time complexity using the
the equation to find the time complexity of the binary search program
Let's understand how it works. Suppose we have a sorted array BinarySearchArray =[2, 4, 6, 11,13]
To find the time complexity of a binary search we use the method:
In which we will assign values as:
It will implement like this:
After solving this equation we will find the complexity as:
The time complexity of the binary search is
Free Resources