0% found this document useful (0 votes)
33 views

Data Structure

The document discusses backtracking algorithms and the Rabin-Karp algorithm. It provides an overview of backtracking algorithms, including how they work by incrementally building candidates and abandoning partial solutions that cannot be completed. It also gives an example of applying backtracking to solve the N queen problem and includes Python code to implement the solution. The document notes advantages like simplicity and flexibility, and disadvantages like potential high time and space complexity.

Uploaded by

hardik mali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Data Structure

The document discusses backtracking algorithms and the Rabin-Karp algorithm. It provides an overview of backtracking algorithms, including how they work by incrementally building candidates and abandoning partial solutions that cannot be completed. It also gives an example of applying backtracking to solve the N queen problem and includes Python code to implement the solution. The document notes advantages like simplicity and flexibility, and disadvantages like potential high time and space complexity.

Uploaded by

hardik mali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

DATA STRUCTURE

Innovative – Backtracking algorithm & Rabin-


Karp algorithm

Hardik Mali & Jay Patel


22MCA032
22MCA043
Backtracking 1

Algorithm the proof that the Generalized


Riemann Hypothesis is false.
What is Backtracking
Algorithm ? The basic idea is to try to build
a solution incrementally, one
Backtracking is a general step at a time, and to discard
algorithm for finding all (or any partial solution as soon as
some) solutions to some it is clear that it cannot be
computational problems, that completed to a full solution.
incrementally builds candidates
to the solutions, and abandons
a candidate ("backtracks") as
soon as it determines that the
candidate cannot possibly be
completed to a valid solution.

Backtracking is an important
tool for solving constraint
satisfaction problems, such as
crosswords, verbal arithmetic,
Sudoku, and many other
puzzles. It is often the most
convenient (if not the most
efficient) technique for parsing,
for the knapsack problem and
other combinatorial
optimization problems. It may
also be used in theoretical
computer science to find
"proofs" of impossibility, as in
How Backtracking 2

Algorithm works ? If it is not, discard it


and move on to the
next solution.
Here is a general outline of how 3. If no satisfactory solution
a backtracking algorithm might is found, return failure.
work:
4. And algorithm prints Solution
1. Start at the initial state of not found
the problem and consider
all possible solutions. To understand Backtracking
2. For each solution, do the algorithm, we will take an
following: example of N queen problem.
1. Check if the solution
is valid. If it is not,
discard it and move The N Queen is the problem of
on to the next placing N chess queens on an
solution. N×N chessboard so that no
2. If the solution is valid, two queens attack each other.
try to extend it to a For example, the following is a
more complete solution for the 4 Queen
solution by problem.
considering all
possible next steps.
3. If no more steps can
be taken, check if the
solution is a
complete and
satisfactory solution
to the problem. If it
is, return the solution.
3

The idea is to place queens one


by one in different columns, for (i = row, j = col; i >= 0 && j
>= 0; i--, j--)
starting from the leftmost if (board[i][j])
column. When we place a return false;

queen in a column, we check for (i = row, j = col; j >= 0 && i


for clashes with already placed < N; i++, j--)
if (board[i][j])
queens. In the current column, return false;
if we find a row for which there
return true;
is no clash, we mark this row }
and column as part of the
bool solveNQUtil(int board[N][N], int
solution. If we do not find such col)
a row due to clashes, then we {
backtrack and return false. if (col >= N)
return true;
Here we will write a programme
for (int i = 0; i < N; i++)
for solving “N Queen” problem {
using Backtracking algorithm
#define N 4 if (isSafe(board, i, col))
#include <stdbool.h> {
#include <stdio.h>
board[i][col] = 1;
void printSolution(int board[N][N])
{ if (solveNQUtil(board, col
for (int i = 0; i < N; i++) + 1))
{ return true;
for (int j = 0; j < N; j++)
printf(" %d ", board[i] board[i][col] = 0;
[j]); }
printf("\n"); }
}
} return false;
bool isSafe(int board[N][N], int row, }
int col)
{ bool solveNQ()
int i, j; {
for (i = 0; i < col; i++) int board[N][N] = {{0, 0, 0, 0},
if (board[row][i]) {0, 0, 0, 0},
return false; {0, 0, 0, 0},
{0, 0, 0, 0}};
4
if (solveNQUtil(board, 0) == false)
{
printf("Solution does not them a good choice for
exist");
return false;
solving problems that
} can be expressed in
printSolution(board);
terms of a search tree.
return true; 2. Flexibility: Backtracking
}
int main()
algorithms can be
{ applied to a wide range
solveNQ();
return 0;
of problems, including
} problems with multiple
solutions, problems with
symmetries, and
problems with large
search spaces.
3. Efficiency: Backtracking
algorithms can be very
OUTPUT efficient, particularly
when the search space
is small or when the
solutions can be found
early on in the search.
4. Modularity:
Backtracking algorithms
can be modular,
meaning that they can
Advantages be broken down into
smaller, more
1. Simplicity: Backtracking
manageable pieces that
algorithms are relatively
can be developed and
simple to implement
tested independently.
and understand, making
5. Reusability:
5
Backtracking algorithms
can be easily adapted
2. Space complexity:
and reused for different
Backtracking algorithms
problems, making them
can also be space-
a good choice for
intensive, as they often
developing libraries of
require the storage of
reusable code.
partial solutions and the
6. Debugging:
creation of large data
Backtracking algorithms
structures to keep track of
can be easier to debug
the search.
than other types of
3. Limited to problems with
algorithms because they
discrete solution spaces:
provide a clear, step-by-
Backtracking algorithms
step process for finding
are only suitable for
solutions.
problems with a discrete
set of possible solutions.
Disadvantages They may not be effective
for problems with
1. Time complexity:
continuous solution
Backtracking algorithms
spaces or for problems
can be computationally
that require optimization.
expensive, particularly
4. Limited to problems with
when the search space is
deterministic solutions:
large or the solutions are
Backtracking algorithms
deep in the search tree. In
are also limited to
these cases, the algorithm
problems with
may need to consider a
deterministic solutions,
large number of
meaning that there is a
possibilities, which can
unique solution for each
result in a long running
input. They may not be
time.
effective for problems with
multiple valid solutions or
6
for problems with
uncertain or probabilistic
solutions.
5. Limited to problems with
finite solution spaces:
Backtracking algorithms
are only suitable for
problems with a finite set
of possible solutions. They
may not be effective for Rabin-Karp
problems with an infinite Algorithm.
set of possible solutions.

What is Rabin-Karp
Algorithm ?
Rabin-Karp algorithm is a string
matching algorithm that uses
hashing to search for patterns
within a larger body of text. It is
named after its inventors,
Michael O. Rabin and Richard
M. Karp.

The Rabin-Karp algorithm


works by calculating the hash
value of a pattern and then
comparing it to the hash values
of substrings within the text. If
the hash values match, it is
likely that the pattern appears
7
within the text. The algorithm
then checks the actual
characters of the text and
pattern to confirm that the
match is valid.
How Rabin-Karp
The Rabin-Karp algorithm has a
time complexity of O(n+m), Algorithm works ?
where n is the length of the text
and m is the length of the The Rabin-Karp algorithm is a
pattern. This makes it more string matching algorithm that
efficient than brute-force string uses hashing to search for
matching algorithms, which patterns within a larger body of
have a time complexity of text. It works by calculating the
O(nm). However, the Rabin- hash value of a pattern and
Karp algorithm is not as precise then comparing it to the hash
as other string matching values of substrings within the
algorithms, such as the Knuth- text. If the hash values match, it
Morris-Pratt algorithm, and is likely that the pattern
may produce false positives. appears within the text. The
algorithm then checks the
The Rabin-Karp algorithm is actual characters of the text
often used in applications and pattern to confirm that the
where it is necessary to search match is valid.
for patterns within large bodies
of text, such as in text editors, Here is an outline of how the
spell checkers, and virus Rabin-Karp algorithm works:
scanners. It is also commonly 1. Calculate the hash value of
used in computational biology the pattern. This is done
for sequence alignment. using a hashing function,
which maps the pattern to
a numerical value.
2. Calculate the hash values
8
of substrings within the
text. These substrings
should be the same length Example
as the pattern.
3. Compare the hash value of
the pattern to the hash
values of the substrings. If
a match is found, it is likely
that the pattern appears
within the text.
4. Check the actual
characters of the text and
pattern to confirm that the
match is valid. This step is
necessary to avoid false
positives, which occur
when the hash values
match but the actual
characters do not.
5. If a valid match is found,
return the index at which
the pattern appears in the
text. If no match is found,
return an indication that
the pattern was not found.

The Rabin-Karp algorithm has a


time complexity of O(n+m),
where n is

You might also like