<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Diogo Nunes</title>
    <description>The latest articles on DEV Community by Diogo Nunes (@diogopnunes).</description>
    <link>https://dev.to/diogopnunes</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F39888%2Fdeb75c20-332f-432e-b85d-b31acaddb730.png</url>
      <title>DEV Community: Diogo Nunes</title>
      <link>https://dev.to/diogopnunes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/diogopnunes"/>
    <language>en</language>
    <item>
      <title>The N-Queens-Puzzle is ... (not so?) puzzling.</title>
      <dc:creator>Diogo Nunes</dc:creator>
      <pubDate>Wed, 08 Nov 2017 13:40:00 +0000</pubDate>
      <link>https://dev.to/diogopnunes/the-n-queens-puzzle-is--not-so-puzzling-b53</link>
      <guid>https://dev.to/diogopnunes/the-n-queens-puzzle-is--not-so-puzzling-b53</guid>
      <description>

&lt;h3&gt;
  
  
  &lt;strong&gt;What is the N-Queens-Puzzle?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Following &lt;a href="https://en.wikipedia.org/wiki/Eight_queens_puzzle"&gt;this Wiki link to the 8-Queens-Puzzle&lt;/a&gt; one quickly understands the simple puzzle imposed. Summarizing, the objective is to place the &lt;strong&gt;N&lt;/strong&gt;-Queens on a &lt;strong&gt;(NxN)&lt;/strong&gt; board, so that no queen is attacked by another - vertically, horizontally or diagonally (just like chess).&lt;br&gt;
(One) Solution:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e6JRHkjs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://poj.org/images/3239_1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e6JRHkjs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://poj.org/images/3239_1.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Not&lt;/strong&gt; a solution:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h_qkHG94--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://centurion2.com/AIHomework/Searching/8-Queens.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h_qkHG94--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://centurion2.com/AIHomework/Searching/8-Queens.JPG" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Simple&lt;/em&gt; to understand, yet &lt;em&gt;exponentially hard&lt;/em&gt; to solve.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Problem-Formulation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We now &lt;em&gt;understand&lt;/em&gt; our problem - great - but the question here is:&lt;br&gt;
&lt;strong&gt;How&lt;/strong&gt; will we solve it? - What is the &lt;strong&gt;best&lt;/strong&gt; approach for &lt;strong&gt;most&lt;/strong&gt; plays?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Will we try for &lt;em&gt;each&lt;/em&gt; queen &lt;em&gt;every&lt;/em&gt; not occupied position and verify its validity? - in other words, brute force our way to the solution.&lt;/li&gt;
&lt;li&gt;Will we place a queen and immediately cross out &lt;em&gt;all the positions&lt;/em&gt; it attacks and try to place the next one on &lt;em&gt;all&lt;/em&gt; the valid positions left?&lt;/li&gt;
&lt;li&gt;Do we even need to consider &lt;em&gt;all&lt;/em&gt; the &lt;em&gt;valid&lt;/em&gt; positions left?&lt;/li&gt;
&lt;li&gt;For the first queen, the board is empty - where do we try to place the very first one since &lt;em&gt;all&lt;/em&gt; positions are valid? &lt;em&gt;Everywhere&lt;/em&gt;?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The list goes &lt;em&gt;on and on&lt;/em&gt;. As I'm trying to expose here, there are many different and &lt;em&gt;valid&lt;/em&gt; ways to formulate the problem and find a solution.&lt;br&gt;
&lt;strong&gt;But some are better than others. &lt;em&gt;Way&lt;/em&gt; better.&lt;/strong&gt;&lt;br&gt;
Obviously the first approach always comes down to &lt;em&gt;brute-force&lt;/em&gt;. But this is where our &lt;em&gt;understanding and perspective&lt;/em&gt; of the problem comes into play. By brute-forcing the various possible positions to place a queen we are mistakenly trying solution paths that - if we give it a little thought - are, &lt;em&gt;from the very first&lt;/em&gt; step, &lt;strong&gt;unsolvable&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Just to give you a little taste&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Bear with me in this line of thought:&lt;br&gt;
Given the way a queen attacks the various positions on a board, &lt;strong&gt;does it make any sense&lt;/strong&gt; to place another on the &lt;em&gt;same&lt;/em&gt; row? or the &lt;em&gt;same&lt;/em&gt; column? or even, &lt;em&gt;the same&lt;/em&gt; diagonal? &lt;strong&gt;No, the answer is no, it does not make sense&lt;/strong&gt;, because we know immediately that this positions are invalid once we place a queen - we need not even think of this possibilities.&lt;br&gt;
Even more, since we now know there is no point in placing a queen on the same row, column or diagonal as another, we know exactly where to place the very &lt;em&gt;first&lt;/em&gt; queen. No, we do not have &lt;strong&gt;N * N&lt;/strong&gt; (reads &lt;em&gt;N times N&lt;/em&gt;) possibilities for the first queen, actually, we only have &lt;strong&gt;N&lt;/strong&gt;, on the very first column. And the same goes for the next queen - meaning, there is no point in trying to place a queen somewhere else than the &lt;strong&gt;leftmost free column&lt;/strong&gt;, since every column will have exactly one and only one queen.&lt;br&gt;
&lt;em&gt;This perspective (or, formally, problem-formulation) will immensely reduce the possible solution path's to take - therefore, will be much faster and efficient&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusions&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The way we tackle a problem, starting with how well we understand it, will greatly affect the outcome of our solving. This is actually tangible with some AI resources: &lt;br&gt;
&lt;em&gt;How well does an algorithm preform given a certain problem-formulation?&lt;/em&gt;&lt;br&gt;
&lt;em&gt;How much time, tried possibilities and different solution path's will it expand to reach a solution?&lt;/em&gt;&lt;br&gt;
I have implemented (&lt;a href="https://github.com/diogo-p-nunes/queen-puzzle"&gt;here&lt;/a&gt;) in python all of the above problem-formulations and some more, so that anyone can try and verify it by themselves.&lt;br&gt;
I purposely implemented them with a &lt;strong&gt;blind-search&lt;/strong&gt;, the least efficient way to find a solution for a problem we know so well - to show how well it will preform compared with poor/better problem-formulations.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Regards&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I hope you enjoyed the post and actually give my &lt;a href="https://github.com/diogo-p-nunes/queen-puzzle/tree/master/comparator"&gt;comparator tool&lt;/a&gt; a try to see it in action by yourself! Please give me &lt;strong&gt;feedback&lt;/strong&gt; or implement new ideas of problem-formulations!&lt;br&gt;
See you next time,&lt;br&gt;
Diogo from Portugal&lt;/p&gt;


</description>
      <category>python</category>
      <category>ai</category>
      <category>problemsolving</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
