DEV Community

Cover image for Backtracking in JAVA
Nikhilesh2601
Nikhilesh2601

Posted on

Backtracking in JAVA

Backtracking is an algorithmic-technique for solving problems recursively by trying to build a solution incrementally, one piece at a time, removing those solutions that fail to satisfy the constraints of the problem at any point of time (by time, here, is referred to the time elapsed till reaching any level of the search tree)

There are three types of problems in backtracking –

1)Decision Problem – In this, we search for a feasible solution.
2)Optimization Problem – In this, we search for the best solution.
3)Enumeration Problem – In this, we find all feasible solutions.
4)How to determine if a problem can be solved using Backtracking?

Generally, every constraint satisfaction problem which has clear and well-defined constraints on any objective solution, that incrementally builds candidate to the solution and abandons a candidate (“backtracks”) as soon as it determines that the candidate cannot possibly be completed to a valid solution, can be solved by Backtracking. However, most of the problems that are discussed, can be solved using other known algorithms like Dynamic Programming or Greedy Algorithms in logarithmic, linear, linear-logarithmic time complexity in order of input size, and therefore, outshine the backtracking algorithm in every respect (since backtracking algorithms are generally exponential in both time and space). However, a few problems still remain, that only have backtracking algorithms to solve them until now.

Consider a situation that you have three boxes in front of you and only one of them has a gold coin in it but you do not know which one. So, in order to get the coin, you will have to open all of the boxes one by one. You will first check the first box, if it does not contain the coin, you will have to close it and check the second box and so on until you find the coin. This is what backtracking is, that is solving all sub-problems one by one in order to reach the best possible solution.

Consider the below example to understand the Backtracking approach more formally,

Given an instance of any computational problem P and data D corresponding to the instance, all the constraints that need to be satisfied in order to solve the problem are represented by C . A backtracking algorithm will then work as follows:

The Algorithm begins to build up a solution, starting with an empty solution set S . S = {}

Add to S the first move that is still left (All possible moves are added to S one by one). This now creates a new sub-tree s in the search tree of the algorithm.
Check if S+s satisfies each of the constraints in C .
If Yes, then the sub-tree s is “eligible” to add more “children”.
Else, the entire sub-tree s is useless, so recurs back to step 1 using argument S .
In the event of “eligibility” of the newly formed sub-tree s , recurs back to step 1, using argument S+s .
If the check for S+s returns that it is a solution for the entire data D . Output and terminate the program.
If not, then return that no solution is possible with the current s and hence discard it.

Difference between Recursion and Backtracking:
In recursion, the function calls itself until it reaches a base case. In backtracking, we use recursion to explore all the possibilities until we get the best result for the problem.

Pseudo Code for Backtracking :

  1. Recursive backtracking solution.

void findSolutions(n, other params) :
if (found a solution) :
solutionsFound = solutionsFound + 1;
displaySolution();
if (solutionsFound >= solutionTarget) :
System.exit(0);
return

for (val = first to last) :
    if (isValid(val, n)) :
        applyValue(val, n);
        findSolutions(n+1, other params);
        removeValue(val, n);
Enter fullscreen mode Exit fullscreen mode
  1. Finding whether a solution exists or not

boolean findSolutions(n, other params) :
if (found a solution) :
displaySolution();
return true;

for (val = first to last) :
    if (isValid(val, n)) :
        applyValue(val, n);
        if (findSolutions(n+1, other params))
            return true;
        removeValue(val, n);
    return false;
Enter fullscreen mode Exit fullscreen mode

Let us try to solve a standard Backtracking problem, N-Queen 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. For example, following is a solution for 4 Queen problem.

The expected output is a binary matrix which has 1s for the blocks where queens are placed. For example, following is the output matrix for the above 4 queen solution.

{ 0, 1, 0, 0}
{ 0, 0, 0, 1}
{ 1, 0, 0, 0}
{ 0, 0, 1, 0}

Backtracking Algorithm: The idea is to place queens one by one in different columns, starting from the leftmost column. When we place a queen in a column, we check for clashes with already placed queens. In the current column, if we find a row for which there is no clash, we mark this row and column as part of the solution. If we do not find such a row due to clashes then we backtrack and return false.

1) Start in the leftmost column
2) If all queens are placed
return true
3) Try all rows in the current column. Do following for every tried row.
a) If the queen can be placed safely in this row then mark this [row,
column] as part of the solution and recursively check if placing

queen here leads to a solution.
b) If placing the queen in [row, column] leads to a solution then return
true.
c) If placing queen doesn't lead to a solution then unmark this row,
column
and go to step (a) to try other rows.
4) If all rows have been tried and nothing worked, return false to trigger
backtracking.

**Examples for backtracking:

Program-1:

import java.util.Scanner;
public class pro1 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the values of m and n:");
int m = scanner.nextInt();
int n = scanner.nextInt();
int result = grid(1,1, m, n);
System.out.println("Result: "+ result);
}
public static int grid(int x, int y, int m, int n){
if(x == m && y == n){
return 1;
}
else if(x <= m && y <= n){
return grid(x+1, y, m, n) + grid(x, y+1, m, n);
}
else{
return 0;
}
}
}

Program-2:

import java.util.Scanner;
import java.lang.Math;
public class pro2 {
private static double MAX = Math.pow(10, 9) + 7;
public static int countOrders(int n) {
int sumTillNow = 1;
double productTillNow = 1;
for (int i = 2; i <= n; i++) {
for (int j = 2*i - 2; j <= 2*i - 1; j++) {
sumTillNow += j;
}
productTillNow *= sumTillNow;
productTillNow = productTillNow % MAX;
}
return (int)productTillNow;
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the values of n:");
int n = scanner.nextInt();
System.out.println("Result : "+ countOrders(n));
}
}

Program-3:

import java.util.Scanner;
public class pro3 {
private static int mod = 1000000007;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter n and k:");
int n = scanner.nextInt();
int k = scanner.nextInt();
System.out.println("Result: " + rearrangeSticks(n, k));
}
public static int rearrangeSticks(int n, int k) {
int[][] dp=new int[n+1][k+1];
return helper(n,k,dp);
}
public static int helper(int n, int k, int[][] dp){
int result=0;
if(n==k){
return 1;
}
if(k==0){
return 0;
}
if(dp[n][k]==0){
dp[n][k]=(int)((1L * helper(n - 1, k - 1,dp) + 1L * helper(n - 1, k,dp) * (n - 1)) % mod);
}
return dp[n][k];
}
}

Note:
For more examples, just visit leetcode and practice..!

Top comments (0)