<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mostly a codeBlog &#187; Kruskal</title>
	<atom:link href="http://www.rickigregersen.com/tag/kruskal/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rickigregersen.com</link>
	<description>whatever I am currently programming... and I truly digress</description>
	<lastBuildDate>Mon, 21 Jun 2010 15:40:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Kruskals algorithm in Processing</title>
		<link>http://www.rickigregersen.com/2009/03/18/kruskals-algorithm-in-processing/</link>
		<comments>http://www.rickigregersen.com/2009/03/18/kruskals-algorithm-in-processing/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 17:41:58 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Processing]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Kruskal]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=4</guid>
		<description><![CDATA[I was reading Robert Sedgewicks &#8220;Algorithms in Java&#8221; and had skipped to part 5 concerning Graphs. I know I ought not skip portions of a book concerning something that difficult, but 130 pages into the first part I needed to see where all this theory was going. I stumbled over the Kruskal/Prims/Dijkstra algorithms and the visual [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_37" class="wp-caption alignnone" style="width: 707px"><img class="size-full wp-image-37" title="kruskal" src="http://www.rickigregersen.com/wp-content/uploads/kruskal.png" alt="Minimum Spanning Tree (red path)" width="697" height="496" /><p class="wp-caption-text">Minimum Spanning Tree (red path)</p></div>
<p>I was reading Robert Sedgewicks &#8220;Algorithms in Java&#8221; and had skipped to part 5 concerning <em>Graphs.</em></p>
<p>I know I ought not skip portions of a book concerning something that difficult, but 130 pages into the first part I needed to see where all this theory was going.</p>
<p>I stumbled over the Kruskal/Prims/Dijkstra algorithms and the visual representations caught my eye. They were really simple to look at but you could sense there was some sort of genius system behind it. That is one of my favorite reasons for opening up Processing and start sketching.</p>
<p><strong>A few </strong><em><span style="text-decoration: line-through;"><strong>need to know</strong></span></em><strong> things on Graphs:</strong></p>
<p>Thinking in 2 dimensions, a Graph consists of a number of points on a surface. A point is called a <em>vertex </em>and each vertex can be connected to a number of other vertices. Such a connection between 2, or more, vertexes is called and <em>edge. </em></p>
<p><em></em></p>
<p><em></em></p>
<p><em></em></p>
<p><em></em></p>
<p><em> </em></p>
<div id="attachment_26" class="wp-caption alignnone" style="width: 285px"><img class="size-medium wp-image-26" title="simple graph" src="http://www.rickigregersen.com/wp-content/uploads/graph01.png" alt="simple graph" width="275" height="235" /><p class="wp-caption-text">simple graph</p></div>
<p><em><span style="font-style: normal;">There are many different types of Graphs: </span>directed graphs, </em>where directed means, that two vertices that are connected also states which direction you can travel from one vertex to the other, e.g. V1 -&gt;V2 means you can go from V1 to V2 but not the other way. <em>Undirected Graphs </em>is then, of course, a graph where it does not matter which way you travel. Directed and undirected graphs can also be <em>weighted</em> or not. This means that &#8220;it takes a certain amount of energy&#8221; to travel from one vertex to another.</p>
<div id="attachment_28" class="wp-caption alignnone" style="width: 295px"><img class="size-full wp-image-28" title="simple directed weighted graph" src="http://www.rickigregersen.com/wp-content/uploads/graph02.png" alt="simple directed weighted graph" width="285" height="178" /><p class="wp-caption-text">simple directed weighted graph</p></div>
<p>This comes clear when we think back to the 2 dimensions and that a graph can be points/vertices on a surface. If we were looking to connect three vertices using the least amount of &#8220;energy&#8221;  and vertex v1 is situated a distance of 9 meters from vertex V2 and vertex V1 is situated 4 meters from vertex V3 and vertex V2 is situated 3 meters from vertex V3, we would connect them as such: V1-V3-V2</p>
<div id="attachment_31" class="wp-caption alignnone" style="width: 254px"><img class="size-full wp-image-31" title="Euclidian weighted graph" src="http://www.rickigregersen.com/wp-content/uploads/graph03.png" alt="Euclidian weighted graph" width="244" height="123" /><p class="wp-caption-text">Euclidian weighted graph</p></div>
<h1>Minimum Spanning Tree</h1>
<p>Having scratched the surface of Graphs it is now a bit easier to understand what a <em>minimum spanning tree</em> is.</p>
<p>In the &#8220;Algorithms in Java&#8221; book the minimum spanning tree(MST) is defined as:</p>
<blockquote><p>In a weighted graph, find a minimum-weight set of edges that connects all the vertices.</p></blockquote>
<p>It is also defined as:</p>
<blockquote><p>A minimum spanning tree of a weighted graph is a spanning tree whose weight(the sum of weights of it&#8217;s edges) is no larger than the weight of any other spanning tree.</p></blockquote>
<p>It is good to dwell on that last definition for a second. It says that there might be lots and lots of spanning trees in a graph, and that some of them can have the same combined weight. According to the definition this is okay, the more the merrier, as long as it is still a minimum spanning tree.</p>
<p>This means, that from any vertex you should be able to travel to any other vertex, not necessarily along the shortest path, that is another algorithm altogether, but all the vertices is connected in a way that adds up to the combined shortest edges.</p>
<p>There are quite a few algorithms that strives to solve this problem, two of them is Prim&#8217;s and Kruskal&#8217;s algorithm.</p>
<p>Prim&#8217;s algorithm, it goes under many different names as it has been discovered and implemented by more than one person, but that is a story for <a href="http://en.wikipedia.org/wiki/Prim_algorithm">Wikipedia</a> and not this very short post.</p>
<p>Prim&#8217;s algorithm looks at a graph, selects an arbitrary vertex and starts testing all the edges connected to this vertex, once it finds the shortest edge, the edge with the smallest weight, it adds this to the tree, e.g. V12-V65. It then goes on to look for the next shortest edge from either V12 or V65. Before the algorithm can add another edge it must test that it is not creating a <em>cycle. </em>A cycle is if V1-V2 and V2-V3 and V3-V1 is connected, meaning you could start out from V1 and then end up in V1 again. A cycle is redundant in a minimum spanning tree and not desired.</p>
<p>If the algorithm consists of N vertices the MST will be finished when N-1 edges has been added to the MST, regarding no cycles has been made.</p>
<p>The Kruskla&#8217;s algorithm works in a very similar way, the main difference is that Kruskal&#8217;s algorithm starts at the first vertex, i.e V1 and then works it&#8217;s way outwards from that.</p>
<p>This means that an identical graph will yield the same MST each time when using Kruskal and that Prim&#8217;s can yield different MST&#8217;s depending on which vertex was set as the starting point(remember the second definition&#8230;)</p>
<p><a title="Kruskal Processing example" href="http://www.rickigregersen.com/wp-content/code/kruskal/" target="_blank">Here is the Processing code for the sketch shown at the beginning</a></p>
<p>There are 101 vertices, this adds up to 5050 possible edges, they are drawn in using a transparant white. The MST will be the red path as in the picture in the start of the post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/03/18/kruskals-algorithm-in-processing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
