<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: shortest code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 14:17:53 GMT</pubDate>
    <description>DZone Snippets: shortest code</description>
    <item>
      <title>Bellman-Ford Algorithm</title>
      <link>http://snippets.dzone.com/posts/show/4828</link>
      <description>This is a simple implementation of the &lt;a href="http://en.wikipedia.org/wiki/Bellman-ford"&gt;Bellman-Ford&lt;/a&gt; algorithm for finding the shortest path from a single source in a graph.&lt;br /&gt;&lt;br /&gt;A detailed explanation is given &lt;a href="http://compprog.wordpress.com/2007/11/29/one-source-shortest-path-the-bellman-ford-algorithm/"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;typedef struct {&lt;br /&gt;	int u, v, w;&lt;br /&gt;} Edge;&lt;br /&gt;&lt;br /&gt;int n; /* the number of nodes */&lt;br /&gt;int e; /* the number of edges */&lt;br /&gt;Edge edges[1024]; /* large enough for n &lt;= 2^5=32 */&lt;br /&gt;int d[32]; /* d[i] is the minimum distance from node s to node i */&lt;br /&gt;&lt;br /&gt;#define INFINITY 10000&lt;br /&gt;&lt;br /&gt;void printDist() {&lt;br /&gt;	int i;&lt;br /&gt;&lt;br /&gt;	printf("Distances:\n");&lt;br /&gt;&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		printf("to %d\t", i + 1);&lt;br /&gt;	printf("\n");&lt;br /&gt;&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		printf("%d\t", d[i]);&lt;br /&gt;&lt;br /&gt;	printf("\n\n");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void bellman_ford(int s) {&lt;br /&gt;	int i, j;&lt;br /&gt;&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		d[i] = INFINITY;&lt;br /&gt;&lt;br /&gt;	d[s] = 0;&lt;br /&gt;&lt;br /&gt;	for (i = 0; i &lt; n - 1; ++i)&lt;br /&gt;		for (j = 0; j &lt; e; ++j)&lt;br /&gt;			if (d[edges[j].u] + edges[j].w &lt; d[edges[j].v])&lt;br /&gt;				d[edges[j].v] = d[edges[j].u] + edges[j].w;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]) {&lt;br /&gt;	int i, j;&lt;br /&gt;	int w;&lt;br /&gt;&lt;br /&gt;	FILE *fin = fopen("dist.txt", "r");&lt;br /&gt;	fscanf(fin, "%d", &amp;n);&lt;br /&gt;	e = 0;&lt;br /&gt;&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		for (j = 0; j &lt; n; ++j) {&lt;br /&gt;			fscanf(fin, "%d", &amp;w);&lt;br /&gt;			if (w != 0) {&lt;br /&gt;				edges[e].u = i;&lt;br /&gt;				edges[e].v = j;&lt;br /&gt;				edges[e].w = w;&lt;br /&gt;				++e;&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;	fclose(fin);&lt;br /&gt;&lt;br /&gt;	/* printDist(); */&lt;br /&gt;&lt;br /&gt;	bellman_ford(0);&lt;br /&gt;&lt;br /&gt;	printDist();&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 29 Nov 2007 14:55:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4828</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
  </channel>
</rss>
