7
algorithms.sty - biểu diễn thuật toán Nguyễn Hữu Điển Khoa Toán - Cơ - Tin học ĐHKHTN Hà Nội, ĐHQGHN 1 Giới thiệu gói lệnh Gói lệnh thể hiện các thuật toán, có tại địa chỉ http://tug.ctan.org/tex-archive/macros/latex/contrib/algorithms/ Tác giả của gói lệnh là Rogério Brito. 2 Cách sử dụng 2.1 Môi trường algorithmic 1. Đây là môi trường chính chưa các lệnh điều khiển của thuật toán. Ví dụ: \begin{algorithmic} \STATE $S \leftarrow 0$ \end{algorithmic} Kết quả: S 0 2. Đánh số các dòng lệnh \begin{algorithmic}[1] \FORALL{$i$ such that $0\leq i\leq 10$} \STATE carry out some processing \ENDFOR \end{algorithmic} Kết quả: 1: for all i such that 0 i 10 do 2: carry out some processing 3: end for 2.2 Các loại lệnh trong môi trường 1. Lệnh đơn giản \STATE <text> ví dụ: \begin{algorithmic} \STATE $S \leftarrow 0$ \end{algorithmic}

Algorithms.sty Biểu Diễn Thuật Toán

Embed Size (px)

DESCRIPTION

sfdsfdf

Citation preview

  • algorithms.sty - biu din thut ton

    Nguyn Hu inKhoa Ton - C - Tin hc

    HKHTN H Ni, HQGHN

    1 Gii thiu gi lnh

    Gi lnh th hin cc thut ton, c ti a ch

    http://tug.ctan.org/tex-archive/macros/latex/contrib/algorithms/

    Tc gi ca gi lnh l Rogrio Brito.

    2 Cch s dng

    2.1 Mi trng algorithmic

    1. y l mi trng chnh cha cc lnh iu khin ca thut ton.

    V d:

    \begin{algorithmic}

    \STATE $S \leftarrow 0$

    \end{algorithmic}

    Kt qu:

    S 0

    2. nh s cc dng lnh

    \begin{algorithmic}[1]

    \FORALL{$i$ such that $0\leq i\leq 10$}

    \STATE carry out some processing

    \ENDFOR

    \end{algorithmic}

    Kt qu:

    1: for all i such that 0 i 10 do2: carry out some processing3: end for

    2.2 Cc loi lnh trong mi trng

    1. Lnh n gin \STATE v d:

    \begin{algorithmic}

    \STATE $S \leftarrow 0$

    \end{algorithmic}

  • Kt qu

    S 0

    2. Lnh if-then-else

    \IF{} \ENDIF

    \IF{} \ELSE \ENDIF

    \IF{} \ELSIF{} \ELSE \ENDIF

    V d:

    \begin{algorithmic}

    \IF{C1}

    \STATE B1

    \ELSIF {C2}

    \STATE B2

    \ENDIF

    \end{algorithmic}

    Kt qu:

    if C1 thenB1

    else if C2 thenB2

    end if

    3. Lnh for

    \FOR{} \ENDFOR

    \FORALL{} \ENDFOR

    V d:

    \begin{algorithmic}[1]

    \FORALL{$i$ such that $0\leq i\leq 10$}

    \STATE carry out some processing

    \ENDFOR

    \end{algorithmic}

    Kt qu:

    1: for all i such that 0 i 10 do2: carry out some processing3: end for

    C thm TO

    \begin{algorithmic}

    \FOR{$i=0$ \TO $10$}

    \STATE carry out some processing

    \ENDFOR

    \end{algorithmic}

    for i = 0 to 10 docarry out some processing

    end for

    4. Vng lp while

  • \WHILE{} \ENDWHILE

    V d

    \begin{algorithmic}

    \WHILE{some condition holds}

    \STATE carry out some processing

    \ENDWHILE

    \end{algorithmic}

    Kt qu:

    while some condition holds docarry out some processing

    end while

    5.Vng lp repeat-until

    \REPEAT \UNTIL{}

    V d

    \begin{algorithmic}

    \REPEAT

    \STATE carry out some processing

    \UNTIL{some condition is met}

    \end{algorithmic}

    Kt qu:

    repeat

    carry out some processinguntil some condition is met

    6. Lp v hn

    \LOOP \ENDLOOP

    V d:

    \begin{algorithmic}

    \LOOP

    \STATE this processing will be repeated forever

    \ENDLOOP

    \end{algorithmic}

    loop

    this processing will be repeated foreverend loop

    7. Lnh logic

    \AND

    \OR

    \XOR

    \NOT

    V d:

  • \begin{algorithmic}

    \IF{\NOT ($year \bmod 400$ \XOR $year \bmod 100$ \XOR $year \bmod 4$)}

    \STATE $year$ does not represent a leap year.

    \ENDIF

    \end{algorithmic}

    if not (year mod 400 xor year mod 100 xor year mod 4) thenyear does not represent a leap year.

    end if

    8. Lnh tr v gi tr v gi tr ng sai

    \RETURN

    V d:

    \begin{algorithmic}

    \IF{$n$ is odd}

    \RETURN \TRUE

    \ELSE

    \RETURN \FALSE

    \ENDIF

    \end{algorithmic}

    if n is odd thenreturn true

    else

    return false

    end if

    9. Cc lnh khc

    \PRINT

    \begin{algorithmic}

    \PRINT \texttt{Hello, World!}

    \end{algorithmic}

    print Hello, World!

    \COMMENT{}

    \begin{algorithmic}

    \STATE do something \COMMENT{this is a comment}

    \end{algorithmic}

    do something {this is a comment}

    10. V d tng hp

    \begin{algorithmic}

    \REQUIRE $n \geq 0$

    \ENSURE $y = x^n$

    \STATE $y \leftarrow 1$

    \STATE $X \leftarrow x$

    \STATE $N \leftarrow n$

    \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}

    Require: n 0Ensure: y = xn

    y 1X xN nwhile N 6= 0 do

    if N is even thenX X XN N/2

    else {N is odd}y y XN N 1

    end if

    end while

    2.3 Ty chn v t li cu hnh

    1. Lnh \algsetup{indent=lenght} t tht hng lnh

    \algsetup{indent=2em}

    \begin{algorithmic}[1]

    \STATE $a \leftarrow 1$

    \IF{$a$ is even}

    \PRINT $a$ is even

    \ELSE

    \PRINT $a$ is odd

    \ENDIF

    \end{algorithmic}

    Kt qu:

    a 1if a is even then

    print a is evenelse

    print a is oddend if

    2. t li nh s v du chm

    \algsetup{

    linenosize=\small,

    linenodelimiter=.

    }

    \begin{algorithmic}[1]

    \STATE $i \leftarrow 10$

    \RETURN $i$

    \end{algorithmic}

    1. i 10

  • 2. return i

    3. C th t li ccc lnh trn

    \newcommand{\algorithmicrequire}{\textbf{Require:}}

    \newcommand{\algorithmicensure}{\textbf{Ensure:}}

    \newcommand{\algorithmicend}{\textbf{end}}

    \newcommand{\algorithmicif}{\textbf{if}}

    \newcommand{\algorithmicthen}{\textbf{then}}

    \newcommand{\algorithmicelse}{\textbf{else}}

    \newcommand{\algorithmicelsif}{\algorithmicelse\ \algorithmicif}

    \newcommand{\algorithmicendif}{\algorithmicend\ \algorithmicif}

    \newcommand{\algorithmicfor}{\textbf{for}}

    \newcommand{\algorithmicforall}{\textbf{for all}}

    \newcommand{\algorithmicdo}{\textbf{do}}

    \newcommand{\algorithmicendfor}{\algorithmicend\ \algorithmicfor}

    \newcommand{\algorithmicwhile}{\textbf{while}}

    \newcommand{\algorithmicendwhile}{\algorithmicend\ \algorithmicwhile}

    \newcommand{\algorithmicloop}{\textbf{loop}}

    \newcommand{\algorithmicendloop}{\algorithmicend\ \algorithmicloop}

    \newcommand{\algorithmicrepeat}{\textbf{repeat}}

    \newcommand{\algorithmicuntil}{\textbf{until}}

    \newcommand{\algorithmicprint}{\textbf{print}}

    \newcommand{\algorithmicreturn}{\textbf{return}}

    \newcommand{\algorithmictrue}{\textbf{true}}

    \newcommand{\algorithmicfalse}{\textbf{false}}

    3 Mi trng algorithms

    Mi trng ny dng nh s v nhn cho thut ton

    \begin{algorithm}

    \caption{Calculate $y = x^n$}

    \label{alg1}

    \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}

    Algorithm 3.1 Calculate y = xn

    Require: n 0 x 6= 0Ensure: y = xn

    y 1if n < 0 then

    X 1/xN n

    else

    X xN n

    end if

    while N 6= 0 doif N is even then

    X X XN N/2

    else {N is odd}y y XN N 1

    end if

    end while

    Lit k danh sch thut ton

    \listofalgorithms

    t tn li bng danh sch

    \newcommand{\listalgorithmname}{List of Algorithms}

    Thay tn

    \floatname{algorithm}{Procedure}