<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The Osmosian Order of Plain English Programmers Welcomes You]]></provider_name><provider_url><![CDATA[http://osmosianplainenglishprogramming.blog]]></provider_url><author_name><![CDATA[gerryrzeppa]]></author_name><author_url><![CDATA[https://osmosianplainenglishprogramming.blog/author/gerryrzeppa/]]></author_url><title><![CDATA[The Travelling Salesman]]></title><type><![CDATA[link]]></type><html><![CDATA[<header class="entry-header">
<h1 class="entry-title"></h1>
</header>
<div class="entry-content">
<p>Our goal in this exercise is to find a reasonably short route through an arbitrary number of cities splattered, randomly, on a map (or in this case, on the screen). We begin with just two Plain English type definitions:</p>
<p><span style="color:#00ccff;">A city is a thing with a number, a spot and a visited flag.</span><br />
<span style="color:#00ccff;"> A nearest neighbor is a city.</span></p>
<p>Then we create a list of five cities at random locations on the screen using this routine:</p>
<p><span style="color:#00ccff;">To create some cities:</span><br />
<span style="color:#00ccff;"> Put 5 into a number.</span><br />
<span style="color:#00ccff;"> Make a box 1 inch smaller than the screen&#8217;s box.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Pick a spot anywhere in the box.</span><br />
<span style="color:#00ccff;"> Allocate memory for a city.</span><br />
<span style="color:#00ccff;"> Put the spot into the city&#8217;s spot.</span><br />
<span style="color:#00ccff;"> Append the city to the cities.</span><br />
<span style="color:#00ccff;"> Subtract 1 from the number. If the number is 0, break.</span><br />
<span style="color:#00ccff;"> Repeat.</span><br />
<span style="color:#00ccff;"> Put 1 into the cities&#8217; first&#8217;s number.</span></p>
<p>The starting city is at the top of the list and is marked with the number “1”. When we draw those cities on the screen they look like this:</p>
<p><img loading="lazy" class="alignnone size-large wp-image-322754" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/map-1-1-1024x819.jpg" alt="" width="665" height="532" /></p>
<p>Then we arrange those cities into a reasonable sequence for the travelling salesman. We do this using the Nearest Neighbor algorithm. It doesn’t guarantee the shortest possible path, but it is very simple and very fast and produces a <em>reasonably </em>short path through the cities. Great engineering seeks a balance between competing objectives.</p>
<p>This is the Plain English routine that implements the Nearest Neighbor algorithm:</p>
<p><span style="color:#00ccff;">To arrange some cities for a travelling salesman (nearest neighbor algorithm):</span><br />
<span style="color:#00ccff;"> Get a city from the cities.</span><br />
<span style="color:#00ccff;"> If the city is nil, break.</span><br />
<span style="color:#00ccff;"> Set the city&#8217;s visited flag.</span><br />
<span style="color:#00ccff;"> Find a nearest neighbor to the city in the cities.</span><br />
<span style="color:#00ccff;"> If the nearest neighbor is nil, break.</span><br />
<span style="color:#00ccff;"> Remove the nearest neighbor from the cities.</span><br />
<span style="color:#00ccff;"> Insert the nearest neighbor into the cities after the city.</span><br />
<span style="color:#00ccff;"> Repeat.</span><br />
<span style="color:#00ccff;"> Number the cities.</span><br />
<span style="color:#00ccff;"> Draw the cities.</span></p>
<p>Finding the nearest neighbor of a city requires calculating the distance between the current city and all of the other cities, which is accomplished using this routine:</p>
<p><span style="color:#00ccff;">To find a nearest neighbor to a current city in some cities:</span><br />
<span style="color:#00ccff;"> Void the nearest neighbor.</span><br />
<span style="color:#00ccff;"> Put the largest number into a shortest distance.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Get a city from the cities.</span><br />
<span style="color:#00ccff;"> If the city is nil, break.</span><br />
<span style="color:#00ccff;"> If the city is the current city, repeat.</span><br />
<span style="color:#00ccff;"> If the city&#8217;s visited flag is set, repeat.</span><br />
<span style="color:#00ccff;"> Get a distance between the city and the current city.</span><br />
<span style="color:#00ccff;"> If the distance is not less than the shortest distance, repeat.</span><br />
<span style="color:#00ccff;"> Put the distance into the shortest distance.</span><br />
<span style="color:#00ccff;"> Put the city into the nearest neighbor.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p>Since we Osmosians don’t believe in transcendental functions (like <em>sine </em>and <em>cosine</em>), we calculate the Minkowski distance between the cities using this simple and speedy routine:</p>
<p><span style="color:#00ccff;">To get a distance between a spot and another spot (minkowski):</span><br />
<span style="color:#00ccff;"> Put the spot&#8217;s x minus the other spot&#8217;s x into a number.</span><br />
<span style="color:#00ccff;"> De-sign the number.</span><br />
<span style="color:#00ccff;"> Put the spot&#8217;s y minus the other spot&#8217;s y into another number.</span><br />
<span style="color:#00ccff;"> De-sign the other number.</span><br />
<span style="color:#00ccff;"> Put the number plus the other number into the distance.</span></p>
<p>Once the cities are arranged in a proper sequence, we can redraw the map like this:</p>
<p><img loading="lazy" class="alignnone size-large wp-image-322755" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/map-2-1-1024x819.jpg" alt="" width="665" height="532" /></p>
<p>And that’s about all there is to it. But before we go, let’s see what it does with 99 cities (the biggest number that will fit inside a city circle on the map). I eliminated the intermediate lines and the distance numbers for this test to keep the screen from getting too cluttered:</p>
<p><img loading="lazy" class="alignnone size-large wp-image-322779" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/map-3-1024x819.jpg" alt="" width="665" height="532" /></p>
<p>Less than a second to run, and a pretty good path. I also tried it with 1,000 cities (and smaller city markers, the starting city marked in red). It still took less than a second…</p>
<p><img loading="lazy" class="alignnone size-large wp-image-322804" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/map-4-1024x819.jpg" alt="" width="665" height="532" /></p>
<p>…but you can see it got lost a couple of times. But who knows? Maybe our salesman likes a long, quiet drive now and then, to collect his thoughts or listen to the radio.</p>
</div>
]]></html><thumbnail_url><![CDATA[https://i1.wp.com/cdncontribute.geeksforgeeks.org/wp-content/uploads/map-1-1-1024x819.jpg?fit=440%2C330&ssl=1]]></thumbnail_url><thumbnail_width><![CDATA[413]]></thumbnail_width><thumbnail_height><![CDATA[330]]></thumbnail_height></oembed>