Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since at each step you have 8 possibilities to check and this has to be done for each cell (minus the last one) the time complexity of this algorithm is O(8^(n^2-1)) = O(8^(n^2)) where n is the number of squares on the edges of the checkboard. To be precise this is the worst case time complexity (time taken to explore all the possibilities if none is found or if it is the last one).</p> <p>As for the optimizations there can be 2 types of improvements:</p> <h2>Implementation improvements</h2> <p>You're calculating x-2, x-1, x+1, x+2 and the same for y at least the double of the times. I can suggest to rewrite things like this:</p> <pre><code>int sm1 = size - 1; int xm2 = x - 2; int yp1 = y + 1; if((xm2 &gt;= 0) &amp;&amp; (yp1 &lt;= (sm1))){ mark(arr[x][y]); if(backtrack(xm2, yp1)) return true; else unmark(arr[x][y]); } int ym1 = y-1; if((xm2 &gt;= 0) &amp;&amp; (ym1 &gt;= 0)){ mark(arr[x][y]); if(backtrack(xm2, ym1)) return true; else unmark(arr[x][y]); } </code></pre> <p>note the reusing of precalculated values also in subsequent blocks. I've found this to be more effective than what I was especting; meaning that variable assignment and recall is faster than doing the operation again. Also consider saving <code>sm1 = s - 1;</code> and <code>area = s * s;</code> in the constructor instead of calculating each time.</p> <p>However this (being an implementation improvement and not an algorithm improvement) will not change the time complexity order but only divide the time by a certain factor. I mean time complexity O(8^(n^2)) = k*8^(n^2) and the difference will be in a lower k factor.</p> <h2>Algorithm improvements</h2> <p>I can think this:</p> <ul> <li>for each tour starting on in a cell in the diagonals (like starting in (0,0) as in your example) you can consider only the first moves being on one of the two half checkboards created by the diagonal. <ul> <li>This is beacouse of the simmetry or it exists 2 simmetric solutions or none.</li> <li>This gives O(4*8^(n^2-2)) for that cases but the same remains for non simmetric ones.</li> <li>Note that again O(4*8^(n^2-2)) = O(8^(n^2))</li> </ul></li> <li>try to interrupt the rush early if some global condition suggests that a solution is impossible given the current markings. <ul> <li>for example the horse cannot jump two bulk columns or rows so if you have two bulk marked columns (or rows) and unmarked cells on both sides you're sure that there will be no solution. Consider that this can be checked in O(n) if you mantain number of marked cells per col/row updated. Then if you check this after each marking you're adding O(n*8^(n^2)) time that is not bad if n &lt; = 8. Workaround is simply not to check alwais but maybe every n/8 markings (checking <code>counter % 8 == 4</code> for example or better <code>counter &gt; 2*n &amp;&amp; counter % 8 == 4</code></li> </ul></li> <li>find other ideas to cleverly interrupt the search early but remember that the backtrack algorithm with 8 options will always have its nature of being O(8^(2^n)).</li> </ul> <p>Bye</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload