Thursday, January 12, 2012

How to write an algorithm in Latex : Video Tutorial with sample algorithm

People in Computer Science and Mathematics department often write algorithms for their papers, thesis, and other research articles. In this tutorial I will explain how to write an algorithm in Latex using the algorithm and algorithmic package in Latex. I will explain the basics of an article and show how simple building blocks can be added to Latex to write a full fledged professional quality algorithm.

Specifically, we will learn the following in this tutorial:

How to write an algorithm in Latex

Use of algorithm and algorithmic package

How to use loops in an algorithm

How to use IF statements in an algorithm

How to add caption to an algorithm

How to label an algorithm to refer it in the document

The code for this algorithm is shown below and explained in the video.

Here is the code used in this video:

\documentclass{article}\usepackage{algorithm}\usepackage{algorithmic}\begin{document}\begin{algorithm}\textbf{INPUT:} Set of Base Layer polygon $S_b$ and Set of Overlay Layer polygon $S_o$\\\textbf{OUTPUT:} Intersection Graph ($V$,$E$), where $V$ is set of polygons and $E$ is edges among polygons with intersecting bounding boxes.\begin{algorithmic}\STATE Parallel Merge Sort set $S_o$ of overlay polygons based on X co-ordinates of bounding boxes\footnotemark[1]\FORALL{base polygon $B_i$ in set $S_b$ of base polygons} \STATE find $S_x \subset S_o$ such that $B_i$ intersects with all elements of $S_x$ over $X$ co-ordinate \FORALL{overlay polygon $O_j$ in $S_x$} \IF {$B_i$ intersects $O_j$ over $Y$ co-ordinate} \STATE{Create Link between $O_j$ and $B_i$} \ENDIF \ENDFOR \ENDFOR \end{algorithmic}\caption{Algorithm to create polygon intersection graph}\label{algo:relgraph}\end{algorithm}\end{document}