1

Please help me understand this formula (in case anybody is wondering, that is the Needleman-Wunsch-algorithm), I am supposed to write a code that uses recursion but I don't understand how to do so, I already have the full dynamic version written, so I know how the algorithm works but I have no idea on how to implement it with recursion/the stack. I'm open for any suggestion.

This is the formula:

Formula

Alberto Bonsanto
  • 17,556
  • 10
  • 64
  • 93

1 Answers1

1

It's unclear what you have problem with. That formula is already recursive, nothing special. Do you know how to create local variables? You will need 3:

local i;
local j;
local max = F(i-1, j-1);
max += S(Ai, Bj);
register tmp = F(i, j-1) + d;
if (tmp > max) max = tmp;
tmp = F(i-1, j) + d;
if (tmp > max) max = tmp;
return max;

PS: presumably you have some conditions for the initial elements so that the recursion is not endless.

Jester
  • 56,577
  • 4
  • 81
  • 125