<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Traveling Salesman: Brute Force</title>
	<atom:link href="http://programmingpraxis.com/2010/03/12/traveling-salesman-brute-force/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2010/03/12/traveling-salesman-brute-force/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Sat, 11 Feb 2012 09:48:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: harsha</title>
		<link>http://programmingpraxis.com/2010/03/12/traveling-salesman-brute-force/#comment-2286</link>
		<dc:creator><![CDATA[harsha]]></dc:creator>
		<pubDate>Sun, 09 Jan 2011 04:05:15 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2072#comment-2286</guid>
		<description><![CDATA[On executing this programme on C-free5.0 version, it showed  an error for &quot;rand()&quot; at line no.:121
so i overcomed it by including &quot;stdlib.h&quot; header file for this &quot;rand()&quot; function]]></description>
		<content:encoded><![CDATA[<p>On executing this programme on C-free5.0 version, it showed  an error for &#8220;rand()&#8221; at line no.:121<br />
so i overcomed it by including &#8220;stdlib.h&#8221; header file for this &#8220;rand()&#8221; function</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://programmingpraxis.com/2010/03/12/traveling-salesman-brute-force/#comment-1109</link>
		<dc:creator><![CDATA[Mike]]></dc:creator>
		<pubDate>Thu, 18 Mar 2010 18:22:30 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2072#comment-1109</guid>
		<description><![CDATA[One observation is that most of the permutations of the cities are mere rotations of another permutation.  For example, the tour [city2, ... cityN, city1] is a rotation of the tour [city1, city2, ... cityN].  Indeed, any tour starting with any of city2 .. cityN is a rotation of a tour starting with city1.

A smarter brute force solution is to only consider permutations that start with city1.  For 10 cities this optimisation eliminates 90% of the calculations.

In python:

[sourcecode lang=&quot;python&quot;]
def brute_force(cities):
    other_cities = cities.copy()
    start_city = tuple(other_cities.pop())

    return min((start_city+p for p in permutations(other_cities)), key=path_cost)
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>One observation is that most of the permutations of the cities are mere rotations of another permutation.  For example, the tour [city2, ... cityN, city1] is a rotation of the tour [city1, city2, ... cityN].  Indeed, any tour starting with any of city2 .. cityN is a rotation of a tour starting with city1.</p>
<p>A smarter brute force solution is to only consider permutations that start with city1.  For 10 cities this optimisation eliminates 90% of the calculations.</p>
<p>In python:</p>
<pre class="brush: python;">
def brute_force(cities):
    other_cities = cities.copy()
    start_city = tuple(other_cities.pop())

    return min((start_city+p for p in permutations(other_cities)), key=path_cost)
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Travelling salesman &#171; Wrong Side of Memphis</title>
		<link>http://programmingpraxis.com/2010/03/12/traveling-salesman-brute-force/#comment-1084</link>
		<dc:creator><![CDATA[Travelling salesman &#171; Wrong Side of Memphis]]></dc:creator>
		<pubDate>Tue, 16 Mar 2010 16:56:31 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2072#comment-1084</guid>
		<description><![CDATA[[...] with the length of the data. On Programming Praxis they have proposed to resolve the problem using brute force, and using the closest neighbor (a simplification of the [...]]]></description>
		<content:encoded><![CDATA[<p>[...] with the length of the data. On Programming Praxis they have proposed to resolve the problem using brute force, and using the closest neighbor (a simplification of the [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jebb</title>
		<link>http://programmingpraxis.com/2010/03/12/traveling-salesman-brute-force/#comment-1073</link>
		<dc:creator><![CDATA[Jebb]]></dc:creator>
		<pubDate>Mon, 15 Mar 2010 07:40:14 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2072#comment-1073</guid>
		<description><![CDATA[A C solution. Quite verbose obviously, and I did manage to fall into a couple of pitfalls along the way. It is fast, though (1 second for the 10-second problem).
[sourcecode lang=&quot;cpp&quot;]
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#define NUMCITY	10
#define LANDSIZE 100
#define square(A) ((A) * (A))

typedef int City[2];

void generate(City cities[]);
void print_cities(City cities[]);
float distance(City city1, City city2);
void copy_tour(City citiesDest[], City citiesSource[]);
void copy_City(City dest, City source);
void swap_cities(City city1, City city2);
void circ_perm(City cities[], int numCities);
void scramble(City cities[], City *pivot, int numCities);
void target_function(City cities[]);
float tour_length(City cities[]);

float shortestTourLength;
City shortestTour[NUMCITY];

int main()
{
	City cities[NUMCITY];
	generate(cities);
	shortestTourLength = tour_length(cities);
	copy_tour(shortestTour, cities);
	scramble(cities, cities, NUMCITY);
	printf(&quot;found the shortest tour:\n&quot;);
	print_cities(shortestTour);
	printf(&quot;Length is: %f\n&quot;, shortestTourLength);
	return 0;
}

float tour_length(City cities[])
{
	int i;
	float length = 0.0;
	for(i = 0; i &lt; NUMCITY - 1; i++)
		length += distance(cities[i], cities[i+1]);
	length += distance(cities[NUMCITY - 1], cities[0]);
	return length;
}

void target_function(City cities[])
{
	float length;
	length = tour_length(cities);
	if (length &lt; shortestTourLength) {
		shortestTourLength = length;
		copy_tour(shortestTour, cities);
	}
}

/*pivot is the base address in the cities[NUMCITY] array
 *for the recursive scrambling; the only reason we also
 *pass the unchanged cities address is because we need it
 *to call the target function (which does *something* to
 *the scrambled array) at each recursive call to scramble
 */
void scramble(City cities[], City *pivot, int numCities)
{
	int i;
	City *newPivot;
	if (numCities &lt;= 1) { //Scrambled! Call the target function
		target_function(cities);
		return;
	}
	for (i = 0; i &lt; numCities; i++) {
		newPivot = &amp;pivot[1];
		scramble(cities, newPivot, numCities - 1);
		circ_perm(pivot, numCities);
	}
}

void circ_perm(City cities[], int numCities)
{
	int i;
	City tmp;
	copy_City(tmp, cities[0]);
	for (i = 0; i &lt; numCities - 1; i++)
		copy_City(cities[i], cities[i + 1]);
	copy_City(cities[numCities - 1], tmp);
}

void copy_tour(City citiesDest[], City citiesSource[])
{
	int i;
	for (i = 0; i &lt; NUMCITY; i++)
		copy_City(citiesDest[i], citiesSource[i]);
}

void copy_City(City dest, City source)
{
	dest[0] = source[0];
	dest[1] = source[1];
}

void swap_cities(City city1, City city2)
{
	City tmp;
	copy_City(tmp, city1);
	copy_City(city1, city2);
	copy_City(city2, tmp);
}

float distance(City city1, City city2)
{
	float result;
	result = sqrtf((float)square(city2[0] - city1[0]) +
				 (float)square(city2[1] - city1[1]));
	return result;
}

void generate(City cities[])
{
	int i, j;
	for (i = 0; i &lt; NUMCITY; i++)
		for (j = 0; j &lt; 2; j++)
			cities[i][j] = rand() % LANDSIZE;
}

void print_cities(City cities[])
{
	int i;
	for (i = 0; i &lt; NUMCITY; i++) {
		printf(&quot;(%d,%d)&quot;, cities[i][0], cities[i][1]);
		if (i &lt; NUMCITY - 1)
			printf(&quot; , &quot;);
	}
	printf(&quot;\n&quot;);
}
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>A C solution. Quite verbose obviously, and I did manage to fall into a couple of pitfalls along the way. It is fast, though (1 second for the 10-second problem).</p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#define NUMCITY	10
#define LANDSIZE 100
#define square(A) ((A) * (A))

typedef int City[2];

void generate(City cities[]);
void print_cities(City cities[]);
float distance(City city1, City city2);
void copy_tour(City citiesDest[], City citiesSource[]);
void copy_City(City dest, City source);
void swap_cities(City city1, City city2);
void circ_perm(City cities[], int numCities);
void scramble(City cities[], City *pivot, int numCities);
void target_function(City cities[]);
float tour_length(City cities[]);

float shortestTourLength;
City shortestTour[NUMCITY];

int main()
{
	City cities[NUMCITY];
	generate(cities);
	shortestTourLength = tour_length(cities);
	copy_tour(shortestTour, cities);
	scramble(cities, cities, NUMCITY);
	printf(&quot;found the shortest tour:\n&quot;);
	print_cities(shortestTour);
	printf(&quot;Length is: %f\n&quot;, shortestTourLength);
	return 0;
}

float tour_length(City cities[])
{
	int i;
	float length = 0.0;
	for(i = 0; i &lt; NUMCITY - 1; i++)
		length += distance(cities[i], cities[i+1]);
	length += distance(cities[NUMCITY - 1], cities[0]);
	return length;
}

void target_function(City cities[])
{
	float length;
	length = tour_length(cities);
	if (length &lt; shortestTourLength) {
		shortestTourLength = length;
		copy_tour(shortestTour, cities);
	}
}

/*pivot is the base address in the cities[NUMCITY] array
 *for the recursive scrambling; the only reason we also
 *pass the unchanged cities address is because we need it
 *to call the target function (which does *something* to
 *the scrambled array) at each recursive call to scramble
 */
void scramble(City cities[], City *pivot, int numCities)
{
	int i;
	City *newPivot;
	if (numCities &lt;= 1) { //Scrambled! Call the target function
		target_function(cities);
		return;
	}
	for (i = 0; i &lt; numCities; i++) {
		newPivot = &amp;pivot[1];
		scramble(cities, newPivot, numCities - 1);
		circ_perm(pivot, numCities);
	}
}

void circ_perm(City cities[], int numCities)
{
	int i;
	City tmp;
	copy_City(tmp, cities[0]);
	for (i = 0; i &lt; numCities - 1; i++)
		copy_City(cities[i], cities[i + 1]);
	copy_City(cities[numCities - 1], tmp);
}

void copy_tour(City citiesDest[], City citiesSource[])
{
	int i;
	for (i = 0; i &lt; NUMCITY; i++)
		copy_City(citiesDest[i], citiesSource[i]);
}

void copy_City(City dest, City source)
{
	dest[0] = source[0];
	dest[1] = source[1];
}

void swap_cities(City city1, City city2)
{
	City tmp;
	copy_City(tmp, city1);
	copy_City(city1, city2);
	copy_City(city2, tmp);
}

float distance(City city1, City city2)
{
	float result;
	result = sqrtf((float)square(city2[0] - city1[0]) +
				 (float)square(city2[1] - city1[1]));
	return result;
}

void generate(City cities[])
{
	int i, j;
	for (i = 0; i &lt; NUMCITY; i++)
		for (j = 0; j &lt; 2; j++)
			cities[i][j] = rand() % LANDSIZE;
}

void print_cities(City cities[])
{
	int i;
	for (i = 0; i &lt; NUMCITY; i++) {
		printf(&quot;(%d,%d)&quot;, cities[i][0], cities[i][1]);
		if (i &lt; NUMCITY - 1)
			printf(&quot; , &quot;);
	}
	printf(&quot;\n&quot;);
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2010/03/12/traveling-salesman-brute-force/#comment-1068</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Fri, 12 Mar 2010 09:57:53 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2072#comment-1068</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2010/03/12/programming-praxis-traveling-salesman-brute-force/ for a version with comments):

[sourcecode lang=&quot;css&quot;]
import Data.List
import qualified Data.List.Key as K

dist :: Floating a =&gt; (a, a) -&gt; (a, a) -&gt; a
dist (x1, y1) (x2, y2) = sqrt ((x1 - x2) ** 2 + (y1 - y2) ** 2)

tours :: [b] -&gt; [[(Int, b)]]
tours = map (\(x:xs) -&gt; x:xs ++ [x]) . permutations . zip [0..]

cost :: Floating a =&gt; [(b, (a, a))] -&gt; a
cost xs = sum $ zipWith dist xs (tail xs)

shortestPath :: [(Double, Double)] -&gt; [Int]
shortestPath = init . map fst . K.minimum cost . tours
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2010/03/12/programming-praxis-traveling-salesman-brute-force/" rel="nofollow">http://bonsaicode.wordpress.com/2010/03/12/programming-praxis-traveling-salesman-brute-force/</a> for a version with comments):</p>
<pre class="brush: css;">
import Data.List
import qualified Data.List.Key as K

dist :: Floating a =&gt; (a, a) -&gt; (a, a) -&gt; a
dist (x1, y1) (x2, y2) = sqrt ((x1 - x2) ** 2 + (y1 - y2) ** 2)

tours :: [b] -&gt; [[(Int, b)]]
tours = map (\(x:xs) -&gt; x:xs ++ [x]) . permutations . zip [0..]

cost :: Floating a =&gt; [(b, (a, a))] -&gt; a
cost xs = sum $ zipWith dist xs (tail xs)

shortestPath :: [(Double, Double)] -&gt; [Int]
shortestPath = init . map fst . K.minimum cost . tours
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis &#8211; Traveling Salesman: Brute Force &#171; Bonsai Code</title>
		<link>http://programmingpraxis.com/2010/03/12/traveling-salesman-brute-force/#comment-1067</link>
		<dc:creator><![CDATA[Programming Praxis &#8211; Traveling Salesman: Brute Force &#171; Bonsai Code]]></dc:creator>
		<pubDate>Fri, 12 Mar 2010 09:57:34 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2072#comment-1067</guid>
		<description><![CDATA[[...] Praxis &#8211; Traveling Salesman: Brute&#160;Force By Remco Niemeijer  In today&#8217;s Programming Praxis exercise we have to implement a brute-force algorithm for solving the well-known [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Praxis &#8211; Traveling Salesman: Brute&nbsp;Force By Remco Niemeijer  In today&#8217;s Programming Praxis exercise we have to implement a brute-force algorithm for solving the well-known [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

