<?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 path code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 14:53:43 GMT</pubDate>
    <description>DZone Snippets: shortest path code</description>
    <item>
      <title>Dijkstra's Algorithm</title>
      <link>http://snippets.dzone.com/posts/show/4832</link>
      <description>This is a simple O(n^2) implementation of &lt;a href="http://en.wikipedia.org/wiki/Dijkstra's_algorithm"&gt;Dijkstra's algorithm&lt;/a&gt; for finding the shortest paths from a single source to all nodes in a graph.&lt;br /&gt;&lt;br /&gt;Further explanation is given &lt;a href="http://compprog.wordpress.com/2007/12/01/one-source-shortest-path-dijkstras-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;#define GRAPHSIZE 2048&lt;br /&gt;#define INFINITY GRAPHSIZE*GRAPHSIZE&lt;br /&gt;#define MAX(a, b) ((a &gt; b) ? (a) : (b))&lt;br /&gt;&lt;br /&gt;int e; /* The number of nonzero edges in the graph */&lt;br /&gt;int n; /* The number of nodes in the graph */&lt;br /&gt;long dist[GRAPHSIZE][GRAPHSIZE]; /* dist[i][j] is the distance between node i and j; or 0 if there is no direct connection */&lt;br /&gt;long d[GRAPHSIZE]; /* d[i] is the length of the shortest path between the source (s) and node i */&lt;br /&gt;&lt;br /&gt;void printD() {&lt;br /&gt;	int i;&lt;br /&gt;	for (i = 1; i &lt;= n; ++i)&lt;br /&gt;		printf("%10d", i);&lt;br /&gt;	printf("\n");&lt;br /&gt;	for (i = 1; i &lt;= n; ++i) {&lt;br /&gt;		printf("%10ld", d[i]);&lt;br /&gt;	}&lt;br /&gt;	printf("\n");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void dijkstra(int s) {&lt;br /&gt;	int i, k, mini;&lt;br /&gt;	int visited[GRAPHSIZE];&lt;br /&gt;&lt;br /&gt;	for (i = 1; i &lt;= n; ++i) {&lt;br /&gt;		d[i] = INFINITY;&lt;br /&gt;		visited[i] = 0; /* the i-th element has not yet been visited */&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	d[s] = 0;&lt;br /&gt;&lt;br /&gt;	for (k = 1; k &lt;= n; ++k) {&lt;br /&gt;		mini = -1;&lt;br /&gt;		for (i = 1; i &lt;= n; ++i)&lt;br /&gt;			if (!visited[i] &amp;&amp; ((mini == -1) || (d[i] &lt; d[mini])))&lt;br /&gt;				mini = i;&lt;br /&gt;&lt;br /&gt;		visited[mini] = 1;&lt;br /&gt;&lt;br /&gt;		for (i = 1; i &lt;= n; ++i)&lt;br /&gt;			if (dist[mini][i])&lt;br /&gt;				if (d[mini] + dist[mini][i] &lt; d[i]) &lt;br /&gt;					d[i] = d[mini] + dist[mini][i];&lt;br /&gt;	}&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 u, v, w;&lt;br /&gt;&lt;br /&gt;	FILE *fin = fopen("dist.txt", "r");&lt;br /&gt;	fscanf(fin, "%d", &amp;e);&lt;br /&gt;	for (i = 0; i &lt; e; ++i)&lt;br /&gt;		for (j = 0; j &lt; e; ++j)&lt;br /&gt;			dist[i][j] = 0;&lt;br /&gt;	n = -1;&lt;br /&gt;	for (i = 0; i &lt; e; ++i) {&lt;br /&gt;		fscanf(fin, "%d%d%d", &amp;u, &amp;v, &amp;w);&lt;br /&gt;		dist[u][v] = w;&lt;br /&gt;		n = MAX(u, MAX(v, n));&lt;br /&gt;	}&lt;br /&gt;	fclose(fin);&lt;br /&gt;&lt;br /&gt;	dijkstra(1);&lt;br /&gt;&lt;br /&gt;	printD();&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 01 Dec 2007 19:41:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4832</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>All Sources Shortest Path: The Floyd-Warshall Algorithm</title>
      <link>http://snippets.dzone.com/posts/show/4759</link>
      <description>This is a straightforward implementation of the &lt;a href="http://en.wikipedia.org/wiki/Floyd-Warshall_algorithm"&gt;Floyd-Warshall algorithm&lt;/a&gt; for finding the shortest path between all nodes of a graph.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A more detailed explanation is given &lt;a href="http://compprog.wordpress.com/2007/11/15/all-sources-shortest-path-the-floyd-warshall-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;int n; /* Then number of nodes */&lt;br /&gt;int dist[16][16]; /* dist[i][j] is the length of the edge between i and j if&lt;br /&gt;			it exists, or 0 if it does not */&lt;br /&gt;&lt;br /&gt;void printDist() {&lt;br /&gt;	int i, j;&lt;br /&gt;	printf("    ");&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		printf("%4c", 'A' + i);&lt;br /&gt;	printf("\n");&lt;br /&gt;	for (i = 0; i &lt; n; ++i) {&lt;br /&gt;		printf("%4c", 'A' + i);&lt;br /&gt;		for (j = 0; j &lt; n; ++j)&lt;br /&gt;			printf("%4d", dist[i][j]);&lt;br /&gt;		printf("\n");&lt;br /&gt;	}&lt;br /&gt;	printf("\n");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;	floyd_warshall()&lt;br /&gt;&lt;br /&gt;	after calling this function dist[i][j] will the the minimum distance&lt;br /&gt;	between i and j if it exists (i.e. if there's a path between i and j)&lt;br /&gt;	or 0, otherwise&lt;br /&gt;*/&lt;br /&gt;void floyd_warshall() {&lt;br /&gt;	int i, j, k;&lt;br /&gt;	for (k = 0; k &lt; n; ++k) {&lt;br /&gt;		printDist();&lt;br /&gt;		for (i = 0; i &lt; n; ++i)&lt;br /&gt;			for (j = 0; j &lt; n; ++j)&lt;br /&gt;				/* If i and j are different nodes and if &lt;br /&gt;					the paths between i and k and between&lt;br /&gt;					k and j exist, do */&lt;br /&gt;				if ((dist[i][k] * dist[k][j] != 0) &amp;&amp; (i != j))&lt;br /&gt;					/* See if you can't get a shorter path&lt;br /&gt;						between i and j by interspacing&lt;br /&gt;						k somewhere along the current&lt;br /&gt;						path */&lt;br /&gt;					if ((dist[i][k] + dist[k][j] &lt; dist[i][j]) ||&lt;br /&gt;						(dist[i][j] == 0))&lt;br /&gt;						dist[i][j] = dist[i][k] + dist[k][j];&lt;br /&gt;	}&lt;br /&gt;	printDist();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]) {&lt;br /&gt;	FILE *fin = fopen("dist.txt", "r");&lt;br /&gt;	fscanf(fin, "%d", &amp;n);&lt;br /&gt;	int i, j;&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;dist[i][j]);&lt;br /&gt;	fclose(fin);&lt;br /&gt;&lt;br /&gt;	floyd_warshall();&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 15 Nov 2007 16:19:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4759</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
  </channel>
</rss>
