Latex写算法的伪代码排版_latex 算法 or and(1)
·
- \State r e s u l t ← 0 result \gets 0 result ← 0
- \If { l e f t < r i g h t left < right left < right}
- \State m i d d l e ← ( l e f t + r i g h t ) / 2 middle \gets (left + right) / 2 middle ← (left + right) / 2
- \State r e s u l t ← r e s u l t + result \gets result + result ← result + \Call{MergerSort}{ A r r a y , l e f t , m i d d l e Array, left, middle Array, left, middle}
- \State r e s u l t ← r e s u l t + result \gets result + result ← result + \Call{MergerSort}{ A r r a y , m i d d l e , r i g h t Array, middle, right Array, middle, right}
- \State r e s u l t ← r e s u l t + result \gets result + result ← result + \Call{Merger}{ A r r a y , l e f t , m i d d l e , r i g h t Array,left,middle,right Array,left,middle,right}
- \EndIf
- \State \Return{ r e s u l t result result}
- \EndFunction
- \State
- \Function{Merger}{ A r r a y , l e f t , m i d d l e , r i g h t Array, left, middle, right Array, left, middle, right}
- \State i ← l e f t i\gets left i← left
- \State j ← m i d d l e j\gets middle j← middle
- \State k ← 0 k\gets 0 k← 0
- \State r e s u l t ← 0 result \gets 0 result ← 0
- \While{ i < m i d d l e i<middle i<middle \textbf{and} j < r i g h t j<right j<right}
- \If{ A r r a y [ i ] < A r r a y [ j ] Array[i]<Array[j] Array[i]<Array[j]}
- \State B [ k + + ] ← A r r a y [ i + + ] B[k++]\gets Array[i++] B[k++]← Array[i++]
- \Else
- \State B [ k + + ] ← A r r a y [ j + + ] B[k++] \gets Array[j++] B[k++] ← Array[j++]
- \State r e s u l t ← r e s u l t + ( m i d d l e − i ) result \gets result + (middle - i) result ← result + (middle − i)
- \EndIf
- \EndWhile
- \While{ i < m i d d l e i<middle i<middle}
- \State B [ k + + ] ← A r r a y [ i + + ] B[k++] \gets Array[i++] B[k++] ← Array[i++]
- \EndWhile
- \While{ j < r i g h t j<right j<right}
- \State B [ k + + ] ← A r r a y [ j + + ] B[k++] \gets Array[j++] B[k++] ← Array[j++]
- \EndWhile
- \For{ i = 0 → k − 1 i = 0 \to k-1 i = 0 → k−1}
- \State A r r a y [ l e f t + i ] ← B [ i ] Array[left + i] \gets B[i] Array[left + i] ← B[i]
- \EndFor
- \State \Return{ r e s u l t result result}
- \EndFunction
- \end{algorithmic}
- \end{algorithm}
- \end{document}
algorithm例子
前期准备
[plain]
view plain
copy
- \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
example 1
代码:
[plain]
view plain
copy
- \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 P\_n P_n;
- The set of unlabelled samples for current batch, U _ n U\_n U_n;
- Ensemble of classifiers on former batches, E _ n − 1 E\_{n-1} E_n−1;
- \Ensure
- Ensemble of classifiers on the current batch, E _ n E\_n E_n;
- \State Extracting the set of reliable negative and/or positive samples T _ n T\_n T_n from U _ n U\_n U_n with help of P _ n P\_n P_n;
- \label{code:fram:extract}
- \State Training ensemble of classifiers E E E on T _ n ∪ P _ n T\_n \cup P\_n T_n ∪ P_n, with help of data in former batches;
- \label{code:fram:trainbase}
- \State E _ n = E _ n − 1 c u p E E\_n=E\_{n-1}cup E E_n=E_n−1cup E;
- \label{code:fram:add}
- \State Classifying samples in U _ n − T _ n U\_n-T\_n U_n−T_n by E _ n E\_n E_n;
- \label{code:fram:classify}
- \State Deleting some weak classifiers in E _ n E\_n E_n so as to keep the capacity of E _ n E\_n E_n;
- \label{code:fram:select} \
- \Return E _ n E\_n E_n;
- \end{algorithmic}
- \end{algorithm}
example 2
代码:
[plain]
view plain
copy
- \begin{algorithm}[h]
- \caption{An example for format For & While Loop in Algorithm}
- \begin{algorithmic}[1]
- \For{each i ∈ [ 1 , 9 ] i\in [1,9] i∈ [1,9]}
- \State initialize a tree T _ i T\_{i} T_i with only a leaf (the root);
- \State T = T ∪ T _ i ; T=T\cup T\_{i}; T=T∪ T_i;
- \EndFor
- \ForAll { c c c such that c ∈ R e c e n t M B a t c h ( E _ n − 1 ) c\in RecentMBatch(E\_{n-1}) c∈ RecentMBatch(E_n−1)}
- \label{code:TrainBase:getc}
- \State T = T ∪ P o s S a m p l e ( c ) T=T\cup PosSample(c) T=T∪ PosSample(c);
- \label{code:TrainBase:pos}
- \EndFor;
- \For{ i = 1 i=1 i=1; i < n i<n i<n; i + + i++ i++ }
- \State / / // // Your source here;
- \EndFor
- \For{ i = 1 i=1 i=1 to n n n}
- \State / / // // Your source here;
- \EndFor
- \State / / // // Reusing recent base classifiers.
- \label{code:recentStart}
- \While { ( ∣ E _ n ∣ ≤ L _ 1 ) a n d ( D ≠ ϕ ) (|E\_n| \leq L\_1 )and( D \neq \phi) (∣E_n∣ ≤ L_1 )and( D = ϕ)}
- \State Selecting the most recent classifier c _ i c\_i c_i from D D D;
- \State D = D − c _ i D=D-c\_i D=D−c_i;
- \State E _ n = E _ n + c _ i E\_n=E\_n+c\_i E_n=E_n+c_i;
- \EndWhile
- \label{code:recentEnd}
- \end{algorithmic}
- \end{algorithm}
example 3
代码:
[plain]
view plain
copy
- \begin{algorithm}[h]
- \caption{Conjugate Gradient Algorithm with Dynamic Step-Size Control}
- \label{alg::conjugateGradient}
- \begin{algorithmic}[1]
- \Require
- f ( x ) f(x) f(x): objective funtion;
- x _ 0 x\_0 x_0: initial solution;
- s s s: step size;
- \Ensure
- optimal KaTeX parse error: Undefined control sequence: \* at position 4: x^{\̲*̲}
- \State initial g _ 0 = 0 g\_0=0 g_0=0 and d _ 0 = 0 d\_0=0 d_0=0;
- \Repeat
- \State compute gradient directions g _ k = ▽ f ( x _ k ) g\_k=\bigtriangledown f(x\_k) g_k=▽ f(x_k);
- \State compute Polak-Ribiere parameter β _ k = g _ k T ( g _ k − g _ k − 1 ) ∥ g _ k − 1 ∥ 2 \beta\_k=\frac{g\_k^{T}(g\_k-g\_{k-1})}{\parallel g\_{k-1} \parallel^{2}} β_k=∥ g_k−1 ∥2g_kT(g_k−g_k−1);
- \State compute the conjugate directions d _ k = − g _ k + β _ k d _ k − 1 d\_k=-g\_k+\beta\_k d\_{k-1} d_k=−g_k+β_k d_k−1;
- \State compute the step size α _ k = s / ∥ d _ k ∥ _ 2 \alpha\_k=s/\parallel d\_k \parallel\_{2} α_k=s/∥ d_k ∥_2;
- \Until{( f ( x _ k ) > f ( x _ k − 1 ) f(x\_k)>f(x\_{k-1}) f(x_k)>f(x_k−1))}
- \end{algorithmic}
- \end{algorithm}
example 4
代码:
[plain]
view plain
copy
- \makeatletter
- \def\BState{\State\hskip-\ALG@thistlm}
- \makeatother
- \begin{algorithm}
- \caption{My algorithm}\label{euclid}
- \begin{algorithmic}[1]
- \Procedure{MyProcedure}{}
- \State stringlen ← length of string \textit{stringlen} \gets \text{length of }\textit{string} stringlen ← length of string
- \State i ← patlen i \gets \textit{patlen} i ← patlen
- \BState \emph{top}:
- \If { i > stringlen i > \textit{stringlen} i > stringlen} \Return false
- \EndIf
- \State j ← patlen j \gets \textit{patlen} j ← patlen
- \BState \emph{loop}:
- \If { string ( i ) = path ( j ) \textit{string}(i) = \textit{path}(j) string(i) = path(j)}
- \State j ← j − 1 j \gets j-1 j ← j−1.
- \State i ← i − 1 i \gets i-1 i ← i−1.
- \State \textbf{goto} \emph{loop}.
- \State \textbf{close};
- \EndIf
- \State i ← i + max ( delta _ 1 ( string ( i ) ) , delta _ 2 ( j ) ) i \gets i+\max(\textit{delta}\_1(\textit{string}(i)),\textit{delta}\_2(j)) i ← i+max(delta_1(string(i)),delta_2(j)).
- \State \textbf{goto} \emph{top}.
- \EndProcedure
- \end{algorithmic}
- \end{algorithm}
algorithm2e例子
algorithm2e包可能会与其它包产生冲突,一个常见的错误提示是“Too many }'…”。为了解决这个问题,要在引入algorithm2e包之前加入下面的命令:
[plain]
view plain
copy
- \makeatletter
- \newif\if@restonecol
- \makeatother
- \let\algorithm\relax
- \let\endalgorithm\relax
所以前期准备:
[plain]
view plain
copy
- \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
example 1
代码:
[plain]
view plain
copy
- \begin{algorithm}
- \caption{identify Row Context}
- \KwIn{ r _ i r\_i r_i, B a c k g r d ( T _ i ) Backgrd(T\_i) Backgrd(T_i)= T _ 1 , T _ 2 , … , T _ n {T\_1,T\_2,\ldots ,T\_n} T_1,T_2,… ,T_n and similarity threshold θ _ r \theta\_r θ_r}
- \KwOut{ c o n ( r _ i ) con(r\_i) con(r_i)}
- c o n ( r _ i ) = Φ con(r\_i)= \Phi con(r_i)= Φ;
- \For{ j = 1 ; j ≤ n ; j ≠ i j=1;j \le n;j \ne i j=1;j ≤ n;j = i}
- {
- float m a x S i m = 0 maxSim=0 maxSim=0;
- r m a x S i m = n u l l r^{maxSim}=null rmaxSim=null;
- \While{not end of T _ j T\_j T_j}
- {
- compute Jaro( r _ i , r _ m r\_i,r\_m r_i,r_m)( r _ m ∈ T _ j r\_m\in T\_j r_m∈ T_j);
- \If{ ( J a r o ( r _ i , r _ m ) ≥ θ _ r ) ∧ ( J a r o ( r _ i , r _ m ) ≥ r m a x S i m ) (Jaro(r\_i,r\_m) \ge \theta\_r)\wedge (Jaro(r\_i,r\_m)\ge r^{maxSim}) (Jaro(r_i,r_m) ≥ θ_r)∧ (Jaro(r_i,r_m)≥ rmaxSim)}
- {
- replace r m a x S i m r^{maxSim} rmaxSim with r _ m r\_m r_m;
- }
- }
- c o n ( r _ i ) = c o n ( r _ i ) ∪ r m a x S i m con(r\_i)=con(r\_i)\cup {r^{maxSim}} con(r_i)=con(r_i)∪ rmaxSim;
- }
- return c o n ( r _ i ) con(r\_i) con(r_i);
- \end{algorithm}
example 2
代码:
[plain]
view plain
copy
- \begin{algorithm}
- \caption{Service checkpoint image storage node and routing path selection}
- \LinesNumbered
- \KwIn{host server P M _ s PM\_s PM_s that S e r I m g _ k SerImg\_k SerImg_k is fetched from, s u b n e t _ s subnet\_s subnet_s that P M _ s PM\_s PM_s belongs to, p o d _ s pod\_s pod_s that P M _ s PM\_s PM_s belongs to}
- \KwOut{Service image storage server s t o r a g e s e r v e r storageserver storageserver,and the image transfer path p a t h path path}
- s t o r a g e s e r v e r storageserver storageserver = Storage node selection( P M _ s PM\_s PM_s, S e r I m g _ k SerImg\_k SerImg_k, s u b n e t _ s subnet\_s subnet_s, p o d _ s pod\_s pod_s);
- \If{ s t o r a g e s e r v e r storageserver storageserver ≠ \neq = null}
- {
- select a path from s t o r a g e s e r v e r storageserver storageserver to P M _ s PM\_s PM_s and assign the path to p a t h path path;
- }
- \textbf{final} ;
- \textbf{return} s t o r a g e s e r v e r storageserver storageserver and p a t h path path;
- \end{algorithm}
example 3
代码:
[plain]
view plain
copy
- \begin{algorithm}
- \caption{Storage node selection}
- \LinesNumbered
- \KwIn{host server P M _ s PM\_s PM_s that the checkpoint image I m g Img Img is fetched from, s u b n e t _ s subnet\_s subnet_s that P M _ s PM\_s PM_s belongs to, p o d _ s pod\_s pod_s that P M _ s PM\_s PM_s belongs to}
- \KwOut{Image storage server s t o r a g e s e r v e r storageserver storageserver}
- \For{ each host server P M _ i PM\_i PM_i in the same subnet with P M _ s PM\_s PM_s }
- {
- \If{ P M _ i PM\_i PM_i is not a service providing node or checkpoint image storage node of S _ k S\_k S_k }
- {
- add P M _ i PM\_i PM_i to c a n d i d a t e L i s t candidateList candidateList ;
- }
- }
- sort c a n d i d a t e L i s t candidateList candidateList by reliability desc;
- init s t o r a g e s e r v e r storageserver storageserver ;
- \For{ each P M _ k PM\_k PM_k in c a n d i d a t e L i s t candidateList candidateList}
- {
- \If{ S P ( P M _ k ) SP(PM\_k) SP(PM_k) ≥ \geq ≥ E ( S P ) E(SP) E(SP) of p o d _ i pod\_i pod_i and B M _ k BM\_k BM_k ≤ \le ≤ size of I m g Img Img }
- {
- assign P M _ k PM\_k PM_k to s t o r a g e s e r v e r storageserver storageserver;
- goto final;
- }
- }
- clear c a n d i d a t e L i s t candidateList candidateList;
- add all other subnets in p o d _ s pod\_s pod_s to n e t L i s t netList netList;
- \For{ each subnet s u b n e t _ j subnet\_j subnet_j in n e t L i s t netList netList}
- {
- clear c a n d i d a t e L i s t candidateList candidateList;
- \For {each P M _ i PM\_i PM_i in s u b n e t _ j subnet\_j subnet_j }
- {
- \If{ P M _ i PM\_i PM_i is not a service providing node or checkpoint image storage node of S _ k S\_k S_k }
- {
- add P M _ i PM\_i PM_i to c a n d i d a t e L i s t candidateList candidateList;
- }
- }
- sort all host in c a n d i d a t e L i s t candidateList candidateList by reliability desc;
- \For{ each P M _ k PM\_k PM_k in c a n d i d a t e L i s t candidateList candidateList}
- {
- \If{ S P ( P M _ k ) SP(PM\_k) SP(PM_k) ≥ \geq ≥ E ( S P ) E(SP) E(SP) of p o d _ i pod\_i pod_i and B M _ k BM\_k BM_k ≤ \le ≤ size of I m g Img Img}
- {
- assign P M _ k PM\_k PM_k to s t o r a g e s e r v e r storageserver storageserver ;
- goto final;
- }
- }
- }
- \textbf{final} ;
- \textbf{return} s t o r a g e s e r v e r storageserver storageserver;
- \end{algorithm}
example 4
代码:
[plain]
view plain
copy
- \begin{algorithm}
- \caption{Delta checkpoint image storage node and routing path selection}
- \LinesNumbered
- \KwIn{host server P M _ s PM\_s PM_s that generates the delta checkpoint image D I m g _ k t DImg\_{kt} DImg_kt, s u b n e t _ s subnet\_s subnet_s that P M _ s PM\_s PM_s belongs to, p o d _ s pod\_s pod_s that P M _ s PM\_s PM_s belongs to}
- \KwOut{Delta image storage server s t o r a g e s e r v e r storageserver storageserver,and the image transfer path P a t h Path Path}
- s t o r a g e s e r v e r storageserver storageserver = Storage node selection( P M _ s PM\_s PM_s, D I m g _ k t DImg\_{kt} DImg_kt, s u b n e t _ s subnet\_s subnet_s, p o d _ s pod\_s pod_s);
- \If{ s t o r a g e s e r v e r storageserver storageserver ≡ \equiv ≡ null}
- {
- the delta checkpoint image is stored in the central storage server;
- goto final;
- }
- construct weighted topological graph g r a p h _ s graph\_s graph_s of p o d _ s pod\_s pod_s;
- calculate the shortest path from s t o r a g e s e r v e r storageserver storageserver to P M _ s PM\_s PM_s in g r a p h _ s graph\_s graph_s by using the Dijkstra algorithm;
- \textbf{final} ;
- \textbf{return} s t o r a g e s e r v e r storageserver storageserver and p a t h path path;
- \end{algorithm}
example 5
[plain]
view plain
copy
- \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{ X ∈ R l _ 1 × l _ 2 × ⋯ × l _ N , ε , λ , δ , R \mathcal{X}\in\mathbb{R}^{l\_1\times l\_2\times\cdots\times l\_N},\varepsilon,\lambda,\delta,R X∈Rl_1× l_2×⋯× l_N,ε,λ,δ,R}
- \KwOut{ A ( j ) s A^{(j)}s A(j)s for j = 1 j=1 j=1 to N N N}
- \textbf{Initialize} all A ( j ) s A^{(j)}s A(j)s //which can be seen as the 0 t h 0^{th} 0th round iterations;
- { l l l\hspace*{-1pt}\textacutedbl} = L =L =L //if we need to judge whether ( 11 ) (11) (11) is true then { l l l\hspace*{-1pt}\textacutedbl} denotes L ∣ _ t − 1 L|\_{t-1} L∣_t−1;
- \For{ each A _ i _ j r j ( 1 ≤ j ≤ N , 1 ≤ i _ j ≤ I _ j , 1 ≤ r ≤ R ) A\_{i\_jr}^{{j}}(1\le j\le N,1\le i\_j\le I\_j,1\le r\le R) A_i_jrj(1≤ j≤ N,1≤ i_j≤ I_j,1≤ r≤ R) }
\renewcommand{\algorithmicensure}{\textbf{Output:}} - \begin{document}
- \begin{algorithm}
- \caption{component matrices computing}
- \LinesNumbered
- \KwIn{ X ∈ R l _ 1 × l _ 2 × ⋯ × l _ N , ε , λ , δ , R \mathcal{X}\in\mathbb{R}^{l\_1\times l\_2\times\cdots\times l\_N},\varepsilon,\lambda,\delta,R X∈Rl_1× l_2×⋯× l_N,ε,λ,δ,R}
- \KwOut{ A ( j ) s A^{(j)}s A(j)s for j = 1 j=1 j=1 to N N N}
- \textbf{Initialize} all A ( j ) s A^{(j)}s A(j)s //which can be seen as the 0 t h 0^{th} 0th round iterations;
- { l l l\hspace*{-1pt}\textacutedbl} = L =L =L //if we need to judge whether ( 11 ) (11) (11) is true then { l l l\hspace*{-1pt}\textacutedbl} denotes L ∣ _ t − 1 L|\_{t-1} L∣_t−1;
- \For{ each A _ i _ j r j ( 1 ≤ j ≤ N , 1 ≤ i _ j ≤ I _ j , 1 ≤ r ≤ R ) A\_{i\_jr}^{{j}}(1\le j\le N,1\le i\_j\le I\_j,1\le r\le R) A_i_jrj(1≤ j≤ N,1≤ i_j≤ I_j,1≤ r≤ R) }
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


























所有评论(0)