[Tex/LaTex] Why this ‘Perhaps a missing \item’ error with the algorithmic package

algorithmicalgorithms

I am trying to imitate an example I found in the LaTeX wikibook to write my own algorithm using the algorithmic package. I can't understand it clearly, though, and I am getting errors all the time.

 \documentclass{article}
 \usepackage{times}
 \usepackage{amsthm}
 \usepackage{algorithm}
 \usepackage{algorithmic}
 \usepackage{alltt}
 \usepackage{mathtools}
 \usepackage{parskip}
 \begin{document}
 \begin{algorithm}
 \caption{Assigning j to a center}
 \begin{algorithmic}  
 INITIALIZE $C = \emptyset$
 \FOR {i=1,...,r}  <------Error
 \IF {$N_i \cap N_{i_0} \neq\emptyset$ for some $i_0 \leq i$
 \STATE assign to $p_{i_0}$ all demand nodes j with $i \in   \widetilde{p_j}$
 \ELSE
 \STATE $C = C \cup \{ i \}$ and assign to $p_i$ all the demand nodes $j$ with  $i \in   \widetilde{p_j}$
 \ENDIF
 \ENDFOR
 \end{algorithmic}
 \end{algorithm}
 \end{document}

I am getting the error: Something is wrong. Perphaps a missing \item on the line with the \FOR command. Can anybody help?

Best Answer

Two problems: you had a missing closing brace for the \IF and you were using INITIALIZE in a forbidden place (the beginning of the list internally used). Perhaps you could use

\STATE INITIALIZE $C = \emptyset$

or any other of the keywords provided by the package.

\documentclass{report}
\usepackage{algorithmic,algorithm}

\begin{document}

\begin{algorithm}
 \caption{Assigning j to a center}
 \begin{algorithmic}  
 \STATE INITIALIZE $C = \emptyset$
 \FOR {i=1,...,r}
 \IF {$N_i \cap N_{i_0} \neq\emptyset$ for some $i_0 \leq i$}
 \STATE assign to $p_{i_0}$ all demand nodes j with $i \in   \widetilde{p_j}$
 \ELSE
 \STATE $C = C \cup \{ i \}$ and assign to $p_i$ all the demand nodes $j$ with  $i \in   \widetilde{p_j}$
 \ENDIF
 \ENDFOR
 \end{algorithmic}
 \end{algorithm}

\end{document}

enter image description here