zoukankan      html  css  js  c++  java
  • Latex算法伪代码使用总结

    Latex伪代码使用总结

     

     

    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. enewcommand{algorithmicrequire}{ extbf{输入:}}  
    10. enewcommand{algorithmicensure}{ extbf{输出:}}  
    11.   
    12. egin{document}  
    13.     egin{algorithm}  
    14.         caption{用归并排序求逆序数}  
    15.         egin{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 $igets left$  
    31.                 State $jgets middle$  
    32.                 State $kgets 0$  
    33.                 State $result gets 0$  
    34.                 While{$i<middle$  extbf{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  o 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}  

    algorithm例子

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

    example 1


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

    example 2


    代码:
    [plain] view plain copy
     
     
     
     在CODE上查看代码片派生到我的代码片
    1. egin{algorithm}[h]  
    2.   caption{An example for format For & While Loop in Algorithm}  
    3.   egin{algorithmic}[1]  
    4.     For{each $iin [1,9]$}  
    5.       State initialize a tree $T_{i}$ with only a leaf (the root);  
    6.       State $T=Tcup T_{i};$  
    7.     EndFor  
    8.     ForAll {$c$ such that $cin RecentMBatch(E_{n-1})$}  
    9.       label{code:TrainBase:getc}  
    10.       State $T=Tcup 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  eq 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}  


    example 3

    代码:
    [plain] view plain copy
     
     
     
     在CODE上查看代码片派生到我的代码片
    1. egin{algorithm}[h]  
    2.   caption{Conjugate Gradient Algorithm with Dynamic Step-Size Control}  
    3.   label{alg::conjugateGradient}  
    4.   egin{algorithmic}[1]  
    5.     Require  
    6.       $f(x)$: objective funtion;  
    7.       $x_0$: initial solution;  
    8.       $s$: step size;  
    9.     Ensure  
    10.       optimal $x^{*}$  
    11.     State initial $g_0=0$ and $d_0=0$;  
    12.     Repeat  
    13.       State compute gradient directions $g_k=igtriangledown f(x_k)$;  
    14.       State compute Polak-Ribiere parameter $eta_k=frac{g_k^{T}(g_k-g_{k-1})}{parallel g_{k-1} parallel^{2}}$;  
    15.       State compute the conjugate directions $d_k=-g_k+eta_k d_{k-1}$;  
    16.       State compute the step size $alpha_k=s/parallel d_k parallel_{2}$;  
    17.     Until{($f(x_k)>f(x_{k-1})$)}  
    18.   end{algorithmic}  
    19. end{algorithm}  


    example 4


    代码:
    [plain] view plain copy
     
     
     
     在CODE上查看代码片派生到我的代码片
    1. makeatletter  
    2. defBState{Statehskip-ALG@thistlm}  
    3. makeatother  
    4. egin{algorithm}  
    5. caption{My algorithm}label{euclid}  
    6. egin{algorithmic}[1]  
    7. Procedure{MyProcedure}{}  
    8. State $ extit{stringlen} gets  ext{length of } extit{string}$  
    9. State $i gets  extit{patlen}$  
    10. BState emph{top}:  
    11. If {$i >  extit{stringlen}$} Return false  
    12. EndIf  
    13. State $j gets  extit{patlen}$  
    14. BState emph{loop}:  
    15. If {$ extit{string}(i) =  extit{path}(j)$}  
    16. State $j gets j-1$.  
    17. State $i gets i-1$.  
    18. State  extbf{goto} emph{loop}.  
    19. State  extbf{close};  
    20. EndIf  
    21. State $i gets i+max( extit{delta}_1( extit{string}(i)), extit{delta}_2(j))$.  
    22. State  extbf{goto} emph{top}.  
    23. EndProcedure  
    24. end{algorithmic}  
    25. end{algorithm}  
     

    algorithm2e例子

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

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

    example 1

    代码:
    [plain] view plain copy
     
     
     
     在CODE上查看代码片派生到我的代码片
    1. egin{algorithm}  
    2.   caption{identify Row Context}  
    3.   KwIn{$r_i$, $Backgrd(T_i)$=${T_1,T_2,ldots ,T_n}$ and similarity threshold $ heta_r$}  
    4.   KwOut{$con(r_i)$}  
    5.   $con(r_i)= Phi$;  
    6.   For{$j=1;j le n;j  e i$}  
    7.   {  
    8.     float $maxSim=0$;  
    9.     $r^{maxSim}=null$;  
    10.     While{not end of $T_j$}  
    11.     {  
    12.       compute Jaro($r_i,r_m$)($r_min T_j$);  
    13.       If{$(Jaro(r_i,r_m) ge  heta_r)wedge (Jaro(r_i,r_m)ge r^{maxSim})$}  
    14.       {  
    15.         replace $r^{maxSim}$ with $r_m$;  
    16.       }  
    17.     }  
    18.     $con(r_i)=con(r_i)cup {r^{maxSim}}$;  
    19.   }  
    20.   return $con(r_i)$;  
    21. end{algorithm}  


    example 2

    代码:
    [plain] view plain copy
     
     
     
     在CODE上查看代码片派生到我的代码片
    1. egin{algorithm}  
    2. caption{Service checkpoint image storage node and routing path selection}  
    3. LinesNumbered  
    4. 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}  
    5. KwOut{Service image storage server $storageserver$,and the image transfer path $path$}  
    6. $storageserver$ = Storage node selection($PM_s$, $SerImg_k$,$subnet_s$,$pod_s$);  
    7. If{ $storageserver$ $ eq$ null}  
    8. {  
    9.      select a path from $storageserver$ to $PM_s$ and assign the path to $path$;  
    10. }  
    11.   
    12. extbf{final} ;  
    13. extbf{return} $storageserver$ and $path$;  
    14. end{algorithm}  


    example 3

    代码:
    [plain] view plain copy
     
     
     
     在CODE上查看代码片派生到我的代码片
    1. egin{algorithm}  
    2. caption{Storage node selection}  
    3. LinesNumbered  
    4. 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}  
    5. KwOut{Image storage server $storageserver$}  
    6.   
    7. For{ each host server $PM_i$ in the same subnet with $PM_s$ }  
    8. {  
    9.     If{ $PM_i$ is not a service providing node or checkpoint image storage node of $S_k$ }  
    10.     {  
    11.         add $PM_i$ to $candidateList$ ;  
    12.     }  
    13. }  
    14. sort $candidateList$ by reliability desc;  
    15. init $storageserver$ ;  
    16. For{ each $PM_k$ in $candidateList$}  
    17. {  
    18.   
    19.             If{ $SP(PM_k)$ $geq$ $E(SP)$ of $pod_i$ and $BM_k$ $le$ size of $Img$ }  
    20.              {  
    21.                 assign $PM_k$ to $storageserver$;  
    22.                 goto final;  
    23.              }  
    24. }  
    25. clear $candidateList$;  
    26. add all other subnets in $pod_s$ to $netList$;  
    27. For{ each subnet $subnet_j$ in $netList$}  
    28. {  
    29.         clear $candidateList$;  
    30.         For {each $PM_i$ in $subnet_j$ }  
    31.         {  
    32.             If{ $PM_i$ is not a service providing node or checkpoint image storage node of $S_k$ }  
    33.             {  
    34.                 add $PM_i$ to $candidateList$;  
    35.             }  
    36.         }  
    37.     sort all host in $candidateList$ by reliability desc;  
    38.     For{ each $PM_k$ in $candidateList$}  
    39.     {  
    40.   
    41.             If{$SP(PM_k)$ $geq$ $E(SP)$ of $pod_i$ and $BM_k$ $le$ size of $Img$}  
    42.             {  
    43.                 assign $PM_k$ to $storageserver$ ;  
    44.                 goto final;  
    45.             }  
    46.     }  
    47. }  
    48. extbf{final} ;  
    49. extbf{return} $storageserver$;  
    50. end{algorithm}  


    example 4

    代码:
    [plain] view plain copy
     
     
     
     在CODE上查看代码片派生到我的代码片
    1. egin{algorithm}  
    2. caption{Delta checkpoint image storage node and routing path selection}  
    3. LinesNumbered  
    4. 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}  
    5. KwOut{Delta image storage server $storageserver$,and the image transfer path $Path$}  
    6. $storageserver$ = Storage node selection($PM_s$, $DImg_{kt}$,$subnet_s$,$pod_s$);  
    7. If{ $storageserver$ $equiv$ null}  
    8. {  
    9.      the delta checkpoint image is stored in the central storage server;  
    10.      goto final;  
    11. }  
    12. construct weighted topological graph $graph_s$ of $pod_s$;  
    13. calculate the shortest path from $storageserver$ to $PM_s$ in $graph_s$ by using the Dijkstra algorithm;  
    14. extbf{final} ;  
    15. extbf{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} %命令 extacutedbl的包,二阶导符号  
    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{ extwidth}{9in}   % 纸张宽度Right margin of 8.5 in - 1 in - 6.5 in = 1 in  
    9. setlength{ opmargin}{-.75in}  % Top margin of 2 in -0.75 in = 1 in  
    10. setlength{ extheight}{9.2in}  % Lower margin of 11 in - 9 in - 1 in = 1 in  
    11. setlength{parindent}{0in}  
    12.   
    13.   
    14. makeatletter  
    15. ewifif@restonecol  
    16. makeatother  
    17. letalgorithm elax  
    18. letendalgorithm elax  
    19. usepackage[linesnumbered,ruled,vlined]{algorithm2e}%[ruled,vlined]{  
    20. usepackage{algpseudocode}  
    21. enewcommand{algorithmicrequire}{ extbf{Input:}}   
    22. enewcommand{algorithmicensure}{ extbf{Output:}}   
    23.   
    24. egin{document}  
    25.   
    26. egin{algorithm}  
    27. caption{component matrices computing}  
    28. LinesNumbered  
    29. KwIn{$mathcal{X}inmathbb{R}^{l_1 imes l_2 imescdots imes l_N},varepsilon,lambda,delta,R$}  
    30. KwOut{$A^{(j)}s$ for $j=1$ to $N$}  
    31. extbf{Initialize} all $A^{(j)}s$ //which can be seen as the $0^{th}$ round iterations;  
    32.   
    33. {$l$hspace*{-1pt} extacutedbl}$=L$ //if we need to judge whether $(11)$ is true then {$l$hspace*{-1pt} extacutedbl} denotes $L|_{t-1}$;  
    34.   
    35. For{ each $A_{i_jr}^{{j}}(1le jle N,1le i_jle I_j,1le rle R)$ }  
    36. {//$1^{st}$ round iterations;  
    37.     $g_{i_jr}^{(j)'}=g_{i_jr}^{(j)}$;  
    38.     $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}$;  
    39.     $A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-mathrm{{f sign}}left(g_{i_jr}^{(j)} ight)cdotdelta_{i_jr}^{(j)}$;  
    40. }  
    41.   
    42. Repeat(//other rounds of iterations for computing component matrices){$m{Lle varepsilon}$ 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 $A_{i_jr}^{{j}}(1le jle N,1le i_jle I_j,1le rle R)$}  
    46.     {  
    47.         If{$g_{i_jr}^{(j)}cdot g_{i_jr}^{(j)'}>0$}  
    48.         {  
    49.                 $A_{i_jr}^{(j)'}=A_{i_jr}^{(j)} $;  
    50.                 $g_{i_jr}^{(j)'}=g_{i_jr}^{(j)} $;  
    51.                 $delta_{i_jr}^{(j)}=m{min}left(delta_{i_jr}^{(j)}cdoteta^{+},Max\_Step\_Size ight)$;  
    52.                 $A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-mathrm{{f sign}}left(g_{i_jr}^{(j)} ight)cdotdelta_{i_jr}^{(j)}$;  
    53.         }  
    54.         ElseIf{$g_{i_jr}^{(j)}cdot g_{i_jr}^{(j)'}<0$}  
    55.         {  
    56.             If{$l'>l$hspace*{-1pt} extacutedbl}  
    57.             {  
    58.                 $g_{i_jr}^{(j)'}=g_{i_jr}^{(j)}$;  
    59.                 $A_{i_jr}^{(j)}=A_{i_jr}^{(j)'}$// if $(11)$ is true then rollback as $(12)$;  
    60.                 $delta_{i_jr}^{(j)}=m{max}left(delta_{i_jr}^{(j)} imeseta^{-},Min\_Step\_Size ight)$;  
    61.             }  
    62.             Else  
    63.             {  
    64.                 $A_{i_jr}^{(j)'}=A_{i_jr}^{(j)} $;  
    65.                 $g_{i_jr}^{(j)'}=g_{i_jr}^{(j)} $;  
    66.                 $delta_{i_jr}^{(j)}=m{max}left(delta_{i_jr}^{(j)}cdoteta^{-},Min\_Step\_Size ight)$;  
    67.                 $A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-mathrm{{f sign}}left(g_{i_jr}^{(j)} ight)cdotdelta_{i_jr}^{(j)}$;  
    68.             }  
    69.         }  
    70.         Else  
    71.         {  
    72.                 $A_{i_jr}^{(j)'}=A_{i_jr}^{(j)} $;  
    73.                 $g_{i_jr}^{(j)'}=g_{i_jr}^{(j)} $;  
    74.                 $A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-mathrm{{f sign}}left(g_{i_jr}^{(j)} ight)cdotdelta_{i_jr}^{(j)}$;  
    75.         }  
    76.     }  
    77.     $l$hspace*{-1pt} extacutedbl$=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. egin{algorithm}  
    5.     caption{Learning algorithm of R2P}  
    6.     label{alg:r2p}  
    7.     KwIn{ratings $R$, joint demographic representations $Y$,learning rate $eta$,maximum iterative number $maxIter$, negative sampling number $k$;}  
    8.     KwOut{interaction matrix $m{W}$, movie vectors $V$;}  
    9.     Initialize $m{W},V$ randomly;  
    10.     $t = 0$;  
    11.     For convenience, define $vec{varphi}_n = sum_{min S_n}r_{m,n}vec{v}_m$; %varphi_nm{W}vec{y}_n  
    12.     While{not converged  m{or} $t>maxIter$}  
    13.     {  
    14.       t = t+1;  
    15.       For{$n=1;n le N;n++$}  
    16.       {  
    17.         $m{W} = m{W}+etaig(1-sigmaleft(vec{varphi}_n^Tm{W}vec{y}_n ight)ig)vec{varphi}_nvec{y}_n^T$;label{algline:W}  
    18.         For{$min S_n$}  
    19.         {  
    20.             $vec{v}_m=vec{v}_m+ etaleft(1-sigmaleft(vec{varphi}_n^Tm{W}vec{y}_n ight) ight)r_{m,n}m{W}vec{y}_n$;label{algline:V}  
    21.         }  
    22.         For{$i=1;ile k;i++$}  
    23.         {  
    24.             sample negative sample $vec{y}_i$ from $P_n$;  
    25.             $m{W} = m{W}-etaig(1-sigmaleft(-vec{varphi}_n^Tm{W}vec{y}_n ight)ig)vec{varphi}_nvec{y}_i^T$;  
    26.             For{$min S_n$}  
    27.             {  
    28.                 $vec{v}_m=vec{v}_m- etaleft(1-sigmaleft(-vec{varphi}_n^Tm{W}vec{y}_n ight) ight)r_{m,n}m{W}vec{y}_i$;  
    29.             }  
    30.         }  
    31.       }  
    32.       $m{W} = m{W}-2lambdaetam{W}$;  
    33.       $V=V-2lambdaeta V$  
    34.     }  
    35.     return $m{W},V$;  
    36.     %end{algorithmic}  
    37. end{algorithm}  
     
     
     
     
  • 相关阅读:
    NPM下载模块包说明
    Keycode对照表
    iframe的document操作
    JS中鼠标左右键以及中键的事件
    BOM函数之history对象
    31、开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。 处理: 1.记录最多8条错误记录,对相同的错误记录(即文件名称和行号完全匹配)只记录一条,错误计数增加;(文件所在的目录不同,文件名和行号相同也要合并) 2.超过16个字符的文件名称,只记录文件的最后有效16个字符;(如果文件名不同,而只是文件名的后16个字符和行号相同,也不要合并) 3.输入的文件可能带路径,记录文
    30、最高分是多少 老师想知道从某某同学当中,分数最高的是多少,现在请你编程模拟老师的询问。当然,老师有时候需要更新某位同学的成绩.
    二叉树遍历的算法
    28、输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
    27、输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
  • 原文地址:https://www.cnblogs.com/tsingke/p/6510343.html
Copyright © 2011-2022 走看看