日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Latex算法伪代码使用总结

發布時間:2023/12/19 综合教程 28 生活家
生活随笔 收集整理的這篇文章主要介紹了 Latex算法伪代码使用总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Latex偽代碼使用總結

algorithmicx例子

相應代碼:

[plain]view plaincopy


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$resultgets0$
If{$left<right$}
State$middlegets(left+right)/2$
State$resultgetsresult+$Call{MergerSort}{$Array,left,middle$}
State$resultgetsresult+$Call{MergerSort}{$Array,middle,right$}
State$resultgetsresult+$Call{Merger}{$Array,left,middle,right$}
EndIf
StateReturn{$result$}
EndFunction
State
Function{Merger}{$Array,left,middle,right$}
State$igetsleft$
State$jgetsmiddle$
State$kgets0$
State$resultgets0$
While{$i<middle$ extbf{and}$j<right$}
If{$Array[i]<Array[j]$}
State$B[k++]getsArray[i++]$
Else
State$B[k++]getsArray[j++]$
State$resultgetsresult+(middle-i)$
EndIf
EndWhile
While{$i<middle$}
State$B[k++]getsArray[i++]$
EndWhile
While{$j<right$}
State$B[k++]getsArray[j++]$
EndWhile
For{$i=0 ok-1$}
State$Array[left+i]getsB[i]$
EndFor
StateReturn{$result$}
EndFunction
end{algorithmic}
end{algorithm}
end{document}

algorithm例子

前期準備

[plain]view plaincopy


usepackage{algorithm}
usepackage{algpseudocode}
usepackage{amsmath}
enewcommand{algorithmicrequire}{ extbf{Input:}}%UseInputintheformatofAlgorithm
enewcommand{algorithmicensure}{ extbf{Output:}}%UseOutputintheformatofAlgorithm

example 1

代碼:

[plain]view plaincopy


egin{algorithm}[htb]
caption{Frameworkofensemblelearningforoursystem.}
label{alg:Framwork}
egin{algorithmic}[1]
Require
Thesetofpositivesamplesforcurrentbatch,$P_n$;
Thesetofunlabelledsamplesforcurrentbatch,$U_n$;
Ensembleofclassifiersonformerbatches,$E_{n-1}$;
Ensure
Ensembleofclassifiersonthecurrentbatch,$E_n$;
StateExtractingthesetofreliablenegativeand/orpositivesamples$T_n$from$U_n$withhelpof$P_n$;
label{code:fram:extract}
StateTrainingensembleofclassifiers$E$on$T_ncupP_n$,withhelpofdatainformerbatches;
label{code:fram:trainbase}
State$E_n=E_{n-1}cupE$;
label{code:fram:add}
StateClassifyingsamplesin$U_n-T_n$by$E_n$;
label{code:fram:classify}
StateDeletingsomeweakclassifiersin$E_n$soastokeepthecapacityof$E_n$;
label{code:fram:select}\
Return$E_n$;
end{algorithmic}
end{algorithm}

example 2

代碼:

[plain]view plaincopy


egin{algorithm}[h]
caption{AnexampleforformatFor&WhileLoopinAlgorithm}
egin{algorithmic}[1]
For{each$iin[1,9]$}
Stateinitializeatree$T_{i}$withonlyaleaf(theroot);
State$T=TcupT_{i};$
EndFor
ForAll{$c$suchthat$cinRecentMBatch(E_{n-1})$}
label{code:TrainBase:getc}
State$T=TcupPosSample(c)$;
label{code:TrainBase:pos}
EndFor;
For{$i=1$;$i<n$;$i++$}
State$//$Yoursourcehere;
EndFor
For{$i=1$to$n$}
State$//$Yoursourcehere;
EndFor
State$//$Reusingrecentbaseclassifiers.
label{code:recentStart}
While{$(|E_n|leqL_1)and(D
eqphi)$}
StateSelectingthemostrecentclassifier$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 plaincopy


egin{algorithm}[h]
caption{ConjugateGradientAlgorithmwithDynamicStep-SizeControl}
label{alg::conjugateGradient}
egin{algorithmic}[1]
Require
$f(x)$:objectivefuntion;
$x_0$:initialsolution;
$s$:stepsize;
Ensure
optimal$x^{*}$
Stateinitial$g_0=0$and$d_0=0$;
Repeat
Statecomputegradientdirections$g_k=igtriangledownf(x_k)$;
StatecomputePolak-Ribiereparameter$eta_k=frac{g_k^{T}(g_k-g_{k-1})}{parallelg_{k-1}parallel^{2}}$;
Statecomputetheconjugatedirections$d_k=-g_k+eta_kd_{k-1}$;
Statecomputethestepsize$alpha_k=s/paralleld_kparallel_{2}$;
Until{($f(x_k)>f(x_{k-1})$)}
end{algorithmic}
end{algorithm}

example 4

代碼:

[plain]view plaincopy


makeatletter
defBState{Statehskip-ALG@thistlm}
makeatother
egin{algorithm}
caption{Myalgorithm}label{euclid}
egin{algorithmic}[1]
Procedure{MyProcedure}{}
State$ extit{stringlen}gets ext{lengthof} extit{string}$
State$igets extit{patlen}$
BStateemph{top}:
If{$i> extit{stringlen}$}Returnfalse
EndIf
State$jgets extit{patlen}$
BStateemph{loop}:
If{$ extit{string}(i)= extit{path}(j)$}
State$jgetsj-1$.
State$igetsi-1$.
State extbf{goto}emph{loop}.
State extbf{close};
EndIf
State$igetsi+max( extit{delta}_1( extit{string}(i)), extit{delta}_2(j))$.
State extbf{goto}emph{top}.
EndProcedure
end{algorithmic}
end{algorithm}

algorithm2e例子

algorithm2e包可能會與其它包產生沖突,一個常見的錯誤提示是“Too many }'...”。為了解決這個問題,要在引入algorithm2e包之前加入下面的命令:

[plain]view plaincopy


makeatletter

ewifif@restonecol
makeatother
letalgorithmelax
letendalgorithmelax

所以前期準備:

[plain]view plaincopy


makeatletter

ewifif@restonecol
makeatother
letalgorithmelax
letendalgorithmelax
usepackage[linesnumbered,ruled,vlined]{algorithm2e}%[ruled,vlined]{
usepackage{algpseudocode}
usepackage{amsmath}
enewcommand{algorithmicrequire}{ extbf{Input:}}%UseInputintheformatofAlgorithm
enewcommand{algorithmicensure}{ extbf{Output:}}%UseOutputintheformatofAlgorithm

example 1


代碼:

[plain]view plaincopy


egin{algorithm}
caption{identifyRowContext}
KwIn{$r_i$,$Backgrd(T_i)$=${T_1,T_2,ldots,T_n}$andsimilaritythreshold$ heta_r$}
KwOut{$con(r_i)$}
$con(r_i)=Phi$;
For{$j=1;jlen;j
ei$}
{
float$maxSim=0$;
$r^{maxSim}=null$;
While{notendof$T_j$}
{
computeJaro($r_i,r_m$)($r_minT_j$);
If{$(Jaro(r_i,r_m)ge heta_r)wedge(Jaro(r_i,r_m)ger^{maxSim})$}
{
replace$r^{maxSim}$with$r_m$;
}
}
$con(r_i)=con(r_i)cup{r^{maxSim}}$;
}
return$con(r_i)$;
end{algorithm}

example 2


代碼:

[plain]view plaincopy


egin{algorithm}
caption{Servicecheckpointimagestoragenodeandroutingpathselection}
LinesNumbered
KwIn{hostserver$PM_s$that$SerImg_k$isfetchedfrom,$subnet_s$that$PM_s$belongsto,$pod_s$that$PM_s$belongsto}
KwOut{Serviceimagestorageserver$storageserver$,andtheimagetransferpath$path$}
$storageserver$=Storagenodeselection($PM_s$,$SerImg_k$,$subnet_s$,$pod_s$);
If{$storageserver$$
eq$null}
{
selectapathfrom$storageserver$to$PM_s$andassignthepathto$path$;
}

extbf{final};
extbf{return}$storageserver$and$path$;
end{algorithm}

example 3


代碼:

[plain]view plaincopy


egin{algorithm}
caption{Storagenodeselection}
LinesNumbered
KwIn{hostserver$PM_s$thatthecheckpointimage$Img$isfetchedfrom,$subnet_s$that$PM_s$belongsto,$pod_s$that$PM_s$belongsto}
KwOut{Imagestorageserver$storageserver$}

For{eachhostserver$PM_i$inthesamesubnetwith$PM_s$}
{
If{$PM_i$isnotaserviceprovidingnodeorcheckpointimagestoragenodeof$S_k$}
{
add$PM_i$to$candidateList$;
}
}
sort$candidateList$byreliabilitydesc;
init$storageserver$;
For{each$PM_k$in$candidateList$}
{

If{$SP(PM_k)$$geq$$E(SP)$of$pod_i$and$BM_k$$le$sizeof$Img$}
{
assign$PM_k$to$storageserver$;
gotofinal;
}
}
clear$candidateList$;
addallothersubnetsin$pod_s$to$netList$;
For{eachsubnet$subnet_j$in$netList$}
{
clear$candidateList$;
For{each$PM_i$in$subnet_j$}
{
If{$PM_i$isnotaserviceprovidingnodeorcheckpointimagestoragenodeof$S_k$}
{
add$PM_i$to$candidateList$;
}
}
sortallhostin$candidateList$byreliabilitydesc;
For{each$PM_k$in$candidateList$}
{

If{$SP(PM_k)$$geq$$E(SP)$of$pod_i$and$BM_k$$le$sizeof$Img$}
{
assign$PM_k$to$storageserver$;
gotofinal;
}
}
}
extbf{final};
extbf{return}$storageserver$;
end{algorithm}

example 4


代碼:

[plain]view plaincopy


egin{algorithm}
caption{Deltacheckpointimagestoragenodeandroutingpathselection}
LinesNumbered
KwIn{hostserver$PM_s$thatgeneratesthedeltacheckpointimage$DImg_{kt}$,$subnet_s$that$PM_s$belongsto,$pod_s$that$PM_s$belongsto}
KwOut{Deltaimagestorageserver$storageserver$,andtheimagetransferpath$Path$}
$storageserver$=Storagenodeselection($PM_s$,$DImg_{kt}$,$subnet_s$,$pod_s$);
If{$storageserver$$equiv$null}
{
thedeltacheckpointimageisstoredinthecentralstorageserver;
gotofinal;
}
constructweightedtopologicalgraph$graph_s$of$pod_s$;
calculatetheshortestpathfrom$storageserver$to$PM_s$in$graph_s$byusingtheDijkstraalgorithm;
extbf{final};
extbf{return}$storageserver$and$path$;
end{algorithm}

example 5

[plain]view plaincopy


documentclass[8pt,twocolumn]{ctexart}
usepackage{amssymb}
usepackage{bm}
usepackage{textcomp}%命令 extacutedbl的包,二階導符號

%Pagelengthcommandsgohereinthepreamble
setlength{oddsidemargin}{-0.25in}%Leftmarginof1in+0in=1in
setlength{ extwidth}{9in}%紙張寬度Rightmarginof8.5in-1in-6.5in=1in
setlength{ opmargin}{-.75in}%Topmarginof2in-0.75in=1in
setlength{ extheight}{9.2in}%Lowermarginof11in-9in-1in=1in
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{componentmatricescomputing}
LinesNumbered
KwIn{$mathcal{X}inmathbb{R}^{l_1 imesl_2 imescdots imesl_N},varepsilon,lambda,delta,R$}
KwOut{$A^{(j)}s$for$j=1$to$N$}
extbf{Initialize}all$A^{(j)}s$//whichcanbeseenasthe$0^{th}$rounditerations;

{$l$hspace*{-1pt} extacutedbl}$=L$//ifweneedtojudgewhether$(11)$istruethen{$l$hspace*{-1pt} extacutedbl}denotes$L|_{t-1}$;

For{each$A_{i_jr}^{{j}}(1lejleN,1lei_jleI_j,1lerleR)$}
{//$1^{st}$rounditerations;
$g_{i_jr}^{(j)'}=g_{i_jr}^{(j)}$;
$A_{i_jr}^{(j)'}=A_{i_jr}^{(j)}$//iftherollbackshownas$(12)$isneeded,$A_{i_jr}^{(j)'}$denotes$A_{i_jr}^{(j)}|_{t-1}$;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-mathrm{{fsign}}left(g_{i_jr}^{(j)}ight)cdotdelta_{i_jr}^{(j)}$;
}

Repeat(//otherroundsofiterationsforcomputingcomponentmatrices){$m{Llevarepsilon}$ormaximumiterationsexhausted}
{
$l'=L$//ifweneedtojudgewhether$(11)$istruethen$l'$denotes$L|_t$;
For{each$A_{i_jr}^{{j}}(1lejleN,1lei_jleI_j,1lerleR)$}
{
If{$g_{i_jr}^{(j)}cdotg_{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}left(delta_{i_jr}^{(j)}cdoteta^{+},Max\_Step\_Sizeight)$;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-mathrm{{fsign}}left(g_{i_jr}^{(j)}ight)cdotdelta_{i_jr}^{(j)}$;
}
ElseIf{$g_{i_jr}^{(j)}cdotg_{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)$istruethenrollbackas$(12)$;
$delta_{i_jr}^{(j)}=m{max}left(delta_{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}left(delta_{i_jr}^{(j)}cdoteta^{-},Min\_Step\_Sizeight)$;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-mathrm{{fsign}}left(g_{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{{fsign}}left(g_{i_jr}^{(j)}ight)cdotdelta_{i_jr}^{(j)}$;
}
}
$l$hspace*{-1pt} extacutedbl$=l'$;
}
end{algorithm}
end{document}

example 6

[plain]view plaincopy


usepackage[ruled,linesnumbered]{algorithm2e}
usepackage{amsmath}

egin{algorithm}
caption{LearningalgorithmofR2P}
label{alg:r2p}
KwIn{ratings$R$,jointdemographicrepresentations$Y$,learningrate$eta$,maximumiterativenumber$maxIter$,negativesamplingnumber$k$;}
KwOut{interactionmatrix$m{W}$,movievectors$V$;}
Initialize$m{W},V$randomly;
$t=0$;
Forconvenience,define$vec{varphi}_n=sum_{minS_n}r_{m,n}vec{v}_m$;%varphi_nm{W}vec{y}_n
While{notconvergedm{or}$t>maxIter$}
{
t=t+1;
For{$n=1;nleN;n++$}
{
$m{W}=m{W}+etaig(1-sigmaleft(vec{varphi}_n^Tm{W}vec{y}_night)ig)vec{varphi}_nvec{y}_n^T$;label{algline:W}
For{$minS_n$}
{
$vec{v}_m=vec{v}_m+etaleft(1-sigmaleft(vec{varphi}_n^Tm{W}vec{y}_night)ight)r_{m,n}m{W}vec{y}_n$;label{algline:V}
}
For{$i=1;ilek;i++$}
{
samplenegativesample$vec{y}_i$from$P_n$;
$m{W}=m{W}-etaig(1-sigmaleft(-vec{varphi}_n^Tm{W}vec{y}_night)ig)vec{varphi}_nvec{y}_i^T$;
For{$minS_n$}
{
$vec{v}_m=vec{v}_m-etaleft(1-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-2lambdaetaV$
}
return$m{W},V$;
%end{algorithmic}
end{algorithm}


總結

以上是生活随笔為你收集整理的Latex算法伪代码使用总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。