1

Which problem does the algorithm Delta below solve, where m, n >= 0 are integers?

enter image description here

So Im finding the algorithm very hard break down due to the nature of the nested recursion and how it calls on another recursive algorithm. If I had to guess I would say that Delta solves the LCS(longest common subsequence) problem, but Im not able to give a good explanation as to why.

Could someone help me break down the algorithm and explain the recursion and how it works?

4
  • I don't think it is LCS algorithm since there is no max(x,y) operation. It looks like interview or exam question where candidate should explain recursion. You could code it in your IDE and then debug (also drawing the stacks and the state after each method call) would help Commented Sep 15, 2021 at 11:39
  • @Stef I tried coding this in Python and it turns out that it computes the product of m and n. I just can't seem to wrap my head around how it actually works. Commented Sep 15, 2021 at 13:36
  • @Pame As a first step, you can simplify Gamma a lot. gamma(n,m) = gamma(n, m-1) + 1 = (gamma(n, m-2) + 1) + 1 = ... gamma(n, 0) + 1 + 1 + ... + 1 = gamma(n, 0) + m = n + m Commented Sep 15, 2021 at 14:05
  • @Pame and then replace Gamma(a,b) by a+b in the definition of Delta, and try to do the same kind of simplification. Commented Sep 15, 2021 at 14:06

1 Answer 1

1

As you found out yourself, delta computes the product of two integers.

The recursion indeed makes this confusing to look at but the best way to gain intuition is to perform the computation by hand on some example data. But by looking at the functions separately, you will find that:

Gamma is just summation. Gamma(n,m) = gamma(n, m - 1) + 1 essentially performs a naive summation where you count down the second term, while adding 1 to the first. Example:

3 + 3 =

(3 + 2) + 1 =

((3 + 1) + 1) + 1 =

(((3 + 0) + 1) + 1) + 1 =

6

Knowing this, we can simplify Delta:

Delta(n, m) = n + Delta(n, m - 1) (if m!=0, else return 0).

In the same way, we are counting down on the second factor, but instead of adding 1, we add n. This is in indeed one definition of multiplication. It is easy to understand this if you manually solve an example just like above.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.