Solving Recurrences | Algorithms - Computer Science Engineering (CSE) PDF Download

Introduction

Many algorithms are recursive in nature. When we analyze them, we get a recurrence relation for time complexity. We get running time on an input of size n as a function of n and the running time on inputs of smaller sizes. For example in Merge Sort, to sort a given array, we divide it in two halves and recursively repeat the process for the two halves. Finally, we merge the results. Time complexity of Merge Sort can be written as T(n) = 2T(n / 2) + cn. There are many other algorithms like Binary Search, Tower of Hanoi, etc.

There are mainly three ways for solving recurrences:

  1. Substitution Method: We make a guess for the solution and then we use mathematical induction to prove the guess is correct or incorrect.
    For example consider 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
  2. Recurrence Tree Method: In this method, we draw a recurrence tree and calculate the time taken by every level of tree. Finally, we sum the work done at all levels. To draw the recurrence tree, we start from the given recurrence and keep drawing till we find a pattern among levels. The pattern is typically a arithmetic or geometric series.
    For example consider the recurrence relation
    T(n) = T(n / 4) + T(n / 2) + cn2
    Solving Recurrences | Algorithms - Computer Science Engineering (CSE)
    If we further break down the expression T(n / 4) and T(n / 2),
    we get following recursion tree.
    Solving Recurrences | Algorithms - Computer Science Engineering (CSE)
    Breaking down further gives us followingSolving Recurrences | Algorithms - Computer Science Engineering (CSE)
    To know the value of T(n), we need to calculate sum of tree
    nodes level by level. If we sum the above tree level by level,
    we get the following series
    T(n)  = c(n2 + 5(n2) / 16 + 25(n2) / 256) + ....
    The above series is geometrical progression with ratio 5 / 16.
    To get an upper bound, we can sum the infinite series.
    We get the sum as (n2) / (1 - 5 / 16) which is O(n2)
  3. Master Method: Master Method is a direct way to get the solution. The master method works only for following type of recurrences or for recurrences that can be transformed to following type.
    T(n) = aT(n/b) + f(n) where a >= 1 and b > 1
    (i) There are following three cases:
    (a) If f(n) = Θ(nc) where c < Logba then T(n) = Θ(nLogba)
    (b) If f(n) = Θ(nc) where c = Logba then T(n) = Θ(ncLog n)
    (c) If f(n) = Θ(nc) where c > Logba then T(n) = Θ(f(n))
    (ii) How does this work?
    Master method is mainly derived from recurrence tree method. If we draw recurrence tree of T(n) = aT(n / b) + f(n), we can see that the work done at root is f(n) and work done at all leaves is Θ(nc) where c is Logba. And the height of recurrence tree is Logbn
    Solving Recurrences | Algorithms - Computer Science Engineering (CSE)In recurrence tree method, we calculate total work done. If the work done at leaves is polynomially more, then leaves are the dominant part, and our result becomes the work done at leaves (Case 1). If work done at leaves and root is asymptotically same, then our result becomes height multiplied by work done at any level (Case 2). If work done at root is asymptotically more, then our result becomes work done at root (Case 3).
    (iii) Examples of some standard algorithms whose time complexity can be evaluated using Master Method
    (a) Merge Sort: T(n) = 2T(n/2) + Θ(n). It falls in case 2 as c is 1 and Logba] is also 1. So the solution is Θ(n Logn)
    (b) Binary Search: T(n) = T(n/2) + Θ(1). It also falls in case 2 as c is 0 and Logba is also 0. So the solution is Θ(Logn)

Notes:

  • It is not necessary that a recurrence of the form T(n) = aT(n / b) + f(n) can be solved using Master Theorem. The given three cases have some gaps between them. For example, the recurrence T(n) = 2T(n / 2) + n / Logn cannot be solved using master method.
  • Case 2 can be extended for f(n) = Θ(ncLogkn)
    If f(n) = Θ(ncLogkn) for some constant k >= 0 and c = Logba, then T(n) = Θ(ncLogk + 1n)
The document Solving Recurrences | Algorithms - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Algorithms.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
81 videos|80 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Solving Recurrences - Algorithms - Computer Science Engineering (CSE)

1. What is a recurrence relation in computer science?
Ans. A recurrence relation in computer science is a mathematical equation that defines a sequence of values based on one or more previous terms in the sequence. It is often used to analyze the time complexity of recursive algorithms and to solve problems that can be divided into subproblems of the same nature.
2. How can we solve recurrences in computer science engineering?
Ans. There are several methods to solve recurrences in computer science engineering. Some commonly used techniques include substitution method, recurrence tree method, master theorem, and generating functions. These methods help in finding closed-form solutions or asymptotic bounds for the recurrence relations.
3. What is the importance of solving recurrences in computer science engineering?
Ans. Solving recurrences in computer science engineering is important as it helps in analyzing the time complexity of algorithms and understanding their behavior. It allows us to make informed decisions about algorithm design, optimize code, and predict the performance of programs. Additionally, solving recurrences is crucial in designing efficient algorithms for various computational problems.
4. What is the role of the master theorem in solving recurrences?
Ans. The master theorem is a powerful tool used to solve recurrences that have a specific form. It provides a formula to determine the time complexity of a recursive algorithm directly, without the need for detailed analysis. The master theorem is particularly useful for divide-and-conquer algorithms, such as those based on binary search or merge sort.
5. Can generating functions be used to solve recurrences in computer science engineering?
Ans. Yes, generating functions can be used to solve recurrences in computer science engineering. Generating functions are mathematical functions that encode a sequence of numbers into a power series. By manipulating these power series, one can derive closed-form solutions for recurrence relations. Generating functions offer a systematic and elegant approach to solving recurrences and have applications in various areas of computer science and engineering.
81 videos|80 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Semester Notes

,

Exam

,

Extra Questions

,

Important questions

,

Sample Paper

,

practice quizzes

,

Previous Year Questions with Solutions

,

past year papers

,

mock tests for examination

,

video lectures

,

Objective type Questions

,

Solving Recurrences | Algorithms - Computer Science Engineering (CSE)

,

Solving Recurrences | Algorithms - Computer Science Engineering (CSE)

,

Summary

,

pdf

,

shortcuts and tricks

,

ppt

,

Free

,

study material

,

Viva Questions

,

Solving Recurrences | Algorithms - Computer Science Engineering (CSE)

,

MCQs

;