[Tex/LaTex] Number Algorithm floats with dependency (subalgorithms environment)

algorithmsfloatsnumbering

What I have: 3 algorithms, like so:

\begin{algorithm}
\caption{Caption One}
...
\label{alg:one}
\end{algorithm}

\begin{algorithm}
\caption{Caption Two}
...
\label{alg:two}
\end{algorithm}

\begin{algorithm}
\caption{Caption Three}
...
\label{alg:three}
\end{algorithm}

What I want: I want algorithm 1 to be numbered in the same manner as the rest of the algorithms in the document, say, as Algorithm 1. Then I want Algorithms 2 and 3 to be numbered Algorithm 1a (or 1.1, or 1-a, or whatever) and Algorithm 1b.

Then I want numbering to continue as previous, so that the next algorithm is Algorithm 2. Can this be done, and if so, how?

Best Answer

You could borrow the subequations definition from amsmath

\documentclass{article}
\usepackage{algorithm}

\makeatletter
\newcounter{parentalgorithm}
\newenvironment{subalgorithms}{%
  \refstepcounter{algorithm}%
  \protected@edef\theparentalgorithm{\thealgorithm}%
  \setcounter{parentalgorithm}{\value{algorithm}}%
  \setcounter{algorithm}{0}%
  \def\thealgorithm{\theparentalgorithm\alph{algorithm}}%
  \ignorespaces
}{%
  \setcounter{algorithm}{\value{parentalgorithm}}%
  \ignorespacesafterend
}
\makeatother

\begin{document}

\begin{subalgorithms}

\begin{algorithm}
\caption{Caption One}
...
\label{alg:one}
\end{algorithm}

\begin{algorithm}
\caption{Caption Two}
...
\label{alg:two}
\end{algorithm}

\begin{algorithm}
\caption{Caption Three}
...
\label{alg:three}
\end{algorithm}

\end{subalgorithms}

\end{document}