0% found this document useful (0 votes)
20 views6 pages

Daa 250207 194917

The document discusses various algorithmic techniques including backtracking, NP-hard and NP-complete problems, and greedy algorithms. It provides a detailed explanation of the N-Queens problem, including a solution process and pseudo-code, as well as a comparison between NP-hard and NP-complete classifications. Additionally, it presents examples of state space trees and graph coloring using a greedy approach.

Uploaded by

Yeamin Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Daa 250207 194917

The document discusses various algorithmic techniques including backtracking, NP-hard and NP-complete problems, and greedy algorithms. It provides a detailed explanation of the N-Queens problem, including a solution process and pseudo-code, as well as a comparison between NP-hard and NP-complete classifications. Additionally, it presents examples of state space trees and graph coloring using a greedy approach.

Uploaded by

Yeamin Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

hello dear 😎🫰

Ans to Ques No (1)


Backtracking can be defined as a general algorithm technique that considers searching evenly possible
combination in order to solve a computational problem. It is efficient for solving constraint-based
problems. It is flexible for problems like puzzles, mazes, and optimization.

Example:

Let’s place 4 queens on a 4×4 chessboard such that no two queens threaten each other.

Solution Process Using Backtracking

1. Start placing queens’ row by row, one queen in each row.

2. For each row, try placing the queen in different columns.

3. After placing a queen, check:

o If no other queen threatens it, proceed to the next row.

o If it’s threatened, backtrack and try the next column in the current row.

4. Continue this process until all queens are placed or no solution exists.

hello dear 😎🫰
Ans to Ques No (2)

State Space Tree

Algorithm:

• Start in the leftmost column

• If all queens are placed return true

• Try all rows in the current column. Do the following for every row.

o If the queen can be placed safely in this row

o Then mark this [row, column] as part of the solution and recursively check if
placing queen here leads to a solution.

o If placing the queen in [row, column] leads to a solution then return true.

o If placing queen doesn’t lead to a solution then unmark this [row, column] then
backtrack and try other rows.

o If all rows have been tried and valid solution is not found return false to trigger
backtracking.

hello dear 😎🫰
Pseudo-Code

function solveNQueens(n):
board = initialize n x n board with 0
if placeQueens(board, 0, n) == true:
printSolution(board)
else:
print("No solution exists")

function placeQueens(board, row, n):


if row == n:
return true
for col = 0 to n-1:
if isSafe(board, row, col, n):
board[row][col] = 1
if placeQueens(board, row + 1, n) == true:
return true
board[row][col] = 0 // Backtrack
return false

function isSafe(board, row, col, n):


for i = 0 to row-1:
if board[i][col] == 1:
return false
i = row - 1, j = col - 1
while i >= 0 and j >= 0:
if board[i][j] == 1:
return false
i--, j--
i = row - 1, j = col + 1
while i >= 0 and j < n:
if board[i][j] == 1:
return false
i--, j++
return true

hello dear 😎🫰
Ans to Ques No (3)
Difference between NP-Hard and NP-Complete

NP-Hard NP-Complete
NP-Hard is the class of problems as hard as the
NP-complete is both NP and NP-hard.
hardest problems in NP.
Problems in NP-Hard do not need to be in the NP
Problems in NP-Complete are a subset of NP,
class.

Solutions may not be verifiable in polynomial time Solutions can be verified in polynomial time.
It focuses on the difficulty of solving the problem
It focuses on problems that are both difficult
but doesn’t always guarantee solution verification in
to solve and verifiable in polynomial time.
polynomial time.

Examples: Subset Sum Problem, Knapsack


Examples: Halting Problem, Travelling Salesman
Problem, Travelling Salesman Problem
Problem (general case).
(decision version).

Ans to Ques No (4)


Applying Greedy Algorithm, we have the color table-

Vertex a b c d e f
Color C1 C2 C1 C2 C1 C2

From here,

Minimum number of colors used to color the given graph


are 2.

Therefore, Chromatic Number of the given graph = 2.

hello dear 😎🫰
Ans to Ques No (5)
(i) State Space Tree:

All the possibilities are:


{B1,B2,G}
{B1,G,B2}
{B2,B1,G}
{B2,G,B1}
{G,B1,B2}
{G,B2,B1}

(ii) State Space Tree for If "girl must not sit in between 2 boys":

All the possibilities are:


{B1,B2,G}
{B2,B1,G}
{G,B1,B2}
{G,B2,B1}

hello dear 😎🫰

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy