如何在 Algoritmicx 中定义块并使用 Step、Input 和 Output,如下图所示?事实上,我想准确创建此图像,但我对“Step”有疑问。
我不知道如何定义这些:
这是我目前所做的事情:
\documentclass{article}
\usepackage{amsmath,algorithm,tabularx,amsfonts}
\usepackage{algpseudocode}
\makeatletter
\newcommand{\multiline}[1]{%
\begin{tabularx}{\dimexpr\linewidth-\ALG@thistlm}[t]{@{}X@{}}
#1
\end{tabularx}
}
\begin{document}
\begin{algorithm}
\caption{Dynamic Programming and Knapsack Problem}
\begin{algorithmic}[1]
\State $I = (w_1, \cdots w_n, c_1, \cdots c_n, b) \in (\mathbb{N}-\left\{0\right\})^{2n+1}$, $n$ a positive integer
\Comment{Input}
\State $TRIPLE(1) := \left\{(0, 0, \emptyset)\right\} \cup \{(c_1, w_1, \left\{1\right\}) | \; if \; w_1 \le b\}$
\Comment{Step 1}
\For {$i \leftarrow 1$ to $n-1$}
\Comment{Step 2}
\State {$SET(i+1):=TRIPLE(i)$};
\For {$(k, w, T) \in TRIPLE(i)$}
\If {$w+w_{i+1} \le b$}
\State {$SET(i+1):=SET(i+1) \cup \left\{k+c_{i+1}, w+w_{i+1}, T \cup \left\{i+1\right\})\right\}$};
\EndIf
\EndFor
\State \multiline{%
Set $TRIPLE(i+1)$ as a subset of $SET(i+1)$ containing exactly one triple $(m, w', T')$ for every achievable profit $m$ in $SET(i+1)$ by choosing a triple with the minimal weight for the given $m$}
\EndFor
\State Compute $c:=max \{k \in \{1, \cdots, \sum_{i=1}^n c_i\} | (k, w, T) \in TRIPLE(n)$ for some $w$ and $T\}$
\Comment{Step 3}
\State The index set $T$ such that $(c, w, T) \in TRIPLE(n)$
\Comment{Output}
\end{algorithmic}
\end{algorithm}
\end{document}
答案1
尝试在重新定义时定义新命令alglinenumber
。以下是所需输出的代码:
\documentclass{article}
\usepackage{amsmath,algorithm,float,tabularx,amsfonts}
\usepackage{algpseudocode}
\makeatletter
\newcommand{\multiline}[1]{%
\begin{tabularx}{\dimexpr\linewidth-\ALG@thistlm}[t]{@{}X@{}}
#1
\end{tabularx}
}
\newcommand{\Input}[1]{\algrenewcommand{\alglinenumber}[1]{Input: \ \setcounter{ALG@line}{\numexpr##1-1}} #1}
\newcommand{\Step}[1]{\algrenewcommand{\alglinenumber}[1]{Step ##1: } #1}
\newcommand{\NoNumber}{\algrenewcommand{\alglinenumber}[1]{\setcounter{ALG@line}{\numexpr##1-1} \ \ \ \ \ \ \ \ \ \ }}
\newcommand{\Output}[1]{\algrenewcommand{\alglinenumber}[1]{Output:\setcounter{ALG@line}{\numexpr##1-1}} #1}
\begin{document}
\begin{algorithm}
\renewcommand{\thealgorithm}{3.2.2.2}
\caption{(DPKP)}
\begin{algorithmic}[1]
\Input \State $I = (w_1, \cdots w_n, c_1, \cdots c_n, b) \in (\mathbb{N}-\left\{0\right\})^{2n+1}$, $n$ a positive integer
\Step \State $TRIPLE(1) := \left\{(0, 0, \emptyset)\right\} \cup \{(c_1, w_1, \left\{1\right\}) | \; if \; w_1 \le b\}$
\Step \For {$i \leftarrow 1$ to $n-1$}
\NoNumber \State {$SET(i+1):=TRIPLE(i)$};
\For {$(k, w, T) \in TRIPLE(i)$}
\If {$w+w_{i+1} \le b$}
\State {$SET(i+1):=SET(i+1) \cup \left\{k+c_{i+1}, w+w_{i+1}, T \cup \left\{i+1\right\})\right\}$};
\EndIf
\EndFor
\State \multiline{%
Set $TRIPLE(i+1)$ as a subset of $SET(i+1)$ containing exactly one triple $(m, w', T')$ for every achievable profit $m$ in $SET(i+1)$ by choosing a triple with the minimal weight for the given $m$}
\EndFor
\Step \State Compute $c:=max \{k \in \{1, \cdots, \sum_{i=1}^n c_i\} | (k, w, T) \in TRIPLE(n)$ for some $w$ and $T\}$
\Output \State The index set $T$ such that $(c, w, T) \in TRIPLE(n)$
\end{algorithmic}
\end{algorithm}
\end{document}
请注意,为了确保所有数字标签对齐,我们在字符较少的标签后添加了额外的空格,包括NoNumber
包含空标签的命令。