> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/vi/chu-de-khac/02-algorithms.md).

# Thuật toán

## Giới thiệu

*(Để viết danh sách mã chương trình, vui lòng tham khảo* [*trang trợ giúp này*](/latex/vi/dinh-dang/11-code-listing.md) *thay vào đó.)*

Để dàn trang thuật toán hoặc mã giả trong LaTeX, bạn có thể dùng một trong các tùy chọn sau:

* Chọn MỘT trong các (`algpseudocode` HOẶC `algcompatible` HOẶC `algorithmic`) gói để dàn phần thân thuật toán, và gói `algorithm` để thêm chú thích cho thuật toán.
* Tính năng `algorithm2e` gói.

**Lưu ý rằng bạn chỉ nên chọn một trong các nhóm gói ở trên, và chỉ dùng các lệnh cũng như cú pháp do gói bạn chọn cung cấp.** Các gói này không thể được nạp đồng thời; nếu không bạn sẽ gặp rất nhiều lỗi.

## Các gói algpseudocode và algorithm

Tính năng `algpseudocode` gói cung cấp một `algorithmic` môi trường và một số lệnh hữu ích. Bạn có thể [mở một ví dụ đầy đủ trên Overleaf](https://www.overleaf.com/project/new/template/20763?id=71202437\&templateName=algpseudocode+%2B+algorithm+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=) , và chúng ta sẽ đi vào một số chi tiết trong phần này.

Đây là thuật toán đầu tiên của chúng ta, sử dụng các môi trường và lệnh từ `algpseudocode` gói:

```latex
\documentclass{article}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithmic}
\State $i \gets 10$
\If{$i\geq 5$}
    \State $i \gets i-1$
\Else
    \If{$i\leq 3$}
        \State $i \gets i+2$
    \EndIf
\EndIf
\end{algorithmic}

\end{document}
```

[Mở ví dụ ngắn algpseudocode này trên Overleaf](https://www.overleaf.com/docs?engine=\&snip_name=algpseudocode+short+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgpseudocode%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Balgorithmic%7D%0A%5CState+%24i+%5Cgets+10%24%0A%5CIf%7B%24i%5Cgeq+5%24%7D+%0A++++%5CState+%24i+%5Cgets+i-1%24%0A%5CElse%0A++++%5CIf%7B%24i%5Cleq+3%24%7D%0A++++++++%5CState+%24i+%5Cgets+i%2B2%24%0A++++%5CEndIf%0A%5CEndIf+%0A%5Cend%7Balgorithmic%7D%0A%0A%5Cend%7Bdocument%7D)

Đây là kết quả đầu ra:

![Algpseudocode-0.png](/files/52c4164d5759699b5586c1ca2c75b0d6c88e47d9)

*Bạn không nên nạp `algorithm2e`, `algcompatible`, `algorithmic` các gói nếu bạn đã nạp `algpseudocode`.*

Lưu ý rằng các tên lệnh do `algpseudocode` thường được viết theo kiểu chữ cái đầu viết hoa, ví dụ: `\State`, `\While`, `\EndWhile`.

Nếu bạn muốn thêm số dòng cho thuật toán, bạn có thể thêm số dòng đầu tiên vào `algorithmic` môi trường như sau: `\begin{algorithmic}[1]` và nhận được kết quả sau:

![Algpseudocode-00.png](/files/ce48dea140a9879ac9a85aa4d5781272adc836a1)

Ví dụ thuật toán ở trên không có chú thích cũng không được đánh số. Nếu bạn cần một thuật toán có chú thích, bạn cũng sẽ cần nạp gói `algorithm` và thêm

```latex
\begin{algorithm}
\caption{...}
...
\end{algorithm}
```

bao quanh `algorithmic` môi trường. Bạn có thể dùng `\label{...}` sau `\caption{...}`, để số hiệu thuật toán có thể được tham chiếu chéo bằng `\ref{...}`.

```latex
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Một thuật toán có chú thích}\label{alg:cap}
\begin{algorithmic}
\Require $n \geq 0$
\Ensure $y = x^n$
\State $y \gets 1$
\State $X \gets x$
\State $N \gets n$
\While{$N \neq 0$}
\If{$N$ is even}
    \State $X \gets X \times X$
    \State $N \gets \frac{N}{2}$  \Comment{Đây là một chú thích}
\ElsIf{$N$ is odd}
    \State $y \gets y \times X$
    \State $N \gets N - 1$
\EndIf
\EndWhile
\end{algorithmic}
\end{algorithm}

\end{document}
```

[Mở ví dụ ngắn algorithm+algpseudocode này trên Overleaf](https://www.overleaf.com/docs?engine=\&snip_name=Captioned+algorithm%2Balgpseudocode+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgorithm%7D%0A%5Cusepackage%7Balgpseudocode%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Balgorithm%7D%0A%5Ccaption%7BAn+algorithm+with+caption%7D%5Clabel%7Balg%3Acap%7D%0A%5Cbegin%7Balgorithmic%7D%0A%5CRequire+%24n+%5Cgeq+0%24%0A%5CEnsure+%24y+%3D+x%5En%24%0A%5CState+%24y+%5Cgets+1%24%0A%5CState+%24X+%5Cgets+x%24%0A%5CState+%24N+%5Cgets+n%24%0A%5CWhile%7B%24N+%5Cneq+0%24%7D%0A%5CIf%7B%24N%24+is+even%7D%0A++++%5CState+%24X+%5Cgets+X+%5Ctimes+X%24%0A++++%5CState+%24N+%5Cgets+%5Cfrac%7BN%7D%7B2%7D%24++%5CComment%7BThis+is+a+comment%7D%0A%5CElsIf%7B%24N%24+is+odd%7D%0A++++%5CState+%24y+%5Cgets+y+%5Ctimes+X%24%0A++++%5CState+%24N+%5Cgets+N+-+1%24%0A%5CEndIf%0A%5CEndWhile%0A%5Cend%7Balgorithmic%7D%0A%5Cend%7Balgorithm%7D%0A%0A%5Cend%7Bdocument%7D)

![Algpseudocode-1.png](/files/6664b65764302277b8f632e147e1aaff96b528c2)

Tính năng `algorithm` môi trường là một [float](/latex/vi/hinh-va-bang/02-positioning-images-and-tables.md) như `table` và `figure`, vì vậy bạn có thể thêm các bộ sửa vị trí nổi `[hbt!]` sau `\begin{algorithm}` nếu cần. Điều này cũng có nghĩa là trong khi một `algorithmic` môi trường dài đứng một mình có thể ngắt qua các trang, thì một `algorithm` môi trường thì không.

Tính năng `algorithm` gói cũng cung cấp một `\listofalgorithms` lệnh hoạt động giống như `\listoffigures`, nhưng dành cho các thuật toán có chú thích, như thế này.

![Algpseudocode-2.png](/files/f4eaca4ae9202c0f96be53d720f54dc4bd7b86e1)

[Mở một ví dụ đầy đủ trên Overleaf](https://www.overleaf.com/project/new/template/20763?id=71202437\&templateName=algpseudocode+%2B+algorithm+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## Các gói algcompatible/algorithmic và algorithm

Tính năng `algorithmic` gói sử dụng cú pháp tương tự như `algpseudocode`; **nhưng** tên lệnh của nó được viết hoa toàn bộ, ví dụ: `\STATE`, `\WHILE`, `\ENDWHILE`.

Mặt khác, `algcompatible` sẽ nhận diện các tên lệnh viết hoa toàn bộ và viết hoa chữ cái đầu, vì vậy `\STATE`, `\WHILE`, `\ENDWHILE`, `\State`, `\While`, `\EndWhile` đều được nhận diện. Ngoài tên lệnh ra, `algcompatible` và `algorithmic` các lệnh sử dụng cùng cú pháp đối số như `algpseudocode`.

```latex
\documentclass{article}
\usepackage{algcompatible}
% HOẶC \usepackage{algorithmic}
\begin{document}
\begin{algorithmic}
\STATE $i\gets 10$
\IF {$i\geq 5$}
  \STATE $i\gets i-1$
\ELSE
  \IF {$i\leq 3$}
    \STATE $i\gets i+2$
  \ENDIF
\ENDIF
\end{algorithmic}

\end{document}
```

[Mở ví dụ ngắn algcompatible này trên Overleaf](https://www.overleaf.com/docs?engine=\&snip_name=algcompatible\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgcompatible%7D%0A%25+OR+%5Cusepackage%7Balgorithmic%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Balgorithmic%7D%0A%5CSTATE+%24i%5Cgets+10%24%0A%5CIF+%7B%24i%5Cgeq+5%24%7D+%0A++%5CSTATE+%24i%5Cgets+i-1%24%0A%5CELSE%0A++%5CIF+%7B%24i%5Cleq+3%24%7D%0A++++%5CSTATE+%24i%5Cgets+i%2B2%24%0A++%5CENDIF%0A%5CENDIF+%0A%5Cend%7Balgorithmic%7D%0A%0A%5Cend%7Bdocument%7D)

Một số mẫu cũ hoặc lớp tài liệu có thể đã nạp `algorithmic`, vì vậy bạn sẽ phải theo cú pháp và tên lệnh được cung cấp. *Bạn không nên nạp `algorithm2e`, `algpseudocode` các gói nếu `algorithmic` hoặc `algcompatible` gói đã được nạp.*

Tính năng `algorithm` gói có thể được dùng với `algorithmic`/`algcompatible` để thêm chú thích có đánh số cho các thuật toán.

[Mở một ví dụ đầy đủ trên Overleaf](https://www.overleaf.com/project/new/template/20768?id=71235978\&templateName=algpseudocode+%2B+algorithm+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## Gói algorithm2e

Tính năng `algorithm2e` gói có cấu trúc cú pháp khá khác so với các `algpseudocode`, `algcompatible` và `algorithmic` gói, vì vậy bạn cần cẩn thận về việc muốn dùng gói nào, hoặc mẫu của bạn đã nạp gói nào.

Tính năng `algorithm2e` gói cung cấp một `algorithm` môi trường:

```latex
\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}
$i\gets 10$\;
\eIf{$i\geq 5$}
{
    $i\gets i-1$\;
}{
    \If{$i\leq 3$}
    {
        $i\gets i+2$\;
    }
}
\end{algorithm}

\end{document}
```

[Mở ví dụ ngắn algorithm2e này trên Overleaf](https://www.overleaf.com/docs?engine=\&snip_name=algorithm2e+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgorithm2e%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Balgorithm%7D%0A%24i%5Cgets+10%24%5C%3B%0A%5CeIf%7B%24i%5Cgeq+5%24%7D%0A%7B%0A++++%24i%5Cgets+i-1%24%5C%3B%0A%7D%7B%0A++++%5CIf%7B%24i%5Cleq+3%24%7D%0A++++%7B%0A++++++++%24i%5Cgets+i%2B2%24%5C%3B%0A++++%7D%0A%7D%0A%5Cend%7Balgorithm%7D%0A%0A%5Cend%7Bdocument%7D)

![Algorithm2e-0.png](/files/3d93cb7f0857260577e3d025770e6fdf4dc9d8f3)

Mỗi dòng trong mã nguồn của bạn *phải* kết thúc bằng `\;` nếu không thuật toán của bạn sẽ tiếp tục trên cùng một dòng văn bản trong kết quả đầu ra. Chỉ những dòng có một macro bắt đầu một khối mới không nên kết thúc bằng `\;`.

Khi sử dụng `algorithm2e` bạn có thể dùng `\caption{...}\ref{...}` trong chính `algorithm` môi trường này mà không cần nạp bất kỳ gói nào khác. Tuy nhiên nếu bạn muốn thêm chú thích trong thuật toán, trước tiên bạn sẽ phải khai báo tên lệnh sẽ dùng:

```latex
%% Dòng này khai báo một lệnh \Comment
%% Đối số sẽ được bao quanh bởi /* ... */
\SetKwComment{Comment}{/* }{ */}

\begin{algorithm}
\caption{Một thuật toán có chú thích}\label{alg:two}
\KwData{$n \geq 0$}
\KwResult{$y = x^n$}
$y \gets 1$\;
$X \gets x$\;
$N \gets n$\;
\While{$N \neq 0$}{
  \eIf{$N$ is even}{
    $X \gets X \times X$\;
    $N \gets \frac{N}{2}$ \Comment*[r]{Đây là một chú thích}
  }{\If{$N$ is odd}{
      $y \gets y \times X$\;
      $N \gets N - 1$\;
    }
  }
}
\end{algorithm}
```

[Mở ví dụ algorithm2e có chú thích này trên Overleaf](https://www.overleaf.com/docs?engine=\&snip_name=Captioned+algorithm2e+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgorithm2e%7D%0A%5Cbegin%7Bdocument%7D%0A%25%25+This+declares+a+command+%5CComment%0A%25%25+The+argument+will+be+surrounded+by+%2F%2A+...+%2A%2F%0A%5CSetKwComment%7BComment%7D%7B%2F%2A+%7D%7B+%2A%2F%7D%0A%0A%5Cbegin%7Balgorithm%7D%0A%5Ccaption%7BAn+algorithm+with+caption%7D%5Clabel%7Balg%3Atwo%7D%0A%5CKwData%7B%24n+%5Cgeq+0%24%7D%0A%5CKwResult%7B%24y+%3D+x%5En%24%7D%0A%24y+%5Cgets+1%24%5C%3B%0A%24X+%5Cgets+x%24%5C%3B%0A%24N+%5Cgets+n%24%5C%3B%0A%5CWhile%7B%24N+%5Cneq+0%24%7D%7B%0A++%5CeIf%7B%24N%24+is+even%7D%7B%0A++++%24X+%5Cgets+X+%5Ctimes+X%24%5C%3B%0A++++%24N+%5Cgets+%5Cfrac%7BN%7D%7B2%7D%24+%5CComment%2A%5Br%5D%7BThis+is+a+comment%7D%0A++%7D%7B%5CIf%7B%24N%24+is+odd%7D%7B%0A++++++%24y+%5Cgets+y+%5Ctimes+X%24%5C%3B%0A++++++%24N+%5Cgets+N+-+1%24%5C%3B%0A++++%7D%0A++%7D%0A%7D%0A%5Cend%7Balgorithm%7D%0A%0A%5Cend%7Bdocument%7D)

![Algorithm2e-1.png](/files/b1def7b529894768ad9793b96e419565e6ce7eda)

Theo mặc định, kiểu `plain` algorithm được sử dụng. Nhưng nếu bạn muốn có các đường bao quanh thuật toán và chú thích, bạn có thể thêm tùy chọn gói `ruled` khi nạp `algorithm2e`, hoặc viết `\RestyleAlgo{ruled}` trong tài liệu của bạn. Khi đó các thuật toán có chú thích của bạn sẽ được dàn trang như sau:

![Algorithm2e-11.png](/files/9c39fa866c7d848d6df07225c9ada1210febdb82)

Tính năng `algorithm2e` gói cung cấp nhiều tùy chọn tùy chỉnh. Ví dụ, nếu bạn muốn loại bỏ các đường dọc đánh dấu các khối while—end while, if—end if, bạn có thể thêm `noline` khi nạp `algorithm2e`, hoặc viết `\SetNoline`. Một `\listofalgorithms` lệnh cũng có sẵn trong `algorithm2e`.

[Mở một ví dụ đầy đủ trên Overleaf](https://www.overleaf.com/project/new/template/20787?id=71277562\&templateName=algpseudocode+%2B+algorithm+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## Đọc thêm

* [`algpseudocode` tài liệu gói](https://texdoc.org/pkg/algorithmicx) (xem mục 3.1)
* [`algorithmic` và `thuật toán` tài liệu gói](https://texdoc.org/pkg/algorithms)
* [`algorithm2e` tài liệu gói](https://texdoc.org/pkg/algorithm2e)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/vi/chu-de-khac/02-algorithms.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
