Question

how to write n-queens function using bitwise operations using MIPS or just give an explained algorithm...

how to write n-queens function using bitwise operations using MIPS or just give an explained algorithm ?

Homework Answers

Answer #1

The N Queens problem is a fairly well-known puzzle in the computer science community. The goal is to place N queens on an N x N chessboard in such a way that none of the queens can attack one another.


-->As you might think this problem gets even more harder with the increase of N, especially when you want to find all possible solutions rather than just finding possible solution.

-->No one has find possible solution for more than N=27. It’s a world record.

-->The implementation of this algorithm in Javascript is more efficient because it is using bitwise operators.

Algorithm Explanation:

---------------------------------------

--->In the game of the chess, a queen can attack in and direction and any steps. So, there can only be one queen in each row and column in addition at most one in each diagonal.
--->Now we know we can only place one queen in each row so we’ll start from the first raw then second row to Nth row until... a)   We get a valid solution b)   We reach at dead end (i.e. we can’t place queens safely)
--->We are putting one queen in one row so don’t need to worry about horizontal attack because in every case there will be only one queen in each row.
--->Now we need to check only three more conditions… a) Column doesn’t have more than one queen b) Left diagonal doesn’t have more than one queen c) Right diagonal doesn’t have more than one queen
--->Now if all the above conditions are fulfilled then that square is safe to put the queen so we’ll increase the counter or we’ll give up and try next possible situation.

Example Program:
---------------------------

Basically, this is a recursive function and let’s directly come on the main logic of the program innerRecursion, which takes three arguments col means column, leftDia means left diagonal, rightDia means right diagonal. These arguments are basically integers but our algorithm takes advantage of their bit representation.
* For example… For N=4, col having a value of 0010 would mean that the 3rd column is already occupied by a queen

countNQueensSolutions = (n) => {
//Keeps track of the # of valid solutions
let count = 0;

//Helps identify valid solutions (How may possible squares to visit?)
const done = Math.pow(2,n) - 1;

//Checks all possible board configurations
innerRecursion = (leftDia, col, rightDia) => {
  
//Base condition
//All columns are occupied, so the solution must be complete
if (col === done) {
count++;
return;
}

//Gets a bit sequence with "1"s
//whereever there is an open "slot"
let poss = ~(leftDia | rightDia | col);

//Loops as long as there is a valid
//place to put another queen.
while ( poss & done ) {
let bit = poss & -poss;
poss -= bit;
innerRecursion((leftDia|bit)>>1, col|bit, (rightDia|bit)<<1);
}
};
innerRecursion(0,0,0);
return count;
};

console.log(countNQueensSolutions(10));


Explanation:
-------------------

--> In Line number 6(const done = Math.pow(2,n) - 1;), The "done" variable simply allows me to not worry about any bits beyond the Nth. Most computers are either 32 bits or 64 bits, so in reality, all integers are stored with that many bits. done simply has a bit sequence with 1 for every entry up to the Nth. For example, when N=5, done will equal 11111.

-->Line number 9(innerRecursion = (leftDia, col, rightDia) => {),First of all I’m calling innerRecursion(0, 0, 0), that means I’m calling function with all arguments from position 0. If N=4, we can say all arguments being equal to 0000. Means we haven’t placed any queens yet.

-->Line number 13(if (col === done) {
count++;
return;
}),This is a base condition for the function which checks whether the solution is found or not!!
* It checks it whether all the columns are occupied or not in each calling of function.
* Our algorithm never places queens wrong so we can check if all columns are filled then we have a valid solution.

-->In Line 20(let poss = ~(leftDia | rightDia | col);),The poss variable shows the columns in the current row which are not under attack of any queen.
* Suppose after some iterations we have leftDia = 0001, rightDia = 0101, col = 1001 and we are doing OR operation on them so we’ll get 1101 and at last by adding ~ we are flipping the bits so we’ll get 0010.

-->Line 25,26(let bit = poss & -poss;
poss -= bit;),First statement let bit = poss & -poss stores the first available position means first non-zero position from the poss and stores it in bit.
* So we are placing our queen on that available position, so the next statement poss -= bit will mark that position as occupied and change the bit as ‘1’.


--> In Line 27,innerRecursion( (leftDia | bit) >> 1, col | bit, ( rightDia | bit) << 1) as a my point of view this last statement is most difficult to be understood in this entire function so let’s first break it to understand.
* >>1 and <<1 are the right shift and left shift operators respectively which moves a bit to right or left. (rightDia | bit) >> 1 means combine rightDia and bit apply OR operation and then left shift by 1 bit.
* More specifically, if rightDia is 0001 (meaning that the top-right-to-bottom-left diagonal through column 4 of the current row is occupied), and bit is 0100 (meaning that we are planning to place a queen in column 2 of the current row), (rd|bit) results in 0101 (meaning that after we place a queen in column 2 of the current row, the second and the fourth top-right-to-bottom-left diagonals will be occupied).
* Now, if add in the << operator, we get (rd|bit)<<1, which takes the 0101 we worked out in our previous bullet point, and moves everything to the left by one. The result, therefore, is 1010.
* Now important question is why we are shifting all these values?
The answer is when we put a queen on a square we move down to the different rows at that time we need our left and right diagonals to be up to date.
-->Here is the output

N # of Solution Estimated Timing
4 2 < 1/2 ms
5 10 < 1/2 ms
6 4 < 1/2 ms
7 40 < 1/2 ms
8 92 < 1/2 ms
9 352 1/2 ms
10 724 1 ms
11 2680 6 ms
12 14200 24 ms
13 73712 114 ms
14 365596 667 ms
15 2279184 4077 ms
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the function with header: int foo(int s, int n, int a, int b). Write MIPS...
Consider the function with header: int foo(int s, int n, int a, int b). Write MIPS assembly code for the following statement located inside the above function: return(s+n);
Algorithm problem b. Give an example of a single nonnegative function ƒ(n) such that for all...
Algorithm problem b. Give an example of a single nonnegative function ƒ(n) such that for all functions g(n) in part (a), ƒ(n) is neither in O(g(sub(i))(n)) nor in Ω(g(n)).
Give a recursive algorithm to solve the following recursive function.    f(0) = 0;    f(1)...
Give a recursive algorithm to solve the following recursive function.    f(0) = 0;    f(1) = 1; f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
Write out an algorithm (using a list of steps or a flowchart), for describing to someone...
Write out an algorithm (using a list of steps or a flowchart), for describing to someone how to take the derivative of a polynomial of order n. ?(?) = ?0 + ?1? 1 + ?2? 2 + ⋯ + ??? ?
Without using move or li, write MIPS assembly language using MARS simulator to print a half...
Without using move or li, write MIPS assembly language using MARS simulator to print a half pyramid depending on a value n of a user input. Such that if n = 5 were entered the following would be printed: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
1- Write algorithm (in pseudo-code) for the following problem: sum of the series: 1 +2+3+…+N 2-...
1- Write algorithm (in pseudo-code) for the following problem: sum of the series: 1 +2+3+…+N 2- Convert an algorithm to a flowchart diagram. 3- Counting the primitive operations of the algorithm. 4- Determine algorithm complexity. 5- Write a complete C++ program for the previous algorithm where the program should contain the following:  Display messages.  Input any number;  Display the series.  Display the sum of the series.
(a) Write an algorithm (use pseudo-code) to determine whether a function f ∶ Z100 → Z100...
(a) Write an algorithm (use pseudo-code) to determine whether a function f ∶ Z100 → Z100 is surjective. That is, supply a “Method” to go with Input: A function (array) f with f(i) ∈ Z100 for i = 0, 1, . . . , 99. Output: Boolean B. B=‘true’ if f is surjective, ‘false’ otherwise. Try to make your algorithm as efficient as possible. Do NOT include an implementation of your algorithm in a programming language. (b) How many comparisons...
What is the algorithm of rchisq() function in R? I am trying to build my myrchisq(),...
What is the algorithm of rchisq() function in R? I am trying to build my myrchisq(), can anyone build the function that will give me the same result as rchisq()? Note: I don't need ncp = 0 I just need myrchisq = function(n, df){}
Write a function called mysum that sums the first n positive numbers. Give the result x1...
Write a function called mysum that sums the first n positive numbers. Give the result x1 for n=20,and the result x2 for n=100. Matlab code
Consider the following function : F 1 = 2, F n = (F n-1 ) 2...
Consider the following function : F 1 = 2, F n = (F n-1 ) 2 , n ≥ 2 i. What is the complexity of the algorithm that computes F n using the recursive definition given above. ii. Describe a more efficient algorithm to calculate F n and give its running time.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT