Skip to main content

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:

  1. How to write an algorithm in Latex
  2. Use of algorithm and algorithmic package
  3. How to use loops in an algorithm
  4. How to use IF statements in an algorithm
  5. How to add caption to an algorithm
  6. 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}

The output looks like the following:




Comments

  1. I don't have adobe flash player. This movie sucked and I didn't learn anything. >:-(

    ReplyDelete
  2. Hello. thx for the video. its been really helpful.
    btw i have question. what if there is decision branch in algorithm that we want to write?
    for example from above:

    \IF {$B_i$ intersects $O_j$ over $Y$ co-ordinate}
    \STATE{Create Link between $O_j$ and $B_i$}

    my question, what if Bi NOT intersects Oi over Y coordinate?
    what syntax we use? IFELSE?
    what if theres more decision branch?
    how do write it in latex?
    hope you reply. im really a starter at using latex.

    Great videos btw.
    We demand more :D

    ReplyDelete

Post a Comment

Comments on QuickLatex are strictly moderated. As much as I hate moderation, I must fight spam. Please rest assured that if your comment is relevant I will approve it and answer.

Popular posts from this blog

Latex Tutorial: How to Create Images, Diagrams, and Flow Charts Using Latex

This tutorial is an extension of previous video tutorial on how to use tikz for image creation in Latex . In this video I explain how to set up your document for flow chat design. This equally applies to any other image that uses similar structure. Specifically, in this video I talk about following things: How to create a flow chart How to setup styles for reusable components of an image How to draw arrows from one component to other How to change the styles of components How to change the arrowhead for Latex arrows How to align components left, right, top, and below of each other The code use:d in the video is here: \documentclass{article} \usepackage{tikz} \usetikzlibrary{shapes,shadows,arrows} \begin{document} \tikzstyle{decision} = [diamond, draw, fill=blue!50] \tikzstyle{line} = [draw, -stealth, thick] \tikzstyle{elli}=[draw, ellipse, fill=red!50,minimum height=8mm, text width=5em, text centered] \tikzstyle{block} = [draw, rectangle, fill=blue!50, text width...

LaTeX Video Tutorial: How to Create a Resume or CV (Curriculum Vitae) using LaTeX

One of the most frequent questions my colleagues ask me is how to create a Resume or Curriculum Vitae (CV), if you will, in LaTeX . I have a style file that was passed to me by a friend who found it on Internet. Since this looked good I used it and, thanks to the original contributor, I am going to share it with you all today. You will be able to download these files and create a professional Resume for yourself.  The files can be download from the linked locations: The Resume.tex file (main document) The Class file res.cls (for page setting) In the video tutorial , attached below, I have explained how to change the text for your needs. The structure of the Resume (or CV) is highly flexible and once you get hold of the basics it will be pretty straight forward for you to tweak it for your own use.  I have tried to add the basic sections that are present in most of the Resumes but you will most probably add more depending on your requirements. I am prett...

How to draw Reddit Alien in LaTeX using Tikz - Video tutorial and code

If you are not a Redditor you are a lucky person. It's a one way traffic scenario with no dead end. Once you subscribe to Reddit there is no way back and it is no good being there. I am expecting my colleagues to have an intervention for my Reddit addiction soon but till then I am all here to invest (read:waste) my precious time. Since we got that out of our way, let us get back to business. Today, we are going to see how to draw a cartoon figure using LaTeX. The motivation behind this post is /r/latex on Reddit as I wanted that subreddit to have its logo drawn in LaTeX. I did not know it already was in LaTeX but it seems my work is appreciated there so I am going to contribute my 2 cents to the community. Specifically, we will learn following things in this tutorial: How to work with Colors - defining RGB colors in LaTeX. How to use multiple layers to set order of document objects (send to back, bring forward like functionality). How to use Arcs in an effective way....