Mazdak Farrokhzad edited dynamic.tex  almost 10 years ago

Commit id: 276522c4991fed4017db9eaf109568b4dcc8d60b

deletions | additions      

       

...  \end{verbatim}  \begin{figure}[H]  \centering  \begin{tikzpicture}[->, >=stealth',shorten >=1pt,auto,node distance=3.5cm,semithick]  \tikzset{level distance=50pt}  \Tree [.$mr(i,x,0)$  [.$mr(i,x-1,1)$  $\dots$  [.$mr(i-1,x-1,0)$  $\dots$  $mr(i-2,x-1,0)$ ] ]  [.$mr(i-1,x,0)$  [.$mr(i-1,x-1,1)$  $\dots$  $mr(i-2,x-1,0)$ ]  $\dots$ ]  ]  \end{tikzpicture}  \label{fig:execution-path}  \caption{Parts of the path of execution.}  \end{figure}  Looking at this tree, we can see that \verb|mr(i-2, x-1, 0)| is computed twice. The same will be true for many other values in the execution tree. Thus, it is likely that dynamic programming will be of use here.  Another thing to realise, is that \verb|mr(i, x, a)| where $a>0$ can only be called from \verb|mr(i, x+1, a-1)|. \verb|mr(i, x, 0)| on the other hand, can be called from \verb|mr(i+1, x, a)| with any value of $a$. Since each unique \verb|mr(i, x, a)| is only computed once, it therefore becomes wasteful to store anything other than values where $a=0$ in memory. To avoid having to treat $a=0$ and $a>0$ as separate cases, we change the algorithm to use loops instead of recursive calls to \verb|mr(i, x-1, a+1)|, and then add memory to it.