Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary - ADVANCE DATA STRUCTURE

Rating
-
Sold
-
Pages
16
Uploaded on
16-12-2024
Written in
2024/2025

Provides with key concepts

Institution
Course

Content preview

Backtracking: General Method
Backtracking is a general algorithmic technique that involves searching for a solution to a problem incrementally, one
piece at a time, and removing solutions that fail to satisfy the constraints of the problem. It is often used to solve
constraint satisfaction problems such as puzzles, games, and combinatorial optimization problems.

Key Steps in Backtracking:
Choose: Select a possible choice for the current position (candidate solution).
Explore: Recursively proceed to solve the problem by moving to the next position.
Check Constraints: If the current solution violates the constraints, discard (backtrack) it.
Backtrack: If a solution is invalid or reaches a dead end, undo the last choice and try a new one.
Backtracking can be represented as a state space tree, where each node represents a partial solution. The algorithm
explores the tree in a depth-first manner, pruning branches that are invalid or lead to dead ends. This reduces the
search space and improves efficiency over brute force approaches.

Example Applications:

N-Queens Problem
Sudoku Solving
Graph Coloring
Hamiltonian Path Problem

--------------------------------------------------------------------------------------------------------------------------------------------------------------
8-Queens Problem Using Backtracking
The 8-Queens problem involves placing 8 queens on a chessboard such that no two queens can attack each other. In
chess, a queen can move horizontally, vertically, or diagonally, so no two queens can share the same row, column, or
diagonal.

Approach:
The problem can be solved using backtracking by placing queens row by row and checking for conflicts in the current
column and both diagonals (primary and secondary) for each placement.

Algorithm Steps:
Start with the first row.
Try placing a queen in each column of the current row.
For each placement, check if it’s safe (i.e., no other queens can attack it).
If it’s safe, move to the next row and repeat the process.
If placing a queen leads to a conflict, backtrack by removing the last placed queen and trying a new position in the
previous row.
Continue this process until all 8 queens are successfully placed on the board.

Algorithm (Pseudo-code):
function solveNQueens(board, row):
if row >= N: // If all queens are placed
print(board) // Print or store the board configuration

, return

for col in 0 to N-1: // Try all columns in the current row
if isSafe(board, row, col): // Check if placing queen is safe
board[row][col] = 1 // Place the queen

solveNQueens(board, row + 1) // Recur for the next row

board[row][col] = 0 // Backtrack by removing the queen

function isSafe(board, row, col):
// Check the column, left diagonal, and right diagonal
for each previous row i:
if a queen is placed in the same column or diagonal:
return false
return true

N = 8 // Number of queens
board = createEmptyBoard(N)
solveNQueens(board, 0)
Explanation:
solveNQueens is the recursive function that tries to place queens row by row.
isSafe checks if placing a queen at a particular position would result in a conflict.
The solution is built row by row. If a conflict occurs, the algorithm backtracks by removing the queen and trying the next
possible position in the previous row.
Example:
A solution for the 8-Queens problem can look like this:


. . . . . . Q .
. . Q. . . . .
. . . . . . . Q
. Q . . . . . .
. . . . Q . . .
Q. . . . . . .
. . . . . Q . .
. . . Q . . . .
This board represents one valid arrangement of 8 queens where none of them can attack each other.

Complexity:
Time complexity: O(N!), where N is the number of queens (since each row has N possible positions, but many invalid
solutions are pruned).
Space complexity: O(N^2) for storing the chessboard.
Summary

, Backtracking is a general method used for solving constraint satisfaction problems by incrementally building solutions
and discarding those that do not satisfy constraints.
The 8-Queens problem is a classic application of backtracking, where the solution is built row by row, and invalid
configurations are pruned by checking for conflicts.
---------------------------------------------------------------------------------------------------------------------------------------------------------------

Backtracking: Sum of Subsets Problem
The Sum of Subsets problem is a classic combinatorial problem where the objective is to find all subsets of a given set
whose sum equals a specified target value. Backtracking is an ideal approach for this problem as it helps in exploring all
possible subsets while efficiently pruning solutions that cannot lead to the desired sum.

Problem Definition:
Given a set of positive integers,
S={s 1,s2,…,sn}, and a target sum T, the goal is to find all subsets of S such that the sum of the elements in each
subset equals T.

Approach:
Search Space: The power set (all subsets) of S is explored. For each element, there are two possibilities:
Include the element in the subset.
Exclude the element from the subset.
Backtracking Process: The algorithm incrementally builds the subset, checking at each step whether:
The subset’s current sum is equal to the target (solution found).
The current sum exceeds the target (prune the branch).

Algorithm Steps:
Start with an empty subset and explore adding elements one by one.
After adding an element, check if the sum of the subset exceeds the target sum.
If it exceeds, backtrack by removing the last added element and explore other options.
If the sum is equal to the target, print or store the current subset as a valid solution.
Recursively repeat the process for the remaining elements.

Algorithm (Pseudo-code):
function sumOfSubsets(set, subset, index, current_sum, target_sum):
if current_sum == target_sum: // If subset sum equals target
print(subset) // Valid subset found
return

if current_sum > target_sum or index == len(set): // If the sum exceeds target or no elements left
return // Prune the branch

// Include the current element
subset.append(set[index])
sumOfSubsets(set, subset, index + 1, current_sum + set[index], target_sum)

Written for

Institution
Course

Document information

Uploaded on
December 16, 2024
Number of pages
16
Written in
2024/2025
Type
SUMMARY

Subjects

$5.69
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
priyankamuthabathula18

Also available in package deal

Get to know the seller

Seller avatar
priyankamuthabathula18 Srinivasa Institute of Engineering and Technology
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
6
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions