\begin{algorithm}[!htb]
\caption{ Adaptive Dynamic Sampling for Frame-specific Patterns}
\label{alg:ads}
\KwInput{Initial data $\tilde{\vec{y}}_0 \in \mathbb{C}^{n \times n_c \times n_f}$, Acceleration $R$, Sample Space $\Omega$}
\KwOutput{Acquired $k$-space data $\tilde{\vec{y}}$ for $R$}
$n_a \gets |\Omega|$;\\
$ \vec{n_b}   = (n_b^1, \cdots, n_b^{n_f}) \gets (\frac{n_a}{R} - |\Lambda_0^1|, \cdots, \frac{n_a}{R} - |\Lambda_0^{n_f}|)$ \tcp*{   Calculate total budget}
\For{$m \gets 1$ to $N$}
{
    % $\vec{n_b}^{m} \gets \vec{n_b} / N$ \tcp*{ Calculate layer budget}
    $\vec{p}_{m} \gets \mathcal{M}_{\boldsymbol{\psi}_{m}} \circ \mathcal{E}_{\boldsymbol{\theta}_{m}}(\tilde{\vec{y}}_{m-1})$ \tcp*{Produce adaptive probabilities $\vec{p}_m \in \mathbb{R}^{n_f \times n_a}$}
    \For{$t \gets 1$ to $n_f$}
    {   
        \For{\textit{each} $i \in \left(\bigcup_{j=0}^{m-1}\Lambda_{j}^t\right)$}
        {
            $(\vec{p}_{m}^t)_i \gets 0$  \tcp*{Zero-out already sampled indices}
        }
        $\vec{p}_{m}^t \gets \text{Softplus} (\vec{p}_{m}^t)$ \tcp*{ Apply Softplus}
        $\vec{p}_{m}^t \gets \text{Rescale}(\vec{p}_{m}^t; \texttt{budget} = n_b^t/N)$ \tcp*{Rescale such that $\mathbb{E}(\vec{p}_{m}^t)=\frac{n_b^t}{N \times n_a}$}
        $\Lambda_{m}^t \gets \text{STE}(\vec{p}_{m}^t)$
        % such that $\Lambda_{m-1}^t\cap\Lambda_{m}^t = \emptyset$ and $|\Lambda_{m}^t| = |\Lambda_0^t| + \frac{m \times n_b^t}{N}$ 
        \tcp*{Produce adaptive sampling}
        }
        $\Lambda_m \gets (\Lambda_m^1, \cdots, \Lambda_m^{n_f}) $;\\
        $\vec{z}_m \gets \vec{U}_{\Lambda_m} {\vec{y}}$ \tcp*{Acquire new $k$-space on $\Lambda_m$}
        $\tilde{\vec{y}}_{m} \gets \tilde{\vec{y}}_{m-1} +  \vec{z}_m = \vec{U}_{\cup_{j=0}^{m}\Lambda_{j}}  {\vec{y}}$ \tcp*{Aggregate with previous data $ \tilde{\vec{y}}_{m-1}$}
    }
$\tilde{\vec{y}} \gets \tilde{\vec{y}}_N$;\\
% \For{$t \gets 1$ to $n_f$}
% {
%     $\Lambda^t \gets \bigcup_{m=0}^{N} \Lambda_m^t$
% }
% $\Lambda \gets \{\Lambda^1, \cdots, \Lambda^{n_f}\}$\\
\KwRet{$\tilde{\vec{y}}$}\\
\tcp*[h]{Note: Lines \textbf{6-7} ensure $\Lambda_{m-1}^t\cap\Lambda_{m}^t = \emptyset$ and line \textbf{9} $|\Lambda_{m}^t| = |\Lambda_0^t| + \frac{m \times n_b^t}{N}$}
\end{algorithm}

