<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: dijkstra code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 04:07:15 GMT</pubDate>
    <description>DZone Snippets: dijkstra code</description>
    <item>
      <title>Speeding up Dijkstra's Algorithm</title>
      <link>http://snippets.dzone.com/posts/show/4996</link>
      <description>Demonstrates a way of speeding up the O(n&lt;sup&gt;2&lt;/sup&gt;) version of Dijkstra's Algorithm by about 4x.&lt;br /&gt;&lt;br /&gt;Here, dist[][] is the adjacency matrix representing the graph and n is the number of nodes. d2[] is where the lengths of the shortest paths are saved.&lt;br /&gt;&lt;br /&gt;For a full explanation, see &lt;a href="http://compprog.wordpress.com/2008/01/17/speeding-up-dijkstras-algorithm-1/"&gt;this&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;void dijkstra2(int s) {&lt;br /&gt;	int queue[GRAPHSIZE];&lt;br /&gt;	char inQueue[GRAPHSIZE];&lt;br /&gt;	int begq = 0,&lt;br /&gt;	    endq = 0;&lt;br /&gt;	int i, mini;&lt;br /&gt;	int visited[GRAPHSIZE];&lt;br /&gt;&lt;br /&gt;	for (i = 1; i &lt;= n; ++i) {&lt;br /&gt;		d2[i] = INFINITY;&lt;br /&gt;		visited[i] = 0; /* the i-th element has not yet been visited */&lt;br /&gt;		inQueue[i] = 0;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	d2[s] = 0;&lt;br /&gt;	queue[endq] = s;&lt;br /&gt;	endq = (endq + 1) % GRAPHSIZE;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	while (begq != endq) {&lt;br /&gt;		mini = queue[begq];&lt;br /&gt;		begq = (begq + 1) % GRAPHSIZE;&lt;br /&gt;		inQueue[mini] = 0;&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 (d2[mini] + dist[mini][i] &lt; d2[i]) { &lt;br /&gt;					d2[i] = d2[mini] + dist[mini][i];&lt;br /&gt;					if (!inQueue[i]) {&lt;br /&gt;						queue[endq] = i;&lt;br /&gt;						endq = (endq + 1) % GRAPHSIZE;&lt;br /&gt;						inQueue[i] = 1;&lt;br /&gt;					}&lt;br /&gt;				}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 Jan 2008 15:57:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4996</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <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>
  </channel>
</rss>
