Nqueen
Nqueen
Design a program to find all occurrences of a pattern within a text string using a brute
force approach. Your algorithm should return a list of starting indices for each occurrence
of the pattern in the text.
Example:
● Text: "abracadabra"
● Pattern: "abra"
In this above tree, backtracking which uses Depth-First search, considers the below states only
when the state provides a feasible solution. Consider the above tree, start from the root node,
then move to node A and then node C.
If node C does not provide the feasible solution, then there is no point in considering the states G
and H. We backtrack from node C to node A. Then, we move from node A to node D. Since
node D does not provide the feasible solution, we discard this state and backtrack from node D to
node A.
We move to node B, then we move from node B to node E. We move from node E to node K;
Since k is a solution, it takes 10 steps to find the solution. In this way, we eliminate more states
in a single iteration. Therefore, we can say that backtracking is faster and more efficient than the
brute force approach.
EXAMPLE:
If there is a lock of 4-digit PIN. The digits to be chosen from 0-9 then the brute force will be
trying all possible combinations one by one like 0001, 0002, 0003, 0004, and so on until we get
the right PIN. In the worst case, it will take 10,000 tries to find the right combination.
1. Sequential search
2. String matching algorithm
3. Travelling salesman problem
4. Knapsack problem
Program for all occurrences of a pattern within a text string using a Brute Force
Approach:
#include <stdio.h>
#include <string.h>
int n = strlen(text);
int m = strlen(pattern);
int j;
if (text[i + j] != pattern[j])
break;
if (j == m) {
}
}
int main() {
printf("Occurrences:\n");
findOccurrences(text, pattern);
return 0;
OUTPUT:
Text: abracadabra
Pattern: abra
Occurrences:
ALGORITHM:
STEP2: For each position in the text string, compare the characters with the pattern starting from
that position.
STEP 3: If a mismatch is found at any position, move to the next position in the text string.
STEP 4: If all characters in the pattern match the corresponding characters in the text string
starting from a position, record that position as an occurrence of the pattern.
STEP 5: Repeat this process until the entire text string has been checked.
ADVANTAGES:
DISADVANTAGES:
Inefficient for large problem sizes due to the exponential growth in computation time.
Not suitable for complex problems with a large search space.
Can be resource-intensive and slow.
FEATURES:
Exhaustive search: The brute force approach systematically tries all possible solutions.
Guarantees solution: It ensures finding the correct solution given enough time and resources.
Simplicity: Easy to implement and understand, making it accessible for various problems.
Effectiveness for small instances: Particularly useful for manageable problem sizes where all
solutions can be explored.
APPLICATION:
Password cracking.
Cryptanalysis.
Combinatorial optimization problems.
Network security testing.
3.Implement a backtracking algorithm to solve the N-Queens problem for a given N. Given
an N x N chessboard, the task is to place N queens on the board such that no two queens
attack each other. Your algorithm should return all possible solutions to the problem.
N-QUEENS PROBLEM:
The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two
queens attack each other.
The backtracking algorithm works by placing queens one by one in different columns,
and then checking if the current placement is safe or not.
If a safe placement is found, the algorithm continues to place the next queen.
If not, it backtracks and tries a different place of the location of the chess board.
This process continues until all queens are placed on the board without attacking each
other.
Step 2:
Put our next Queen (Q2) in the (1,2) cell .
After this move to the next row [ 1 -> 2 ].
Step 3:
At row 2 there is no cell which are safe to place Queen (Q3) .
So, backtrack and remove queen Q2 queen from cell ( 1, 2 ) .
Step 4:
There is still a safe cell in the row 1 i.e. cell ( 1, 3 ).
Put Queen ( Q2 ) at cell ( 1, 3).
Step 5:
Put queen ( Q3 ) at cell ( 2, 1 ).
Step 6:
There is no any cell to place Queen ( Q4 ) at row 3.
Backtrack and remove Queen ( Q3 ) from row 2.
Again there is no other safe cell in row 2, So backtrack again and remove queen
( Q2 ) from row 1.
Queen ( Q1 ) will be remove from cell (0,0) and move to next safe cell i.e. (0 , 1).
Step 7:
Place Queen Q1 at cell (0 , 1), and move to next row.
Step 8:
Place Queen Q2 at cell (1 , 3), and move to next row.
Step 9:
Place Queen Q3 at cell (2 , 0), and move to next row.
Step 10:
Place Queen Q4 at cell (3 , 2), and move to next row.
This is one possible configuration of solution
ALGORITHM:
STEP 1: Start with an empty N x N chessboard.
STEP 2: Place a queen in the first row of the board.
STEP 3: Move to the next row and place a queen in a safe position where it does not attack any
other queens.
STEP 4: Repeat step 3 for all rows until all N queens are placed on the board.
STEP 5: If all queens are placed successfully, add the current configuration to the list of
solutions.
STEP 6: Backtrack and try different positions if a conflict is encountered.
PROGRAM:
#include <stdio.h>
#include <stdbool.h>
#define N 4// Define the size of the chessboard
// Function to check if a queen can be placed at board[row][col]
bool isSafe(int board[N][N], int row, int col) {
int i, j;
return res;
}
// Function to solve the N-Queens problem
voids solve N Queens () {
int board[N][N] = {0};
int main () {
printf("Solutions to the 4-Queens problem:\n");
solveNQueens();
return 0;
}
OUTPUT:
The output for the 4-Queens problem should display the solutions where queens can be placed on
the chessboard without attacking each other. The program uses backtracking to find all possible
solutions to the N-Queens problem.