Kembali ke semua templat

Pseudocode Algoritma

Lihat PDF
Kategori:Math
Lisensi:Gratis digunakan (MIT)
Abstrak:

Dokumentasi algoritma dengan pseudocode

Tag:Math
Loading preview...

An algorithm pseudocode template in LaTeX

This algorithm pseudocode template in LaTeX is a short document for presenting one algorithm properly: a numbered pseudocode listing, a complexity analysis and a worked example. It is built on the standard article class at 12 pt with 1 inch margins and uses the two packages most papers and theses rely on for pseudocode: algorithm, which provides the captioned, floating Algorithm box, and algpseudocode from the algorithmicx bundle, which provides the commands inside it.

The sample procedure shows the constructs you will use most: \Procedure with an argument, \State assignments with \gets, a \For loop, an \If / \Else branch and \Return, with line numbers switched on by \begin{algorithmic}[1]. After the listing come Time and Space Complexity bullets in big-O notation and an Input / Output example in typewriter type. The preamble also loads listings and xcolor, which the sample does not use yet, so you can add real source code next to the pseudocode without touching the preamble. The same snippets drop into a thesis, lab report or course assignment that needs an algorithm block.

What is included

  • \documentclass[12pt]{article} with a title block for the algorithm name
  • An algorithm float with an algpseudocode procedure and line numbers
  • Complexity Analysis subsection with time and space bounds
  • Example subsection with input and output
  • amsmath, amssymb, listings and xcolor preloaded

How to use it

  1. Click Open as Template to edit in Octree, or View Source and paste the code into Overleaf or a local algorithm.tex.
  2. Compile with pdfLaTeX; no bibliography or extra files are needed.
  3. Replace [Algorithm Name] in \title, the \caption and the \Procedure{AlgorithmName}{$input$} line.
  4. Rewrite the body of algorithmic line by line, closing every block with its matching \EndFor, \EndIf or \EndProcedure.
  5. Update the complexity bounds and the example to match.

Customise the template

Add Input/Output lines, a while loop and comments. These are all algpseudocode commands; the two \algrenewcommand lines go in the preamble:

\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}

\begin{algorithm}
\caption{Binary search}
\label{alg:bsearch}
\begin{algorithmic}[1]
\Require sorted array $A[1..n]$, target $x$
\Ensure index of $x$, or $0$ if absent
\State $lo \gets 1$, $hi \gets n$
\While{$lo \le hi$} \Comment{search interval is non-empty}
    \State $mid \gets \lfloor (lo + hi)/2 \rfloor$
    \If{$A[mid] = x$}
        \State \Return $mid$
    \ElsIf{$A[mid] < x$}
        \State $lo \gets mid + 1$
    \Else
        \State $hi \gets mid - 1$
    \EndIf
\EndWhile
\State \Return $0$
\end{algorithmic}
\end{algorithm}
Algorithm~\ref{alg:bsearch} runs in $O(\log n)$ time.

Show the real implementation with listings. The package is already loaded; this sets a readable style once and typesets a Python version of the sample procedure:

\lstset{basicstyle=\ttfamily\small, keywordstyle=\color{blue},
  commentstyle=\color{gray}, numbers=left, numberstyle=\tiny,
  frame=single, breaklines=true}

\begin{lstlisting}[language=Python, caption={Python implementation}]
def algorithm_name(values, condition):
    result = 0
    for v in values:
        result += v if condition(v) else -v  # the If/Else branch
    return result
\end{lstlisting}

Rename the float. To call the boxes Procedure instead of Algorithm, put \floatname{algorithm}{Procedure} in the preamble; references made with \ref keep working.

Related templates: technical report, lab report and thesis.

Algorithm template FAQ

Which pseudocode package does this template use?
algpseudocode, the most common layout of the algorithmicx bundle, inside the float from the algorithm package. Its commands are capitalised words such as \State, \If, \For, \While, \Procedure and \Return. The older algorithmic package uses upper-case commands (\STATE, \IF) instead; do not load both, because they define the same algorithmic environment.
How do I write Input and Output lines?
Use \Require and \Ensure at the top of the algorithmic environment. They print Require: and Ensure: by default; add \algrenewcommand\algorithmicrequire{\textbf{Input:}} and \algrenewcommand\algorithmicensure{\textbf{Output:}} to the preamble to relabel them.
How do I stop the algorithm from floating to another page?
The algorithm environment is a float, like figure. Load the float package and write \begin{algorithm}[H] to keep it exactly where it appears in the source, or use [t] or [p] for top of page and float page.
Does this algorithm template work in Overleaf?
Yes. algorithm, algpseudocode, listings and xcolor are all in TeX Live, so the template compiles unchanged in Overleaf, Octree or a local install with pdfLaTeX.
Math poweredTulis LaTeX menggunakan AI

Editor LaTeX AI Terbaik, Alternatif Overleaf

Octree membantu peneliti dari 15+ universitas menulis makalah penelitian lebih cepat menggunakan AI

  • Hasilkan diagram TikZ yang indah menggunakan AI
  • Unggah gambar dan konversi ke LaTeX dengan mulus
  • Dapatkan bantuan instan untuk menulis dan memformat makalah penelitian