What are Delannoy Numbers? Write a program in C++ to find the Delannoy number

C++Server Side ProgrammingProgramming

Delannoy Numbers − A Delannoy number D describes the number of paths from the southwest corner(0,0) to northeast corner(a,b) in a rectangular grid using only allowed steps east ( →), northeast ( ↗ ) and north ( ↑ ).

Thus, we can say that a recurrence relation is,

D(a,b) = D(a-1,b) + D(a, b-1) + D(a-1, b-1) where D(0,0)=1.

For example, the Delannoy number D(3,3) equals 63.

Algorithm to find the Delannoy Number

  • Take two coordinates (a,b) as Input.

  • An Integer function generateDelannoy(int a, int b) which takes coordinates ‘a’ and ‘b’ as input.

  • In the base case, we will check If coordinates ‘a’ and ‘b’ are zero then return 1.

  • In the other case generate the Delannoy number and return the result by using recurrence relation for generating the Delannoy number D(a-1,b) + D(a,b-1) + D(a-1,b1).

Example

#include<iostream>
using namespace std;
int generateDelannoy(int a, int b){
   int d=1;
   if((a==0) || (b==0)){
      d=1;
   } else {
      d = generateDelannoy(a-1,b) + generateDelannoy(a,b-1) + generateDelannoy(a1,b-1);
   }
   return d;
}
int main(){
   int a=3;
   int b=3;
   int result=0;
   result= generateDelannoy(a,b);
   cout<<result<<endl;
}

Output

Running the above code will generate the output as,

63

For the given points (a,b) = (3,3), using the recurrence relation D(a-1,b) + D(a,b-1) + D(a-1,b-1) will generate the Delannoy number ‘63’ as output.

raja
Published on 05-Feb-2021 10:54:39
Advertisements