<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4895207774785648921</id><updated>2026-07-15T21:51:19.987-07:00</updated><category term="gate 2013"/><category term="programming in c"/><category term="gate 2012"/><category term="gate"/><category term="computer science"/><category term="C"/><category term="previous papers"/><category term="computer graphics"/><category term="sort"/><category term="Recursion"/><category term="interview questions"/><category term="algorithm"/><category term="answer key"/><category term="linked list"/><category term="string"/><category term="Bresenham"/><category term="Bresenham&#39;s circle drawing"/><category term="Divide and Conquer"/><category term="GATE 2011"/><category term="opengl"/><category term="swapping"/><category term="temporary variable"/><category term="2D transformation"/><category term="GATE 2010"/><category term="array in c"/><category term="array of numbers"/><category term="binary tree"/><category term="circle drawing"/><category term="convolution"/><category term="count number of nodes"/><category term="greedy technique"/><category term="openCV"/><category term="pattern"/><category term="programming"/><category term="python"/><category term="reverse a linked list"/><category term="selection sort"/><category term="transformation"/><category term="2D rotation"/><category term="2D scaling"/><category term="2D translation"/><category term="5 3 2"/><category term="5+3+2 = 151022"/><category term="AND operator"/><category term="Binary Search"/><category term="C++"/><category term="DDA"/><category term="DDA line drawing"/><category term="DDA line drawing algorithm"/><category term="DDA using opengl"/><category term="Dunder methods in python"/><category term="Factorial"/><category term="LIFO"/><category term="Last in first out"/><category term="Modelling Transformation"/><category term="Programming in python"/><category term="Quick sort"/><category term="Searching"/><category term="Stack"/><category term="Stack operations"/><category term="Swapping using multiplication and division"/><category term="Swapping with XOR"/><category term="bellman ford"/><category term="bubblesort"/><category term="cat 2010"/><category term="cat question"/><category term="cheatsheet"/><category term="circular doubly linked list"/><category term="classes in C++"/><category term="clock"/><category term="composite number"/><category term="computer architecture"/><category term="count bits"/><category term="count the number of leaf nodes in a binary tree"/><category term="delete an element from the array"/><category term="dijkstra"/><category term="dijkstra&#39;s algorithm"/><category term="duplicate removal"/><category term="factorial c"/><category term="fork c"/><category term="gate 2008"/><category term="generalised bresenham"/><category term="glRotatef"/><category term="glScalef"/><category term="glTranslatef"/><category term="heap"/><category term="heap tree"/><category term="heapify"/><category term="heapsort"/><category term="height of a binary tree"/><category term="iim cat 2010"/><category term="image convolution"/><category term="insertion sort"/><category term="iterative algorithm"/><category term="lexicographic sorting"/><category term="line drawing"/><category term="matrix convolution"/><category term="mergesort"/><category term="palindrome"/><category term="pattern replacement"/><category term="pointer in c"/><category term="prime"/><category term="prime number"/><category term="programming in c++"/><category term="puzzles"/><category term="quicksort c"/><category term="recursive quicksort"/><category term="reverse a circular doubly linked list"/><category term="rotation"/><category term="scaling"/><category term="shift operator in C"/><category term="smiley"/><category term="sorting"/><category term="static clock"/><category term="string pattern replacement"/><category term="string replacement"/><category term="strstr"/><category term="substring"/><category term="translation"/><category term="use of the keyword virtual in C++"/><category term="virtual keyword"/><title type='text'>Code Blocks</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>109</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-517900675736666664</id><published>2026-07-15T21:44:43.569-07:00</published><updated>2026-07-15T21:51:19.987-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Dunder methods in python"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming in python"/><title type='text'>Understanding Python Dunder Methods: __str__ vs __repr__</title><content type='html'>&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;If you&#39;ve spent any time writing Python classes, you&#39;ve probably noticed something odd: print an object and you get a memory address like &lt;code&gt;&amp;lt;__main__.Point object at 0x7f8b1c0a5d90&amp;gt;&lt;/code&gt;. It&#39;s not exactly useful. This is where two of Python&#39;s most important &quot;dunder&quot; (double underscore) methods come in — &lt;code&gt;__str__&lt;/code&gt; and &lt;code&gt;__repr__&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;They look similar, get confused constantly, and yet they serve genuinely different purposes. Let&#39;s clear that up.&lt;/p&gt;

&lt;h3&gt;What Are Dunder Methods, Anyway?&lt;/h3&gt;

&lt;p&gt;&quot;Dunder&quot; is short for &quot;double underscore.&quot; Methods like &lt;code&gt;__init__&lt;/code&gt;, &lt;code&gt;__len__&lt;/code&gt;, &lt;code&gt;__add__&lt;/code&gt;, &lt;code&gt;__str__&lt;/code&gt;, and &lt;code&gt;__repr__&lt;/code&gt; are special methods Python calls automatically in response to built-in operations. You rarely call them directly — instead, Python&#39;s syntax and built-in functions trigger them behind the scenes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;obj + other&lt;/code&gt; triggers &lt;code&gt;__add__&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;len(obj)&lt;/code&gt; triggers &lt;code&gt;__len__&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;print(obj)&lt;/code&gt; triggers &lt;code&gt;__str__&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;repr(obj)&lt;/code&gt; (and the interactive shell) triggers &lt;code&gt;__repr__&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This system is often called &quot;dunder methods&quot; or the &quot;data model,&quot; and it&#39;s what lets custom objects behave like built-in types.&lt;/p&gt;

&lt;h3&gt;The Default Behavior (Why You Need These)&lt;/h3&gt;

&lt;p&gt;Without any customization, printing an object gives you almost nothing useful:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
&lt;code class=&quot;lang-python&quot;&gt;
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

p = Point(3, 4)
print(p)
# &amp;lt;__main__.Point object at 0x7f8b1c0a5d90&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;
&lt;br/&gt;

&lt;p&gt;That&#39;s technically &quot;correct,&quot; but useless for debugging or logging. Defining &lt;code&gt;__str__&lt;/code&gt; and &lt;code&gt;__repr__&lt;/code&gt; fixes this.&lt;/p&gt;

&lt;h3&gt;__repr__: The Developer&#39;s View&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;__repr__&lt;/code&gt; should return a string that is &lt;strong&gt;unambiguous&lt;/strong&gt; and, ideally, could be used to recreate the object. Think of it as the representation you&#39;d want to see in a debugger, a log file, or the Python REPL.&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
&lt;code class=&quot;lang-python&quot;&gt;
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return f&quot;Point(x={self.x}, y={self.y})&quot;

p = Point(3, 4)
p
# Point(x=3, y=4)

repr(p)
# &#39;Point(x=3, y=4)&#39;
&lt;/code&gt;
&lt;/pre&gt;
&lt;br/&gt;

&lt;p&gt;The guiding principle (straight from Python&#39;s own documentation philosophy) is:&lt;/p&gt;

&lt;blockquote&gt;&lt;code&gt;eval(repr(obj)) == obj&lt;/code&gt; should ideally hold true.&lt;/blockquote&gt;

&lt;p&gt;In our example, &lt;code&gt;Point(x=3, y=4)&lt;/code&gt; is literally valid Python code that recreates the object — that&#39;s a well-behaved &lt;code&gt;__repr__&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;__str__: The User&#39;s View&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;__str__&lt;/code&gt; is meant to return a &lt;strong&gt;readable, human-friendly&lt;/strong&gt; string — something you&#39;d show to an end user, not a developer debugging the internals.&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
&lt;code class=&quot;lang-python&quot;&gt;
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return f&quot;Point(x={self.x}, y={self.y})&quot;

    def __str__(self):
        return f&quot;({self.x}, {self.y})&quot;

p = Point(3, 4)
print(p)      # calls __str__
# (3, 4)

print(repr(p))  # calls __repr__
# Point(x=3, y=4)
&lt;/code&gt;
&lt;/pre&gt;
&lt;br/&gt;

&lt;p&gt;Notice the difference in intent: &lt;code&gt;__str__&lt;/code&gt; gives a clean coordinate pair for a user-facing message, while &lt;code&gt;__repr__&lt;/code&gt; gives full detail for debugging.&lt;/p&gt;

&lt;h3&gt;What Happens If You Only Define One?&lt;/h3&gt;

&lt;p&gt;This is the part that trips people up. Python has a fallback rule:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If &lt;code&gt;__str__&lt;/code&gt; is not defined, Python falls back to &lt;code&gt;__repr__&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
&lt;code class=&quot;lang-python&quot;&gt;
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return f&quot;Point(x={self.x}, y={self.y})&quot;

p = Point(3, 4)
print(p)
# Point(x=3, y=4)   &amp;lt;- uses __repr__ since __str__ isn&#39;t defined
&lt;/code&gt;
&lt;/pre&gt;
&lt;br/&gt;

&lt;p&gt;The reverse is &lt;strong&gt;not&lt;/strong&gt; true — if you only define &lt;code&gt;__str__&lt;/code&gt;, &lt;code&gt;repr(obj)&lt;/code&gt; still falls back to the default &lt;code&gt;&amp;lt;Point object at 0x...&amp;gt;&lt;/code&gt; unless you explicitly define &lt;code&gt;__repr__&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is why the common advice is: always define &lt;code&gt;__repr__&lt;/code&gt;. Define &lt;code&gt;__str__&lt;/code&gt; only if you need a different, more user-friendly output.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;Where Each One Actually Gets Used&lt;/h3&gt;

&lt;table border=&quot;1&quot; cellpadding=&quot;6&quot; cellspacing=&quot;0&quot;&gt;
&lt;tr&gt;&lt;th&gt;Context&lt;/th&gt;&lt;th&gt;Method called&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;print(obj)&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;__str__&lt;/code&gt; (falls back to &lt;code&gt;__repr__&lt;/code&gt;)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;str(obj)&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;__str__&lt;/code&gt; (falls back to &lt;code&gt;__repr__&lt;/code&gt;)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;repr(obj)&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;__repr__&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Interactive REPL (typing &lt;code&gt;obj&lt;/code&gt; and hitting Enter)&lt;/td&gt;&lt;td&gt;&lt;code&gt;__repr__&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Inside a list/dict: &lt;code&gt;print([obj1, obj2])&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;__repr__&lt;/code&gt; (containers always use repr on their elements)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Debuggers, logging, &lt;code&gt;f&quot;{obj!r}&quot;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;__repr__&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;f&quot;{obj}&quot;&lt;/code&gt; or &lt;code&gt;f&quot;{obj!s}&quot;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;__str__&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;That container detail is worth calling out explicitly — it surprises a lot of people:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
&lt;code class=&quot;lang-python&quot;&gt;
points = [Point(1, 2), Point(3, 4)]
print(points)
# [Point(x=1, y=2), Point(x=3, y=4)]
&lt;/code&gt;
&lt;/pre&gt;
&lt;br/&gt;

&lt;p&gt;Even though &lt;code&gt;Point&lt;/code&gt; has a &lt;code&gt;__str__&lt;/code&gt;, printing a list of points uses &lt;code&gt;__repr__&lt;/code&gt; for each element, because containers always show the &lt;code&gt;repr&lt;/code&gt; of their contents.&lt;/p&gt;

&lt;h3&gt;A Practical, Real-World Example&lt;/h3&gt;

&lt;p&gt;Here&#39;s a slightly more realistic class — a simple &lt;code&gt;User&lt;/code&gt; model — showing both methods pulling their proper weight:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
&lt;code class=&quot;lang-python&quot;&gt;
class User:
    def __init__(self, username, email, is_active=True):
        self.username = username
        self.email = email
        self.is_active = is_active

    def __repr__(self):
        return (f&quot;User(username={self.username!r}, &quot;
                f&quot;email={self.email!r}, is_active={self.is_active})&quot;)

    def __str__(self):
        status = &quot;active&quot; if self.is_active else &quot;inactive&quot;
        return f&quot;{self.username} ({status})&quot;


user = User(&quot;mchen&quot;, &quot;mchen@example.com&quot;)

print(user)
# mchen (active)          &amp;lt;- friendly, for UI/logs shown to humans

print(repr(user))
# User(username=&#39;mchen&#39;, email=&#39;mchen@example.com&#39;, is_active=True)
# &amp;lt;- precise, for debugging

users = [user, User(&quot;jsmith&quot;, &quot;jsmith@example.com&quot;, is_active=False)]
print(users)
# [User(username=&#39;mchen&#39;, ...), User(username=&#39;jsmith&#39;, ...)]
# &amp;lt;- repr used automatically in the list
&lt;/code&gt;
&lt;/pre&gt;
&lt;br/&gt;

&lt;p&gt;Notice the &lt;code&gt;!r&lt;/code&gt; inside the f-string in &lt;code&gt;__repr__&lt;/code&gt; — that forces the &lt;code&gt;repr()&lt;/code&gt; of &lt;code&gt;self.username&lt;/code&gt; and &lt;code&gt;self.email&lt;/code&gt;, wrapping strings in quotes. This is a small but important habit: it keeps your repr unambiguous (you can tell a string field apart from a number or &lt;code&gt;None&lt;/code&gt; at a glance).&lt;/p&gt;

&lt;h3&gt;Quick Rules of Thumb&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Always implement &lt;code&gt;__repr__&lt;/code&gt;.&lt;/strong&gt; It&#39;s your safety net for debugging, logging, and the REPL.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Implement &lt;code&gt;__str__&lt;/code&gt; only when a different, friendlier output makes sense&lt;/strong&gt; for end users.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Make &lt;code&gt;__repr__&lt;/code&gt; unambiguous&lt;/strong&gt; — ideally valid Python that recreates the object, or at least clearly labeled with class name and field values.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code&gt;!r&lt;/code&gt; in f-strings&lt;/strong&gt; when building &lt;code&gt;__repr__&lt;/code&gt; to correctly quote string fields.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Remember containers use &lt;code&gt;__repr__&lt;/code&gt;&lt;/strong&gt; on their elements, not &lt;code&gt;__str__&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;Wrapping Up&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;__str__&lt;/code&gt; and &lt;code&gt;__repr__&lt;/code&gt; aren&#39;t just cosmetic — they&#39;re part of how Python objects communicate with the people who use and debug them. &lt;code&gt;__repr__&lt;/code&gt; is for developers: precise, unambiguous, ideally reconstructable. &lt;code&gt;__str__&lt;/code&gt; is for everyone else: clean and readable. Define both thoughtfully, and your objects will be far more pleasant to work with — whether you&#39;re staring at a log file at 2 AM or showing output to an actual user.&lt;/p&gt;

&lt;br/&gt;
&lt;small&gt;Written by &lt;a rel=&quot;author&quot; href=&quot;https://plus.google.com/105162279647438527853/&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;br/&gt;
</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/517900675736666664/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2026/07/understanding-python-dunder-methods-str.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/517900675736666664'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/517900675736666664'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2026/07/understanding-python-dunder-methods-str.html' title='Understanding Python Dunder Methods: __str__ vs __repr__'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-7478217499952998393</id><published>2015-01-11T06:59:00.000-08:00</published><updated>2015-01-11T06:59:45.994-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="computer graphics"/><category scheme="http://www.blogger.com/atom/ns#" term="glRotatef"/><category scheme="http://www.blogger.com/atom/ns#" term="glScalef"/><category scheme="http://www.blogger.com/atom/ns#" term="glTranslatef"/><category scheme="http://www.blogger.com/atom/ns#" term="Modelling Transformation"/><category scheme="http://www.blogger.com/atom/ns#" term="opengl"/><category scheme="http://www.blogger.com/atom/ns#" term="transformation"/><title type='text'>Modelling Transformations in OpenGL</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;GL/glut.h&amp;gt;

/* This function is to draw a triangle  */
void draw_triangle()
{
  glBegin(GL_LINES);
  glVertex2f(-0.40, -0.25);
  glVertex2f(-0.60, -0.25);

  glVertex2f(-0.60, -0.25);
  glVertex2f(-0.50, 0.25);

  glVertex2f(-0.40, -0.25);
  glVertex2f(-0.50, 0.25);
  glEnd();
  glFlush();
}

void draw()
{
  glClear(GL_COLOR_BUFFER_BIT);

  /* Draw a triangle using solid lines */
  draw_triangle();                   /* solid lines */

  /* The same triangle is drawn again, but with a dashed  */
  /* line stipple and translated (to the left along the  */
  /* negative x­axis) */
  glEnable(GL_LINE_STIPPLE);         /* dashed lines */
  glLineStipple(1, 0xF0F0); 
  glLoadIdentity();
  glTranslatef(-0.30, 0.0, 0.0);
  draw_triangle();

  /* A triangle is drawn with a long dashed line stipple,  */
  /* with its height (y­axis) halved and its width (x­axis)  */
  /* increased by 50%  */
  glLineStipple(1, 0xF00F);          /*long dashed lines */
  glLoadIdentity();
  glScalef(1.5, 0.5, 1.0);
  draw_triangle();

  /* A rotated triangle(rotated at 90 degree w.r.t the z-axix), 
  made of dotted lines, is drawn */
  glLineStipple(1, 0x8888);          /* dotted lines */
  glLoadIdentity();
  glRotatef (90.0, 0.0, 0.0, 1.0);
  draw_triangle();
  glDisable (GL_LINE_STIPPLE);

  glFlush();
}

void Init()
{
  /* Set clear color to black */
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  /* Set fill color to white */
  glColor3f(1.0, 1.0, 1.0);
  gluOrtho2D(0.0 , 1.0 , 0.0 , 1.0);
  /* glViewport() command is used to define the rectangle of */
  /* the rendering area where the final image is mapped */
  glViewport(0.0, 0.0, 1.0, 1.0);
  /* glMatrixMode specifies the mode of transformation */
  glMatrixMode(GL_MODELVIEW);
  /* set the current matrix to the identity matrix */
  glLoadIdentity();
}

int main(int argc, char **argv)
{
  glutInit(&amp;amp;argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(640, 480);
  glutCreateWindow(&quot;Transformation&quot;);
  Init();
  glutDisplayFunc(draw);
  glutMainLoop();
  return 0;
}

&lt;/code&gt;&lt;/pre&gt;
Output&lt;br /&gt;
======
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhq0-7bPZlwFTMJq_Yrz-_H-_QbT4Hc4E_dcjuWaxFHVzJ4Me3pEmVG8Yf593avTGDw032-ji4xOHPd63y0Ta7Z9FQ3HfEBuBQAgGZ2d0vfHsUeNKNorB9px0qhfzdzv8dXgvZ_p3cp0PE/s1600/mod.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhq0-7bPZlwFTMJq_Yrz-_H-_QbT4Hc4E_dcjuWaxFHVzJ4Me3pEmVG8Yf593avTGDw032-ji4xOHPd63y0Ta7Z9FQ3HfEBuBQAgGZ2d0vfHsUeNKNorB9px0qhfzdzv8dXgvZ_p3cp0PE/s1600/mod.png&quot; height=&quot;252&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/7478217499952998393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2015/01/modelling-transformations-in-opengl.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/7478217499952998393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/7478217499952998393'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2015/01/modelling-transformations-in-opengl.html' title='Modelling Transformations in OpenGL'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhq0-7bPZlwFTMJq_Yrz-_H-_QbT4Hc4E_dcjuWaxFHVzJ4Me3pEmVG8Yf593avTGDw032-ji4xOHPd63y0Ta7Z9FQ3HfEBuBQAgGZ2d0vfHsUeNKNorB9px0qhfzdzv8dXgvZ_p3cp0PE/s72-c/mod.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-2559929866772712103</id><published>2014-12-23T00:22:00.000-08:00</published><updated>2015-01-11T06:46:07.507-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="2D scaling"/><category scheme="http://www.blogger.com/atom/ns#" term="2D transformation"/><category scheme="http://www.blogger.com/atom/ns#" term="computer graphics"/><category scheme="http://www.blogger.com/atom/ns#" term="opengl"/><category scheme="http://www.blogger.com/atom/ns#" term="scaling"/><title type='text'>2D scaling Without Using OpenGL Function For Scaling</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Program to draw a square of side 100 units at the center of the screen and scale it such that it enlarges to a square of side 150 units without using OpenGL function for scaling
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;
&lt;/code&gt;
#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;GL/glut.h&amp;gt;

int i;
float scalex, scaley, a[10], b[10], x, y, square_side;

void line(float x, float y, float a, float b)
{
  glBegin(GL_LINES);
  glVertex2f(x, y);
  glVertex2f(a, b);
  glEnd();
}

void display_square()
{
  for(i = 0; i &amp;lt; 4; i++)
  {
    if(i != 3)
      line(a[i], b[i], a[i+1], b[i+1]);
    else
      line(a[i], b[i], a[0], b[0]);
  }
}

void Init()
{
  /* Set clear color to white */
  glClearColor(1.0, 1.0, 1.0, 0);
  /* Set fill color to black */
  glColor3f(0.0, 0.0, 0.0);
  gluOrtho2D(0.0 , 640.0 , 0.0 , 480.0);
}

void scale()
{
  // Find the translated rectangle vertices
  for(i = 0; i &amp;lt; 4; i++)
  {
    a[i] = a[i] * scalex; b[i] = b[i] * scaley;
  }
}

void draw()
{
 glClear(GL_COLOR_BUFFER_BIT);

  // Find the original square vertices
  // (x, y) represents the upper left point
  x = 270.0; y = 290.0;

  a[0] = x; b[0] = y;
  a[1] = x + square_side; b[1] = y;
  a[2] = x + square_side; b[2] = y - square_side;
  a[3] = x; b[3] = y - square_side;
  // Draw original square
  display_square();

  // Perform scaling
  scale();
  // Draw scaled square using dotted lines
  glEnable(GL_LINE_STIPPLE);
  glLineStipple(1, 0xF0F0);
  display_square();
  glDisable(GL_LINE_STIPPLE);

  glFlush();
}

void main(int argc, char **argv)
{
  square_side = 100;
  printf(&quot;\n************************************************\n&quot;);
  printf(&quot;Length of the side of the square: %f\n&quot;, square_side);

  scalex = 1.5;
  scaley = 1.5;
  printf(&quot;\n************************************************\n&quot;);
  printf(&quot;Scaling factor along both directions: %f\n&quot;, scalex);
  printf(&quot;\n************************************************\n&quot;);

  glutInit(&amp;amp;argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(640, 480);
  glutCreateWindow(&quot;Scaling&quot;);
  Init();
  glutDisplayFunc(draw);
  glutMainLoop();
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/2559929866772712103/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2014/12/2d-scaling-without-using-opengl.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/2559929866772712103'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/2559929866772712103'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2014/12/2d-scaling-without-using-opengl.html' title='2D scaling Without Using OpenGL Function For Scaling'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-6697049674671219320</id><published>2014-12-23T00:16:00.000-08:00</published><updated>2014-12-23T00:17:09.797-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="2D rotation"/><category scheme="http://www.blogger.com/atom/ns#" term="2D transformation"/><category scheme="http://www.blogger.com/atom/ns#" term="computer graphics"/><category scheme="http://www.blogger.com/atom/ns#" term="rotation"/><title type='text'>2D Rotation Without Using OpenGL Function For Rotation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Program to perform 2D rotation without using OpenGL function for rotation

&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;
#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;math.h&amp;gt;
#include&amp;lt;GL/glut.h&amp;gt;

int x, y, p, q, xa, ya, ra, i, a[10], b[10], da, db;
float dx, dy, theta;

void line(int x, int y, int a, int b)
{
  glVertex2d(x, y);
  glVertex2d(a, b);
}

void display_rectangle()
{
  for(i = 0; i &amp;lt; 4; i++)
  {
    if(i != 3)
      line(a[i], b[i], a[i+1], b[i+1]);
    else
      line(a[i], b[i], a[0], b[0]);
  }
}

void Init()
{
  /* Set clear color to white */
  glClearColor(1.0, 1.0, 1.0, 0);
  /* Set fill color to black */
  glColor3f(0.0, 0.0, 0.0);
  gluOrtho2D(0 , 640 , 0 , 480);
}

void rotate()
{
  // Find the vertices of the rotated rectangle
  theta = (float)(ra*(3.14/180));
  for(i = 0;i&amp;lt;4;i++)
  {
    a[i] = (xa + ((a[i] - xa)*cos(theta) - (b[i] - ya)*sin(theta)));
    b[i] = (ya + ((a[i] - xa)*sin(theta) + (b[i] - ya)*cos(theta)));
  }
}

void transform()
{
  glClear(GL_COLOR_BUFFER_BIT);

  glBegin(GL_LINES);
  // Find the original rectangle vertices
  da = p - x; db = q - y;
  a[0] = x; b[0] = y;
  a[1] = x + da; b[1] = y;
  a[2] = x + da; b[2] = y + db;
  a[3] = x; b[3] = y + db;

  // Draw original rectangle
  display_rectangle();

  rotate();
  
  // Draw rotated rectangle
  display_rectangle();
  
  glEnd();

  glFlush();

}

void main(int argc, char **argv)
{
  printf(&quot;\n**********************************************\n&quot;);
  printf(&quot;Enter the upper left corner of the rectangle:\n&quot;);
  scanf(&quot;%d%d&quot;, &amp;amp;x, &amp;amp;y);
  printf(&quot;\n**********************************************\n&quot;);
  printf(&quot;Enter the lower right corner of the rectangle:\n&quot;);
  scanf(&quot;%d%d&quot;, &amp;amp;p, &amp;amp;q);

  printf(&quot;\n********************Rotation********************\n&quot;);
  printf(&quot;Enter the value of fixed point and angle of rotation:\n&quot;);
  scanf(&quot;%d%d%d&quot;, &amp;amp;xa, &amp;amp;ya, &amp;amp;ra);

  glutInit(&amp;amp;argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(640, 480);
  glutCreateWindow(&quot;Rotation&quot;);
  Init();
  glutDisplayFunc(transform);
  glutMainLoop();
}&lt;/code&gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/6697049674671219320/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2014/12/2d-rotation-without-using-opengl.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/6697049674671219320'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/6697049674671219320'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2014/12/2d-rotation-without-using-opengl.html' title='2D Rotation Without Using OpenGL Function For Rotation'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-476596130754040982</id><published>2014-12-22T21:29:00.002-08:00</published><updated>2014-12-23T00:24:18.392-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="2D translation"/><category scheme="http://www.blogger.com/atom/ns#" term="computer graphics"/><category scheme="http://www.blogger.com/atom/ns#" term="transformation"/><category scheme="http://www.blogger.com/atom/ns#" term="translation"/><title type='text'>2D Translation Without Using OpenGL Function For Translation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Program to draw a rectangle and translate it without using OpenGL function for translation
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;
#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;GL/glut.h&amp;gt;

int x, y, p, q, a[10], b[10], da, db, i;
float dx, dy;

void line(int x, int y, int a, int b)
{
  glBegin(GL_LINES);
  glVertex2d(x, y);
  glVertex2d(a, b);
  glEnd();
}

void display_rectangle()
{
  for(i = 0; i &amp;lt; 4; i++)
  {
    if(i != 3)
      line(a[i], b[i], a[i+1], b[i+1]);
    else
      line(a[i], b[i], a[0], b[0]);
  }
}

void Init()
{
  /* Set clear color to white */
  glClearColor(1.0, 1.0, 1.0, 0);
  /* Set fill color to black */
  glColor3f(0.0, 0.0, 0.0);
  gluOrtho2D(0 , 640 , 0 , 480);
}

void translate()
{
  // Find the translated rectangle vertices
  for(i = 0; i &amp;lt; 4; i++)
  {
    a[i] = a[i] + dx; b[i] = b[i] + dy;
  }
}

void transform()
{
 glClear(GL_COLOR_BUFFER_BIT);

  // Find the original rectangle vertices
  da = p-x; db = q-y;
  a[0] = x; b[0] = y;
  a[1] = x + da; b[1] = y;
  a[2] = x + da; b[2] = y + db;
  a[3] = x; b[3] = y + db;
  // Draw original rectangle
  display_rectangle();

  // Perform translation
  translate();
  // Draw translated rectangle using dotted lines
  glEnable(GL_LINE_STIPPLE);
  glLineStipple(1, 0xF0F0);
  display_rectangle();
  glDisable(GL_LINE_STIPPLE);

  glFlush();
}

void main(int argc, char **argv)
{
  printf(&quot;\n************************************************\n&quot;);
  printf(&quot;Enter the upper left corner of the rectangle:\n&quot;);
  scanf(&quot;%d%d&quot;, &amp;amp;x, &amp;amp;y);
  printf(&quot;\n************************************************\n&quot;);
  printf(&quot;Enter the lower right corner of the rectangle:\n&quot;);
  scanf(&quot;%d%d&quot;, &amp;amp;p, &amp;amp;q);

  printf(&quot;\n******************Translation******************\n&quot;);
  printf(&quot;Enter the value of shift vector:\n&quot;);
  scanf(&quot;%f%f&quot;, &amp;amp;dx, &amp;amp;dy);

  glutInit(&amp;amp;argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(640, 480);
  glutCreateWindow(&quot;Translation&quot;);
  Init();
  glutDisplayFunc(transform);
  glutMainLoop();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/476596130754040982/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2014/12/2d-translation-without-using-opengl.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/476596130754040982'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/476596130754040982'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2014/12/2d-translation-without-using-opengl.html' title='2D Translation Without Using OpenGL Function For Translation'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-6663003834383949195</id><published>2014-12-19T00:49:00.000-08:00</published><updated>2014-12-19T00:49:11.922-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="lexicographic sorting"/><category scheme="http://www.blogger.com/atom/ns#" term="sorting"/><category scheme="http://www.blogger.com/atom/ns#" term="string"/><title type='text'>Lexicographic sorting of strings</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
/* Program to perform lexicographic sorting of strings */
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;
&lt;/code&gt;
#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;string.h&amp;gt;
#include&amp;lt;ctype.h&amp;gt;

void main()
{
  char str[50][50], temp[50], str1[50][50], n[1], temp1[50];
  int i, j, m, k;
  printf(&quot;\n##############################\n&quot;);
  printf(&quot;Enter the number of strings\n&quot;);
  gets(n);
  m = atoi(n);
  printf(&quot;\n##############################\n&quot;);
  printf(&quot;Enter the strings\n&quot;);
  for(i = 0; i &amp;lt; m; i++)
    gets(str[i]);

  // Convert all letters to lowercase
  for(i = 0; i &amp;lt; m; i++)
    for(j = 0; j &amp;lt; strlen(str[i]); j++)
      if(isupper(str[i][j]) != 0)
        str1[i][j] = tolower(str[i][j]);
      else
        str1[i][j] = str[i][j];

  for(i = 0; i &amp;lt; m; i++)
    for(j = i + 1; j &amp;lt; m; j++)
      if((strcmp(str1[i], str1[j])) &amp;gt; 0)
      {
        strcpy(temp, str[i]);strcpy(temp1, str1[i]);
        strcpy(str[i], str[j]);strcpy(str1[i], str1[j]);
        strcpy(str[j], temp);strcpy(str1[j], temp1);
      }

  printf(&quot;\n##############################\n&quot;);
  printf(&quot;Sorted list is \n&quot;);
  for(i = 0; i &amp;lt; m; i++)
    puts(str[i]);
  printf(&quot;##############################\n&quot;);
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/6663003834383949195/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2014/12/lexicographic-sorting-of-strings.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/6663003834383949195'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/6663003834383949195'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2014/12/lexicographic-sorting-of-strings.html' title='Lexicographic sorting of strings'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-1378103491322299548</id><published>2014-12-14T22:31:00.001-08:00</published><updated>2014-12-14T22:31:04.704-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bresenham"/><category scheme="http://www.blogger.com/atom/ns#" term="Bresenham&#39;s circle drawing"/><category scheme="http://www.blogger.com/atom/ns#" term="computer graphics"/><category scheme="http://www.blogger.com/atom/ns#" term="smiley"/><title type='text'>Smiley using Bresenham&#39;s Circle Drawing in OpenGL</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;GL/glut.h&amp;gt;

// Center of the cicle = (320, 240)
int xc = 320, yc = 240;

// Plot two points on the lower quadrants 
// using circle&#39;s symmetrical property
void plot_point(int x, int y)
{
  glBegin(GL_POINTS);
  glVertex2i(xc+x, yc-y);
  glVertex2i(xc-x, yc-y);
  glEnd();
}

// Function to draw a circle using bresenham&#39;s
// circle drawing algorithm
void bresenham_circle(int r)
{
  int x=0,y=r;
  float pk=(5.0/4.0)-r;

  /* Plot the points */
  plot_point(x,y);
  int k;
  /* Find all vertices till x=y */
  while(x &amp;lt; y)
  {
    x = x + 1;
    if(pk &amp;lt; 0)
      pk = pk + 2*x+1;
    else
    {
      y = y - 1;
      pk = pk + 2*(x - y) + 1;
    }
    plot_point(x,y);
  }
}

// Function to draw a smiley
void smiley(void)
{
  /* Clears buffers to preset values */
  glClear(GL_COLOR_BUFFER_BIT);

  int radius = 100;
  // Draw an arc
  bresenham_circle(radius);

  // Draw a vertical line
  glBegin(GL_LINES);
  glVertex2i(xc, yc);
  glVertex2i(xc, yc-60);
  glEnd();

  // Draw 2 points on either side 
  // of the line
  glBegin(GL_POINTS);
  // A big dot made of 4 small dots
  // to the left of the line
  glVertex2i(xc-20, yc-20);
  glVertex2i(xc-19, yc-20);
  glVertex2i(xc-20, yc-19);
  glVertex2i(xc-19, yc-19);

  // A big dot made of 4 small dots
  // to the right of the line
  glVertex2i(xc+20, yc-20);
  glVertex2i(xc+21, yc-20);
  glVertex2i(xc+21, yc-19);
  glVertex2i(xc+20, yc-19);
  glEnd();
  glFlush();
}

void Init()
{
  /* Set clear color to black */
  glClearColor(0.0,0.0,0.0,0);
  /* Set fill color to white*/
  glColor3f(1.0,1.0,1.0);
  gluOrtho2D(0 , 640 , 0 , 480);
}

void main(int argc, char **argv)
{
  /* Initialise GLUT library */
  glutInit(&amp;amp;argc,argv);
  /* Set the initial display mode */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  /* Set the initial window position and size */
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  /* Create the window with title &quot;DDA_Line&quot; */
  glutCreateWindow(&quot;Smiley :-)&quot;);
  /* Initialize drawing colors */
  Init();
  /* Call the displaying function */
  glutDisplayFunc(smiley);
  /* Keep displaying untill the program is closed */
  glutMainLoop();
}
&lt;/code&gt;
&lt;/pre&gt;
Output&lt;br /&gt;
=====&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjICwlwomEkEd1BeW5EeYJNirBhGLig4L2Bi6N9hmWIzms6C-IDu5xpFS5lYxUZsq8ZwmL0oOZdjkj3f5gJUqqLfzrKmXtFiOzz_Wqx7TfBraBzyIV7PRnaCaFEIgepAFEsvdvS7QvYm8k/s1600/out_smiley.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjICwlwomEkEd1BeW5EeYJNirBhGLig4L2Bi6N9hmWIzms6C-IDu5xpFS5lYxUZsq8ZwmL0oOZdjkj3f5gJUqqLfzrKmXtFiOzz_Wqx7TfBraBzyIV7PRnaCaFEIgepAFEsvdvS7QvYm8k/s1600/out_smiley.png&quot; height=&quot;253&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/1378103491322299548/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2014/12/smiley-using-bresenhams-circle-drawing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/1378103491322299548'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/1378103491322299548'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2014/12/smiley-using-bresenhams-circle-drawing.html' title='Smiley using Bresenham&#39;s Circle Drawing in OpenGL'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjICwlwomEkEd1BeW5EeYJNirBhGLig4L2Bi6N9hmWIzms6C-IDu5xpFS5lYxUZsq8ZwmL0oOZdjkj3f5gJUqqLfzrKmXtFiOzz_Wqx7TfBraBzyIV7PRnaCaFEIgepAFEsvdvS7QvYm8k/s72-c/out_smiley.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-7479749493852318484</id><published>2014-12-12T01:41:00.003-08:00</published><updated>2014-12-12T01:41:49.599-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bresenham&#39;s circle drawing"/><category scheme="http://www.blogger.com/atom/ns#" term="circle drawing"/><category scheme="http://www.blogger.com/atom/ns#" term="clock"/><category scheme="http://www.blogger.com/atom/ns#" term="computer graphics"/><category scheme="http://www.blogger.com/atom/ns#" term="static clock"/><title type='text'>Static Clock using Bresenham&#39;s Circle Drawing in OpenGL</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;/* To draw a static clock using Bresenham&#39;s circle drawing algorithm */
&lt;code class=&quot;lang-c&quot;&gt;&lt;stdio .h=&quot;&quot;&gt;&lt;gl glut.h=&quot;&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;GL/glut.h&amp;gt;

// Center of the cicle = (320, 240)
int xc = 320, yc = 240;

// Plot eight points using circle&#39;s symmetrical property
void plot_point(int x, int y)
{
  glBegin(GL_POINTS);
  glVertex2i(xc+x, yc+y);
  glVertex2i(xc+x, yc-y);
  glVertex2i(xc+y, yc+x);
  glVertex2i(xc+y, yc-x);
  glVertex2i(xc-x, yc-y);
  glVertex2i(xc-y, yc-x);
  glVertex2i(xc-x, yc+y);
  glVertex2i(xc-y, yc+x);
  glEnd();
}

// Function to draw a circle using bresenham&#39;s
// circle drawing algorithm
void bresenham_circle(int r)
{
  int x=0,y=r;
  float pk=(5.0/4.0)-r;

  /* Plot the points */
  /* Plot the first point */
  plot_point(x,y);
  int k;
  /* Find all vertices till x=y */
  while(x &amp;lt; y)
  {
    x = x + 1;
    if(pk &amp;lt; 0)
      pk = pk + 2*x+1;
    else
    {
      y = y - 1;
      pk = pk + 2*(x - y) + 1;
    }
    plot_point(x,y);
  }
}

// Function to draw a static clock
void static_clock(void)
{
  /* Clears buffers to preset values */
  glClear(GL_COLOR_BUFFER_BIT);

  int radius = 100;
  // Draw a circle
  bresenham_circle(radius);
  glBegin(GL_LINES);
  // Minute hand
  glVertex2i(xc, yc);
  glVertex2i(xc, yc+95);
  // Hour hand
  glVertex2i(xc, yc);
  glVertex2i(xc+30, yc+50);
  glEnd();
  glFlush();
}

void Init()
{
  /* Set clear color to black */
  glClearColor(0.0,0.0,0.0,0);
  /* Set fill color to white */
  glColor3f(1.0,1.0,1.0);
  gluOrtho2D(0 , 640 , 0 , 480);
}

void main(int argc, char **argv)
{
  /* Initialise GLUT library */
  glutInit(&amp;amp;argc,argv);
  /* Set the initial display mode */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  /* Set the initial window position and size */
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  /* Create the window with title &quot;DDA_Line&quot; */
  glutCreateWindow(&quot;bresenham_circle&quot;);
  /* Initialize drawing colors */
  Init();
  /* Call the displaying function */
  glutDisplayFunc(static_clock);
  /* Keep displaying untill the program is closed */
  glutMainLoop();
}
&lt;/gl&gt;&lt;/stdio&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;b&gt;Output&lt;/b&gt;


&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJY9dkh8L4NeIiSB9G3D8NSB7TsZ9WTxN7AIwSnHvKTehQ_2GTP9mgcQYXWClc51k6pMHAJ3it0fBD1Ur3kqQXiaAiLuUmj7DZurM7_NnJPns4xcuyI5T9lAiYkMQq4cAEFaS3JS9ZIA8/s1600/clock.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJY9dkh8L4NeIiSB9G3D8NSB7TsZ9WTxN7AIwSnHvKTehQ_2GTP9mgcQYXWClc51k6pMHAJ3it0fBD1Ur3kqQXiaAiLuUmj7DZurM7_NnJPns4xcuyI5T9lAiYkMQq4cAEFaS3JS9ZIA8/s320/clock.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: 13px;&quot;&gt;Written by&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot; style=&quot;font-size: 13px;&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/7479749493852318484/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2014/12/static-clock-using-bresenhams-circle.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/7479749493852318484'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/7479749493852318484'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2014/12/static-clock-using-bresenhams-circle.html' title='Static Clock using Bresenham&#39;s Circle Drawing in OpenGL'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJY9dkh8L4NeIiSB9G3D8NSB7TsZ9WTxN7AIwSnHvKTehQ_2GTP9mgcQYXWClc51k6pMHAJ3it0fBD1Ur3kqQXiaAiLuUmj7DZurM7_NnJPns4xcuyI5T9lAiYkMQq4cAEFaS3JS9ZIA8/s72-c/clock.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-4298630970690228029</id><published>2014-12-10T20:44:00.000-08:00</published><updated>2014-12-10T20:44:08.691-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bresenham"/><category scheme="http://www.blogger.com/atom/ns#" term="Bresenham&#39;s circle drawing"/><category scheme="http://www.blogger.com/atom/ns#" term="circle drawing"/><category scheme="http://www.blogger.com/atom/ns#" term="computer graphics"/><title type='text'>Bresenham&#39;s Circle Drawing Algorithm using OpenGL</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This program is to draw two concentric circles using bresenham&#39;s circle drawing 
algorithm with center (320, 240) and radii of circles as 100 and 200.
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;math.h&amp;gt;
#include &amp;lt;GL/glut.h&amp;gt;

// Center of the cicle = (320, 240)
int xc = 320, yc = 240;

// Plot eight points using circle&#39;s symmetrical property
void plot_point(int x, int y)
{
  glBegin(GL_POINTS);
  glVertex2i(xc+x, yc+y);
  glVertex2i(xc+x, yc-y);
  glVertex2i(xc+y, yc+x);
  glVertex2i(xc+y, yc-x);
  glVertex2i(xc-x, yc-y);
  glVertex2i(xc-y, yc-x);
  glVertex2i(xc-x, yc+y);
  glVertex2i(xc-y, yc+x);
  glEnd();
}

// Function to draw a circle using bresenham&#39;s
// circle drawing algorithm
void bresenham_circle(int r)
{
  int x=0,y=r;
  float pk=(5.0/4.0)-r;

  /* Plot the points */
  /* Plot the first point */
  plot_point(x,y);
  int k;
  /* Find all vertices till x=y */
  while(x &amp;lt; y)
  {
    x = x + 1;
    if(pk &amp;lt; 0)
      pk = pk + 2*x+1;
    else
    {
      y = y - 1;
      pk = pk + 2*(x - y) + 1;
    }
    plot_point(x,y);
  }
  glFlush();
}

// Function to draw two concentric circles
void concentric_circles(void)
{
  /* Clears buffers to preset values */
  glClear(GL_COLOR_BUFFER_BIT);

  int radius1 = 100, radius2 = 200;
  bresenham_circle(radius1);
  bresenham_circle(radius2);
}

void Init()
{
  /* Set clear color to white */
  glClearColor(1.0,1.0,1.0,0);
  /* Set fill color to black */
  glColor3f(0.0,0.0,0.0);
  /* glViewport(0 , 0 , 640 , 480); */
  /* glMatrixMode(GL_PROJECTION); */
  /* glLoadIdentity(); */
  gluOrtho2D(0 , 640 , 0 , 480);
}

void main(int argc, char **argv)
{
  /* Initialise GLUT library */
  glutInit(&amp;amp;argc,argv);
  /* Set the initial display mode */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  /* Set the initial window position and size */
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  /* Create the window with title &quot;DDA_Line&quot; */
  glutCreateWindow(&quot;bresenham_circle&quot;);
  /* Initialize drawing colors */
  Init();
  /* Call the displaying function */
  glutDisplayFunc(concentric_circles);
  /* Keep displaying untill the program is closed */
  glutMainLoop();
}&lt;/code&gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/4298630970690228029/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2014/12/bresenhams-circle-drawing-algorithm.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/4298630970690228029'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/4298630970690228029'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2014/12/bresenhams-circle-drawing-algorithm.html' title='Bresenham&#39;s Circle Drawing Algorithm using OpenGL'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-5565800679704052846</id><published>2014-12-10T20:21:00.001-08:00</published><updated>2014-12-10T20:34:32.868-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bresenham"/><category scheme="http://www.blogger.com/atom/ns#" term="computer graphics"/><category scheme="http://www.blogger.com/atom/ns#" term="generalised bresenham"/><category scheme="http://www.blogger.com/atom/ns#" term="line drawing"/><title type='text'>Generalized Bresenham&#39;s Line Drawing Algorithm using OpenGL</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Bresenham&#39;s line algorithm &lt;/b&gt;is an algorithm that determines which points in an n-dimensional raster should be plotted in order to form a close approximation to a straight line between two given points. It is commonly used to draw lines on a computer screen, as it uses only integer addition, subtraction and bit shifting, all of which are very cheap operations in standard computer architectures[&lt;a href=&quot;http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm&quot;&gt;&lt;b&gt;source&lt;/b&gt;&lt;/a&gt;].

For more visit &lt;a href=&quot;http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html&quot;&gt;&lt;b&gt;this website&lt;/b&gt;&lt;/a&gt;.


&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;GL/gl.h&amp;gt;
#include&amp;lt;GL/glut.h&amp;gt;

/* Function that returns -1,0,1 depending on whether x */
/* is &amp;lt;0, =0, &amp;gt;0 respectively */
#define sign(x) ((x&amp;gt;0)?1:((x&amp;lt;0)?-1:0))

/* Function to plot a point */
void setPixel(GLint x, GLint y) {
  glBegin(GL_POINTS);
  glVertex2i(x,y);
  glEnd();
}

/* Generalized Bresenham&#39;s Algorithm */
void bres_general(int x1, int y1, int x2, int y2)
{
  int dx, dy, x, y, d, s1, s2, swap=0, temp;

  dx = abs(x2 - x1);
  dy = abs(y2 - y1);
  s1 = sign(x2-x1);
  s2 = sign(y2-y1);

  /* Check if dx or dy has a greater range */
  /* if dy has a greater range than dx swap dx and dy */
  if(dy &amp;gt; dx){temp = dx; dx = dy; dy = temp; swap = 1;}

  /* Set the initial decision parameter and the initial point */
  d = 2 * dy - dx;
  x = x1;
  y = y1;

  int i;
  for(i = 1; i &amp;lt;= dx; i++)
  {
    setPixel(x,y);
    
    while(d &amp;gt;= 0) 
    {
      if(swap) x = x + s1;
      else 
      {
        y = y + s2;
        d = d - 2* dx;
      }
    }
    if(swap) y = y + s2;
    else x = x + s1;
    d = d + 2 * dy;
  }
  glFlush();
}

/* Function to draw a rhombus inscribed in a rectangle and roll */
/* number printed in it */
void draw(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
 
  /* Draw rectangle */
  bres_general(20,40,620,40);
  bres_general(620,40,620,440);
  bres_general(620,440,20,440);
  bres_general(20,440,20,40);

  /* Draw rhombus */
  bres_general(320,440,20,240);
  bres_general(20,240,320,40);
  bres_general(320,40,620,240);
  bres_general(620,240,320,440);

  /* 1 */
  bres_general(250,150,250,250);
  /* 0 */
  bres_general(300,150,300,250);
  bres_general(300,250,400,250);
  bres_general(400,250,400,150);
  bres_general(400,150,300,150);

  glFlush();
}

void init() {  
  glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640, 480);
  glutCreateWindow(&quot;Green Window&quot;);
  glClearColor(0.0,0.0,0.0,0);
  glColor3f(1.0,1.0,1.0);
  gluOrtho2D(0,640,0,480);
  
}

int main(int argc, char **argv) 
{
  glutInit(&amp;amp;argc, argv);
  init();
  glutDisplayFunc(draw);
  glutMainLoop();
  return 0;
}
&lt;/code&gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/5565800679704052846/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2014/12/generalized-bresenhams-line-drawing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/5565800679704052846'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/5565800679704052846'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2014/12/generalized-bresenhams-line-drawing.html' title='Generalized Bresenham&#39;s Line Drawing Algorithm using OpenGL'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-7750573848185904027</id><published>2014-12-01T04:04:00.000-08:00</published><updated>2014-12-01T04:04:20.179-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="pattern replacement"/><category scheme="http://www.blogger.com/atom/ns#" term="string"/><category scheme="http://www.blogger.com/atom/ns#" term="string pattern replacement"/><category scheme="http://www.blogger.com/atom/ns#" term="string replacement"/><title type='text'>String replacement program in C</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
A program to find a particular pattern in a string and replace all occurrences of that pattern with a new pattern to produce a modified string.
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;

&lt;/code&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;string.h&amp;gt;

void main()
{
  char str[100], pattern[50], replace[50], new_str[100], temp[50];
  int l1, l2, l3, flag_eos = 0, pattern_found = 0;
  int i = 0, j, k, n;
  printf(&quot;\n###################################\n&quot;);
  printf(&quot;Enter any string\n&quot;);
  gets(str);
  printf(&quot;\n###################################\n&quot;);
  printf(&quot;Enter pattern to be replaced\n&quot;);
  gets(pattern);
  printf(&quot;\n###################################\n&quot;);
  printf(&quot;Enter new pattern\n&quot;);
  gets(replace);

  l1 = strlen(str);
  l2 = strlen(pattern);
  l3 = strlen(replace);
 
  while(flag_eos == 0)
  {
    for(; i &amp;lt; l1; i++)
      // if any character of str = first character of the pattern
      if((str[i] == pattern[0]) &amp;amp;&amp;amp; (i + l2 - 1 &amp;lt; l1))
      {
        for(j = i, k = 0; j &amp;lt; i + l2; j++)
          temp[k++] = str[j];
        temp[k] = &#39;\0&#39;;
        break;
      }
    if(i == l1)
    {
      flag_eos = 1;
      temp[0] = &#39;\0&#39;;
    }
    if(strcmp(temp, pattern) == 0)
    {
      pattern_found = 1;
      n = 0;
      for(j = 0; j &amp;lt; i; j++)
        new_str[n++] = str[j];
      for(j = 0; j &amp;lt; l3; j++)
        new_str[n++] = replace[j];
      for(j = i + l2; j &amp;lt; l1; j++)
        new_str[n++] = str[j];
      new_str[n] = &#39;\0&#39;;
      // Recompute the new string length and
      // continue to look for more occurrences
      strcpy(str, new_str);
      l1 = strlen(str);
    }
    else
      i = i + 1;
  }
  if(pattern_found == 1)
  {
    printf(&quot;\n###################################\n&quot;);
    printf(&quot;The modified string is \n&quot;);
    puts(str);
    printf(&quot;###################################\n&quot;);
  }
  else
  {
    printf(&quot;\n###################################\n&quot;);
    printf(&quot;Pattern not found in the string !!!\n&quot;);
    printf(&quot;###################################\n&quot;);
  }
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/7750573848185904027/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2014/12/string-replacement-program-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/7750573848185904027'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/7750573848185904027'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2014/12/string-replacement-program-in-c.html' title='String replacement program in C'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-5780926735880155555</id><published>2014-11-16T08:30:00.000-08:00</published><updated>2014-11-16T08:37:33.992-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DDA"/><category scheme="http://www.blogger.com/atom/ns#" term="DDA line drawing"/><category scheme="http://www.blogger.com/atom/ns#" term="DDA line drawing algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="DDA using opengl"/><category scheme="http://www.blogger.com/atom/ns#" term="opengl"/><title type='text'>Implementation of DDA line drawing algorithm in OpenGL</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In computer graphics, a digital differential analyzer (DDA) is hardware or software used for linear interpolation of variables over an interval between start and end point. DDAs are used for rasterization of lines, triangles and polygons.
For more on DDA : Visit &lt;a href=&quot;https://www.google.co.in/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=2&amp;ved=0CCQQFjAB&amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDigital_differential_analyzer_(graphics_algorithm)&amp;ei=s89oVJD9BYXdsATm1ICICA&amp;usg=AFQjCNEA4PFjh8WcLuP8vmtoKfbuyDk88g&amp;sig2=PF-BGC_cq1Emd4VhsmJ2lg&amp;bvm=bv.79142246,d.cWc&amp;cad=rja&quot;&gt;Digital Differential Analyzer - Wikipedia page&lt;/a&gt;. The following code is a DDA line drawing algorithm that draws a line between two given points.
&lt;pre class=&quot;prettyprint&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;math.h&amp;gt;
#include &amp;lt;GL/glut.h&amp;gt;

double X1, Y1, X2, Y2;

float round_value(float v)
{
  return floor(v + 0.5);
}
void LineDDA(void)
{
  double dx=(X2-X1);
  double dy=(Y2-Y1);
  double steps;
  float xInc,yInc,x=X1,y=Y1;
  /* Find out whether to increment x or y */
  steps=(abs(dx)&amp;gt;abs(dy))?(abs(dx)):(abs(dy));
  xInc=dx/(float)steps;
  yInc=dy/(float)steps;

  /* Clears buffers to preset values */
  glClear(GL_COLOR_BUFFER_BIT);

  /* Plot the points */
  glBegin(GL_POINTS);
  /* Plot the first point */
  glVertex2d(x,y);
  int k;
  /* For every step, find an intermediate vertex */
  for(k=0;k&amp;lt;steps;k++)
  {
    x+=xInc;
    y+=yInc;
    /* printf(&quot;%0.6lf %0.6lf\n&quot;,floor(x), floor(y)); */
    glVertex2d(round_value(x), round_value(y));
  }
  glEnd();

  glFlush();
}
void Init()
{
  /* Set clear color to white */
  glClearColor(1.0,1.0,1.0,0);
  /* Set fill color to black */
  glColor3f(0.0,0.0,0.0);
  /* glViewport(0 , 0 , 640 , 480); */
  /* glMatrixMode(GL_PROJECTION); */
  /* glLoadIdentity(); */
  gluOrtho2D(0 , 640 , 0 , 480);
}
void main(int argc, char **argv)
{
  printf(&quot;Enter two end points of the line to be drawn:\n&quot;);
  printf(&quot;\n************************************&quot;);
  printf(&quot;\nEnter Point1( X1 , Y1):\n&quot;);
  scanf(&quot;%lf%lf&quot;,&amp;amp;X1,&amp;amp;Y1);
  printf(&quot;\n************************************&quot;);
  printf(&quot;\nEnter Point1( X2 , Y2):\n&quot;);
  scanf(&quot;%lf%lf&quot;,&amp;amp;X2,&amp;amp;Y2);
  
  /* Initialise GLUT library */
  glutInit(&amp;amp;argc,argv);
  /* Set the initial display mode */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  /* Set the initial window position and size */
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  /* Create the window with title &quot;DDA_Line&quot; */
  glutCreateWindow(&quot;DDA_Line&quot;);
  /* Initialize drawing colors */
  Init();
  /* Call the displaying function */
  glutDisplayFunc(LineDDA);
  /* Keep displaying untill the program is closed */
  glutMainLoop();
}
&lt;/pre&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/5780926735880155555/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2014/11/implementation-of-dda-line-drawing_90.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/5780926735880155555'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/5780926735880155555'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2014/11/implementation-of-dda-line-drawing_90.html' title='Implementation of DDA line drawing algorithm in OpenGL'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-87335215721773963</id><published>2013-08-29T01:11:00.000-07:00</published><updated>2013-08-29T01:21:47.143-07:00</updated><title type='text'>Noise removal from foreground and background area in an image using opencv (python)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;
&lt;/code&gt;
import cv2
import numpy as np

# To display a single image in a window
# Window is destroyed on pressing any key
def display(windowName, image):
  cv2.namedWindow(windowName, 1)
  showtime(windowName, image)
  cv2.waitKey(0)
  cv2.destroyAllWindows()

# Read image
img = cv2.imread(&#39;imagename.jpg&#39;)
# Convert to grayscale image
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
display(&#39;gray&#39;, gray)
# Convert to binary image
ret,thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
display(&#39;binary&#39;, thresh)

# noise removal
# to remove any small white noises use morphological opening
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
sure_bg = cv2.dilate(opening,kernel,iterations=3)
display(&#39;Sure Background&#39;, sure_bg)

dist_transform = cv2.distanceTransform(opening,cv.CV_DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)
display(&#39;Sure Foreground&#39;, sure_fg)

# Finding unknown region
unknown = cv2.subtract(sure_bg,sure_fg)
display(&#39;unknown area&#39;, unknown)

&lt;/pre&gt;
&lt;br /&gt;
For knowing more on morphological transformations using opening and closing refer &lt;a href=&quot;http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html&quot;&gt;Morphological Transformation&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/87335215721773963/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2013/08/foregroundbackgroundimageopencvpython.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/87335215721773963'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/87335215721773963'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2013/08/foregroundbackgroundimageopencvpython.html' title='Noise removal from foreground and background area in an image using opencv (python)'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-8415662817043944727</id><published>2013-06-19T23:55:00.001-07:00</published><updated>2013-06-19T23:55:16.391-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="classes in C++"/><category scheme="http://www.blogger.com/atom/ns#" term="interview questions"/><category scheme="http://www.blogger.com/atom/ns#" term="programming"/><category scheme="http://www.blogger.com/atom/ns#" term="programming in c++"/><category scheme="http://www.blogger.com/atom/ns#" term="use of the keyword virtual in C++"/><category scheme="http://www.blogger.com/atom/ns#" term="virtual keyword"/><title type='text'>Interview Question (Programming in C++)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Give the output of the following program :
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;
class Animal
{
  public :
  virtual void draw()
  {
    cout&amp;lt;&amp;lt;”Animal”;
  }
};
class Leopard : public Animal
{
  public :
  virtual void draw()
  {
    cout&amp;lt;&amp;lt;”Leopard”;
  }
};
main()
{
  Leopard l;
  Animal *a=&amp;amp;l;
  l.draw();
}
&lt;/code&gt;
&lt;/pre&gt;
(A) Leopard
(B) Animal
(C) LeopardAnimal
(D) AnimalLeopard


&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Answer :&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
(A) Leopard&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Explanation :&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
The &lt;i&gt;virtual&lt;/i&gt; keyword &amp;nbsp;indicates to the compiler that it should choose the appropriate definition of the function &lt;i&gt;draw&lt;/i&gt;&amp;nbsp;not by the type of reference, but by the type of object that the reference refers to.&lt;br /&gt;
For more details on the use of &lt;i&gt;virtual&lt;/i&gt; keyword : &lt;a href=&quot;http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr139.htm&quot;&gt;Reference&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;

&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/8415662817043944727/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2013/06/interview-question-programming_19.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/8415662817043944727'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/8415662817043944727'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2013/06/interview-question-programming_19.html' title='Interview Question (Programming in C++)'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-7231570098976105999</id><published>2013-06-19T22:55:00.000-07:00</published><updated>2013-07-05T06:37:31.998-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AND operator"/><category scheme="http://www.blogger.com/atom/ns#" term="interview questions"/><category scheme="http://www.blogger.com/atom/ns#" term="programming"/><category scheme="http://www.blogger.com/atom/ns#" term="programming in c"/><title type='text'>Interview Question (Programming)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
What does the following function check?
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;
bool f(unsigned int v)
{
  return ((v!=0)&amp;amp;&amp;amp;!(v &amp;amp; v-1));
}
&lt;/code&gt;
&lt;/pre&gt;
(A) v is an odd number
(B) v is a multiple of 2
(C) v is a power of 2
(D) v has a 0 bit&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;u&gt;Answer :&lt;/u&gt;&lt;/b&gt;

&lt;br /&gt;
(C) v is a power of 2&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;Explanation :&lt;/b&gt;&lt;/u&gt; &lt;br /&gt;
1) If v is a power of 2,&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;v is never 0 (even 2^0 = 1) and (v != 0) will always be 1(True).&lt;/li&gt;
&lt;li&gt;(v &amp;amp; v-1) will always be 0(False) and hence their negation will always be 1(True).&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
2) If v is not a power of 2, either&lt;/div&gt;
&lt;div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;v = 0, or&lt;/li&gt;
&lt;li&gt;(v &amp;amp; v-1) is something other than 0 and their negation is 0(False)&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;

&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/7231570098976105999/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2013/06/interview-question-programming.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/7231570098976105999'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/7231570098976105999'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2013/06/interview-question-programming.html' title='Interview Question (Programming)'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-6676379356696790322</id><published>2013-06-05T23:22:00.000-07:00</published><updated>2013-07-14T08:28:33.296-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="interview questions"/><category scheme="http://www.blogger.com/atom/ns#" term="puzzles"/><title type='text'>Cut the Gold Bar Twice and Pay for 7 Days - Puzzle#1</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;u&gt;Puzzle&lt;/u&gt;&lt;/b&gt; : You have a 7 inch gold bar with you. You need to pay the servant 1 inch gold per day of work. (At the end of first day, servant should have 1 inch gold; at the end of second day, he should have 2 inch gold and so on). You may take back some of the given pieces if needed. How will you do this with minimum number of cuts of the gold bar?&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;b&gt;&lt;u&gt;Answer&lt;/u&gt;&lt;/b&gt; : 2 cuts (i.e 3 pieces =&amp;gt; a 4 inch piece, a 2 inch piece and a 1 inch piece)&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;b&gt;&lt;u&gt;Explanation&lt;/u&gt;&lt;/b&gt; :
TB = Take back&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiztdQI63CWOnuwjDoaH8Vus968eK5L_N_r259UICb8uBElITxWJl2xOq1VBHmY5t1z3M7wfA4dvSRRGgG966ArUslR1kVjpXxccLUuOivT5jLCv1dXD52QGOQt8LaFa9eDm4wF0s82evk/s1600/puzzl.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;296&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiztdQI63CWOnuwjDoaH8Vus968eK5L_N_r259UICb8uBElITxWJl2xOq1VBHmY5t1z3M7wfA4dvSRRGgG966ArUslR1kVjpXxccLUuOivT5jLCv1dXD52QGOQt8LaFa9eDm4wF0s82evk/s640/puzzl.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;&lt;br /&gt;&lt;/small&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;

&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/6676379356696790322/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2013/06/puzzle-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/6676379356696790322'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/6676379356696790322'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2013/06/puzzle-1.html' title='Cut the Gold Bar Twice and Pay for 7 Days - Puzzle#1'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiztdQI63CWOnuwjDoaH8Vus968eK5L_N_r259UICb8uBElITxWJl2xOq1VBHmY5t1z3M7wfA4dvSRRGgG966ArUslR1kVjpXxccLUuOivT5jLCv1dXD52QGOQt8LaFa9eDm4wF0s82evk/s72-c/puzzl.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-7569738289697308561</id><published>2013-06-01T22:39:00.000-07:00</published><updated>2013-07-05T06:33:36.149-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="convolution"/><category scheme="http://www.blogger.com/atom/ns#" term="matrix convolution"/><category scheme="http://www.blogger.com/atom/ns#" term="openCV"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>Convolution of Two Images (matrix form) - OpenCV - Python</title><content type='html'>&lt;pre class=&quot;prettyprint&quot;&gt;
To perform convolution of two matrices

&lt;code class=&quot;lang-c&quot;&gt;
import cv2.cv as cv
import sys

if __name__ == &#39;__main__&#39;:
  mat1 = cv.CreateMat(3, 3, 8)
  mat2 = cv.CreateMat(3, 3, 8)
  dst = cv.CreateImage(cv.GetSize(mat1), 8, 3)

  kernel = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
  matrix2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  for i in range(3):
    for j in range(3):
      mat1[i][j] = kernel[i][j]
  for i in range(3):
    for j in range(3):
      mat2[i][j] = matrix2[i][j]

  cv.NamedWindow(&quot;convolution&quot;, 1)
  cv.Filter2D(mat2, dst, mat1)
  cv.ShowImage(&#39;convolution&#39;, dst)

  print &#39;Press any key to quit&#39;
  cv.WaitKey(0)
  print &#39;Exiting...&#39;
  cv.DestroyAllWindows()
  sys.exit(0)
&lt;/code&gt;
&lt;/pre&gt;

&lt;br/&gt;

&lt;small&gt;Written by &lt;a rel=&quot;author&quot; href=&quot;https://plus.google.com/105162279647438527853/&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;br/&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/7569738289697308561/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2013/06/convolution-of-two-images-matrix-form.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/7569738289697308561'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/7569738289697308561'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2013/06/convolution-of-two-images-matrix-form.html' title='Convolution of Two Images (matrix form) - OpenCV - Python'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-3909979794197498758</id><published>2013-06-01T22:36:00.000-07:00</published><updated>2013-07-05T06:34:22.517-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="convolution"/><category scheme="http://www.blogger.com/atom/ns#" term="image convolution"/><category scheme="http://www.blogger.com/atom/ns#" term="openCV"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>Convolution of Two Images - OpenCV - Python</title><content type='html'>&lt;pre class=&quot;prettyprint&quot;&gt;
# To convolute two images img1.bmp and img2.bmp

In mathematics and, in particular, functional analysis, convolution is a m-
athematical operation on two functions f and g, producing a third function
that is typically viewed as a modified version of one of the original func-
tions, giving the area overlap between the two functions as a function of 
the amount that one of the original functions is translated.
-&lt;a href=&quot;http://en.wikipedia.org/wiki/Convolution&quot;&gt;Wikipedia&lt;/a&gt;

&lt;code class=&quot;lang-c&quot;&gt;
import cv2.cv as cv
import sys

if __name__ == &quot;__main__&quot;:
 # Load two images
 im1 = cv.LoadImageM(&quot;img1.bmp&quot;)
 im2 = cv.LoadImageM(&quot;img2.bmp&quot;)
 # Create a destination image of the same size that of the source
 dst = cv.CreateImage(cv.GetSize(im1), 8, 3)

 # Create a window named &quot;convolution(1 for colour)&quot;
 cv.NamedWindow(&quot;convolution&quot;, 1)
 # Convolute the 2 images
 cv.Filter2D(im1, dst, im2)
 # Show the convoluted result
 cv.ShowImage(&#39;convolution&#39;, dst)

 # Wait for any key press to close the window
 print &#39;Press any key to quit&#39;
 cv.WaitKey(0)
 # Destroy all created windows and exit
 print &#39;Exiting...&#39;
 cv.DestroyAllWindows()
 sys.exit(0)
&lt;/code&gt;
&lt;/pre&gt;

&lt;br/&gt;

&lt;small&gt;Written by &lt;a rel=&quot;author&quot; href=&quot;https://plus.google.com/105162279647438527853/&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;br/&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/3909979794197498758/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2013/06/convolution-openCV-python.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/3909979794197498758'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/3909979794197498758'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2013/06/convolution-openCV-python.html' title='Convolution of Two Images - OpenCV - Python'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-6250302686430167727</id><published>2013-03-29T04:54:00.000-07:00</published><updated>2013-03-30T23:03:19.213-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="gate 2013"/><title type='text'>GATE 2013 - Papers, Answer Keys and Solutions</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
GATE 2013 Question papers and the corresponding answer keys are now available for download. These are the official questions papers and answer keys provided by &lt;a href=&quot;http://www.gate.iitb.ac.in/gate2013/&quot; target=&quot;_blank&quot;&gt;IITB&lt;/a&gt;. They are also available from IITB GATE 2013 website.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;http://www.codeblocks.info/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html&quot; target=&quot;_blank&quot; onclick=&quot;_gaq.push([&#39;_trackEvent&#39;, &#39;Gate Links&#39;, &#39;Gate 2013&#39;, &#39;CS&#39;]);&quot;&gt;Computer Science &amp;amp; Information Technology&lt;/a&gt;&lt;/h3&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Question Papers&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.codeblocks.info/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html#cs-2013-paper-code-a&quot; target=&quot;_blank&quot;&gt;Code A&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.codeblocks.info/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html#cs-2013-paper-code-b&quot; target=&quot;_blank&quot;&gt;Code B&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.codeblocks.info/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html#cs-2013-paper-code-c&quot; target=&quot;_blank&quot;&gt;Code C&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.codeblocks.info/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html#cs-2013-paper-code-d&quot; target=&quot;_blank&quot;&gt;Code D&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.codeblocks.info/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html#cs-2013-answer-keys&quot; target=&quot;_blank&quot;&gt;Answer Key&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/110282636899691244440/&quot; rel=&quot;author&quot;&gt;Code Blocks&lt;/a&gt;&lt;/small&gt;&lt;br /&gt;
&lt;small&gt;Information courtesy: &lt;a href=&quot;http://www.gate.iitb.ac.in/gate2013/&quot; target=&quot;_blank&quot;&gt;IITB GATE 2013 Website&lt;/a&gt;&lt;/small&gt;

&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/6250302686430167727/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2013/03/gate-2013-papers-answer-keys-and-solutions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/6250302686430167727'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/6250302686430167727'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2013/03/gate-2013-papers-answer-keys-and-solutions.html' title='GATE 2013 - Papers, Answer Keys and Solutions'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09100860436377691690</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-8016763939284129699</id><published>2013-03-29T04:46:00.001-07:00</published><updated>2013-03-30T23:00:58.139-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="gate 2013"/><title type='text'>GATE 2013 - CS/IT Question Papers, Answer Keys and Solutions</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
GATE 2013 &lt;b&gt;Computer Science and Information Technology&lt;/b&gt; Question Papers are available under &lt;i&gt;4 paper codes&lt;/i&gt;; &lt;b&gt;A, B, C &amp;amp; D&lt;/b&gt;. All of them have the same questions (both in number and content) but presented in different order. You can view the questions papers here or download for offline use by clicking the corresponding &lt;i&gt;&amp;quot;&lt;b&gt;Download&amp;quot;&lt;/b&gt;&lt;/i&gt; button.&lt;br&gt;
&lt;br&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;

&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;
«&lt;a href=&quot;http://www.codeblocks.info/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html#cs-2013-paper-code-a&quot; onclick=&quot;_gaq.push([&amp;#39;_trackEvent&amp;#39;, &amp;#39;Gate Links&amp;#39;, &amp;#39;Gate 2013&amp;#39;, &amp;#39;Question Paper A&amp;#39;]);&quot;&gt;Paper Code A&lt;/a&gt;» 
«&lt;a href=&quot;http://www.codeblocks.info/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html#cs-2013-paper-code-b&quot; onclick=&quot;_gaq.push([&amp;#39;_trackEvent&amp;#39;, &amp;#39;Gate Links&amp;#39;, &amp;#39;Gate 2013&amp;#39;, &amp;#39;Question Paper B&amp;#39;]);&quot;&gt;Paper Code B&lt;/a&gt;» 
«&lt;a href=&quot;http://www.codeblocks.info/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html#cs-2013-paper-code-c&quot; onclick=&quot;_gaq.push([&amp;#39;_trackEvent&amp;#39;, &amp;#39;Gate Links&amp;#39;, &amp;#39;Gate 2013&amp;#39;, &amp;#39;Question Paper C&amp;#39;]);&quot;&gt;Paper Code C&lt;/a&gt;» 
«&lt;a href=&quot;http://www.codeblocks.info/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html#cs-2013-paper-code-d&quot; onclick=&quot;_gaq.push([&amp;#39;_trackEvent&amp;#39;, &amp;#39;Gate Links&amp;#39;, &amp;#39;Gate 2013&amp;#39;, &amp;#39;Question Paper D&amp;#39;]);&quot;&gt;Paper Code D&lt;/a&gt;» 
«&lt;a href=&quot;http://www.codeblocks.info/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html#cs-2013-answer-keys&quot; onclick=&quot;_gaq.push([&amp;#39;_trackEvent&amp;#39;, &amp;#39;Gate Links&amp;#39;, &amp;#39;Gate 2013&amp;#39;, &amp;#39;Answer Keys&amp;#39;]);&quot;&gt;Answer Keys&lt;/a&gt;»&lt;/span&gt;&lt;br&gt;

&lt;br&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;a href=&quot;http://code-blocks.blogspot.com/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html#more&quot;&gt;Read more »&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/8016763939284129699/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/8016763939284129699'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/8016763939284129699'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2013/03/gate-2013-cse-it-question-papers-answer-keys-and-solutions.html' title='GATE 2013 - CS/IT Question Papers, Answer Keys and Solutions'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09100860436377691690</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-1685754202940075990</id><published>2013-01-07T20:25:00.000-08:00</published><updated>2013-01-08T03:07:28.000-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="bellman ford"/><category scheme="http://www.blogger.com/atom/ns#" term="greedy technique"/><title type='text'>Bellman-Ford Algorithm - Shortest Path Algorithm </title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;i&gt;&quot;The &lt;b&gt;Bellman–Ford algorithm&lt;/b&gt; computes &lt;b&gt;single-source shortest paths&lt;/b&gt; in a weighted digraph.For graphs with only non-negative edge weights, the faster Dijkstra&#39;s algorithm also solves the problem. Thus, Bellman–Ford is used primarily for graphs with &lt;b&gt;negative edge weights&lt;/b&gt;. The algorithm is named after its developers, Richard Bellman and Lester Ford, Jr.&amp;nbsp;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;
&lt;i&gt;Negative edge weights are found in various applications of graphs, hence the usefulness of this algorithm. However, if a graph contains a &quot;negative cycle&quot;, i.e., a cycle whose edges sum to a negative value, then walks of arbitrarily low weight can be constructed by repeatedly following the cycle, so there may not be a shortest path. In such a case, the Bellman-Ford algorithm can detect negative cycles and report their existence, but it cannot produce a correct &quot;shortest path&quot; answer if a negative cycle is reachable from the source.&quot;&lt;/i&gt; - &lt;a href=&quot;http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm&quot; target=&quot;_blank&quot;&gt;Wikipedia&lt;/a&gt;
&lt;br /&gt;

&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg83kqJYu0tUGoDvYz6h5IeTXmMfLBisLPCLBYAGkaQUaJwCm-_YMOL5cLrL3n2EhoVCwEEac6HI2shoh50BaHywhowGKz2g9xtTkex6noyzaM0LAG3_hWb-UzeH-emofPr3setdFoHUzRM/s1600/3.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;331&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg83kqJYu0tUGoDvYz6h5IeTXmMfLBisLPCLBYAGkaQUaJwCm-_YMOL5cLrL3n2EhoVCwEEac6HI2shoh50BaHywhowGKz2g9xtTkex6noyzaM0LAG3_hWb-UzeH-emofPr3setdFoHUzRM/s400/3.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
&lt;code class=&quot;lang-c&quot;&gt;
INITIALISE_SINGLE_SOURCE(G, start)
{
  for each vertex v ∈ v(G)
    dist[v] &lt;- ∞
    pred[v] &lt;- NIL
  dist[start] &lt;- 0
}
// dist[v] &lt;- distance of vertex &quot;v&quot; from vertex &quot;start&quot;
// pred[v] &lt;- predecessor of &quot;v&quot; in the shortest path 
//            from &quot;start&quot;

RELAX(u, v, cost)
{
  if dist[v] &gt; dist[u] + cost[u, v]
    dist[v] &lt;- dist[u] + cost[u, v]
    pred[v] &lt;- u
} 

BELLMAN_FORD(G, cost, start)
{
  INITIALISE_SINGLE_SOURCE(G, start)
  for i &lt;- 1 to |v(G)| - 1
    do for each edge (u, v) ∈ E[G]
      do RELAX(u, v, cost)
  for each edge (u, v) ∈ E[G]
    do if dist[v] &gt; dist[u] + cost[u, v]
      then return FALSE
  return TRUE
}
&lt;/code&gt;
&lt;/pre&gt;

&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;br /&gt;
&lt;small&gt;
Image courtesy - &lt;a href=&quot;http://arsitekturrouting028089090110.blogspot.in/&quot;&gt;ArsRout&lt;/a&gt;
&lt;/small&gt;
&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/1685754202940075990/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2013/01/bellman-ford.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/1685754202940075990'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/1685754202940075990'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2013/01/bellman-ford.html' title='Bellman-Ford Algorithm - Shortest Path Algorithm '/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg83kqJYu0tUGoDvYz6h5IeTXmMfLBisLPCLBYAGkaQUaJwCm-_YMOL5cLrL3n2EhoVCwEEac6HI2shoh50BaHywhowGKz2g9xtTkex6noyzaM0LAG3_hWb-UzeH-emofPr3setdFoHUzRM/s72-c/3.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-2990920404077061772</id><published>2013-01-05T00:32:00.000-08:00</published><updated>2013-01-08T02:58:00.734-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="dijkstra"/><category scheme="http://www.blogger.com/atom/ns#" term="dijkstra&#39;s algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="greedy technique"/><title type='text'>Dijkstra&#39;s Algorithm - Shortest Path Algorithm</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;i&gt;&quot;&lt;b&gt;Dijkstra&#39;s algorithm&lt;/b&gt;, conceived by Dutch computer scientist &lt;b&gt;Edsger Dijkstra&lt;/b&gt; in 1956 and published in 1959, is a &lt;b&gt;graph search algorithm&lt;/b&gt; that solves the &lt;b&gt;single-source shortest path&lt;/b&gt; problem for a graph with &lt;b&gt;non negative edge path costs&lt;/b&gt;, producing a shortest path tree.&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;
&lt;i&gt;For a given source vertex (node) in the graph, the algorithm finds the path with lowest cost (i.e. the shortest path) between that vertex and every other vertex. It can also be used for finding costs of shortest paths from a single vertex to a single destination vertex by stopping the algorithm once the shortest path to the destination vertex has been determined.&quot;&lt;/i&gt; - &lt;a href=&quot;http://en.wikipedia.org/wiki/Dijkstra&#39;s_algorithm&quot; target=&quot;_blank&quot;&gt;Wikipedia&lt;/a&gt;&lt;br /&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
&lt;code class=&quot;lang-c&quot;&gt;
INITIALISE_SINGLE_SOURCE(G, start)
{
  for each vertex v ∈ v(G)
    dist[v] &lt;- ∞
    pred[v] &lt;- NIL
  dist[start] &lt;- 0
}
// dist[v] &lt;- distance of vertex &quot;v&quot; from vertex &quot;start&quot;
// pred[v] &lt;- predecessor of &quot;v&quot; in the shortest path 
//            from &quot;start&quot;

RELAX(u, v, cost)
{
  if dist[v] &gt; dist[u] + cost[u, v]
    dist[v] &lt;- dist[u] + cost[u, v]
    pred[v] &lt;- u
} 

DIJKSTRA(G, cost, start)
{
  INITIALISE_SINGLE_SOURCE(G, start)
  S &lt;- φ
  Q &lt;- V[G]
  while Q ≠ φ do
    u &lt;- EXTRACT_MIN(Q)
    S &lt;- S U {u}
    for each vertex v ∈ Adj[u]
      do RELAX(u, v, cost)
}
// S &lt;- Set of vertices whose final shortest path weights
//      from the source (start) have already been determined
// Q &lt;- min_priority queue of vertices, keyed by their &#39;dist&#39;
//      values

&lt;/code&gt;
&lt;/pre&gt;

&lt;br/&gt;
&lt;small&gt;Written by &lt;a rel=&quot;author&quot; href=&quot;https://plus.google.com/105162279647438527853/&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;
&lt;br/&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/2990920404077061772/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2013/01/dijkstra-algorithm.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/2990920404077061772'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/2990920404077061772'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2013/01/dijkstra-algorithm.html' title='Dijkstra&#39;s Algorithm - Shortest Path Algorithm'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-889894695214298797</id><published>2012-12-04T08:31:00.002-08:00</published><updated>2012-12-04T09:02:43.060-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="computer science"/><title type='text'>Cloud Computing - Characteristics, Service Models &amp; Deployment Models</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Cloud computing is a model for enabling ubiquitous, convenient, on-demand network access to a shared pool of configurable computing resources (e.g., networks, servers, storage, applications, and services) that can be rapidly provisioned and released with minimal management effort or service provider interaction. This cloud model is composed of five essential characteristics, three service models, and four deployment models.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://wiki.cloudsecurityalliance.org/guidance/images/a/a0/NIST_Visual_Model_of_Cloud_Computing_Definition.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;300&quot; src=&quot;https://wiki.cloudsecurityalliance.org/guidance/images/a/a0/NIST_Visual_Model_of_Cloud_Computing_Definition.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Visual Model of Cloud Computing&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Essential Characteristics:&lt;/b&gt;&lt;/h2&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;On-demand self-service:&lt;/u&gt;&lt;/b&gt;&amp;nbsp;A consumer can unilaterally provision computing capabilities, such as server time and network storage, as needed automatically without requiring human interaction with each service provider.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Broad network access:&lt;/u&gt;&lt;/b&gt; Capabilities are available over the network and accessed through standard mechanisms that promote use by &amp;nbsp;heterogeneous thin or thick &amp;nbsp;client platforms (e.g., mobile phones, tablets, laptops, and workstations).&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Resource pooling:&lt;/u&gt;&lt;/b&gt; The provider’s computing resources are pooled to serve multiple consumers using a multi-tenant model, with different physical and virtual resources dynamically assigned and reassigned according to consumer demand. There is a sense of location independence in that the customer generally has no control or knowledge over the exact location of the provided resources but may be able to specify location at a higher level of abstraction (e.g., country, state, or datacenter). Examples of resources include storage, processing, memory, and network bandwidth.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Rapid elasticity:&lt;/u&gt;&lt;/b&gt; Capabilities can be elastically provisioned and released, in some cases automatically, to scale rapidly outward and inward commensurate with demand. To the consumer, the capabilities available for provisioning often appear to be unlimited and can be appropriated in any quantity at any time.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Measured service:&lt;/u&gt;&lt;/b&gt; Cloud systems automatically control and optimize resource use by leveraging a metering capability at some level of abstraction appropriate to the type of service (e.g., storage, processing, bandwidth, and active user accounts). Resource usage can be monitored, controlled, and reported, providing transparency for both the provider and consumer of the utilized service.&lt;br /&gt;
&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
Service Models:&lt;/h2&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;http://upload.wikimedia.org/wikipedia/commons/3/3c/Cloud_computing_layers.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;296&quot; src=&quot;http://upload.wikimedia.org/wikipedia/commons/3/3c/Cloud_computing_layers.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Software as a Service (SaaS):&lt;/u&gt;&lt;/b&gt; The capability provided to the consumer is to use the provider’s applications running on a cloud infrastructure. The applications are accessible from various client devices through either a thin client interface, such as a web browser (e.g., web-based email), or a program interface. The consumer does not manage or control the underlying cloud infrastructure including network, servers, operating systems, storage, or even individual application capabilities, with the possible exception of limited &amp;nbsp;userspecific application configuration settings.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Platform as a Service (PaaS):&lt;/u&gt;&lt;/b&gt; The capability provided to the consumer is to deploy onto the cloud infrastructure consumer-created or acquired applications created using programming languages, libraries, services, and tools supported by the provider. The consumer does not manage or control the underlying cloud infrastructure including network, servers, operating systems, or storage, but has control over the deployed applications and possibly configuration settings for the application-hosting environment.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Infrastructure as a Service (IaaS):&lt;/u&gt;&lt;/b&gt; &amp;nbsp;The capability provided to the consumer is to provision processing, storage, networks, and other fundamental computing resources where the consumer is able to deploy and run arbitrary software, which can include operating systems and applications. The consumer does not manage or control the underlying cloud infrastructure but has control over operating systems, storage, and deployed applications; and possibly limited control of select networking components (e.g., host firewalls).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;http://blog.fibertown.com/wp-content/uploads/2011/07/cloud-types.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;266&quot; src=&quot;http://blog.fibertown.com/wp-content/uploads/2011/07/cloud-types.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
Deployment Models:&lt;/h2&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Private cloud:&lt;/u&gt;&lt;/b&gt; The cloud infrastructure is provisioned for exclusive use by a single organization comprising multiple consumers (e.g., business units). It may be &amp;nbsp;owned, managed, and operated by the organization, a third party, or some combination of them, and it may exist on or off premises.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Community &amp;nbsp;cloud:&lt;/u&gt;&lt;/b&gt; The cloud infrastructure &amp;nbsp;is &amp;nbsp;provisioned for exclusive use by &amp;nbsp;a specific community of consumers from organizations that &amp;nbsp;have shared concerns (e.g., mission, security requirements, &amp;nbsp;policy, and compliance considerations). It may be &amp;nbsp;owned, managed, and operated by &amp;nbsp;one or more of the organizations in the community, a third party, or some combination of them, and it may exist on or off premises.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Public cloud:&lt;/u&gt;&lt;/b&gt; The cloud infrastructure is provisioned for open use by the general public. It may be owned, managed, and operated by a business, academic, or government organization, or some combination of them. &amp;nbsp;It exists on the premises of the cloud provider.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Hybrid cloud:&lt;/u&gt;&lt;/b&gt; The cloud infrastructure is a &amp;nbsp;composition of two or more &amp;nbsp;distinct &amp;nbsp;cloud infrastructures (private, community, or public) that remain unique entities, but are bound together by standardized or proprietary technology that enables &amp;nbsp;data and application portability (e.g., cloud bursting for load balancing between clouds).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Cloud_computing_types.svg/500px-Cloud_computing_types.svg.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;236&quot; src=&quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Cloud_computing_types.svg/500px-Cloud_computing_types.svg.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;small&gt;&lt;b&gt;References:&lt;/b&gt;&lt;/small&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;http://csrc.nist.gov/publications/nistpubs/800-145/SP800-145.pdf&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot; onclick=&quot;_gaq.push([&#39;_trackEvent&#39;, &#39;Cloud Computing&#39;, &#39;Links&#39;, &#39;NIST&#39;]);&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;The NIST Definition of Cloud&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Computing&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Cloud_computing&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot; onclick=&quot;_gaq.push([&#39;_trackEvent&#39;, &#39;Cloud Computing&#39;, &#39;Links&#39;, &#39;Wikipedia&#39;]);&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Cloud Computing @ Wikipedia&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://blog.fibertown.com/2011/07/27/who-else-wants-to-understand-the-3-types-of-cloud-computing/&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot; onclick=&quot;_gaq.push([&#39;_trackEvent&#39;, &#39;Cloud Computing&#39;, &#39;Links&#39;, &#39;fibertown&#39;]);&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Who Else Wants to Understand the 3 Types of Cloud Computing?&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;

&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/889894695214298797/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2012/12/cloud-computing-characteristics-service-models-and-deployment-models.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/889894695214298797'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/889894695214298797'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2012/12/cloud-computing-characteristics-service-models-and-deployment-models.html' title='Cloud Computing - Characteristics, Service Models &amp; Deployment Models'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09100860436377691690</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-1771224813321832429</id><published>2012-11-17T01:12:00.001-08:00</published><updated>2012-11-17T01:52:03.387-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="gate 2013"/><title type='text'>GATE 2013 Materials - Books, Solved Question Paper Banks &amp; Guides</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;http://www.snapdeal.com/products/books-competitive-exams?q=Sub_Genre_s%3AGATE%7C&amp;amp;sort=plrty&quot; imageanchor=&quot;1&quot; onclick=&quot;_gaq.push([&#39;_trackEvent&#39;, &#39;Gate 2013&#39;, &#39;GATE Materials&#39;, &#39;Snapdeal-Image&#39;]);&quot; rel=&quot;nofollow&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://i1.sdlcdn.com/img/snapdeal/sprite/snapdeal_logo_tagline.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
Snapdeal has discounts running on GATE 2013 preparatory materials - Guides, 10 Years Solved Question Banks etc. It is available at the following location&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.snapdeal.com/products/books-competitive-exams?q=Sub_Genre_s%3AGATE%7C&amp;amp;sort=plrty&quot; onclick=&quot;_gaq.push([&#39;_trackEvent&#39;, &#39;Gate 2013&#39;, &#39;GATE Materials&#39;, &#39;Snapdeal-Link&#39;]);&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Snapdeal - Competitive Exam Books: Buy Entrance Exams Preparation Books Online&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/110282636899691244440/&quot; rel=&quot;author&quot;&gt;Code Blocks&lt;/a&gt;&lt;/small&gt;

&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/1771224813321832429/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2012/11/gate-2013-materials-books-and-solved-papers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/1771224813321832429'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/1771224813321832429'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2012/11/gate-2013-materials-books-and-solved-papers.html' title='GATE 2013 Materials - Books, Solved Question Paper Banks &amp; Guides'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09100860436377691690</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4895207774785648921.post-7715272786752316482</id><published>2012-11-13T07:39:00.000-08:00</published><updated>2012-11-13T09:35:04.300-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithm"/><title type='text'>Prim&#39;s Algorithm - Minimum Cost Spanning Tree problem</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;i&gt;&quot;&lt;b&gt;Prim&#39;s algorithm&lt;/b&gt; is a &lt;b&gt;greedy algorithm&lt;/b&gt; that finds a minimum spanning tree for a &lt;b&gt;connected weighted undirected graph&lt;/b&gt;. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. The algorithm was developed in 1930 by Czech mathematician &lt;b&gt;Vojtěch Jarník&lt;/b&gt; and later independently by computer scientist &lt;b&gt;Robert C. Prim&lt;/b&gt; in 1957 and rediscovered by &lt;b&gt;Edsger Dijkstra&lt;/b&gt; in 1959. Therefore it is also sometimes called the &lt;b&gt;DJP algorithm&lt;/b&gt;, the &lt;b&gt;Jarník algorithm&lt;/b&gt;, or the&lt;b&gt; Prim–Jarník algorithm&lt;/b&gt;.&quot;&lt;/i&gt; - &lt;a href=&quot;http://en.wikipedia.org/wiki/Prim&#39;s_algorithm&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Wikipedia&lt;/a&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;lang-c&quot;&gt;
1. &lt;b&gt;MST_PRIM(G, cost, root)&lt;/b&gt;
2.   for each vertex u &amp;isin; V[G]
3.     do Key[u] &lt;- &amp;infin;
4.        Pred[u] &lt;- NIL
5.   Key[root] &lt;- 0
6.   Q &lt;- V[G] // create a priority queue of the vertices
7.   while Q &amp;ne; &amp;phi;
8.     do u &lt;- EXTRACT_MIN(Q)
9.       for each v &amp;isin; Adj[u]
10.        do if v &amp;isin; Q &amp;amp; (cost(u,v) &lt; Key[v])
11.          then Pred[v] &lt;- u
12.               Key[v] &lt;- cost(u,v)

Key[v] -&gt; minimum weight of any edge connecting v to a vertex in the tree

Pred[v] -&gt; parent of v in the tree

Q -&gt; min priority queue of all the vertices that are not in the tree

Initially, A = { (v, Pred[v]) : v &amp;isin; V - {root} - Q }
Finally, A = { (v, Pred[v] : v &amp;isin; V - {root} } // Q is empty
&lt;/code&gt;
&lt;/pre&gt;
&lt;br /&gt;
References :&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Introduction_to_Algorithms&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Introduction to Algorithms&lt;/a&gt; by&amp;nbsp;Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein (Available @ &lt;a href=&quot;http://www.flipkart.com/introduction-algorithms-8120340078/p/itmczynzhyhxv2gs&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Flipkart&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;small&gt;Written by &lt;a href=&quot;https://plus.google.com/105162279647438527853/&quot; rel=&quot;author&quot;&gt;Munia Balayil&lt;/a&gt;&lt;/small&gt;

&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-blocks.blogspot.com/feeds/7715272786752316482/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-blocks.blogspot.com/2012/11/prims-algorithm-minimum-cost-spanning-tree.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/7715272786752316482'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4895207774785648921/posts/default/7715272786752316482'/><link rel='alternate' type='text/html' href='http://code-blocks.blogspot.com/2012/11/prims-algorithm-minimum-cost-spanning-tree.html' title='Prim&#39;s Algorithm - Minimum Cost Spanning Tree problem'/><author><name>Munia</name><uri>http://www.blogger.com/profile/09285304305048985843</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>