Recurrence relation in DAA

Recurrence relation in DAA

The model that uses mathematical concepts to calculate the time complexity of an algorithm is known as the recurrence relational model.

A recursive relation, T(n), is a recursive function of integer n. Every recursive function consists of both recursive and base cases.

The section of the definition that does not contain T is called a base case of the recurrence relation, and the portion that contains T is called recursive or recursive relation.

Recurrence relations are very useful in calculating the running time complexities of recursive algorithms.

Example:

A Fibonacci sequence is defined by the recurrence relation - Fn = F(n-1) + F(n-2)

with initial conditions f1=1 and f2 = 1.

Example: Write the recurrence relation for the following method.

 void f(int n) {
                    if (n > 0) {
                cout << n;
     f(n-1);
 }
                   } 

The base case is reached when n == 0. The method performs one comparison. Thus, the number of operations when n==0, T(0), is some constant a.

When n > 0, the method performs two basic operations and then calls itself, using ONE recursive call, with a parameter n - 1.

Therefore the recurrence relation is:

                   T(0) = a where a is constant

 T(n) = b + T(n-1) where b is constant, n > 0

Solving recurrence relation

The following are the ways to solve a recurrence relation :

  • Recursion Tree method
  • Substitution method
  • Master method

SUBSTITUTION METHOD

In the substitution method, we make a guess for the solution, and then we use mathematical induction to prove the guessed answer is right or wrong.

For example, Let us take the recurrence T(n) = 2T(n/2) + n

We guess the solution as T(n) = O(nLogn). Now we use induction

to prove our guess.

We need to prove that T(n) <= cnLogn. We can assume that it is true

for values smaller than n.

T(n) = 2T(n/2) + n

    <= 2cn/2Log(n/2) + n

    =  cnLogn - cnLog2 + n

    =  cnLogn - cn + n

    <= cnLogn

RECURRENCE TREE METHOD

In the recurrence tree method, a recurrence tree is drawn and calculates the time taken by every level of a tree. After analyzing each level, we sum up the work done at all levels. In order to draw a recurrence tree, start from the given recurrences and keep drawing till we find a pattern among all the levels. The pattern follows arithmetic or geometric series.

For example, consider the recurrence relation.

T(n) = T(n/4) + T(n/2) + cn2

           cn2

         /      \

     T(n/4)     T(n/2)

If we further break down the branches  T(n/4) and T(n/2),

We get the following recursion tree:

                cn2

           /           \     

       c(n2)/16      c(n2)/4

      /      \          /     \

  T(n/16)     T(n/8)  T(n/8)    T(n/4)

Breaking down the tree further gives us the following result

                        cn2

            /                         \     

       c(n2)/16                  c(n2)/4

       /            \                     /      \

c(n2)/256   c(n2)/64  c(n2)/64    c(n2)/16

 /    \      /    \    /    \       /    \ 

To know the value of T(n), we need to calculate the sum of tree nodes level by level. The following series - T(n)  = c(n^2 + 5(n^2)/16 + 25(n^2)/256) + ....  is obtained

on analyzing the end levels of the tree.

The above series is a geometric progression with a ratio of 5/16. We can sum the infinite series by calculating an upper bound. 

We get the sum as (n2)/(1 - 5/16), which is having a time complexity of O(n2).

MASTER METHOD

We can get an immediate solution using the master method. The master method only works on the recurrence relations that can be transformed into the following relation:

T(n) = aT(n/b) + f(n) Here,  a >= 1 and b > 1

There are following three cases:

1. If f(n) = ?(nc)

if c < Logba then T(n) = ?(Logban)

2. If f(n) = ?(nc)

if c = Logba then T(n) = ?(ncLog n)

3.If f(n) = ?(nc)

if c > Logba then T(n) = ?(f(n))

How does this work?

The master method is basically the derivation of the recurrence tree method. After drawing the recurrence tree of recurrence relation, T(n) = aT(n/b) + f(n),

 we can observe that the task done at root is f(n) and task done at all leaves is ?(nc) where c is Logba. And the height of the recurrence tree is Logbn.

Recurrence relation in DAA

For example,

Merge sort - T(n) = 2T(n/2) + ?(n). The relation falls in 2 as c is 1 and Logba ] is also 1. So the solution is ?(n Logn)

Binary search: T(n) = T(n/2) + ?(1). This relation falls in 2 as c is 0 and Logba  is also 0. So the solution is ?(Logn).


Related Topics

DAA: Insertion Sort Algorithm on Singly Link List

Insertion Sort Algorithm on Singly Link List We will sort a singly link list using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The insertion sort technique works...

3 minutes read.

DAA: Construct a Tree from Inorder and Preorder Traversals

Construct a Tree from Inorder and Preorder Traversals We are given inorder and preorder traversals of a tree. We need to generate a tree from these traversals. Example: Inorder[]   = { 3, 1,...

4 minutes read.

DAA: Rabin Karp Algorithm

Rabin Karp Algorithm The Rabin Karp or Karp Rabin algorithm is used to matching a specific pattern in the string. It uses the technique of hashing to match a specific text. There also...

6 minutes read.

Introduction to Sorting in DAA

DAA: What is Sorting? The technique in which a data structure is rearranged in decreasing order, increasing order, or in a specified order is called sorting. We apply to sort in our...

4 minutes read.

DAA: Interpolation Search Algorithm

Interpolation Search Algorithm There is no doubt that binary search is a great algorithm with average time complexity of log n. The feature of discarding one half of the array reduces...

4 minutes read.

DAA: Depth-First Search Algorithm

Depth-first search: DFS is a traversing algorithm of a graph or tree in which one node is taken as arbitrary, and with the help of that arbitrary node, all its...

6 minutes read.

DAA: Euclid Algorithm

Euclid Algorithm The Euclid algorithm finds the GCD of two numbers in the efficient time complexity. To find the GCD of two numbers, we take the two numbers’ common factors and multiply...

8 minutes read.

DAA: Algorithm to Find the Maximum Width of a Tree

Algorithm to Find the Maximum Width of a Tree The width of a binary tree is defined as the maximum number of nodes at a given level. The level having the...

5 minutes read.

DAA: Bottom view of a Binary Tree

Bottom view of a Binary Tree The bottom view of a binary tree is the number of nodes visible when viewed from the bottom. At every horizontal distance, there would be...

3 minutes read.

Symmetric Trees in DAA

Symmetric Trees The trees that are mirror images of themselves are known as symmetric trees. Look at the following tree image below: The tree is symmetric as the left subtree is the mirror...

4 minutes read.

DAA: Application of DFS and BFS

Application of DFS and BFS Depth-first search and breadth-first searches are the most famous algorithms used in daily life and the programming world. Let us now explore each application in which...

3 minutes read.

DAA: Expression Trees

Expression Trees Expression trees are those in which the leaf nodes have the values to be operated, and internal nodes contain the operator on which the leaf node will be performed. Example:...

4 minutes read.

DAA: Continuous Tree

Continuous Tree A continuous tree is the one in which the nodes from root to leaf path, the two adjacent node values, have a difference of 1. Input :          3                     /   \                   ...

5 minutes read.

Recurrence relation in DAA

Recurrence relation in DAA The model that uses mathematical concepts to calculate the time complexity of an algorithm is known as the recurrence relational model. A recursive relation, T(n), is a recursive...

5 minutes read.

Boyer Moore Algorithm

Boyer Moore Algorithm The Boyer Moore algorithm is a searching algorithm in which a string of length n and a pattern of length m is searched. It prints all the occurrences...

11 minutes read.

DAA: Dijkstra’s Algorithm (Shortest Path)

Dijkstra’s Algorithm (Shortest Path) Dijkstra’s algorithm finds the shortest distance from a source to all the vertices in a graph. This algorithm is used in network protocols like IS-IS and OSPF(Open...

3 minutes read.

DAA: Floyd Cycle Detection

Floyd Cycle Detection Floyd Cycle algorithm is one of the cycle detection algorithms to detect the cycle in a given singly linked list. In the Floyd Cycle algorithm, we have two pointers...

4 minutes read.

DAA: Binary Tree and its Categories

Binary Tree and its Categories The binary tree is a non-linear data structure in which there are 0 or utmost 2 nodes.  Each node has two children, i.e., left and right...

4 minutes read.

DAA: Density of a Binary Tree Algorithm

The Density of a Binary Tree Algorithm The density of a binary tree is defined as the ratio of the tree’s size to the tree’s height.  The height of the tree is...

2 minutes read.

DAA: Insert a node in Binary Search Tree

Insert a node in Binary Search Tree (BST) We have a Binary search tree and a key. Insert the key in the binary search tree if not present. In the above figure,...

4 minutes read.