Question

In: Computer Science

In this case study, your task is to study different search algorithms to solve the N-Queens...

In this case study, your task is to study different search algorithms to solve the N-Queens Problem which has been presented in class. We will focus on the incremental formulation in which we add a queen to any square in the leftmost empty column that is not attacked by any other queen.

Question: Does Simulated Annealing (SA) algorithms (with 10 iterations) solve the 14-Queens Problem? What about Simulated Annealing (SA) algorithms (with 50 iterations). Show your answer.

Solutions

Expert Solution

NAVIE ALGORITHM:

while there are untried configurations
{
   generate the next configuration
   if queens don't attack in this configuration then
   {
      print this configuration;
   }
}

BACKTRACKING ALGORITHM:

1) Start in the leftmost column
2) If all queens are placed
    return true
3) Try all rows in the current column. 
   Do following for every tried row.
    a) If the queen can be placed safely in this row 
       then mark this [row, column] as part of the 
       solution and recursively check if placing
       queen here leads to a solution.
    b) If placing the queen in [row, column] leads to
       a solution then return true.
    c) If placing queen doesn't lead to a solution then
       unmark this [row, column] (Backtrack) and go to 
       step (a) to try other rows.
3) If all rows have been tried and nothing worked,
   return false to trigger backtracking.

C# PROGRAM FOR SOLVING QUEEN PROBLEM

using System;

     

class GFG

{

    readonly int N = 4;

void printSolution(int [,]board)

    {

        for (int i = 0; i < N; i++)

        {

            for (int j = 0; j < N; j++)

                Console.Write(" " + board[i, j]

                                  + " ");

            Console.WriteLine();

        }

    }

bool isSafe(int [,]board, int row, int col)

    {

        int i, j;

for (i = 0; i < col; i++)

            if (board[row,i] == 1)

                return false;

        for (i = row, j = col; i >= 0 &&

             j >= 0; i--, j--)

            if (board[i,j] == 1)

                return false;

        for (i = row, j = col; j >= 0 &&

                      i < N; i++, j--)

            if (board[i, j] == 1)

                return false;

        return true;

    }

    bool solveNQUtil(int [,]board, int col)

    {

        if (col >= N)

            return i;

        for (int i = 0; i < N; i++)

{

            if (isSafe(board, i, col))

            {

                /* Place this queen in board[i,col] */

                board[i, col] = 1;

                if (solveNQUtil(board, col + 1) == true)

                    return true;

board[i, col] = 0; // BACKTRACK

            }

        }

        return false;

    }

    bool solveNQ()

    {

        int [,]board = {{ 0, 0, 0, 0 },

                        { 0, 0, 0, 0 },

                        { 0, 0, 0, 0 },

                        { 0, 0, 0, 0 }};

if (solveNQUtil(board, 0) == false)

        {

            Console.Write("Solution does not exist");

            return false;

        }

        printSolution(board);

        return true;

    }

    public static void Main(String []args)

    {

        GFG Queen = new GFG();

        Queen.solveNQ();

    }

}


Related Solutions

In this case study, your task is to study different search algorithms to solve the N-Queens...
In this case study, your task is to study different search algorithms to solve the N-Queens Problem which has been presented in class. We will focus on the incremental formulation in which we add a queen to any square in the leftmost empty column that is not attacked by any other queen. We will compare the following algorithms: 1- Breadth-first Seatch (BFS) 2- Depth-first Search (DFS) 3- Simulated Annealing (SA) 4- Hill Climbing (HC) Question is:. Using any of the...
In this case study, your task is to study different search algorithms to solve the N-Queens...
In this case study, your task is to study different search algorithms to solve the N-Queens Problem which has been presented in class. We will focus on the incremental formulation in which we add a queen to any square in the leftmost empty column that is not attacked by any other queen. Question: Using Depth-first Search (DFS) algorithms, how many steps are needed to find the solution for the 8-Queens Problem? What is it? Draw on an 8x8 table. Show...
In this case study, your task is to study different search algorithms to solve the N-Queens...
In this case study, your task is to study different search algorithms to solve the N-Queens Problem which has been presented in class. We will focus on the incremental formulation in which we add a queen to any square in the leftmost empty column that is not attacked by any other queen. Question: Using Hill Climbing (HC) algorithms, how many steps are needed to find the solution for the 12-Queens Problem? What is it? Draw on an 12x12 table. Show...
Introduction to Algorithms - Analysis of Algorithms Solve the following recurrence relation: T(n) = T(an) +...
Introduction to Algorithms - Analysis of Algorithms Solve the following recurrence relation: T(n) = T(an) + T((1 - a)n) + n
CASE STUDY:                                         &n
CASE STUDY:                                           “THE CARPENTER” Chris is a shy, anxious-looking, 31-year old carpenter who has been hospitalized after making suicide attempt by putting his head in a plastic bag. He asks to meet with the psychiatrist in a darkened room. He is wearing a baseball cap pulled down over his forehead and partially covering his eyes. Looking down at the floor, Chris says he has no friends, has just been fired from his job, and was recently rejected by his...
Using the case study below or your own health issue with goal and objective, your task...
Using the case study below or your own health issue with goal and objective, your task is to develop three outputs and one indicator for each output, considering the means of verification and assumptions for your indicators through completing the table below Goal: Decrease the complications of Hyperglycemia Objective: To maintain the blood glucose level within normal range Output Indicators (Measure to verify to what extent the goal is fulfilled – include targets and baseline data where possible) Means of...
Module 7 showed that one way of comparing different algorithms for accomplishing the same task is...
Module 7 showed that one way of comparing different algorithms for accomplishing the same task is complexity analysis. You will recall that in complexity analysis we express the time an algorithm takes to run as a function of the size of the input, and we used the big-Oh notation. For example, if an algorithm has a complexity of O(1), then it always runs in the same amount of time, no matter what the size of the input is; if it...
There are two algorithms that perform a particular task. Algorithm 1 has a complexity function: f(n)...
There are two algorithms that perform a particular task. Algorithm 1 has a complexity function: f(n) = 5n + 50. Algorithm 2 has a complexity function g(n) = n^2 + 10g . (Show your answer) a)Which algorithm is more efficient when n = 5? b) Which algorithm is more efficient when n = 20? c) what are complexity of f(n) and g(n)
Task: Read the case study below and answer the following questions. Case Study: The Reveton Ransomware...
Task: Read the case study below and answer the following questions. Case Study: The Reveton Ransomware Attacks In August 2012, the Internet Crime Complaint Center (IC3), a partnership between the FBI and the National White Collar Crime Center, was inundated with reports of a new type of cybercrime. Victims across the United States reported that while searching the Internet, their computers locked up, and they received the following message, purportedly from the FBI: “This operating system is locked due to...
What are the different algorithms we may use to solve the 0/1 Knapsack problem? Compare and...
What are the different algorithms we may use to solve the 0/1 Knapsack problem? Compare and contrast the different methods.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT