0

I want to write this pseudo code in Latex. enter image description here

This is the code I have.

\documentclass[runningheads]{elsarticle}
%

\setlength{\belowdisplayskip}{3pt}

\usepackage{algorithm}
\usepackage{algpseudocode}

\renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm  
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm  

\newcommand{\tabincell}[2]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}  
%\usepackage[table]{xcolor}

\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
{\centering
\begin{minipage}{.8\linewidth}
\begin{algorithm}[H]
\caption{GraphSAGE embedding generation (i.e., forward propagation) algorithm}

\label{alg:algorithm1}
  \begin{algorithmic}[1]
  \Require Graph $\mathcal{G}$ ($\mathcal{V}$, $\mathcal{E}$); input features $\{x_v, \forall{v} \in \mathcal{V}\}$; depth $\mathcal{K}$; weight matrices $W^k, \forall{$\textit{\mathcal{k}}$} \in \{1, ...,\mathcal{K}\}$; non-linearity $\sigma$; differentiable aggregator functions $AGGREGATE_k$, $x_v, \forall{k} \in \{1, ...,\mathcal{K}\}$; neighborhood function $\mathcal{N}: v \leftarrow 2^\mathcal{V}$
  \Ensure
      Vector representations $z_v$ for all ${v} \in \mathcal{V}$;
      \State $h^0_v \leftarrow x_v, \forall v \in \mathcal{V}$; 
      \For{$k = 1...\mathcal{K}$}
        \For{$v \in \mathcal{V}$}
        \State $h^k_\mathcal{N}_(v)$
        \leftarrow AGGREGATE_k $(\{h^{k}_u, \forall{u} \in \mathcal{N}_(v)\})$;
        \State $h^{k}_v \leftarrow \sigma (W^k \cdot CONCAT(h^{k-1}_v, $h^k_\mathcal{N}__(v)$}; 
        \EndFor
       \State $h^k_v \leftarrow h^k_v / \mid\mid h^k_v \mid\mid_2,\forall{v} \in \mathcal{V}$;
      \EndFor
      \State $z_v \leftarrow h_v^K, \forall{v} \in \mathcal{V}$;
  \end{algorithmic}
\end{algorithm}
\end{minipage}
\par
}
\end{document}

However, with this code, I get something different in line 4 and 5, as shown in red line, can any experts help to correct this? enter image description here

1
  • 1
    Put the whole subscript in braces: _{\mathcal{N}(v)} and don't use _ if you don't want to have a subscript. Commented Aug 27, 2022 at 11:37

1 Answer 1

1

Here is a possible solution:

\documentclass[runningheads]{elsarticle}
\setlength{\belowdisplayskip}{3pt}

\usepackage{bm}
\usepackage{mathtools}
\usepackage{algorithm}
\usepackage{algpseudocode}

\renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm  
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm  

\begin{document}
    
\begin{center}
    \begin{minipage}{.8\textwidth}
        \begin{algorithm}[H]
            \caption{GraphSAGE embedding generation (i.e., forward propagation) algorithm}
            \label{alg:algorithm1}
            \begin{algorithmic}[1]
                \Require Graph $\mathcal{G}$ ($\mathcal{V}$, $\mathcal{E}$); input features $\{x_v, \forall{v} \in \mathcal{V}\}$; depth $K$; weight matrices $W^k, \forall{\mathcal{\textit{k}}} \in \{1, ...,K\}$; non-linearity $\sigma$; differentiable aggregator functions AGGREGATE$_k$, $x_v, \forall{k} \in \{1, ...,K\}$; neighborhood function $\mathcal{N}: v \leftarrow 2^\mathcal{V}$
                \Ensure Vector representations $z_v$ for all ${v} \in \mathcal{V}$
                \State $h^0_v \leftarrow x_v, \forall v \in \mathcal{V}$; 
                \For{$k = 1...\mathcal{K}$}
                \For{$v \in \mathcal{V}$}
                \State $h^k_{\mathcal{N}(v)} \leftarrow\text{AGGREGATE}_k \left(\{h^{k-1}_u, \forall{u} \in \mathcal{N}(v)\}\right)$
                \State $h^k_v \leftarrow \sigma \left(W^k \cdot \text{CONCAT}(h^{k-1}_v, h^k_{\mathcal{N}(v)})\right)$
                \EndFor
                \State $h^k_v \leftarrow h^k_v / \mid\mid h^k_v \mid\mid_2,\forall{v} \in \mathcal{V}$
                \EndFor
                \State $z_v \leftarrow h_v^K, \forall{v} \in \mathcal{V}$;
            \end{algorithmic}
        \end{algorithm}
    \end{minipage}
    \par
\end{center}

\end{document}

Here are the observations:

  1. You were using \mathcal where it was not required (according to the given example). For instance in \mathcal{K};
  2. The highlighted terms were wrong. You should only use _ if you have a subscript. That is, intead of \mathcal{N}_(v), the correct is \mathcal{N}(v);
  3. If you have a subscript inside another subscript, you must use _{_{}}. For instance, h^k_{\mathcal{N}_{(v)}};
  4. The words "CONCAT" and "AGGREGATE" are not mathematical terms. So you must put them in the text format. You can do that by either closing the math ambient ($ for example) or by using \text{}. This last option, though, requires the mathtools package. I did an example with the booth, so you can choose any of them in accordance;
  5. In your given example, there some variables which look to be bolded for me. I suggest you take a look at this, and if that is the case you can use bm package to bold the variable properly. For instance, \bm{z}.
5
  • Thanks a lot, for line 5. I checked your code and that works well! But the bracket of $h^k_v$ is not the same as the example, could you please help me?
    – James
    Commented Aug 28, 2022 at 8:43
  • I have no access to my PC today. What is the diference @James?
    – AXL
    Commented Aug 28, 2022 at 14:19
  • The inside bracket is smaller in the original figure
    – James
    Commented Aug 29, 2022 at 9:48
  • Ok. You just need to eliminate the \left and \right before the opening and closing parenthesis. I modified the above solution, despite the previous way being more appropriate.
    – AXL
    Commented Aug 30, 2022 at 23:44
  • You should delete the inner $ from \Require Graph $\mathcal{G}$ ($\mathcal{V}$, $\mathcal{E}$);, and move that last one after the closing parenthesis.
    – Teepeemm
    Commented Aug 31, 2022 at 1:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .