<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: speeding code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 20:57:47 GMT</pubDate>
    <description>DZone Snippets: speeding 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>
  </channel>
</rss>
