<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: prim code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 04:13:08 GMT</pubDate>
    <description>DZone Snippets: prim code</description>
    <item>
      <title>Prim's Algorithm for Finding Minimum Spanning Trees in C</title>
      <link>http://snippets.dzone.com/posts/show/4743</link>
      <description>The is a straight-forward implementation of Prim's Algorithm for finding the minimum spanning tree of a graph.&lt;br /&gt;&lt;br /&gt;A detailed explanation is given &lt;a href="http://compprog.wordpress.com/2007/11/09/minimal-spanning-trees-prims-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;/*&lt;br /&gt;	The input file (weight.txt) look something like this&lt;br /&gt;		4&lt;br /&gt;		0 0 0 21&lt;br /&gt;		0 0 8 17&lt;br /&gt;		0 8 0 16&lt;br /&gt;		21 17 16 0&lt;br /&gt;&lt;br /&gt;	The first line contains n, the number of nodes.&lt;br /&gt;	Next is an nxn matrix containg the distances between the nodes&lt;br /&gt;	NOTE: The distance between a node and itself should be 0&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;int n; /* The number of nodes in the graph */&lt;br /&gt;&lt;br /&gt;int weight[100][100]; /* weight[i][j] is the distance between node i and node j;&lt;br /&gt;			if there is no path between i and j, weight[i][j] should&lt;br /&gt;			be 0 */&lt;br /&gt;&lt;br /&gt;char inTree[100]; /* inTree[i] is 1 if the node i is already in the minimum&lt;br /&gt;			spanning tree; 0 otherwise*/&lt;br /&gt;&lt;br /&gt;int d[100]; /* d[i] is the distance between node i and the minimum spanning&lt;br /&gt;		tree; this is initially infinity (100000); if i is already in&lt;br /&gt;		the tree, then d[i] is undefined;&lt;br /&gt;		this is just a temporary variable. It's not necessary but speeds&lt;br /&gt;		up execution considerably (by a factor of n) */&lt;br /&gt;&lt;br /&gt;int whoTo[100]; /* whoTo[i] holds the index of the node i would have to be&lt;br /&gt;			linked to in order to get a distance of d[i] */&lt;br /&gt;&lt;br /&gt;/* updateDistances(int target)&lt;br /&gt;	should be called immediately after target is added to the tree;&lt;br /&gt;	updates d so that the values are correct (goes through target's&lt;br /&gt;	neighbours making sure that the distances between them and the tree&lt;br /&gt;	are indeed minimum)&lt;br /&gt;*/&lt;br /&gt;void updateDistances(int target) {&lt;br /&gt;	int i;&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		if ((weight[target][i] != 0) &amp;&amp; (d[i] &gt; weight[target][i])) {&lt;br /&gt;			d[i] = weight[target][i];&lt;br /&gt;			whoTo[i] = target;&lt;br /&gt;		}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]) {&lt;br /&gt;	FILE *f = fopen("dist.txt", "r");&lt;br /&gt;	fscanf(f, "%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(f, "%d", &amp;weight[i][j]);&lt;br /&gt;	fclose(f);&lt;br /&gt;&lt;br /&gt;	/* Initialise d with infinity */&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		d[i] = 100000;&lt;br /&gt;&lt;br /&gt;	/* Mark all nodes as NOT beeing in the minimum spanning tree */&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		inTree[i] = 0;&lt;br /&gt;&lt;br /&gt;	/* Add the first node to the tree */&lt;br /&gt;	printf("Adding node %c\n", 0 + 'A');&lt;br /&gt;	inTree[0] = 1;&lt;br /&gt;	updateDistances(0);&lt;br /&gt;&lt;br /&gt;	int total = 0;&lt;br /&gt;	int treeSize;&lt;br /&gt;	for (treeSize = 1; treeSize &lt; n; ++treeSize) {&lt;br /&gt;		/* Find the node with the smallest distance to the tree */&lt;br /&gt;		int min = -1;&lt;br /&gt;		for (i = 0; i &lt; n; ++i)&lt;br /&gt;			if (!inTree[i])&lt;br /&gt;				if ((min == -1) || (d[min] &gt; d[i]))&lt;br /&gt;					min = i;&lt;br /&gt;&lt;br /&gt;		/* And add it */&lt;br /&gt;		printf("Adding edge %c-%c\n", whoTo[min] + 'A', min + 'A');&lt;br /&gt;		inTree[min] = 1;&lt;br /&gt;		total += d[min];&lt;br /&gt;&lt;br /&gt;		updateDistances(min);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	printf("Total distance: %d\n", total);&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Nov 2007 12:31:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4743</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
  </channel>
</rss>
