# Algorithm in LaTeX

I wrote the following algorithm, however its not compiling and throws a few errors. I am not sure of what I did wrong. Any help would be appreciated.

\begin{algorithm}
\caption{Bridging Score(BRS) Computation}
\begin{algorithmic}
Given: User-Noun Matrix $X$
\For {$i = 1 \to n$}
Let $S_i$ = {$s{i,j}$ | $0\le j \le l_i$} be set of nodes which have links to $i$;
\IF $\left({l_i=0$ OR $l_i=1 \right)}$ \THEN
$b_i=0$;
\ELSE
Compute the similarity score vectors $r_{s,1},r_{s,2},...,r_{s,l_i}$ for each $S_i=s_1,s_2,...s_{l_i}$; \\
Construct $l_i \times l_i$ matrix $R_i= \left[r_{s,1},r_{s,2},...,r_{s,l_i}\right]$;
Take the average of all non-diagonal elements in $R_i$ to obtain $\hat{r_s}$;
$b_i= 1/\hat{r_s}$;
\ENDIF
\ENDFOR
\RETURN vector $b$ of bridging scores;
\end{algorithmic}
\end{algorithm}

-
Please add a minimal working example (MWE) that illustrates your problem. It is considered a lot better to put in some code that will compile, as it makes it a lot easier for us to copy it into our text editor and work with it, and see exactly what it is you are trying to do. –  Kurt Nov 16 '12 at 3:45
Only my first given statement works properly. Thereafter when I use \FOR it does not compile. Throws an error saying –  princess of persia Nov 16 '12 at 3:52
Something's wrong - perhaps a missing \item. \FOR {$i = 1 \to n$} –  princess of persia Nov 16 '12 at 4:04
Without a complete MWE (including \documentclass til \end{document}I can't say more. How do you call the needed packages? Which document class? Which code encryption? –  Kurt Nov 16 '12 at 4:14
It is well used practice to search errors with building a MWE. Create a new file and copy all relevant for creating the error. For a MWE you can use for example article, use standard font, and call only the packages, you need for compiling the MWE, not your original. So I or somebody else can simply copy your MWE and try it on the own computer. Searching errors is fun, creating MWEs not so much ... While building your MWE it often happens that you find the error by your own ... So please give the MWE a try. –  Kurt Nov 16 '12 at 4:24

It is not completely clear to me which algorithm typesetting package are you using, since you did not include the document preamble, and your code is a strange jumble of several different syntaxes, mostly looking like the old algorithms package syntax, but not quite. Your tags seem to indicate that you are using the algorithmicx bundle, so I assume that you use the algpseudocode package. You need to correct some syntax, for example algpseudocode does noy capitalize command, only first letter, so you should have \If ... \Else ... \EndIf etc.

Also, command \For and \If take an argument, so you need

\If{something something}


The \If does not use \Then, you should leave that out.

Every line must have a command, so you should either define your own commands for things like "Given:", or just use \State.

The following will hopefully do what you expect it to do:

\documentclass{article}
\usepackage{algpseudocode}
\usepackage{algorithm}
\begin{document}
\begin{algorithm}
\caption{Blah blah}
\begin{algorithmic}[1]
\State Given: User-Noun Matrix $X$
\For{$i = 1 \to n$}
\State Let $S_i$ = {$s{i,j}$ | $0\le j \le l_i$} be set of nodes which have links to $i$;
\If{$\left(l_i=0 \hbox{OR} l_i=1 \right)$}
\State $b_i=0$;
\Else
\State Compute the similarity score vectors $r_{s,1},r_{s,2},...,r_{s,l_i}$ for each $S_i=s_1,s_2,...s_{l_i}$; \\
\State Construct $l_i \times l_i$ matrix $R_i= \left[r_{s,1},r_{s,2},...,r_{s,l_i}\right]$;
\State Take the average of all non-diagonal elements in $R_i$ to obtain $\hat{r_s}$;
\State $b_i= 1/\hat{r_s}$;
\EndIf
\EndFor
\State \Return vector $b$ of bridging scores;
\end{algorithmic}
\end{algorithm}
\end{document}


The algorithmicx bundle have pretty good documentation with a number of examples, I suggest you take a look at it.

-
\usepackage{algorithm} \usepackage{algorithmic} –  princess of persia Nov 16 '12 at 4:45
Thanks for your help, could you tell me why are all the line numbers 0 and why is the last return statement not on the new line ? –  princess of persia Nov 16 '12 at 4:49
Aparently, \Return has to be preceded by \State as well. As far as the line numbering, it does not happen to me. I have the lines numbered the right way. –  Jan Hlavacek Nov 16 '12 at 12:19