*本文属于转载。

*转载链接:https://blog.csdn.net/lwb102063/article/details/53046265

目录

  clrscode

  algorithm

  algorithm2e

  algorithmic

clrscode

前期准备

 \usepackage{clrscode3e}

 \begin{codebox}
\Procname{$\proc{Insertion-Sort(A)}$}
\li \For $j \gets 2$ \To $\id{length}[A]$ \label{li:for}
\li \Do $\id{key} \gets A[j]$ \label{li:for-begin}
\li \Comment Insert $A[j]$ into the sorted sequence $A[1 \twodots j-1]$.
\li $i \gets j-1$
\li \While $i>0$ and $A[i]>\id{key}$ \label{li:while}
\li \Do $A[i+1] \gets A[i]$ \label{li:while-begin}
\li $i \gets i-1$ \label{li:while-end}
\End
\li $A[i+1] \gets \id{key}$ \label{li:for-end}
\End
\end{codebox}

algorithm

前期准备

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

 \begin{algorithm}[htb]
\caption{ Framework of ensemble learning for our system.}
\label{alg:Framwork}
\begin{algorithmic}[1]
\Require
The set of positive samples for current batch, $P_n$;
The set of unlabelled samples for current batch, $U_n$;
Ensemble of classifiers on former batches, $E_{n-1}$;
\Ensure
Ensemble of classifiers on the current batch, $E_n$;
\State Extracting the set of reliable negative and/or positive samples $T_n$ from $U_n$ with help of $P_n$;
\label{code:fram:extract}
\State Training ensemble of classifiers $E$ on $T_n \cup P_n$, with help of data in former batches;
\label{code:fram:trainbase}
\State $E_n=E_{n-1}cup E$;
\label{code:fram:add}
\State Classifying samples in $U_n-T_n$ by $E_n$;
\label{code:fram:classify}
\State Deleting some weak classifiers in $E_n$ so as to keep the capacity of $E_n$;
\label{code:fram:select} \\
\Return $E_n$;
\end{algorithmic}
\end{algorithm}

  1. \begin{algorithm}[h]
  2. \caption{An example for format For \& While Loop in Algorithm}
  3. \begin{algorithmic}[1]
  4. \For{each $i\in [1,9]$}
  5. \State initialize a tree $T_{i}$ with only a leaf (the root);
  6. \State $T=T\cup T_{i};$
  7. \EndFor
  8. \ForAll {$c$ such that $c\in RecentMBatch(E_{n-1})$}
  9. \label{code:TrainBase:getc}
  10. \State $T=T\cup PosSample(c)$;
  11. \label{code:TrainBase:pos}
  12. \EndFor;
  13. \For{$i=1$; $i<n$; $i++$ }
  14. \State $//$ Your source here;
  15. \EndFor
  16. \For{$i=1$ to $n$}
  17. \State $//$ Your source here;
  18. \EndFor
  19. \State $//$ Reusing recent base classifiers.
  20. \label{code:recentStart}
  21. \While {$(|E_n| \leq L_1 )and( D \neq \phi)$}
  22. \State Selecting the most recent classifier $c_i$ from $D$;
  23. \State $D=D-c_i$;
  24. \State $E_n=E_n+c_i$;
  25. \EndWhile
  26. \label{code:recentEnd}
  27. \end{algorithmic}
  28. \end{algorithm}

 \begin{algorithm}[h]
\caption{Conjugate Gradient Algorithm with Dynamic Step-Size Control}
\label{alg::conjugateGradient}
\begin{algorithmic}[1]
\Require
$f(x)$: objective funtion;
$x_0$: initial solution;
$s$: step size;
\Ensure
optimal $x^{*}$
\State initial $g_0=0$ and $d_0=0$;
\Repeat
\State compute gradient directions $g_k=\bigtriangledown f(x_k)$;
\State compute Polak-Ribiere parameter $\beta_k=\frac{g_k^{T}(g_k-g_{k-1})}{\parallel g_{k-1} \parallel^{2}}$;
\State compute the conjugate directions $d_k=-g_k+\beta_k d_{k-1}$;
\State compute the step size $\alpha_k=s/\parallel d_k \parallel_{2}$;
\Until{($f(x_k)>f(x_{k-1})$)}
\end{algorithmic}
\end{algorithm}

 \makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\begin{algorithm}
\caption{My algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{MyProcedure}{}
\State $\textit{stringlen} \gets \text{length of }\textit{string}$
\State $i \gets \textit{patlen}$
\BState \emph{top}:
\If {$i > \textit{stringlen}$} \Return false
\EndIf
\State $j \gets \textit{patlen}$
\BState \emph{loop}:
\If {$\textit{string}(i) = \textit{path}(j)$}
\State $j \gets j-1$.
\State $i \gets i-1$.
\State \textbf{goto} \emph{loop}.
\State \textbf{close};
\EndIf
\State $i \gets i+\max(\textit{delta}_1(\textit{string}(i)),\textit{delta}_2(j))$.
\State \textbf{goto} \emph{top}.
\EndProcedure
\end{algorithmic}
\end{algorithm}

algorithm2e

algorithm2e包可能会与其它包产生冲突,一个常见的错误提示是“Too many }'...”。为了解决这个问题,要在引入algorithm2e包之前加入下面的命令:

 \makeatletter
\newif\if@restonecol
\makeatother
\let\algorithm\relax
\let\endalgorithm\relax

前期准备:

 \makeatletter
\newif\if@restonecol
\makeatother
\let\algorithm\relax
\let\endalgorithm\relax
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}%[ruled,vlined]{
\usepackage{algpseudocode}
\usepackage{amsmath}
\renewcommand{\algorithmicrequire}{\textbf{Input:}} % Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm

 \begin{algorithm}
\caption{identify Row Context}
\KwIn{$r_i$, $Backgrd(T_i)$=${T_1,T_2,\ldots ,T_n}$ and similarity threshold $\theta_r$}
\KwOut{$con(r_i)$}
$con(r_i)= \Phi$\;
\For{$j=1;j \le n;j \ne i$}
{
float $maxSim=0$\;
$r^{maxSim}=null$\;
\While{not end of $T_j$}
{
compute Jaro($r_i,r_m$)($r_m\in T_j$)\;
\If{$(Jaro(r_i,r_m) \ge \theta_r)\wedge (Jaro(r_i,r_m)\ge r^{maxSim})$}
{
replace $r^{maxSim}$ with $r_m$\;
}
}
$con(r_i)=con(r_i)\cup {r^{maxSim}}$\;
}
return $con(r_i)$\;
\end{algorithm}

 \begin{algorithm}
\caption{Service checkpoint image storage node and routing path selection}
\LinesNumbered
\KwIn{host server $PM_s$ that $SerImg_k$ is fetched from, $subnet_s$ that $PM_s$ belongs to, $pod_s$ that $PM_s$ belongs to}
\KwOut{Service image storage server $storageserver$,and the image transfer path $path$}
$storageserver$ = Storage node selection($PM_s$, $SerImg_k$,$subnet_s$,$pod_s$)\;
\If{ $storageserver$ $\neq$ null}
{
select a path from $storageserver$ to $PM_s$ and assign the path to $path$\;
} \textbf{final} \;
\textbf{return} $storageserver$ and $path$;
\end{algorithm}

 \begin{algorithm}
\caption{Storage node selection}
\LinesNumbered
\KwIn{host server $PM_s$ that the checkpoint image $Img$ is fetched from, $subnet_s$ that $PM_s$ belongs to, $pod_s$ that $PM_s$ belongs to}
\KwOut{Image storage server $storageserver$} \For{ each host server $PM_i$ in the same subnet with $PM_s$ }
{
\If{ $PM_i$ is not a service providing node or checkpoint image storage node of $S_k$ }
{
add $PM_i$ to $candidateList$ \;
}
}
sort $candidateList$ by reliability desc\;
init $storageserver$ ;
\For{ each $PM_k$ in $candidateList$}
{ \If{ $SP(PM_k)$ $\geq$ $E(SP)$ of $pod_i$ and $BM_k$ $\le$ size of $Img$ }
{
assign $PM_k$ to $storageserver$\;
goto final\;
}
}
clear $candidateList$\;
add all other subnets in $pod_s$ to $netList$\;
\For{ each subnet $subnet_j$ in $netList$}
{
clear $candidateList$\;
\For {each $PM_i$ in $subnet_j$ }
{
\If{ $PM_i$ is not a service providing node or checkpoint image storage node of $S_k$ }
{
add $PM_i$ to $candidateList$\;
}
}
sort all host in $candidateList$ by reliability desc\;
\For{ each $PM_k$ in $candidateList$}
{ \If{$SP(PM_k)$ $\geq$ $E(SP)$ of $pod_i$ and $BM_k$ $\le$ size of $Img$}
{
assign $PM_k$ to $storageserver$ \;
goto final\;
}
}
}
\textbf{final} \;
\textbf{return} $storageserver$;
\end{algorithm}

 \begin{algorithm}
\caption{Delta checkpoint image storage node and routing path selection}
\LinesNumbered
\KwIn{host server $PM_s$ that generates the delta checkpoint image $DImg_{kt}$, $subnet_s$ that $PM_s$ belongs to, $pod_s$ that $PM_s$ belongs to}
\KwOut{Delta image storage server $storageserver$,and the image transfer path $Path$}
$storageserver$ = Storage node selection($PM_s$, $DImg_{kt}$,$subnet_s$,$pod_s$)\;
\If{ $storageserver$ $\equiv$ null}
{
the delta checkpoint image is stored in the central storage server\;
goto final\;
}
construct weighted topological graph $graph_s$ of $pod_s$\;
calculate the shortest path from $storageserver$ to $PM_s$ in $graph_s$ by using the Dijkstra algorithm\;
\textbf{final} \;
\textbf{return} $storageserver$ and $path$;
\end{algorithm}

 \documentclass[8pt,twocolumn]{ctexart}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{textcomp} %命令\textacutedbl的包,二阶导符号 % Page length commands go here in the preamble
\setlength{\oddsidemargin}{-0.25in} % Left margin of 1 in + 0 in = 1 in
\setlength{\textwidth}{9in} % 纸张宽度Right margin of 8.5 in - 1 in - 6.5 in = 1 in
\setlength{\topmargin}{-.75in} % Top margin of 2 in -0.75 in = 1 in
\setlength{\textheight}{9.2in} % Lower margin of 11 in - 9 in - 1 in = 1 in
\setlength{\parindent}{0in} \makeatletter
\newif\if@restonecol
\makeatother
\let\algorithm\relax
\let\endalgorithm\relax
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}%[ruled,vlined]{
\usepackage{algpseudocode}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}} \begin{document} \begin{algorithm}
\caption{component matrices computing}
\LinesNumbered
\KwIn{$\mathcal{X}\in\mathbb{R}^{l_1\times l_2\times\cdots\times l_N},\varepsilon,\lambda,\delta,R$}
\KwOut{$A^{(j)}s$ for $j=1$ to $N$}
\textbf{Initialize} all $A^{(j)}s$ //which can be seen as the $0^{th}$ round iterations\; {$l$\hspace*{-1pt}\textacutedbl}$=L$ //if we need to judge whether $(11)$ is true then {$l$\hspace*{-1pt}\textacutedbl} denotes $L|_{t-1}$\; \For{ each $A_{i_jr}^{{j}}(1\le j\le N,1\le i_j\le I_j,1\le r\le R)$ }
{//$1^{st}$ round iterations\;
$g_{i_jr}^{(j)'}=g_{i_jr}^{(j)}$\;
$A_{i_jr}^{(j)'}=A_{i_jr}^{(j)}$//if the rollback shown as $(12)$ is needed,$A_{i_jr}^{(j)'}$ denotes $A_{i_jr}^{(j)}|_{t-1}$\;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-\mathrm{{\bf sign}}\left(g_{i_jr}^{(j)}\right)\cdot\delta_{i_jr}^{(j)}$\;
} \Repeat(//other rounds of iterations for computing component matrices){$\bm{L\le \varepsilon}$ or maximum iterations exhausted}
{
$l'=L$ //if we need to judge whether $(11)$ is true then $l'$ denotes $L|_t$\;
\For{ each $A_{i_jr}^{{j}}(1\le j\le N,1\le i_j\le I_j,1\le r\le R)$}
{
\If{$g_{i_jr}^{(j)}\cdot g_{i_jr}^{(j)'}>0$}
{
$A_{i_jr}^{(j)'}=A_{i_jr}^{(j)} $\;
$g_{i_jr}^{(j)'}=g_{i_jr}^{(j)} $\;
$\delta_{i_jr}^{(j)}=\bm{\min}\left(\delta_{i_jr}^{(j)}\cdot\eta^{+},Max\_Step\_Size\right)$\;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-\mathrm{{\bf sign}}\left(g_{i_jr}^{(j)}\right)\cdot\delta_{i_jr}^{(j)}$\;
}
\ElseIf{$g_{i_jr}^{(j)}\cdot g_{i_jr}^{(j)'}<$}
{
\If{$l'>l$\hspace*{-1pt}\textacutedbl}
{
$g_{i_jr}^{(j)'}=g_{i_jr}^{(j)}$\;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)'}$// if $(11)$ is true then rollback as $(12)$\;
$\delta_{i_jr}^{(j)}=\bm{\max}\left(\delta_{i_jr}^{(j)}\times\eta^{-},Min\_Step\_Size\right)$\;
}
\Else
{
$A_{i_jr}^{(j)'}=A_{i_jr}^{(j)} $\;
$g_{i_jr}^{(j)'}=g_{i_jr}^{(j)} $\;
$\delta_{i_jr}^{(j)}=\bm{\max}\left(\delta_{i_jr}^{(j)}\cdot\eta^{-},Min\_Step\_Size\right)$\;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-\mathrm{{\bf sign}}\left(g_{i_jr}^{(j)}\right)\cdot\delta_{i_jr}^{(j)}$\;
}
}
\Else
{
$A_{i_jr}^{(j)'}=A_{i_jr}^{(j)} $\;
$g_{i_jr}^{(j)'}=g_{i_jr}^{(j)} $\;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-\mathrm{{\bf sign}}\left(g_{i_jr}^{(j)}\right)\cdot\delta_{i_jr}^{(j)}$\;
}
}
$l$\hspace*{-1pt}\textacutedbl$=l'$\;
}
\end{algorithm}
\end{document}

 \usepackage[ruled,linesnumbered]{algorithm2e}
\usepackage{amsmath} \begin{algorithm}
\caption{Learning algorithm of R2P}
\label{alg:r2p}
\KwIn{ratings $R$, joint demographic representations $Y$,learning rate $\eta$,maximum iterative number $maxIter$, negative sampling number $k$\;}
\KwOut{interaction matrix $\bm{W}$, movie vectors $V$\;}
Initialize $\bm{W},V$ randomly\;
$t = 0$\;
For convenience, define $\vec{\varphi}_n = \sum_{m\in S_n}r_{m,n}\vec{v}_m$\; %\varphi_n\bm{W}\vec{y}_n
\While{not converged \rm{or} $t>maxIter$}
{
t = t+1\;
\For{$n=1;n \le N;n++$}
{
$\bm{W} = \bm{W}+\eta\big(1-\sigma\left(\vec{\varphi}_n^T\bm{W}\vec{y}_n\right)\big)\vec{\varphi}_n\vec{y}_n^T$\;\label{algline:W}
\For{$m\in S_n$}
{
$\vec{v}_m=\vec{v}_m+ \eta\left(1-\sigma\left(\vec{\varphi}_n^T\bm{W}\vec{y}_n\right)\right)r_{m,n}\bm{W}\vec{y}_n$\;\label{algline:V}
}
\For{$i=1;i\le k;i++$}
{
sample negative sample $\vec{y}_i$ from $P_n$\;
$\bm{W} = \bm{W}-\eta\big(1-\sigma\left(-\vec{\varphi}_n^T\bm{W}\vec{y}_n\right)\big)\vec{\varphi}_n\vec{y}_i^T$\;
\For{$m\in S_n$}
{
$\vec{v}_m=\vec{v}_m- \eta\left(1-\sigma\left(-\vec{\varphi}_n^T\bm{W}\vec{y}_n\right)\right)r_{m,n}\bm{W}\vec{y}_i$\;
}
}
}
$\bm{W} = \bm{W}-2\lambda\eta\bm{W}$\;
$V=V-2\lambda\eta V$
}
return $\bm{W},V$\;
%\end{algorithmic}
\end{algorithm}

algorithmic

 1 \documentclass[11pt]{ctexart}  
2 \usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry} 
3 \usepackage{algorithm} 
4 \usepackage{algorithmicx} 
5 \usepackage{algpseudocode} 
6 \usepackage{amsmath} 
7  
8 \floatname{algorithm}{算法} 
9 \renewcommand{\algorithmicrequire}{\textbf{输入:}} 
10 \renewcommand{\algorithmicensure}{\textbf{输出:}} 
11  
12 \begin{document} 
13     \begin{algorithm} 
14         \caption{用归并排序求逆序数} 
15         \begin{algorithmic}[1] %每行显示行号 
16             \Require $Array$数组,$n$数组大小 
17             \Ensure 逆序数 
18             \Function {MergerSort}{$Array, left, right$} 
19                 \State $result \gets 0$ 
20                 \If {$left < right$} 
21                     \State $middle \gets (left + right) / 2$ 
22                     \State $result \gets result +$ \Call{MergerSort}{$Array, left, middle$} 
23 \State $result \gets result +$ \Call{MergerSort}{$Array, middle, right$}
24                     \State $result \gets result +$ \Call{Merger}{$Array,left,middle,right$} 
25                 \EndIf 
26                 \State \Return{$result$} 
27             \EndFunction 
28             \State 
29             \Function{Merger}{$Array, left, middle, right$} 
30                 \State $i\gets left$ 
31                 \State $j\gets middle$ 
32                 \State $k\gets 0$ 
33                 \State $result \gets 0$ 
34                 \While{$i<middle$ \textbf{and} $j<right$} 
35                     \If{$Array[i]<Array[j]$} 
36                         \State $B[k++]\gets Array[i++]$ 
37                     \Else 
38                         \State $B[k++] \gets Array[j++]$ 
39                         \State $result \gets result + (middle - i)$ 
40                     \EndIf 
41                 \EndWhile 
42                 \While{$i<middle$} 
43                     \State $B[k++] \gets Array[i++]$ 
44                 \EndWhile 
45                 \While{$j<right$} 
46                     \State $B[k++] \gets Array[j++]$ 
47                 \EndWhile 
48                 \For{$i = 0 \to k-1$} 
49                     \State $Array[left + i] \gets B[i]$ 
50                 \EndFor 
51                 \State \Return{$result$} 
52             \EndFunction 
53         \end{algorithmic} 
54     \end{algorithm} 
55 \end{document} 

Latex:插入伪代码的更多相关文章

  1. LATeX 插入脚注

    LATeX 插入脚注: 使用  \footnote{...注释内容} 命令: To maximize the lower-bound $ we employ conjugate gradient me ...

  2. 【Latex】如何在Latex中插入伪代码 —— clrscode3e

    1. 简介clrscode3e是<算法导论(第三版)>使用的伪代码的宏包,clrs其实表示的是Cormen.Leiserson.Rivest和Stein.它有个更老的版本clrscode, ...

  3. 在博客中使用LaTeX插入数学公式

    在博客中使用LaTeX插入数学公式 在学习机器学习中会接触到大量的数学公式,所以在写博客是会非常的麻烦.用公式编辑器一个一个写会非常的麻烦,这时候我们可以使用LaTeX来插入公式. 写这篇博文的目的在 ...

  4. Latex插入图片 分类: LaTex 2014-11-18 20:07 261人阅读 评论(0) 收藏

    在Latex中插入图片的方式很多,我这里只介绍自己常用的一种方式,欢迎大家指导. 我习惯于使用graphicx宏包来插入图片,有时候会配合上subfigure宏包来同时插入多幅图片组合. 首先,需要在 ...

  5. Latex: 插入数学公式

    write equations align equations to left To only align one equation, you can \begin{flalign} &\te ...

  6. 13LaTeX学习系列之---LaTeX插入表格

    目录 目录 前言 (一)插入表格的基础语法 1.说明 2.源代码 3.输出效果 (二)查看文档 目录 本系列是有关LaTeX的学习系列,共计19篇,本章节是第13篇. 前一篇:12LaTeX学习系列之 ...

  7. Latex算法伪代码使用总结

    Latex伪代码使用总结 algorithmicx例子 相应代码: \documentclass[11pt]{ctexart} \usepackage[top=2cm, bottom=2cm, lef ...

  8. [转载]latex插入两栏的图形或表格

    转载自:http://blog.csdn.net/huilingwu/article/details/51649250 latex进行两栏排版时,有时会遇到一些宽度较大的图形或表格,没办法挤在一栏中, ...

  9. LaTex 插入图像,以及应用表格

    插入图像 参考:http://www.ctex.org/documents/latex/graphics/ 1: \includegraphics[width=20mm]{head.png} 应用表格 ...

随机推荐

  1. spring cloud 测试的时候报 BeanCreationNotAllowedException: Error creating bean with name 'eurekaAutoServiceRegistration' 但能正确跑完测试方法

    因为都能正确的跑测试方法,所以我也不太注意它,但是有时候闲得蛋疼就会找一下原因. 具体原因我也说不清,直接丢个连接 https://github.com/spring-cloud/spring-clo ...

  2. 小G的城堡

    B 小 G 的城堡文件名 输入文件 输出文件 时间限制 空间限制castle.pas/c/cpp castle.in castle.out 1s 128MB题目描述小 G 家有一座城堡.城堡里面有 n ...

  3. 1979 第K个数

    1979 第K个数 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 黄金 Gold         题目描述 Description 给定一个长度为N(0<n<=10000) ...

  4. windows下vue-cli及webpack 构建网站(一)环境安装

    1.安装Node.js,node.js安装包及源码下载地址为:https://nodejs.org/en/download/. 2.安装npm,由于新版的nodejs已经集成了npm,所以之前npm也 ...

  5. 【复习笔记】CSS基础

    外观 color:rgba(255,255,255,1),a表示alpha,透明度值0~1 font-family:字体1,字体2,字体3;确保某字体不存在时自动选择下一个,最好使用字体的英文名称保证 ...

  6. 织梦dedecms手机版上下篇链接错误的解决方法

    打开 \include\arc.archives.class.php 1. 找到 $this->PreNext['pre'] = "上一篇:<a href='$mlink'> ...

  7. iOS 最新判断机型设备方法

    #define isIphoneXXS [UIScreen mainScreen].bounds.size.width == 375  && [UIScreen mainScreen] ...

  8. cocos的Director、Scence、Layer(一)---摘自于官方文档

    基本结构图(重要) Director: 有那些作用? OpenGL ES的初始化,场景的转换,游戏暂停继续的控制,世界坐标和GL坐标之间的切换,对节点(游戏元素)的控制,游戏数据的保存调用,屏幕尺寸的 ...

  9. zfs和ufs文件系统

    系统环境:freebsd 在zfs文件系统上的文件拷贝到ufs文件系统之后,大小翻了一倍.初步断定是文件系统导致的.不知道以后还会不会有新发现.come on!

  10. Image(支持 XML 序列化),注意C#中原生的Image类是无法进行Xml序列化的

    /// <summary> /// Image(支持 XML 序列化) /// </summary> [XmlRoot("XmlImage")] publi ...