Latex写算法的伪代码排版

http://blog.csdn.net/lwb102063/article/details/53046265


algorithmicx例子


相应代码:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  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 result0  
  20.                 \If {left<right}  
  21.                     \State middle(left+right)/2  
  22.                     \State resultresult+ \Call{MergerSort}{Array,left,middle}  
  23.                     \State resultresult+ \Call{MergerSort}{Array,middle,right}  
  24.                     \State resultresult+ \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 ileft  
  31.                 \State jmiddle  
  32.                 \State k0  
  33.                 \State result0  
  34.                 \While{i<middle \textbf{and} j<right}  
  35.                     \If{Array[i]<Array[j]}  
  36.                         \State B[k++]Array[i++]  
  37.                     \Else  
  38.                         \State B[k++]Array[j++]  
  39.                         \State resultresult+(middlei)  
  40.                     \EndIf  
  41.                 \EndWhile  
  42.                 \While{i<middle}  
  43.                     \State B[k++]Array[i++]  
  44.                 \EndWhile  
  45.                 \While{j<right}  
  46.                     \State B[k++]Array[j++]  
  47.                 \EndWhile  
  48.                 \For{i=0k1}  
  49.                     \State Array[left+i]B[i]  
  50.                 \EndFor  
  51.                 \State \Return{result}  
  52.             \EndFunction  
  53.         \end{algorithmic}  
  54.     \end{algorithm}  
  55. \end{document}  

algorithm例子

前期准备
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. \usepackage{algorithm}  
  2. \usepackage{algpseudocode}  
  3. \usepackage{amsmath}  
  4. \renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm  
  5. \renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm  

example 1


代码:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.   \begin{algorithm}[htb]  
  2.   \caption{ Framework of ensemble learning for our system.}  
  3.   \label{alg:Framwork}  
  4.   \begin{algorithmic}[1]  
  5.     \Require  
  6.       The set of positive samples for current batch, Pn;  
  7.       The set of unlabelled samples for current batch, Un;  
  8.       Ensemble of classifiers on former batches, En1;  
  9.     \Ensure  
  10.       Ensemble of classifiers on the current batch, En;  
  11.     \State Extracting the set of reliable negative and/or positive samples Tn from Un with help of Pn;  
  12.     \label{code:fram:extract}  
  13.     \State Training ensemble of classifiers E on TnPn, with help of data in former batches;  
  14.     \label{code:fram:trainbase}  
  15.     \State En=En1cupE;  
  16.     \label{code:fram:add}  
  17.     \State Classifying samples in UnTn by En;  
  18.     \label{code:fram:classify}  
  19.     \State Deleting some weak classifiers in En so as to keep the capacity of En;  
  20.     \label{code:fram:select} \\  
  21.     \Return En;  
  22.   \end{algorithmic}  
  23. \end{algorithm}  

example 2


代码:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. \begin{algorithm}[h]  
  2.   \caption{An example for format For \& While Loop in Algorithm}  
  3.   \begin{algorithmic}[1]  
  4.     \For{each i[1,9]}  
  5.       \State initialize a tree Ti with only a leaf (the root);  
  6.       \State T=TTi;  
  7.     \EndFor  
  8.     \ForAll {c such that cRecentMBatch(En1)}  
  9.       \label{code:TrainBase:getc}  
  10.       \State T=TPosSample(c);  
  11.       \label{code:TrainBase:pos}  
  12.     \EndFor;  
  13.     \For{i=1i<ni++ }  
  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 {(|En|L1)and(Dϕ)}  
  22.       \State Selecting the most recent classifier ci from D;  
  23.       \State D=Dci;  
  24.       \State En=En+ci;  
  25.     \EndWhile  
  26.     \label{code:recentEnd}  
  27.   \end{algorithmic}  
  28. \end{algorithm}  


example 3

代码:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. \begin{algorithm}[h]  
  2.   \caption{Conjugate Gradient Algorithm with Dynamic Step-Size Control}  
  3.   \label{alg::conjugateGradient}  
  4.   \begin{algorithmic}[1]  
  5.     \Require  
  6.       f(x): objective funtion;  
  7.       x0: initial solution;  
  8.       s: step size;  
  9.     \Ensure  
  10.       optimal x  
  11.     \State initial g0=0 and d0=0;  
  12.     \Repeat  
  13.       \State compute gradient directions gk=f(xk);  
  14.       \State compute Polak-Ribiere parameter βk=gkT(gkgk1)gk12;  
  15.       \State compute the conjugate directions dk=gk+βkdk1;  
  16.       \State compute the step size αk=s/dk2;  
  17.     \Until{(f(xk)>f(xk1))}  
  18.   \end{algorithmic}  
  19. \end{algorithm}  


example 4


代码:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. \makeatletter  
  2. \def\BState{\State\hskip-\ALG@thistlm}  
  3. \makeatother  
  4. \begin{algorithm}  
  5. \caption{My algorithm}\label{euclid}  
  6. \begin{algorithmic}[1]  
  7. \Procedure{MyProcedure}{}  
  8. \State stringlenlength of string  
  9. \State ipatlen  
  10. \BState \emph{top}:  
  11. \If {i>stringlen} \Return false  
  12. \EndIf  
  13. \State jpatlen  
  14. \BState \emph{loop}:  
  15. \If {string(i)=path(j)}  
  16. \State jj1.  
  17. \State ii1.  
  18. \State \textbf{goto} \emph{loop}.  
  19. \State \textbf{close};  
  20. \EndIf  
  21. \State ii+max(delta1(string(i)),delta2(j)).  
  22. \State \textbf{goto} \emph{top}.  
  23. \EndProcedure  
  24. \end{algorithmic}  
  25. \end{algorithm}  

algorithm2e例子

algorithm2e包可能会与其它包产生冲突,一个常见的错误提示是“Too many }'...”。为了解决这个问题,要在引入algorithm2e包之前加入下面的命令:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. \makeatletter  
  2. \newif\if@restonecol  
  3. \makeatother  
  4. \let\algorithm\relax  
  5. \let\endalgorithm\relax  

所以前期准备:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. \makeatletter  
  2. \newif\if@restonecol  
  3. \makeatother  
  4. \let\algorithm\relax  
  5. \let\endalgorithm\relax  
  6. \usepackage[linesnumbered,ruled,vlined]{algorithm2e}%[ruled,vlined]{  
  7. \usepackage{algpseudocode}  
  8. \usepackage{amsmath}  
  9. \renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm  
  10. \renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm   

example 1


代码:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. \begin{algorithm}  
  2.   \caption{identify Row Context}  
  3.   \KwIn{riBackgrd(Ti)=T1,T2,,Tn and similarity threshold θr}  
  4.   \KwOut{con(ri)}  
  5.   con(ri)=Φ\;  
  6.   \For{j=1;jn;ji}  
  7.   {  
  8.     float maxSim=0\;  
  9.     rmaxSim=null\;  
  10.     \While{not end of Tj}  
  11.     {  
  12.       compute Jaro(ri,rm)(rmTj)\;  
  13.       \If{(Jaro(ri,rm)θr)(Jaro(ri,rm)rmaxSim)}  
  14.       {  
  15.         replace rmaxSim with rm\;  
  16.       }  
  17.     }  
  18.     con(ri)=con(ri)rmaxSim\;  
  19.   }  
  20.   return con(ri)\;  
  21. \end{algorithm}  


example 2


代码:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. \begin{algorithm}  
  2. \caption{Service checkpoint image storage node and routing path selection}  
  3. \LinesNumbered  
  4. \KwIn{host server PMs that SerImgk is fetched from, subnets that PMs belongs to, pods that PMs belongs to}  
  5. \KwOut{Service image storage server storageserver,and the image transfer path path}  
  6. storageserver = Storage node selection(PMsSerImgk,subnets,pods)\;  
  7. \If{ storageserver  null}  
  8. {  
  9.      select a path from storageserver to PMs and assign the path to path\;  
  10. }  
  11.   
  12. \textbf{final} \;  
  13. \textbf{return} storageserver and path;  
  14. \end{algorithm}  


example 3


代码:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. \begin{algorithm}  
  2. \caption{Storage node selection}  
  3. \LinesNumbered  
  4. \KwIn{host server PMs that the checkpoint image Img is fetched from, subnets that PMs belongs to, pods that PMs belongs to}  
  5. \KwOut{Image storage server storageserver}  
  6.   
  7. \For{ each host server PMi in the same subnet with PMs }  
  8. {  
  9.     \If{ PMi is not a service providing node or checkpoint image storage node of Sk }  
  10.     {  
  11.         add PMi to candidateList \;  
  12.     }  
  13. }  
  14. sort candidateList by reliability desc\;  
  15. init storageserver ;  
  16. \For{ each PMk in candidateList}  
  17. {  
  18.   
  19.             \If{ SP(PMk)  E(SP) of podi and BMk  size of Img }  
  20.              {  
  21.                 assign PMk to storageserver\;  
  22.                 goto final\;  
  23.              }  
  24. }  
  25. clear candidateList\;  
  26. add all other subnets in pods to netList\;  
  27. \For{ each subnet subnetj in netList}  
  28. {  
  29.         clear candidateList\;  
  30.         \For {each PMi in subnetj }  
  31.         {  
  32.             \If{ PMi is not a service providing node or checkpoint image storage node of Sk }  
  33.             {  
  34.                 add PMi to candidateList\;  
  35.             }  
  36.         }  
  37.     sort all host in candidateList by reliability desc\;  
  38.     \For{ each PMk in candidateList}  
  39.     {  
  40.   
  41.             \If{SP(PMk)  E(SP) of podi and BMk  size of Img}  
  42.             {  
  43.                 assign PMk to storageserver \;  
  44.                 goto final\;  
  45.             }  
  46.     }  
  47. }  
  48. \textbf{final} \;  
  49. \textbf{return} storageserver;  
  50. \end{algorithm}  


example 4


代码:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. \begin{algorithm}  
  2. \caption{Delta checkpoint image storage node and routing path selection}  
  3. \LinesNumbered  
  4. \KwIn{host server PMs that generates the delta checkpoint image DImgktsubnets that PMs belongs to, pods that PMs belongs to}  
  5. \KwOut{Delta image storage server storageserver,and the image transfer path Path}  
  6. storageserver = Storage node selection(PMsDImgkt,subnets,pods)\;  
  7. \If{ storageserver  null}  
  8. {  
  9.      the delta checkpoint image is stored in the central storage server\;  
  10.      goto final\;  
  11. }  
  12. construct weighted topological graph graphs of pods\;  
  13. calculate the shortest path from storageserver to PMs in graphs by using the Dijkstra algorithm\;  
  14. \textbf{final} \;  
  15. \textbf{return} storageserver and path;  
  16. \end{algorithm}  


example 5

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. \documentclass[8pt,twocolumn]{ctexart}  
  2. \usepackage{amssymb}  
  3. \usepackage{bm}  
  4. \usepackage{textcomp} %命令\textacutedbl的包,二阶导符号  
  5.   
  6. % Page length commands go here in the preamble  
  7. \setlength{\oddsidemargin}{-0.25in} % Left margin of 1 in + 0 in = 1 in  
  8. \setlength{\textwidth}{9in}   % 纸张宽度Right margin of 8.5 in - 1 in - 6.5 in = 1 in  
  9. \setlength{\topmargin}{-.75in}  % Top margin of 2 in -0.75 in = 1 in  
  10. \setlength{\textheight}{9.2in}  % Lower margin of 11 in - 9 in - 1 in = 1 in  
  11. \setlength{\parindent}{0in}  
  12.   
  13.   
  14. \makeatletter  
  15. \newif\if@restonecol  
  16. \makeatother  
  17. \let\algorithm\relax  
  18. \let\endalgorithm\relax  
  19. \usepackage[linesnumbered,ruled,vlined]{algorithm2e}%[ruled,vlined]{  
  20. \usepackage{algpseudocode}  
  21. \renewcommand{\algorithmicrequire}{\textbf{Input:}}   
  22. \renewcommand{\algorithmicensure}{\textbf{Output:}}   
  23.   
  24. \begin{document}  
  25.   
  26. \begin{algorithm}  
  27. \caption{component matrices computing}  
  28. \LinesNumbered  
  29. \KwIn{XRl1×l2××lN,ε,λ,δ,R}  
  30. \KwOut{A(j)s for j=1 to N}  
  31. \textbf{Initialize} all A(j)s //which can be seen as the 0th round iterations\;  
  32.   
  33. {l\hspace*{-1pt}\textacutedbl}=L //if we need to judge whether (11) is true then {l\hspace*{-1pt}\textacutedbl} denotes L|t1\;  
  34.   
  35. \For{ each Aijrj(1jN,1ijIj,1rR) }  
  36. {//1st round iterations\;  
  37.     gijr(j)=gijr(j)\;  
  38.     Aijr(j)=Aijr(j)//if the rollback shown as (12) is needed,Aijr(j) denotes Aijr(j)|t1\;  
  39.     Aijr(j)=Aijr(j)sign(gijr(j))δijr(j)\;  
  40. }  
  41.   
  42. \Repeat(//other rounds of iterations for computing component matrices){Lε or maximum iterations exhausted}  
  43. {  
  44.     l=L //if we need to judge whether (11) is true then l denotes L|t\;  
  45.     \For{ each Aijrj(1jN,1ijIj,1rR)}  
  46.     {  
  47.         \If{gijr(j)gijr(j)>0}  
  48.         {  
  49.                 Aijr(j)=Aijr(j)\;  
  50.                 gijr(j)=gijr(j)\;  
  51.                 δijr(j)=min(δijr(j)η+,Max_Step_Size)\;  
  52.                 Aijr(j)=Aijr(j)sign(gijr(j))δijr(j)\;  
  53.         }  
  54.         \ElseIf{gijr(j)gijr(j)<0}  
  55.         {  
  56.             \If{l>l\hspace*{-1pt}\textacutedbl}  
  57.             {  
  58.                 gijr(j)=gijr(j)\;  
  59.                 Aijr(j)=Aijr(j)// if (11) is true then rollback as (12)\;  
  60.                 δijr(j)=max(δijr(j)×η,Min_Step_Size)\;  
  61.             }  
  62.             \Else  
  63.             {  
  64.                 Aijr(j)=Aijr(j)\;  
  65.                 gijr(j)=gijr(j)\;  
  66.                 δijr(j)=max(δijr(j)η,Min_Step_Size)\;  
  67.                 Aijr(j)=Aijr(j)sign(gijr(j))δijr(j)\;  
  68.             }  
  69.         }  
  70.         \Else  
  71.         {  
  72.                 Aijr(j)=Aijr(j)\;  
  73.                 gijr(j)=gijr(j)\;  
  74.                 Aijr(j)=Aijr(j)sign(gijr(j))δijr(j)\;  
  75.         }  
  76.     }  
  77.     l\hspace*{-1pt}\textacutedbl=l\;  
  78. }  
  79. \end{algorithm}  
  80. \end{document}  


example 6

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. \usepackage[ruled,linesnumbered]{algorithm2e}  
  2. \usepackage{amsmath}  
  3.   
  4. \begin{algorithm}  
  5.     \caption{Learning algorithm of R2P}  
  6.     \label{alg:r2p}  
  7.     \KwIn{ratings R, joint demographic representations Y,learning rate η,maximum iterative number maxIter, negative sampling number k\;}  
  8.     \KwOut{interaction matrix W, movie vectors V\;}  
  9.     Initialize W,V randomly\;  
  10.     t=0\;  
  11.     For convenience, define φn=mSnrm,nvm\; %\varphi_n\bm{W}\vec{y}_n  
  12.     \While{not converged \rm{or} t>maxIter}  
  13.     {  
  14.       t = t+1\;  
  15.       \For{n=1;nN;n++}  
  16.       {  
  17.         W=W+η(1σ(φnTWyn))φnynT\;\label{algline:W}  
  18.         \For{mSn}  
  19.         {  
  20.             vm=vm+η(1σ(φnTWyn))rm,nWyn\;\label{algline:V}  
  21.         }  
  22.         \For{i=1;ik;i++}  
  23.         {  
  24.             sample negative sample yi from Pn\;  
  25.             W=Wη(1σ(φnTWyn))φnyiT\;  
  26.             \For{mSn}  
  27.             {  
  28.                 vm=vmη(1σ(φnTWyn))rm,nWyi\;  
  29.             }  
  30.         }  
  31.       }  
  32.       W=W2ληW\;  
  33.       V=V2ληV  
  34.     }  
  35.     return W,V\;  
  36.     %\end{algorithmic}  
  37. \end{algorithm}  

posted @   张同光  阅读(1611)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示