<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hello World</title>
	<atom:link href="http://blog.alirabiee.com/Index.php?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.alirabiee.com</link>
	<description>The programmed world!</description>
	<lastBuildDate>Sat, 21 Aug 2010 23:08:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Listing vertices of a negative-weight cycle in un/directed graphs using Bellman-Ford algorithm</title>
		<link>http://blog.alirabiee.com/?p=576</link>
		<comments>http://blog.alirabiee.com/?p=576#comments</comments>
		<pubDate>Sat, 21 Aug 2010 22:06:56 +0000</pubDate>
		<dc:creator>ali</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Laboratory]]></category>
		<category><![CDATA[Bellman-Ford algorithm]]></category>
		<category><![CDATA[negative-weight cycle]]></category>

		<guid isPermaLink="false">http://blog.alirabiee.com/?p=576</guid>
		<description><![CDATA[Again, there&#8217;s something that seems be rare on the net, and that&#8217;s the problem of finding the ordered list of vertices of a negative-cycle in a directed or undirected graph[1]. Well, the Bellman-Ford algorithm already finds out that there&#8217;s a negative cycle, but how can we make it to also list the vertices of that [...]]]></description>
			<content:encoded><![CDATA[<p>Again, there&#8217;s something that seems be rare on the net, and that&#8217;s the problem of finding the ordered list of vertices of a negative-cycle in a directed or undirected graph<sup>[1]</sup>. Well, the Bellman-Ford algorithm already finds out that there&#8217;s a negative cycle, but how can we make it to also list the vertices of that cycle?</p>
<p>Let&#8217;s just look at how the algorithm finds out that there&#8217;s such a cycle. If there&#8217;s a negative-weight cycle, after |V|-1 iterations of relaxing all edges, there are still vertices that could have smaller distance value, and those are the vertices participating in a negative-weight cycle. So, if after |V|-1-th iteration, there&#8217;s a vertex v that can be relaxed through edge (u,v), therefore that vertex is on a negative cycle. Acordingly, vertex u is also on the same negative cycle. Thus, we can list a negative cycle by looking at relaxable vertices and walking the path out a vertex, until we reach the initial vertex again.</p>
<p>Traslation of the above description in C++ is as follows:</p>
<p><code>
<pre style="background-color: #ccc">/**
 * Listing vertices of a negative-weight cycle in a/n un/directed graph
 * using Bellman-Ford algorithm.
 * Copyright (C) 2010 Ali Rabiee  (http://alirabiee.com/).  All  rights
 * reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  If you  have modified  this program, you  are required to mark it in
 * reasonable ways as different from the original version.
 *
 *  THERE  IS NO  WARRANTY FOR THE  PROGRAM, TO  THE EXTENT PERMITTED BY
 * APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
 * HOLDERS  AND/OR  OTHER PARTIES  PROVIDE THE PROGRAM  "AS IS"  WITHOUT
 * WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMA-
 * NCE OF  THE PROGRAM IS WITH YOU. SHOULD  THE PROGRAM PROVE DEFECTIVE,
 * YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
 *
 * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
 * WILL ANY  COPYRIGHT HOLDER, OR  ANY OTHER PARTY  WHO MODIFIES  AND/OR
 * CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
 * INCLUDING ANY  GENERAL, SPECIAL, INCIDENTAL  OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
 * NOT  LIMITED  TO LOSS OF  DATA OR DATA  BEING RENDERED  INACCURATE OR
 * LOSSES SUSTAINED BY YOU OR THIRD PARTIES  OR A FAILURE OF THE PROGRAM
 * TO OPERATE  WITH ANY OTHER  PROGRAMS), EVEN  IF SUCH  HOLDER OR OTHER
 * PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 *  You  should  have received a copy of the  GNU General Public License
 * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
 *
 *  Enjoy the codes ;)
 *
 */
#include &lt;iostream&gt;
#include &lt;map&gt;
#include &lt;vector&gt;
#include &lt;stack&gt;

using namespace std;

struct Edge {
	int u /* from vertex */,
		v /* to vertex   */,
		w /* weight      */;
	Edge(){}
	Edge(int a, int b, int c = 0) { u=a; v=b; w=c; }
};

#define VMAX 100 //maximum number of vertices
#define EMAX VMAX*VMAX //maximum number of edges
#define UVPAIR Edge // a (u,v) pair
#define ELIST vector&lt;Edge&gt; //list of edges

/**
 * Bellman-Ford algorithm
 *
 * @param	int[]	Distance vector
 * @param	ELIST&#038;	Edges using adjacency list structure
 * @param	int		Number of vertices
 * @param	int		Source vertex
 * @return	UVPAIR	u and v vertices on the negative-weight cycle. (-1,-1) if there's no cycle.
 */
UVPAIR bellman_ford(int d[], ELIST &#038;e, int V, int s = 0) {
	UVPAIR r(-1,-1);

	for(int i=0; i&lt;V;i++)
		d[i] = 0xffffff; // initilializing the elements to infinity
	d[s] = 0; // setting source distance to zero

	//relaxing edges |V|-1 times
	for(int i=1; i&lt;V; i++)
		for(ELIST::iterator it=e.begin(); it!=e.end(); it++)
			d[it-&gt;v] &lt;?= d[it-&gt;u] + it-&gt;w;

	//Looking for a negative cycle;
	for(ELIST::iterator it=e.begin(); it!=e.end(); it++)
		if(d[it-&gt;v] &gt; d[it-&gt;u] + it-&gt;w) {
			d[it-&gt;v] = d[it-&gt;u] + it-&gt;w;
			r = *it;
			break;
		}
	return r;
}

/**
 * List the vertices in a negtive-weight cycle, given a participant edge.
 *
 * @param	int[]	Distance vector
 * @param	ELIST[]	Adjacency lists
 * @param	UVPAIR	A participant edge
 */
void list_cycle(int d[], ELIST adjList[], UVPAIR e) {

	cout &lt;&lt; e.u;

	Edge t = e;

	do {
		for(ELIST::iterator it=adjList[t.v].begin(); it!=adjList[t.v].end(); it++) {
			if(d[it-&gt;v] &gt; d[it-&gt;u] + it-&gt;w) {
				d[it-&gt;v] = d[it-&gt;u] + it-&gt;w;
				t = *it;
				break;
			}
		}
		cout &lt;&lt; " " &lt;&lt; t.u;
	} while( t.v != e.u );
	cout &lt;&lt; endl;
}

int main() {
	int n, ne, d[VMAX];
	ELIST e;
	ELIST adjList[VMAX];

	int u,v,w;

	cout &lt;&lt; "Enter the number of vertices: ";
	cin  &gt;&gt; n;
	cout &lt;&lt; "Enter the number of edges: ";
	cin  &gt;&gt; ne;
	cout &lt;&lt; "Describe "&lt;&lt; ne &lt;&lt;" edge(s) as (from,to,weight):\n";
	cout &lt;&lt; "# Vertex index starts from 0.\n\n";

	for(int i=0; i&lt;ne; i++) {
		cin &gt;&gt; u &gt;&gt; v &gt;&gt; w;
		e.push_back( Edge(u,v,w) );
		adjList[u].push_back( Edge(u,v,w) );
	}

	UVPAIR p =
		bellman_ford(d, e, n);

	if(p.u&lt;0)
		cout &lt;&lt; "No negative-weight cycle found.";
	else {
		cout &lt;&lt; "Negative-weight cycle: ";
		list_cycle(d, adjList, p);
	}

	return 0;
}
</pre>
<p></code></p>
<p>I hope it was useful, and let me know if you have any questions.</p>
<p>&#8212;<br />
[1] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Exercise 24.1-6.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alirabiee.com/?feed=rss2&amp;p=576</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why can&#8217;t develop secure software?</title>
		<link>http://blog.alirabiee.com/?p=572</link>
		<comments>http://blog.alirabiee.com/?p=572#comments</comments>
		<pubDate>Tue, 22 Jun 2010 16:39:20 +0000</pubDate>
		<dc:creator>ali</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://blog.alirabiee.com/?p=572</guid>
		<description><![CDATA[Despite a wealth of security knowledge and developers&#8217; access to advanced tools, many software security risks remain. Analysts say that vulnerabilities arise because many software developers do not understand how to build security into their code. &#8220;There&#8217;s a lot more acceptance of security as part of the process now, but historically developers have never been [...]]]></description>
			<content:encoded><![CDATA[<p>Despite a wealth of security knowledge and developers&#8217; access to advanced tools, many software security risks remain. Analysts say that vulnerabilities arise because many software developers do not understand how to build security into their code. &#8220;There&#8217;s a lot more acceptance of security as part of the process now, but historically developers have never been responsible for security,&#8221; says Fortify chief scientist Brian Chess. Although there have been several initiatives aimed at educating developers about secure software development practices, &#8220;the talent coming out of schools right now doesn&#8217;t have the security knowledge it needs,&#8221; says SAFECode executive director Paul Kurtz. Some organizations are implementing secure development frameworks, such as the Building Security In Maturity Model (BSIMM), which impose secure best practices throughout the entire development team. &#8220;BSIMM is a good strategy if you have a formalized software development process,&#8221; Chess says. The goal of the frameworks is to help developers identify and remediate the most common coding errors and fix them during development, rather than waiting until after the code is complete.</p>
<div style="text-align: right;">&copy; 2010 INFORMATION, INC.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.alirabiee.com/?feed=rss2&amp;p=572</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fight</title>
		<link>http://blog.alirabiee.com/?p=560</link>
		<comments>http://blog.alirabiee.com/?p=560#comments</comments>
		<pubDate>Mon, 21 Jun 2010 12:57:19 +0000</pubDate>
		<dc:creator>ali</dc:creator>
				<category><![CDATA[Commons]]></category>

		<guid isPermaLink="false">http://blog.alirabiee.com/?p=560</guid>
		<description><![CDATA[Today, we had a contest in our department. Well, you may guess we had some computer contest. But we had a contest on selecting the subject of a research. The teacher constrained each topic to be arbitrarily assigned to only one team. There were 22 topics totally. I was standing from 9:15 AM there, and [...]]]></description>
			<content:encoded><![CDATA[<p>Today, we had a contest in our department. Well, you may guess we had some computer contest. But we had a contest on selecting the subject of a research. The teacher constrained each topic to be arbitrarily assigned to only one team. There were 22 topics totally. I was standing from 9:15 AM there, and the teacher called the topic registration will start exactly at 10 AM. There was a few interesting topics, and I had three topics in hand and had hope that I&#8217;ll find a chance to register one! The clock was about 10 and everyone rushed in for selecting their topic. You know the rest.</p>
<p>I wonder what the purpose of this research is. Is that for finding:</p>
<ul>
<li>who runs faster?</li>
<li>who jumps longer? or</li>
<li>who has chance since his/her birthday?</li>
</ul>
<p>I think nothing is predictable about the purpose of the projects being defined here.</p>
<p>A tasty sandwitch is great as long as it isn&#8217;t spoiled. Defining several arbitrary topics is a good idea, but constraining each topic to only one team, happended to force many to select a different topic than what they wanted; turning the research into an overhead in their programs, and you know what the output of such research could be.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alirabiee.com/?feed=rss2&amp;p=560</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rapidshare, Uploading, Depositfiles, You&#8230; Filtered!</title>
		<link>http://blog.alirabiee.com/?p=553</link>
		<comments>http://blog.alirabiee.com/?p=553#comments</comments>
		<pubDate>Sat, 19 Jun 2010 09:49:35 +0000</pubDate>
		<dc:creator>ali</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://blog.alirabiee.com/?p=553</guid>
		<description><![CDATA[Today I just wanted to download a book from a file-sharing server. I just stuck with the filtering notification. It seems all famous file-sharing websites are filtered. This is not fair. Almost all freeware are hosted on free file-sharing servers. Indeed, why most iranian cannot stop thinking of ways to bypass the filtering system?]]></description>
			<content:encoded><![CDATA[<p>Today I just wanted to download a book from a file-sharing server. I just stuck with the filtering notification. It seems all famous file-sharing websites are filtered. This is not fair. Almost all freeware are hosted on free file-sharing servers.</p>
<p>Indeed, why most iranian cannot stop thinking of ways to bypass the filtering system?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alirabiee.com/?feed=rss2&amp;p=553</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How is it</title>
		<link>http://blog.alirabiee.com/?p=549</link>
		<comments>http://blog.alirabiee.com/?p=549#comments</comments>
		<pubDate>Fri, 18 Jun 2010 20:56:27 +0000</pubDate>
		<dc:creator>ali</dc:creator>
				<category><![CDATA[Commons]]></category>

		<guid isPermaLink="false">http://blog.alirabiee.com/?p=549</guid>
		<description><![CDATA[Have you ever felt something&#8217;s stopping you from reaching your goals? How is it? I know, that&#8217;s quite bad. That&#8217;s life. This summer I have a lot of programs, and planning all is a really difficult task. Beside this, we have genious teachers who think we&#8217;re planning to spend all the summer with fun, jokes [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever felt something&#8217;s stopping you from reaching your goals? How is it? I know, that&#8217;s quite bad. That&#8217;s life. This summer I have a lot of programs, and planning all is a really difficult task. Beside this, we have genious teachers who think we&#8217;re planning to spend all the summer with fun, jokes and friends! They define long projects, starting from the very beginning of semester (we&#8217;ve got to be genious to make them as they expect!), to the half of the summer, saying: Hey, what are you doing in summer? Your&#8217;s completely free and have enough time working on the projects. I don&#8217;t know what to say about such a clever thought. How about you?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alirabiee.com/?feed=rss2&amp;p=549</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
