sequence-algorithms/tmp.tex

67 lines
1.9 KiB
TeX
Raw Normal View History

2024-03-12 13:15:22 +01:00
\documentclass{scrartcl}
\RequirePackage{algorithm}
\RequirePackage{algorithmicx}
\RequirePackage[noEnd=false]{algpseudocodex}
\newcommand{\algorithmautorefname}{Algorithm} % Allow autoref.
% Define macros to typeset boolean in pseudocode environments
\algnewcommand{\True}{\textbf{\texttt{true}}}
\algnewcommand{\False}{\textbf{\texttt{false}}}
\algnewcommand{\NIL}{\textbf{\texttt{NIL}}}
\algnewcommand{\NULL}{\textbf{\texttt{null}}}
\input{definitions.tex}
\begin{document}
\begin{algorithm}
\caption{Backtrack the longest common subsequence}
\begin{algorithmic}[1]
\Function{LCSQ}{$S_{1}$: Array($n$), $S_{2}$: Array($m$)}
\State $M, P \gets $ \Call{LCSQ\_Matrix}{$S_{1}$, $S_{2}$}
\State $L \gets Array(M[n][m])$
\State $k \gets 0$
\State $i \gets n$
\State $j \gets m$
\While{$i > 0$ and $j > 0$}
\If {$P[i][j] = '\nwarrow' $}
\State $L[k] \gets S_{1}[i]$
\State $i--$
\State $j--$
\State $k++$
\ElsIf {$P[i][j] = '\leftarrow'$}
\State $j--$
\Else
\State $i--$
\EndIf
\EndWhile
\State \Return $L$
\EndFunction
\end{algorithmic}
\end{algorithm}
2024-03-12 13:15:22 +01:00
\begin{algorithm}
\caption{Recursive reconstruction of the longest common subsequence}
\begin{algorithmic}[1]
\Procedure{LCSQ}{$S_{1}$: Array($n$), $S_{2}$: Array($m$)}
\State $M, P \gets $ \Call{LCSQ\_Matrix}{$S_{1}$, $S_{2}$}
\State $i \gets n$
\State $j \gets m$
\State \Call{Aux}{$P$, $S_{1}$, $i$, $j$}
\EndProcedure
\Procedure{Aux}{$P$: Array($n+1$, $m+1$), $S_{1}$: Array($n$), $i$, $j$}
\If {$P[i][j] = '\nwarrow' $}
\State $l \gets S_{1}[i]$
\State \Call{Aux}{$P$, $S_{1}$, $i-1$, $j-1$}
\State \texttt{print}($l$)
\ElsIf {$P[i][j] = '\leftarrow'$}
\State \Call{Aux}{$P$, $S_{1}$, $i$, $j-1$}
\Else
\State \Call{Aux}{$P$, $S_{1}$, $i-1$, $j$}
\EndIf
\EndProcedure
\end{algorithmic}
\end{algorithm}
2024-03-12 14:11:33 +01:00
2024-03-12 13:15:22 +01:00
\end{document}
2024-03-15 11:40:26 +01:00
\end{document}