3

I need to write an algorithm where multiple cases are handled. I have succeeded but the output shows a lot of vertical space below and above the cases statement. Anyone help me to fix this. I am using algorithm2e package, a minimal working example with output is attached.

\documentclass[hidelinks,12pt]{report}
\usepackage[ruled,linesnumbered]{algorithm2e}
\usepackage{amsmath}
\begin{document}

\begin{algorithm}
    \DontPrintSemicolon                 
    \caption{BE radix-8 IMML Modular Multiplication}          
    \label{alg1}   
    \KwIn{ $x=\sum_{i=o}^{n-1} x_i.2^i, y=\sum_{i=o}^{n-1} y_i.2^i, p=\sum_{i=o}^{n-1}p_i.2^i$ }
    \KwOut {$z = x \times y$ \text{mod} $p$}
    $z := 0$  $R_1 := 2x$ mod $p$ 
    $R_2 := 3x$ mod $p$, $R_3 := 4x$ mod $p$\; 
    \[
    N= 
    \begin{cases}
    n+3,& \text{if } n \text{ mod } 3 = 0 \quad \tcc{\scriptsize append three 0 to the left of MSB of $y$}\\
    n+2,& \text{if } n \text{ mod } 3 = 1 \quad \tcc{ \scriptsize append two 0 to the left of MSB of $y$}\\
    n+1,& \text{if } n \text{ mod } 3 = 0 \quad \tcc{ \scriptsize append single 0 to the left of MSB of $y$}
    \end{cases}
    \] 
\For {($i=N+1; i\geq 3;  i=i-3 $) }{
    $z := 8z $\text{ mod } $p$\;
\Switch{$(y_{(i:i-3)})$}{
\textbf{when} $  0000 | 1111 \quad z:=z$ \;
\textbf{when}$\lbrace0001\rbrace|\lbrace0010\rbrace\lbrace1101\rbrace|\lbrace1110\rbrace \Longrightarrow z:=z \pm x$ mod $p$ \;
\textbf{when}$\lbrace0011\rbrace|\lbrace0100\rbrace\lbrace1011\rbrace|\lbrace1100\rbrace  \Longrightarrow z:= z \pm R_1$ mod $p$ \;
\textbf{when}$\lbrace0101\rbrace|\lbrace0110\rbrace\lbrace1001\rbrace|\lbrace1010\rbrace$ $\Longrightarrow z:= z \pm R_2$ mod $p$\;
\textbf{else} $\Longrightarrow z:= z \pm R_3$ mod $p$
        }
    }
    \Return{$z$}\;
\end{algorithm}
\end{document}

enter image description here

  • 1
    Can you please complete your example to be compilable? – Ruben Apr 21 '16 at 15:30
  • This is the code for the algorithm shown in the figure, i need to reduce vertical spacing of case (N). – Khalid Apr 21 '16 at 15:44
  • 1
    Sure, but the LaTeX code example you posted can't be compiled as the \documentclass instrcution is missing as well as the needed packages and the document environment. Please add those! That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Ruben Apr 21 '16 at 16:34
  • it's not the \cases that is causing the extra space, but the use of \[ ... \] which specifies a display. displays always have extra space above and below. – barbara beeton Apr 21 '16 at 18:10
5

Is perhaps this that you want?

\documentclass[12pt]{report}
\usepackage[ruled,linesnumbered]{algorithm2e}
\usepackage{amsmath}
\begin{document}

\begin{algorithm}
\DontPrintSemicolon
\caption{BE radix-8 IMML Modular Multiplication}\label{alg1}   
\KwIn{
  $x=\sum_{i=0}^{n-1} x_i\cdot2^i$,
  $y=\sum_{i=0}^{n-1} y_i\cdot2^i$,
  $p=\sum_{i=0}^{n-1}p_i\cdot2^i$
 }
\KwOut {$z = x \times y \bmod p$}
  $z := 0$,  $R_1 := 2x \bmod p$,
  $R_2 := 3x \bmod p$, $R_3 := 4x \bmod p$,
  $N= 
  \begin{cases}
  n+3,& \text{\small if $n \bmod 3 = 0$ append three $0$ to the left of MSB of $y$} \\
  n+2,& \text{\small if $n \bmod 3 = 1$ append two $0$ to the left of MSB of $y$}\\
  n+1,& \text{\small if $n \bmod 3 = 0$ append single $0$ to the left of MSB of $y$}
  \end{cases}
  $\;
\For{$(i=N+1$; $i\geq 3$;  $i=i-3)$}{
  $z := 8z \bmod p$\;
  \Switch{$(y_{(i:i-3)})$}{
    \textbf{when} $0000 \mid 1111$ $z:=z$\;
    \textbf{when} $\{0001\}\mid\{0010\}\{1101\}\mid\{1110\}\Longrightarrow z:=z \pm x \bmod p$\;
    \textbf{when} $\{0011\}\mid\{0100\}\{1011\}\mid\{1100\}\Longrightarrow z:=z \pm R_1 \bmod p$\;
    \textbf{when} $\{0101\}\mid\{0110\}\{1001\}\mid\{1010\}\Longrightarrow z:=z \pm R_2 \bmod p$\;
    \textbf{else} $\Longrightarrow z:= z \pm R_3 \bmod p$
        }
    }
    \Return{$z$}
\end{algorithm}
\end{document}

enter image description here

| improve this answer | |

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.