The algorithm Trie below is an example of how algorithms are written. It produces the trie of a dictionary $X$, finite set of words $X$. It successively considers each word of $X$ during the for loop of lines 2-10 and inserts them into the structure letter by letter during execution of the for loop of lines 4-9. When the latter loop is over, the last considered state $t$, ending the path from the initial state and labelled by the current word, is set as terminal at line 10.
\begin{algorithmic}
\STATE $M\leftarrow $New-automaton()
\FOR{each string $x\in X$}
\STATE $t\leftarrow initial(M)$
\FOR{each letter $a$ of $x$, sequentially}
\STATE $p\leftarrow $Target$(t,a)$
\IF{$p=NIL$}
\STATE $p\leftarrow $New-state()
\STATE $Succ[t]\leftarrow Succ[t] \cup (a,p)$
\ENDIF
\STATE $t\leftarrow p$
\ENDFOR
\STATE $terminal[t]\leftarrow TRUE$
\ENDFOR
\RETURN{$M$}
\end{algorithmic}
|
Writing Conventions of Algorithms |
|
The style of the algorithmic language used here is relatively close to real programming languages but at a higher abstraction level. We adopt the following conventions: