<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Mathematics/Cryptography/Quantum on kenji.blog</title><link>http://kenji.blog/en/categories/mathematics/cryptography/quantum/</link><description>Recent content in Mathematics/Cryptography/Quantum on kenji.blog</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>kenjinote</copyright><lastBuildDate>Wed, 12 Oct 2022 19:05:58 +0900</lastBuildDate><atom:link href="http://kenji.blog/en/categories/mathematics/cryptography/quantum/index.xml" rel="self" type="application/rss+xml"/><item><title>Solving the Traveling Salesperson Problem with Mathematica</title><link>http://kenji.blog/en/p/mathematica%E3%81%A7%E5%B7%A1%E5%9B%9E%E3%82%BB%E3%83%BC%E3%83%AB%E3%82%B9%E3%83%9E%E3%83%B3%E5%95%8F%E9%A1%8C%E3%82%92%E8%A7%A3%E3%81%8F/</link><pubDate>Wed, 12 Oct 2022 19:05:58 +0900</pubDate><guid>http://kenji.blog/en/p/mathematica%E3%81%A7%E5%B7%A1%E5%9B%9E%E3%82%BB%E3%83%BC%E3%83%AB%E3%82%B9%E3%83%9E%E3%83%B3%E5%95%8F%E9%A1%8C%E3%82%92%E8%A7%A3%E3%81%8F/</guid><description>&lt;img src="http://kenji.blog/p/mathematica%E3%81%A7%E5%B7%A1%E5%9B%9E%E3%82%BB%E3%83%BC%E3%83%AB%E3%82%B9%E3%83%9E%E3%83%B3%E5%95%8F%E9%A1%8C%E3%82%92%E8%A7%A3%E3%81%8F/img.png" alt="Featured image of post Solving the Traveling Salesperson Problem with Mathematica" />&lt;h1 id="solving-the-traveling-salesperson-problem-with-mathematica">Solving the Traveling Salesperson Problem with Mathematica
&lt;/h1>&lt;h2 id="problem">Problem
&lt;/h2>&lt;blockquote class="twitter-tweet">&lt;p lang="ja" dir="ltr">電車でこんな広告を見かけました😁📸 &lt;a href="https://t.co/iXEgvtXrpL">pic.twitter.com/iXEgvtXrpL&lt;/a>&lt;/p>&amp;mdash; 早稲田大学 早水桃子研究室 (@hayamizu_lab) &lt;a href="https://x.com/hayamizu_lab/status/1579806418982825984?ref_src=twsrc%5Etfw">October 11, 2022&lt;/a>&lt;/blockquote>
&lt;script async src="https://platform.x.com/widgets.js" charset="utf-8">&lt;/script>
&lt;h2 id="solution">Solution
&lt;/h2>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">d=SparseArray[{{1,2}-&amp;gt;10,{2,1}-&amp;gt;10,{1,5}-&amp;gt;15,{5,1}-&amp;gt;15,{1,4}-&amp;gt;12,{4,1}-&amp;gt;12,{1,3}-&amp;gt;20,{3,1}-&amp;gt;20,{2,5}-&amp;gt;10,{5,2}-&amp;gt;10,{3,4}-&amp;gt;10,{4,3}-&amp;gt;10,{3,8}-&amp;gt;30,{8,3}-&amp;gt;30,{3,7}-&amp;gt;20,{7,3}-&amp;gt;20,{3,6}-&amp;gt;25,{6,3}-&amp;gt;25,{4,5}-&amp;gt;15,{5,4}-&amp;gt;15,{4,8}-&amp;gt;20,{8,4}-&amp;gt;20,{5,9}-&amp;gt;18,{9,5}-&amp;gt;18,{5,8}-&amp;gt;15,{8,5}-&amp;gt;15,{6,7}-&amp;gt;5,{7,6}-&amp;gt;5,{7,8}-&amp;gt;35,{8,7}-&amp;gt;35,{8,9}-&amp;gt;12,{9,8}-&amp;gt;12},{9,9},Infinity];
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>We create a matrix using the SparseArray function. Each element represents the distance between cities at the row and column of that element. For example, the first element &lt;code>{1,2}-&amp;gt;10&lt;/code> means the distance between 1 and 2 is 10. The second to last element &lt;code>{9,9}&lt;/code> indicates the size of the matrix, and the final element &lt;code>Infinity&lt;/code> means the length of paths between unspecified cities is infinite, meaning there is no path.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">{len,tour}=FindShortestTour[{1,2,3,4,5,6,7,8,9},DistanceFunction-&amp;gt;(d[[#1,#2]]&amp;amp;)]
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>You can easily solve the traveling salesperson problem with the FindShortestTour function. &lt;code>{1,2,3,4,5,6,7,8,9}&lt;/code> represents the city numbers. &lt;code>DistanceFunction-&amp;gt;(d[[#1,#2]]&amp;amp;)&lt;/code> passes the matrix d which represents the distance between cities.&lt;/p>
&lt;h2 id="output">Output
&lt;/h2>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">{137, {1, 2, 5, 9, 8, 7, 6, 3, 4}}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>The output gives the shortest distance and the tour route for it. The shortest distance is &lt;code>137&lt;/code>, and the route is &lt;code>1→2→5→9→8→7→6→3→4→1&lt;/code>. Converting this to ABC order gives &lt;code>A, B, E, I, H, G, F, C, D&lt;/code>.&lt;/p></description></item></channel></rss>