0% found this document useful (0 votes)
45 views25 pages

Group 2

Uploaded by

Đàm Thanh Loan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views25 pages

Group 2

Uploaded by

Đàm Thanh Loan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

VIETNAM NATIONAL UNIVERSITY, HANOI

INTERNATIONAL SCHOOL
-------------------------------

Course: Data structures and algorithms

Topic: A study on backtracking algorithm

Lecturer : PhD Diem Cong Hoang


Course ID : INS3050

Group :2
Dam Thanh Loan 22070714
Nguyen Thi Lan Anh 22070844
Tran Thi Huong Ly 21070569
Vu Thi Thuy Nga 22070710
Table of contents

Abstract 4

List of abbreviations 5

List of figures and table 6

Introduction 7

1. Reason for choosing this topic 7

2. Objectives of project 7

Chapter 1: Backtracking Algorithm 9

I. Definition 9

II. Backtracking Algorithm 9

1. Backtracking process 9

2. How does a Backtracking Algorithm Work? 10

3. Advantages and disadvantages of Backtracking 12

A. Advantages of Backtracking 12

B. Disadvantages of Backtracking 13

Chapter 2: Application of Backtracking Algorithm 14

I. Example of Backtracking Algorithm 14

1. Constraint Satisfaction Problems (CSP) 14

2. Encoding and Decoding Problem 15

3. Pathfinding Problems 15

II. Algorithm N_ Queens solution by backtracking 15


2
1. N_ Queens Problem 15

2. Algorithm 4-Queens solution by backtracking 16

Conclusion 23

Reference 24

Member’ contribution 25

3
Abstract

This report presents a comprehensive study of Backtracking Algorithm, which is


an important method in computer science used to solve many combinatorial problems
and constraint satisfaction problems quickly and efficiently. This report presents the
framework and theory of the algorithm; describes in detail the recursive process (how
it prunes branches efficiently to reduce computational cost compared to the full test
method).

The report also analyzes the diversity in the application of the algorithm, for
example in solving constraint satisfaction problems (CSP) such as Sudoku, path
finding problems, etc. The report will develop a detailed implementation of the N-
Queens problem to clearly illustrate the algorithm process of handling constraints by
permuting and backtracking when detecting conflicts.

In addition, the report also discusses the advantages of the algorithm (including
flexibility, pruning mechanism). However, the algorithm also has its own limitations
such as exponentially increasing time complexity and large memory requirements
when solving large-scale problems. However, the backtracking algorithm is still an
indispensable method in solving computational problems using hybrid techniques and
parallel computing.

In conclusion, this study emphasizes the relevance and applicability of the


algorithm in both theory and practice, helping us to understand more deeply its role in
algorithm design and optimization.

4
List of Abbreviations

Abbreviation Definition
BT Backtracking
IS Initial State
C Checkpoints
TN Terminal Nodes
CSP Constraint Satisfaction Problems

5
List of figures and tables

Figure 1.1 Backtracking process 8

Figure 2.1 State-space tree of solving the four queens ‘problem by backtracking 7

Figure 2.2. A deadlock situation of non-attacking 4-queen problem 7

6
Introduction

1. Reasons for choosing this topic

Constraint-based reasoning is a simple, yet powerful paradigm in which many


interesting problems can be formulated. It has received much attention recently, and
numerous methods for dealing with constraint networks have been developed. The
applications include graph coloring, scene labeling, natural language parsing, and
temporal reasoning.

The basic notion of constraint-based reasoning is a constraint network. The


generic backtracking algorithm was first described more than a century ago, and since
then has been rediscovered many times. In recent years, many new backtracking
algorithms have been proposed. The basic ones include Backmarking, Backjumping,
Forward Checking and Conflict-Directed Backjumping. Several hybrid algorithms,
which combine two or more basic algorithms, have also been developed (Kondrak &
Peter van Beek, 1996).

Backtracking algorithms are fundamental in solving combinatorial problems


efficiently. They provide a structured way to explore all possible solutions while
pruning paths that don't lead to valid results. Due to their versatility, they are applied
in various fields such as constraint satisfaction, optimization, and search problems.
Understanding these algorithms enhances problem-solving skills and provides
valuable insights into developing hybrid methods for optimizing performance in
complex systems.

2. Objectives of project

This project aims to provide a comprehensive understanding of the Backtracking


Algorithm, focusing on both its theoretical framework and practical applications. By
analyzing its implementation in solving complex problems like the N-Queens
problem, the project will highlight how backtracking offers an efficient approach to
7
constraint satisfaction. The study also aims to explore the algorithm's versatility and
adaptability in various fields, emphasizing its importance in problem-solving and
optimization tasks.

8
Chapter 1: Backtracking Algorithm

I. Definition

Backtracking is an algorithm designed based on recursion with the idea: At each


step, we will find a reasonable solution for that step and continue to consider the next
step. The first person to propose this term (BT) was the American mathematician D.
H. Lehmer in the 1950s.

Backtracking is a general algorithm technique for finding all possible solutions for
the problem and discards partial solutions as soon as possible without following them
till the end (Pal, D & Halder, S 2018). Backtracking is a depth-first traversal of the
path in the graph where nodes are states of the solution and edge between two states of
solution only if one state can be reached from another state. Each path may lead to a
solution, taking one path at a time and as soon as the path does not lead to the solution
then go back and try with the alternate path.

In fact, in life, there are many activities where we are applying the backtracking
strategy. For instance, in the maze puzzle game, when you enter a maze, you may
encounter many turns. You try to go down one turn, and if you find it is a dead end,
you will backtrack to the nearest turn and try other directions. Eventually, you will try
all possible paths until you find the exit.

II. Backtracking Algorithm

1. Backtracking process

The backtracking algorithm is a search method that extends partial solutions


step-by-step in a fixed order. Suppose we need to enumerate all possible
configurations of a sequence of variables x 1 , x 2 ,... , x n, where each variable can take
values from a specified domain (Kondrak, G., & Van Beek, P, 1997).

9
Then the backtracking algorithm is performed through the following steps:

● Consider all possible values of x 1, try to let x 1 receive those values in turn. For
each test value for x 1, we will:
● Consider all possible values of x 2, try to let x 2 receive those values in turn. For
each test value for x 2, we will consider possible values of x 3 and continue like
that
● Consider all possible values of xn, try to let x n receive those values in turn.
Print out the found configuration.

However, if the algorithm encounters a dead-end, meaning all values for the
current variable are invalid, it backtracks by unassigning the most recent variable and
testing a different value for it. This systematic approach ensures that all possible
configurations are explored by exhaustively checking every potential combination
within the domain of the variables.

2. How Does a Backtracking Algorithm Work?

In a backtracking algorithm, the problem's possible states can be represented as a


state-space tree. A state-space tree is a tree that represents all of the possible states of
the problem, from the root as an initial state to the leaf as a terminal node.

In any backtracking algorithm, the algorithm seeks a path to a feasible solution


that includes some intermediate checkpoints. If the checkpoints do not lead to a viable
solution, the problem can return to the checkpoints and take another path to find a
solution. Consider the following scenario:

10
Figure 1.1 - Backtracking process

where:
IS (Initial state): is the starting point where the algorithm begins its search for a
solution.

C (Checkpoints): At each "C" node, the algorithm evaluates possible choices to extend
the current partial solution. If a choice is viable, the algorithm moves forward to the
next state in the tree.

Non-accepted Terminal Nodes (TN1, TN2, TN3, TN4, TN5): These are dead-end
nodes, where the algorithm finds that the current choices cannot form a valid solution.
Upon reaching these nodes, the algorithm backtracks to the nearest checkpoint node to
try a different path.

Accepted Terminal Node (TN6): This is the goal node, representing a solution that
satisfies all the problem’s conditions. When the algorithm reaches this node, it stops,
as it has found a valid solution.

Backtracking Process:

● We start with an empty solution, S={}. This is where the algorithm begins
searching for a solution.

11
● At each checkpoint C i, the algorithm tries assigning a value v to the next
variable x i. If this choice satisfies the problem’s constraints, it adds the
assignment to the current solution S and moves forward. S = S ∪ { x i = v} (if v
is valid for x i)
● If we reach a point where no valid values are left to assign to x i, then S is at a
dead-end (non-accepted terminal node). The algorithm will backtrack to the
previous checkpoint C i−1 and try a different value for the last variable.
● If all variables x 1 , x 2 ,... , x n have been assigned values, and these values satisfy
all constraints, we reach a valid solution, S, which is our goal.

Key Points:

- The backtracking process is essentially a recursive search.


- It explores different paths in a tree-like structure, where each node represents a
choice.
- The algorithm prunes branches that are known to lead to invalid solutions,
which helps improve efficiency.

3. Advantages and disadvantages of Backtracking

A. Advantages of Backtracking:
- Flexibility: A vast array of issues can be resolved by backtracking, ranging
from straightforward riddles like the N-Queens problem to challenging
combinatorial optimization issues. It can be modified to meet different needs
and restrictions related to a particular issue.

- Pruning: By eliminating some of the search tree's branches that are unlikely to
yield a solution, backtracking can be maximized. This can shorten the search
space and expedite the time it takes to find a solution.

12
- Concurrent execution: Since each recursive call can be carried out
independently, backtracking algorithms can be parallelized and run across
several threads. This may expedite the solution time significantly.

B. Disadvantages of Backtracking:
- Time complexity: Because backtracking covers a wide search space that could
have a lot of dead ends, it can be extremely sluggish for large problems. This
may lead to a high temporal complexity, particularly if the task involves a
complex search space and a lot of restrictions.

- Space complexity: Since backtracking necessitates keeping the partial


solution's state at each stage of the search, it may also be memory-intensive.
This can lead to a high space complexity, particularly in cases when there are a
lot of constraints and variables.

- Inefficiency: Retracing your steps can be inefficient if you find yourself


revisiting the same areas of the search space. This could lead to a lot of
pointless calculations, which would greatly lengthen the time it takes to find
the answer.

- Restricted applicability: Since it could take a while to discover a solution,


backtracking is not appropriate for issues with time limitations. Additionally,
because it can only handle discrete variables and restrictions, it might not be
suitable for issues involving continuous variables.

13
Chapter 2: Application of Backtracking Algorithm

I. Example of Backtracking Algorithm

Backtracking algorithms are used in a wide variety of applications, including:

1. Constraint Satisfaction Problems (CSP)

The backtracking algorithm employs a depth-first search technique to explore


potential solutions in CSPs (Geeksforgeeks, 2024). It works by assigning values to
variables one by one and backtracking if any assignment violates a constraint.

How It Works:

● The algorithm selects a variable and assigns it a value.


● It then recursively assigns values to subsequent variables.
● If a conflict arises (i.e., no valid value can be assigned to a variable), the
algorithm backtracks to the previous variable and tries a different value.
● This process continues until a valid solution is found or all possibilities have
been exhausted.

Examples of CSPs Using Backtracking:

● Sudoku: Backtracking is used to fill a 9x9 grid of numbers, ensuring that each
number is unique within its row, column, and subgrid.
● Crossword Puzzles: As words are placed into the grid, backtracking ensures
that they fit within the available spaces and align correctly with other words.
● N-Queens Problem: The objective is to place N queens on an NxN chessboard
so that no two queens threaten each other. Backtracking systematically
explores different placements and reverts when an invalid configuration is
encountered.

14
● Graph Coloring: The task is to color the vertices of a graph so that no two
adjacent vertices share the same color. Backtracking helps in trying different
colorings and discarding those that violate the constraint.

2. Encoding and Decoding Problem

In Morse code encoding and decoding, the backtracking algorithm is used to


tackle problems related to encoding and decoding messages (Thanh, 2024).

Specifically, backtracking can be employed to decode a Morse code sequence into


English characters and vice versa. When decoding, the algorithm tests each Morse
symbol and checks which corresponding letter it matches in the Morse code alphabet.
For encoding, the algorithm generates a Morse code sequence from the given string of
characters. In this way, backtracking facilitates flexible and efficient encoding and
decoding of messages using Morse code.

3. Pathfinding Problems

Finding paths in a maze or grid where certain paths may be blocked, such as:

● Solving mazes or puzzles like the Rat in a Maze problem.


● Finding Hamiltonian or Eulerian paths in graphs.

II. Algorithm N_Queens solution by backtracking

To illustrate how the backtrack algorithm works, let's consider a concrete example: the
N-Queens Problem.

1. N-Queens Problem

The N-queens problem was proposed for the first time in 1850 by famous German
mathematician and physicist Carl Gauss. Scientists from various fields had been
studying it for more than a century. It is to determine a placement of n queens on an N
× N chessboard, such that no two queens can attack each other [3]. Remember that a
15
queen can attack another if they lie on the same row or same column or on the same
diagonal in either direction. There are a number of ways these N queens can be placed
and each way is one solution. This problem can be solved by using backtracking
method. However, the huge size N-queens problem cannot be resolved by
backtracking.

The problem is is challenging because of the fact that there are ( ❑nn 2 ¿ i.e n2 Cn
possible solutions of the problem. However, since each queen must be placed in a
distinct row and column, the actual number of valid solutions is only n! [5]. Each
solution can be represented as a tuple of length N as (Q1,Q2,………QN). Index
positions of the tuple represent row positions and values represent column position.
Reverse can also be chosen. Thus solutions are basically a permutation of the tuple
(1,2,3,….N).

Also a N-queen problem must follow the following rules [2]:

1. There is at most one queen in each column.

2. There is at most one queen in each row.

3. There is at most one queen in each diagonal.

Suppose one queen already placed at (i, j) and another queen going to place at (m,n)
cell, then they are on the same diagonal either

m–i=n-j
or
m–i=j-n

Therefore two queens lie on the same diagonal if and only if m - i = |j - n|.

2. Algorithm 4-Queens solution by backtracking

16
“How many ways are there to place four queens on a chessboard in such a way that no
queen is attacking any other?”

Input: Number of queens N = 4 (can be generalized for any N).

Output: The Number of Solutions (Placements) of that very number of Queens’


Problem such that none of them attack each other where the chess queens can move
horizontally, vertically, and diagonally.

Backtracking Steps:

● Step 1: Initialization

Start by creating an empty chessboard of size 4x4, where all cells are marked as empty
(no queen placed).

● Step 2: Begin from the First Row

Start placing queens from the first row of the chessboard.

● Step 3: Recursion

For each empty cell in the current row, try placing a queen in that cell.

After placing the queen, check if it is being attacked by any queen already placed in
previous rows (according to the queen's movement rules: row, column, and diagonal).

If the queen is not attacked, mark the cell as occupied and recursively attempt to place
a queen in the next row.

If the queen is attacked, move on to the next empty cell in the same row and try
placing the queen there.

● Step 4: Backtracking

17
If no valid position for placing a queen is found in the current row, backtrack to the
previous row and move the previously placed queen to the next possible position in
that row.

● Step 5: Store the Result

If all N queens have been successfully placed on the chessboard without any conflicts,
store this configuration as a solution and exit the recursion.

● Step 6: Return and Try Other Configurations

If all possibilities have been explored and no valid arrangement has been found,
backtrack to try other queen placements, ensuring that all possible solutions are
checked before ending the algorithm.

A chessboard may be represented by a two-dimensional array. It is known that


each row in the array contains exactly one queen in the solution configuration. Thus, a
one-dimensional array is sufficient as a suitable data structure, where the index of the
array is row number and the value of the array is column number. For example, A[i] =
j means the queen is placed on ith row and of the jth column.

18
Figure 2.1 - State-space tree of solving the four queens‘ problem by backtracking.

denotes an unsuccessful attempt to place a queen in the indicated column. The


numbers above the nodes indicate the order in which the nodes are generated.

In the 4-Queens problem, we place queens row by row while ensuring no two
queens threaten each other. with the empty board and then we place the first queen in
the first available column, column 1 (A[1] = 1). For the second row, the queen is
placed in column 3, the first position that does not conflict with the first queen (A[2] =
3). In the third row, we find that there's no safe column to place a queen without
conflicting with the queens already on the board. It reaches a dead end, because it is
impossible to place the queen in the 3rd row.

19
Figure 2.2 - A deadlock situation of non-attacking 4-queen problem

Algorithm to solve Non-Attacking-Queen problem

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define N 4

int A[N + 1];

// Function to display the board configuration


void displayBoard() {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (A[i] == j)
printf(" Q "); // Place a queen
else
printf(" . "); // Empty space
}
printf("\n");
}
printf("\n");
}

// Function to check if the queen can be placed at (R, C)


int PLACE(int R, int C) {
for (int J = 1; J < R; J++) {
// Check for column conflict
if (A[J] == C) {
return 0; // Conflict in the same column
20
}
// Check for diagonal conflict
if (abs(A[J] - C) == (R - J)) {
return 0; // Conflict on diagonals
}
}
return 1; // No conflicts, safe to place
}

// Non-Attacking-Queen algorithm
void Non_Attacking_Queen(int ROW) {
for (int COL = 1; COL <= N; COL++) {
if (PLACE(ROW, COL)) {
A[ROW] = COL; // Place the queen
if (ROW == N) {
displayBoard(); // Display the board
configuration if all queens are placed
} else {
Non_Attacking_Queen(ROW + 1); // Try to place
queen in the next row
}
}
}
}

int main() {
// Initialize the positions of queens to zero
for (int i = 0; i <= N; i++) {
A[i] = 0;
}

// Start the algorithm from the first row


Non_Attacking_Queen(1);

return 0;
}

Output:

. Q . .
. . . Q
Q. . .
. . Q .

. . Q .
Q. . .
. . . Q
21
. Q . .

Time Complexity

The time complexity for solving the N-Queens problem using backtracking is
derived as follows:

● For n=1, the solution is trivial, and there is no solution for n=2 and n=3.
● For n>1, the time complexity T(n) can be expressed as:

T(n)=nT(n−1)+c

Where c is a constant, expanding this recurrence relation leads to:

T(n) = O(n!)

This is because the number of possibilities to place the queens decreases at each
step, but the total number of operations grows factorially as n increases.

For n=4, this means: T(4)=O(4!)=O(24)

Space complexity

Space complexity for this algorithm is O(n). The algorithm uses an auxiliary array
of length n to store just n positions.

For n=4, this space complexity is: O(4)

22
Conclusion

The study of the Backtracking Algorithm, particularly through its application in


the N-Queens problem, demonstrates its significant value in solving complex
combinatorial problems. The algorithm's systematic approach to exploring solution
spaces while pruning invalid paths makes it more efficient than brute-force methods
for moderate-sized problems. Through the implementation of the 4-Queens problem,
we have shown how backtracking effectively handles multiple constraints
simultaneously, making it an invaluable tool for constraint satisfaction problems.
However, the algorithm's exponential time complexity and memory usage present
limitations for large-scale applications. Despite these challenges, backtracking
remains a fundamental approach in computer science, with potential for further
optimization through hybrid approaches and parallel processing adaptations. Its
practical applications across various domains, from puzzle-solving to real-world
constraint satisfaction problems, underscore its continued relevance in computational
problem-solving.

23
Reference

1. Mukherjee, S., Datta, S., Chanda, P. B., & Pathak, P. (2015). Comparative
study of different algorithms to solve N queens problem. Int. J. Found.
Comput. Sci. Technol, 5(2), 15-27.
2. Russell, S. and Norvig, P. “Artificial Intelligence A Modern Approach”,
Second Edition, pp 139-140.
3. Letavec, C. and Ruggiero, J. 2002. “The n-queen problem”, Informs
Transaction on Education, volume 2 number 3, May 2002.
4. Thada, V., & Dhaka, S. (2014). Performance analysis of N-Queen problem
using backtracking and genetic algorithm techniques. International Journal of
Computer Applications, 102(7).
5. E. Horowitz ,S. Sahni,S.Rajasekaran, “ Fundamentals of Computer
Algorithms”,University Press India.2007.
6. Kondrak, G., & Van Beek, P. (1997). A theoretical evaluation of selected
backtracking algorithms. Artificial Intelligence, 89(1-2), 365-387.
7. Shivammiglani “Constraint Satisfaction Problems (CSP) in Artificial
Intelligence”, Oct 03, 2024.
8. Nguyễn Đức Kiên, Trường Đại học Công Nghệ , ĐHQGHN “ Đệ quy và thuật
toán quay lui” .
9. PT Nguyễn Tiến Thành (2024) “ Thuật toán quay lui Backtracking ”, August
19, 2024 in Students’ Seminar.
10. Haren Drakumar (2024) “Backtracking Algorithm” April 17, 2024.
11. GeeksforGeeks “ Introduction to Backtracking” June 24, 2024.
12. Pratham Sahani “Backtracking Meaning in DSA”, Feb 21, 2023.

13. Julie Zelenski ( 2019) “Recursive Backtracking I”, Oct 7, 2019.

14. Jeff Erickson (2011) “Algorithms” Chapter 2, 71-96.

24
Member’s contribution

Member Tasks
1. Took charge of creating a presentation
(PPT).
Dam Thanh Loan 25%
2. Provided guidelines on content development
22070714
3. Writing Chapter 2
Tran Thi Huong Ly 1. Took charge of finalizing the project report.
25%
21070569 2. Writing Chapter 1
Nguyen Thi Lan Anh 1. Writing Introduction
22070844 25%
2. Took charge of presenting the group's work

Vu Thi Thuy Nga 1. Writing Conclusion


25%
22070710 2. Took charge of presenting the group's work

25

You might also like