Latex算法伪代码使用总结

Latex伪代码使用总结

 

 

algorithmicx例子

相应代码:

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

documentclass[11pt]{ctexart}  
usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}  
usepackage{algorithm}  
usepackage{algorithmicx}  
usepackage{algpseudocode}  
usepackage{amsmath}  
  
floatname{algorithm}{算法}  
enewcommand{algorithmicrequire}{ extbf{输入:}}  
enewcommand{algorithmicensure}{ extbf{输出:}}  
  
egin{document}  
    egin{algorithm}  
        caption{用归并排序求逆序数}  
        egin{algorithmic}[1] %每行显示行号  
            Require $Array$数组,$n$数组大小  
            Ensure 逆序数  
            Function {MergerSort}{$Array, left, right$}  
                State $result gets 0$  
                If {$left < right$}  
                    State $middle gets left + right) / 2$  
                    State $result gets result +$ Call{MergerSort}{$Array, left, middle$}  
                    State $result gets result +$ Call{MergerSort}{$Array, middle, right$}  
                    State $result gets result +$ Call{Merger}{$Array,left,middle,right$}  
                EndIf  
                State Return{$result$}  
            EndFunction  
            State  
            Function{Merger}{$Array, left, middle, right$}  
                State $igets left$  
                State $jgets middle$  
                State $kgets 0$  
                State $result gets 0$  
                While{$i<middle$  extbf{and} $j<right$}  
                    If{$Array[i]<Array[j]$}  
                        State $B[k++]gets Array[i++]$  
                    Else  
                        State $B[k++] gets Array[j++]$  
                        State $result gets result + middle – i)$  
                    EndIf  
                EndWhile  
                While{$i<middle$}  
                    State $B[k++] gets Array[i++]$  
                EndWhile  
                While{$j<right$}  
                    State $B[k++] gets Array[j++]$  
                EndWhile  
                For{$i = 0  o k-1$}  
                    State $Array[left + i] gets B[i]$  
                EndFor  
                State Return{$result$}  
            EndFunction  
        end{algorithmic}  
    end{algorithm}  
end{document}  

algorithm例子

前期准备

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

usepackage{algorithm}  
usepackage{algpseudocode}  
usepackage{amsmath}  
enewcommand{algorithmicrequire}{ extbf{Input:}}  % Use Input in the format of Algorithm  
enewcommand{algorithmicensure}{ extbf{Output:}} % Use Output in the format of Algorithm  

example 1

代码:

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

  egin{algorithm}[htb]  
  caption{ Framework of ensemble learning for our system.}  
  label{alg:Framwork}  
  egin{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}  

example 2

代码:

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

egin{algorithm}[h]  
  caption{An example for format For & While Loop in Algorithm}  
  egin{algorithmic}[1]  
    For{each $iin [1,9]$}  
      State initialize a tree $T_{i}$ with only a leaf the root);  
      State $T=Tcup T_{i};$  
    EndFor  
    ForAll {$c$ such that $cin RecentMBatchE_{n-1})$}  
      label{code:TrainBase:getc}  
      State $T=Tcup PosSamplec)$;  
      label{code:TrainBase:pos}  
    EndFor;  
    For{$i=1$; $i<n$; $i++$ }  
      State $//$ Your source here;  
    EndFor  
    For{$i=1$ to $n$}  
      State $//$ Your source here;  
    EndFor  
    State $//$ Reusing recent base classifiers.  
    label{code:recentStart}  
    While {$|E_n| leq L_1 )and D 
eq phi)$}  
      State Selecting the most recent classifier $c_i$ from $D$;  
      State $D=D-c_i$;  
      State $E_n=E_n+c_i$;  
    EndWhile  
    label{code:recentEnd}  
  end{algorithmic}  
end{algorithm}  

example 3

代码:

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

egin{algorithm}[h]  
  caption{Conjugate Gradient Algorithm with Dynamic Step-Size Control}  
  label{alg::conjugateGradient}  
  egin{algorithmic}[1]  
    Require  
      $fx)$: 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=igtriangledown fx_k)$;  
      State compute Polak-Ribiere parameter $eta_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+eta_k d_{k-1}$;  
      State compute the step size $alpha_k=s/parallel d_k parallel_{2}$;  
    Until{$fx_k)>fx_{k-1})$)}  
  end{algorithmic}  
end{algorithm}  

example 4

代码:

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

makeatletter  
defBState{Statehskip-ALG@thistlm}  
makeatother  
egin{algorithm}  
caption{My algorithm}label{euclid}  
egin{algorithmic}[1]  
Procedure{MyProcedure}{}  
State $ extit{stringlen} gets  ext{length of } extit{string}$  
State $i gets  extit{patlen}$  
BState emph{top}:  
If {$i >  extit{stringlen}$} Return false  
EndIf  
State $j gets  extit{patlen}$  
BState emph{loop}:  
If {$ extit{string}i) =  extit{path}j)$}  
State $j gets j-1$.  
State $i gets i-1$.  
State  extbf{goto} emph{loop}.  
State  extbf{close};  
EndIf  
State $i gets i+max extit{delta}_1 extit{string}i)), extit{delta}_2j))$.  
State  extbf{goto} emph{top}.  
EndProcedure  
end{algorithmic}  
end{algorithm}  

 

algorithm2e例子

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

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

makeatletter  

ewifif@restonecol  
makeatother  
letalgorithmelax  
letendalgorithmelax  

所以前期准备:

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

makeatletter  

ewifif@restonecol  
makeatother  
letalgorithmelax  
letendalgorithmelax  
usepackage[linesnumbered,ruled,vlined]{algorithm2e}%[ruled,vlined]{  
usepackage{algpseudocode}  
usepackage{amsmath}  
enewcommand{algorithmicrequire}{ extbf{Input:}}  % Use Input in the format of Algorithm  
enewcommand{algorithmicensure}{ extbf{Output:}} % Use Output in the format of Algorithm   

 

example 1


代码:

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

egin{algorithm}  
  caption{identify Row Context}  
  KwIn{$r_i$, $BackgrdT_i)$=${T_1,T_2,ldots ,T_n}$ and similarity threshold $ heta_r$}  
  KwOut{$conr_i)$}  
  $conr_i)= Phi$;  
  For{$j=1;j le n;j 
e i$}  
  {  
    float $maxSim=0$;  
    $r^{maxSim}=null$;  
    While{not end of $T_j$}  
    {  
      compute Jaro$r_i,r_m$)$r_min T_j$);  
      If{$Jaror_i,r_m) ge  heta_r)wedge Jaror_i,r_m)ge r^{maxSim})$}  
      {  
        replace $r^{maxSim}$ with $r_m$;  
      }  
    }  
    $conr_i)=conr_i)cup {r^{maxSim}}$;  
  }  
  return $conr_i)$;  
end{algorithm}  

example 2


代码:

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

egin{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$ $
eq$ null}  
{  
     select a path from $storageserver$ to $PM_s$ and assign the path to $path$;  
}  
  
extbf{final} ;  
extbf{return} $storageserver$ and $path$;  
end{algorithm}  

example 3


代码:

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

egin{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{ $SPPM_k)$ $geq$ $ESP)$ 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{$SPPM_k)$ $geq$ $ESP)$ of $pod_i$ and $BM_k$ $le$ size of $Img$}  
            {  
                assign $PM_k$ to $storageserver$ ;  
                goto final;  
            }  
    }  
}  
extbf{final} ;  
extbf{return} $storageserver$;  
end{algorithm}  

example 4


代码:

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

egin{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;  
extbf{final} ;  
extbf{return} $storageserver$ and $path$;  
end{algorithm}  

example 5

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

documentclass[8pt,twocolumn]{ctexart}  
usepackage{amssymb}  
usepackage{bm}  
usepackage{textcomp} %命令 extacutedbl的包,二阶导符号  
  
% Page length commands go here in the preamble  
setlength{oddsidemargin}{-0.25in} % Left margin of 1 in + 0 in = 1 in  
setlength{ extwidth}{9in}   % 纸张宽度Right margin of 8.5 in – 1 in – 6.5 in = 1 in  
setlength{ opmargin}{-.75in}  % Top margin of 2 in -0.75 in = 1 in  
setlength{ extheight}{9.2in}  % Lower margin of 11 in – 9 in – 1 in = 1 in  
setlength{parindent}{0in}  
  
  
makeatletter  

ewifif@restonecol  
makeatother  
letalgorithmelax  
letendalgorithmelax  
usepackage[linesnumbered,ruled,vlined]{algorithm2e}%[ruled,vlined]{  
usepackage{algpseudocode}  
enewcommand{algorithmicrequire}{ extbf{Input:}}   
enewcommand{algorithmicensure}{ extbf{Output:}}   
  
egin{document}  
  
egin{algorithm}  
caption{component matrices computing}  
LinesNumbered  
KwIn{$mathcal{X}inmathbb{R}^{l_1 imes l_2 imescdots imes l_N},varepsilon,lambda,delta,R$}  
KwOut{$A^{j)}s$ for $j=1$ to $N$}  
extbf{Initialize} all $A^{j)}s$ //which can be seen as the $0^{th}$ round iterations;  
  
{$l$hspace*{-1pt} extacutedbl}$=L$ //if we need to judge whether $11)$ is true then {$l$hspace*{-1pt} extacutedbl} denotes $L|_{t-1}$;  
  
For{ each $A_{i_jr}^{{j}}1le jle N,1le i_jle I_j,1le rle 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{{f sign}}leftg_{i_jr}^{j)}ight)cdotdelta_{i_jr}^{j)}$;  
}  
  
Repeat//other rounds of iterations for computing component matrices){$m{Lle 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}}1le jle N,1le i_jle I_j,1le rle 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)}=m{min}leftdelta_{i_jr}^{j)}cdoteta^{+},Max\_Step\_Sizeight)$;  
                $A_{i_jr}^{j)}=A_{i_jr}^{j)}-mathrm{{f sign}}leftg_{i_jr}^{j)}ight)cdotdelta_{i_jr}^{j)}$;  
        }  
        ElseIf{$g_{i_jr}^{j)}cdot g_{i_jr}^{j)’}<0$}  
        {  
            If{$l’>l$hspace*{-1pt} extacutedbl}  
            {  
                $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)}=m{max}leftdelta_{i_jr}^{j)} imeseta^{-},Min\_Step\_Sizeight)$;  
            }  
            Else  
            {  
                $A_{i_jr}^{j)’}=A_{i_jr}^{j)} $;  
                $g_{i_jr}^{j)’}=g_{i_jr}^{j)} $;  
                $delta_{i_jr}^{j)}=m{max}leftdelta_{i_jr}^{j)}cdoteta^{-},Min\_Step\_Sizeight)$;  
                $A_{i_jr}^{j)}=A_{i_jr}^{j)}-mathrm{{f sign}}leftg_{i_jr}^{j)}ight)cdotdelta_{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{{f sign}}leftg_{i_jr}^{j)}ight)cdotdelta_{i_jr}^{j)}$;  
        }  
    }  
    $l$hspace*{-1pt} extacutedbl$=l’$;  
}  
end{algorithm}  
end{document}  

example 6
 

[plain] view plain copy
 

 
 

 在CODE上查看代码片派生到我的代码片

usepackage[ruled,linesnumbered]{algorithm2e}  
usepackage{amsmath}  
  
egin{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 $m{W}$, movie vectors $V$;}  
    Initialize $m{W},V$ randomly;  
    $t = 0$;  
    For convenience, define $vec{varphi}_n = sum_{min S_n}r_{m,n}vec{v}_m$; %varphi_nm{W}vec{y}_n  
    While{not converged m{or} $t>maxIter$}  
    {  
      t = t+1;  
      For{$n=1;n le N;n++$}  
      {  
        $m{W} = m{W}+etaig1-sigmaleftvec{varphi}_n^Tm{W}vec{y}_night)ig)vec{varphi}_nvec{y}_n^T$;label{algline:W}  
        For{$min S_n$}  
        {  
            $vec{v}_m=vec{v}_m+ etaleft1-sigmaleftvec{varphi}_n^Tm{W}vec{y}_night)ight)r_{m,n}m{W}vec{y}_n$;label{algline:V}  
        }  
        For{$i=1;ile k;i++$}  
        {  
            sample negative sample $vec{y}_i$ from $P_n$;  
            $m{W} = m{W}-etaig1-sigmaleft-vec{varphi}_n^Tm{W}vec{y}_night)ig)vec{varphi}_nvec{y}_i^T$;  
            For{$min S_n$}  
            {  
                $vec{v}_m=vec{v}_m- etaleft1-sigmaleft-vec{varphi}_n^Tm{W}vec{y}_night)ight)r_{m,n}m{W}vec{y}_i$;  
            }  
        }  
      }  
      $m{W} = m{W}-2lambdaetam{W}$;  
      $V=V-2lambdaeta V$  
    }  
    return $m{W},V$;  
    %end{algorithmic}  
end{algorithm}  

 
 

 

 

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注