Brian Sunter

Abdul Bari Algorithms - Recurrence Relation and Master's Theorem for Subtracting Functions

Discusses the recurrence relation, which is a mathematical notation to describe a sequence of values based on the previous term, which is useful for describing recursion and time complexity.

Divide and Conquer

If a problem is large, divide the problem into subproblems, solve them, then recombine the solutions

The subproblems should be the same type of problems, for example, if the main problem is sorting, then the subproblems are sorting

Examples of Divide and Conquer Problems

Binary search

Finding Maximum and Minimum

Merge Sort

Quick Sort

Strassen’s Matrix Multiplication

Subtraction Recurrence Relation 1

Recursive function example

function test(n) {
  if (n > 0) {
    console.log(n);
    test(n - 1);
  }
}

How many times is this function called?

test(3) -> prints “3”

test(2) -> prints “2”

test(1) -> prints “1”

test(0) -> does not print, or call itself again, since n is not greater than 0

Does work (printing) 3 times, but calls itself 4 times, where it doesn’t print on the last time

If printing is one unit of time, then this takes 3 units of time

So if you pass n it will make n+1 calls, and print n times

The time depends on the number of calls, so time complexity is

How do we find the recurrence relation?

based on

The original equation

We know

We can find if we know

How do we find ?

based on

Let’s substitute into the equation for

Original equation

Substitute in place of in the original equation for

This simplifies to

Now we can substitute into the original equation for

This simplifies to

So now is in terms of instead of being based on

based on

We can keep doing this

What about in terms of ?

Substitute into original equation

This simplifies to

From before, we know how to write in terms of , so we plug it in to find in terms of

Previous equation: $T(n)=T(n-2)+2

Plug in based on

Simplifies to

What if we wanted to continue for times?

What is the smallest value we know,

Assume

Therefore

So when we can write the equation in terms of just

Simplifies to

We know

So

Subtraction Recurrence Relation 2

function test(n) {
  if (n > 0) {
    for (let i = 0; i < n; i++) {
      console.log(n);
    }
    test(n - 1);
  }
}

Recurrence relation is

For each iteration, it takes units of time, then calls itself -1

Tree Method

2022-12-01-13-33-20
2022-12-01-13-33-20

We can see the amount of work is

To find the total amount of work we can use the integer-sum-formula

This simplifies to for measuring time complexity

Substitution Method

Original equation is

Plug in into original equation

Now we substitute this into the original equation to find in terms of instead of in terms of

Plug Plug in into original equation

Simplify to

If we continue this for times

Assume therefore

Substitute for

Simplify further

We can see is the sum of natural numbers, which we can use the integer-sum-formula to solve

And we know

So the answer is

Which simplifies to in Big O notation

Subtraction Recurrence Relation 3

function test(n) {
  if (n > 0) {
    for (let i = 1; i < n; i = i * 2) {
      console.log(n);
    }
    test(n - 1);
  }
}

We know (let i=1; i< n; i=i*2) will execute times

Tree Method

2022-12-05-15-12-40
2022-12-05-15-12-40

Amount of work is

-> log n factorial

Equivalent to

Substitution method

Plug in n-2

Assume , therefore

Simplifies to

Directly Get Answer

->

->

->

->

Just multiply the term after the + by n, since you know it will be repeated n times via recursion

What if it’s not decreasing by 1? It still works

-> ->

->

However, if there’s a coeffecient on the function ,it’s different though.

Subtraction Recurrence Relation 4

function test(n) {
  if (n > 0) {
    console.log(n);
    test(n - 1);
    test(n - 1);
  }
}

Tree Method

2022-12-06-13-53-46
2022-12-06-13-53-46

function called twice in first row

4 times in second row

8 times in third row

So the work done in each row is

In the series above, and

So we can use the formula above to find the answer for our tree

Simplifies to

Assume , so

So Big O is

Subsitution Method

Assume

Master’s Theorem for Subtracting Functions

->

->

->

->

->

->

Master’s Theorem

General form of recurrence relation

Assume

where

Case 1

For example

Then

also can be thought of as

Case 2

For example

Then

Case 3

If you’re decreasing by more than 1, for example

Then

What if for example .5

Then or

Share this post