0
$\begingroup$

I am currently trying to solve a task with memoization. I have following recursion:

A (i, j) = f( A (i, j-1), A (i-1, j-1), A (i-1, j + 1) )

I am not sure in which order the sub-problems should be solved best, so that multiple calculations are avoided.

Does someone have an idea which order is best?

$\endgroup$
1
$\begingroup$

Let me show you the general approach to figuring this out. Then I'll let you apply that technique to your specific situation.

Well, it's clear that you need to solve $A(i,j-1)$, $A(i-1,j-1)$, and $A(i-1,j+1)$ before solving $A(i,j)$. So, you need to pick an order that ensures all three of those are solved before you try to solve $A(i,j)$. What order would work for that?

I suggest you try drawing a picture. Draw a grid, with one dot for each $(i,j)$. Draw an arrow from $(i,j-1)$ to $(i,j)$, an arrow from $(i-1,j-1)$ to $(i,j)$ and an arrow from $(i-1,j+1)$ to $(i,j)$ for each $(i,j)$. You might do this for, say, a 4x4 grid. Look at the resulting picture. Can you find an order of traversing the dots, so that is consistent with all of the arrows? Hint: try topological sorting that graph. What order does that give you? Can you generalize? What would work for a 5x5 grid?

If you're not sure, just write a recursive program and apply memoization. Then you won't need to explicitly pick an order to build things out bottom-up; the computer will take care of that for you. See When can I use dynamic programming to reduce the time complexity of my recursive algorithm? and https://en.wikipedia.org/wiki/Memoization.

$\endgroup$
  • $\begingroup$ In memoization, we are storing results of past sub-problems as we encounter them for the first time. The algorithm will first look up if the sub-problem has an answer already as it recurses. Should order matter in that case? $\endgroup$ – hazrmard Mar 8 at 19:05
  • 1
    $\begingroup$ @hazrmard, If you are using memoization, as I mentioned in the last paragraph of my answer, you don't need to explicitly pick an order. Write the recursive algorithm, and that code will implicitly determine an order for you. Try writing the recursive algorithm and then running it by hand and perhaps you will get a better idea what I mean. $\endgroup$ – D.W. Mar 8 at 19:07
  • $\begingroup$ I draw a grid and think the order should be : A(i-1,j-1) , A(i,j-1), A(i-1, j+1) . I know that you don't need to specify an order when programming an algorithm, but this was a theoretical task and I have just to answer the question and explain it. I am not allowed to explain it by programming it. $\endgroup$ – user650708 Mar 9 at 18:46
  • $\begingroup$ @user650708, that's not an order. An order on a $n \times n$ grid tells you what order to fill in the $n^2$ cells. $\endgroup$ – D.W. Mar 9 at 19:26
  • $\begingroup$ @D.W. so you mean the solution is column-wise? Starting by A[1,1] and continuing with A[1,2] and so on? But I do not understand why the formulation of the task says "order of the sub-problems". $\endgroup$ – user650708 Mar 9 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.