[Tex/LaTex] Slight change in algorithm

algorithmicxalgorithms

This is my MWE for an algorithm however its throwing an error and I don't know how to get inputs and initialize. I want it to look like the attached image.

\documentclass[a4paper,10pt,twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs,lipsum}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}
\begin{algorithm}
        \caption{Algorithm 2: Boosted $K$ Nearest Neighbor}
        \begin{algorithmic}[1]
%         \qinput{Input:} \\
%    \State $S={s_i}={\left(x_i,y_i\right)$
         \For{t = 1 to T} \do
        \State $S_t \gets S_{t-1}$
        \For{$s_q \in S_t$} \do
          \State $N_q \gets$ k nearest neighbors 
          \State of $s_q$ using $D(s_q,s_i)$
          \State label($s_q$)$=argmax\sum_{s_i \in N_q}D(s_q,s_i)$;
        \If{label($s_q$)$\ne y_q$}
            \For{$s_i \in N_i$} \do
            \If{$y_i \ne y_q$}
              \State $w_{i}^{t} \gets w_{i}^{t} - \lambda/d(x_q,x_i)$;
            \Else
              \State $w_{i}^{t} \gets w_{i}^{t} + \lambda/d(x_q,x_i)$;
            \EndIf
            \EndFor
        \EndIf
        \EndFor
        \If{label($s_q$)$=y_q \forall_{s_q}$}
          \State break
        \EndIf
     \EndFor
        \end{algorithmic}
\end{algorithm}
\end{document}

Best Answer

Here's an option that provides feasible output. I've removed the physical numbering in the caption:

enter image description here

\documentclass[10pt,twocolumn]{article}
\usepackage{algorithm,algpseudocode}% http://ctan.org/pkg/{algorithms,algorithmx}
\algnewcommand{\Inputs}[1]{%
  \State \textbf{Inputs:}
  \Statex \hspace*{\algorithmicindent}\parbox[t]{.8\linewidth}{\raggedright #1}
}
\algnewcommand{\Initialize}[1]{%
  \State \textbf{Initialize:}
  \Statex \hspace*{\algorithmicindent}\parbox[t]{.8\linewidth}{\raggedright #1}
}

\begin{document}
\begin{algorithm}
  \caption{Boosted $K$ Nearest Neighbour}
  \begin{algorithmic}[1]
    \Inputs{$S={s_i}=\left(x_i,y_i\right)$}
    \Initialize{\strut$w_i^0 \gets 0$, $i=1,\ldots,n$ \\ $S_0 \gets S$}
    \For{t = 1 to T}
      \State $S_t \gets S_{t-1}$
      \For{$s_q \in S_t$}
        \State $N_q \gets$ k nearest neighbors 
        \State of $s_q$ using $D(s_q,s_i)$
        \State label($s_q$)$=argmax\sum_{s_i \in N_q}D(s_q,s_i)$;
        \If{label($s_q$)$\ne y_q$}
          \For{$s_i \in N_i$}
            \If{$y_i \ne y_q$}
              \State $w_{i}^{t} \gets w_{i}^{t} - \lambda/d(x_q,x_i)$;
            \Else
              \State $w_{i}^{t} \gets w_{i}^{t} + \lambda/d(x_q,x_i)$;
            \EndIf
          \EndFor
        \EndIf
      \EndFor
      \If{label($s_q$)$=y_q \forall_{s_q}$}
        \State break
      \EndIf
    \EndFor
  \end{algorithmic}
\end{algorithm}
\end{document}

A newly-defined \Inputs and (equivalent) \Initialize takes one argument that sets its contents in a \Statex with an indent of \algorithmicindent - the default - to align with the other algorithm elements. The contents is stored in a top-aligned \parbox that fits across 80% of the line. I'm sure you won't need more, but in case you do, that can also be fixed to not cause overfull boxes.

Related Question