Latex 算法模版整理

1, 模版1

\documentclass{article}
\usepackage[ruled]{algorithm2e}                  %style 1
%\usepackage[ruled,vlined]{algorithm2e}          %style 2
%\usepackage[linesnumbered,boxed]{algorithm2e}   %style 3
\title{Jacobi Method on Hermite Matrix}
\begin{document}
\maketitle
\begin{algorithm}[H]
%\SetAlgoNoLine
        \caption{One loop of Jacobi method on Hermite matrix (upper)}
        \KwIn{A, n} $//A \in H_{n \times n}, \ let\ a_{ij} = A(i,j), i<j$\\
        
        \If{$(a_{ij} \neq 0.0)$}{
            $t = 2|a_{ij}|sign(a_{ii} - a_{jj})/(|a_{ii}-a{jj}|+\sqrt{|a_{ii}-a_{jj}|^2+4|a_{ij}|^2});$\\
            $c = 1/\sqrt{1+t^2};$\\
            $s = t/\sqrt{1+t^2};$\\
            $s_p = s*a_{ij}/|a_{ij}|;$\\
            $s_n = s*|a_{ij}|/a_{ij};$\\
            $a_{ii} = a_{ii} + t|a_{ij}|;$\\
            $a_{jj} = a_{jj} - t|a_{ij}|;$\\
            $a_{ij} = 0.0;$\\
            $a_{ji} = 0.0;$\\

            \For{$(r=1; r<=i-1; r++)$}
            {
                $x = c*a_{ri} + s_n*a_{rj};$\\
                $a_{rj} = -s_p*a_{ri} + c*a_{rj};$\\
                $a_{ri} = x;$\\
            }
            \For{$(r=i+1; r<=j-1; r++)$}
            {
                $x = c*a_{ir} + s_p*\overline{a}_{rj};$\\
                $a_{rj} = -s_p*\overline{a}_{ir} + c*a_{rj};$\\
                $a_{ir} = x;$\\
            }
            \For{$(r=j+1; r<=n; r++)$}
            {
                $x = c*a_{ir} + s_p*a_{jr};$\\
                $a_{jr} = -s_n*a_{ir} + c*a_{jr};$\\
                $a_{ir} = x;$\\
            }
        }

\end{algorithm} 
\end{document}

效果:

2,样式2

 

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\title{Euclid's Algorithm: An example of how to write algorithms in \LaTeX{}}

\author{write\LaTeX{}}

\date{\today}

\begin{document}
\maketitle

\section{Example Algorithm}

Algorithms can be included using the commands as shown in algorithm \ref{alg:euclid}.

\begin{algorithm}
\caption{Euclid’s algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

3,样式3

\documentclass[11pt]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{lmodern}
\usepackage{fullpage}
% for algorithm description
\usepackage{alltt}
% for algorithm description in a box
\usepackage{boxedminipage}
% for colorful comment
\usepackage{color}

\setlength{\parskip}{.2em}

\begin{document}

\title{Cycle Detection Algorithm: the Hare and the Tortoise}
\author{Frank the Giant Bunny}
\date{August 27, 2014}
\maketitle

\thispagestyle{empty}

\paragraph{Question}
Using only two pointers,
decide whether a singly-linked list $L$ contains
a loop or not.

\paragraph{Answer}
The simplest answer is to maintain two pointers:
the \texttt{hare} moving \emph{two} steps at a time and
the \texttt{tortoise} moving \emph{one} step at a time.


\noindent\begin{boxedminipage}{\textwidth}
\begin{alltt}
Hare-and-Tortoise(\(L\))
  \textcolor{red}{// Input: a linked list \(L\)}
  \textcolor{red}{// Output: `yes' if \(L\) contains a loop and `no' otherwise.}
  Two pointers \emph{hare} and \emph{tortoise} are initially located at the head of \(L\).
  Repeat
    If the \emph{hare} reaches the end of \(L\) within two steps, return `no'.
    Else
      Move the \emph{hare} two steps forward and the \emph{tortoise} one step forward.
      If two pointers are located on the same node, return `yes'.
    EndIf
  EndRepeat
\end{alltt}
\end{boxedminipage}

\paragraph{Why it works}
If the list $L$ doesn't contain a loop,
the hare will reach the end of $L$ sooner than the tortoise
does and the algorithm will eventually return `no'.
If the list $L$ contains a loop, it results in a $\rho$
shape when diagrammed.
\begin{itemize}
\item Once the hare and tortoise enter the
cycle part of the $\rho$ shape,
they are trapped in the loop and
keep rotating around the cyclic path.
\item And the hare enters the cyclic part
sooner than the tortoise does.
\item Assume that the hare and tortoise are separated in $d$
steps when the tortoise enters the cycle.
\item As the tortoise moves only one step while the hare
moves two steps, their distance in the cyclic path keeps decreasing by one for each iteration of the \texttt{Repeat} block of the algorithm.
Hence, two pointers will eventually collide
and the algorithm returns `yes'.
\end{itemize}

\paragraph{Ask yourself}
What if the hare moves \emph{three} steps forward at a time while 
the tortoise moves \emph{one} step at a time?
What about other steps? Does the algorithm still work?
\end{document}

4,样式4

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}
\renewcommand{\algorithmicrequire}{ \textbf{Input:}} %Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{ \textbf{Output:}} %UseOutput in the format of Algorithm
% 参考:https://blog.csdn.net/jzwong/article/details/52399112
\begin{document}
% 例1
\begin{algorithm}[htb]
\caption{ Framework of ensemble learning for our system.}
\label{alg:Framwork}
\begin{algorithmic}[1] %这个1 表示每一行都显示数字
\REQUIRE ~~\\ %算法的输入参数:Input
    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 ~~\\ %算法的输出:Output
    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$;
    \STATE Training ensemble of classifiers $E$ on $T_n \cup P_n$, with help of data in former batches;
    \STATE $E_n=E_{n-1}\cup E$;
    \STATE Classifying samples in $U_n-T_n$ by $E_n$;
    \STATE Deleting some weak classifiers in $E_n$ so as to keep the capacity of $E_n$;
\RETURN $E_n$; %算法的返回值
\end{algorithmic}
\end{algorithm}
 
% 例2
\begin{algorithm}
    \caption{An example}
    \label{alg:2}
    \begin{algorithmic}
        \STATE {set $r(t)=x(t)$}
        \REPEAT
        \STATE set $h(t)=r(t)$
        \REPEAT
        \STATE set $h(t)=r(t)$
        \UNTIL{B}
        \UNTIL{B}
    \end{algorithmic}
\end{algorithm}
% 例3
\begin{algorithm}
    \caption{Calculate $y = x^n$}
    \label{alg:3}
    \begin{algorithmic}
        \REQUIRE $n \geq 0 \vee x \neq 0$
        \ENSURE $y = x^n$
        \STATE $y \Leftarrow 1$
    \IF{$n < 0$}
        \STATE $X \Leftarrow 1 / x$
        \STATE $N \Leftarrow -n$
    \ELSE
        \STATE $X \Leftarrow x$
        \STATE $N \Leftarrow n$
    \ENDIF
    \WHILE{$N \neq 0$}
        \IF{$N$ is even}
            \STATE $X \Leftarrow X \times X$
            \STATE $N \Leftarrow N / 2$
        \ELSE[$N$ is odd]
            \STATE $y \Leftarrow y \times X$
            \STATE $N \Leftarrow N - 1$
        \ENDIF
    \ENDWHILE
    \end{algorithmic}
\end{algorithm}
% 例4
\begin{algorithm}[h]
    \caption{An example for format For \& While Loop in Algorithm}
    \label{alg:4}
    \begin{algorithmic}[1]
        \FOR{each $i \in [1,9]$}
            \STATE initialize a tree $T_{i}$ with only a leaf (the root);\
            \STATE $T=T \cup T_{i};$\
        \ENDFOR
        \FORALL {$c$ such that $c \in RecentMBatch(E_{n-1})$}
            \STATE $T=T \cup PosSample(c)$;
        \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.
        \WHILE {$(|E_n| \leq L_1 )and( D \neq \phi)$}
            \STATE Selecting the most recent classifier $c_i$ from $D$;
            \STATE $D=D-c_i$;
            \STATE $E_n=E_n+c_i$;
        \ENDWHILE
    \end{algorithmic}
\end{algorithm}
\end{document}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值