0

I am implementing N-Queen problem solver with backjumping algorithm and I have caught infinite loop error in recursive call.

I have mainly caused trouble in returning function.I think I have error in designing recursive calls.

package Backjumping;

import org.python.google.common.primitives.Ints;

import java.util.*;

public class Backjumping {

    int size;
    List<Integer> columns;
    int numberofplaces;
    int numberofbacktracks;
    HashMap<Integer, List<Integer>> conflict;
    boolean noBreak = true;

    Backjumping(int size) {
        this.size = size;
        columns = new ArrayList();
        conflict = new HashMap<>(size);
        for (int i = 0; i < size; i++) {
            conflict.put(i, new ArrayList<>());
        }
    }


    List place(int startRow) {
        if (columns.size() == size) {
            System.out.println("Solution Found! The board size was :" + size);
            System.out.println(numberofplaces + " total nodes assigned were made.");
            System.out.println(numberofbacktracks + " total backtracks were executed.");
            return this.columns;
        } else {
            for (int row = 0; row < size; row++) {
                if (isSafe(columns.size(), row)) {
                    if (indexExists(columns, columns.size()))
                        columns.set(columns.size(), row);
                    else
                        columns.add(columns.size(), row);
                    numberofplaces += 1;
                    return place(startRow);
                }

            }
            if (noBreak) {
                List<Integer> max_check = conflict.get(columns.size());
                int lastRow = Collections.min(max_check);
                numberofbacktracks += 1;
                conflict.replace(columns.size(), new ArrayList<>());
                int previous_variable = columns.remove(lastRow);
                return place(previous_variable);

            }
        }
        return this.columns;
    }

    private boolean isSafe(int cols, int rows) {

        for (int threatrow : columns) {
            int threatcol = columns.indexOf(threatrow);
            if (rows == threatrow || cols == columns.indexOf(threatrow)) {
                (conflict.get(cols)).add(threatcol);
                return false;
            } else if ((threatrow + threatcol) == (rows + cols) || (threatrow - threatcol) == (rows - cols)) {
                (conflict.get(cols)).add(threatcol);
                return false;
            }
        }
        return true;
    }

    public boolean indexExists(final List list, final int index) {
        return index >= 0 && index < list.size();
    }

    public static void main(String[] args) {
        System.out.println("Enter the size of board");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Backjumping bj = new Backjumping(n);
        double start = System.currentTimeMillis();
        List cols = bj.place(0);
        double end = System.currentTimeMillis();
        System.out.println("Time to solve in second       = " + (end - start) * 0.001 + " s");
        System.out.print("Ths solution is : ");
        cols.forEach(i -> System.out.print(((int) i + 1) + ", "));
        System.out.println("\n\nPlotting CSP result on N_Queens board");
        System.out.println("......................................\n");
        bj.getBoardPic(n, cols);
    }

    public void getBoardPic(int size, List columns) {
        int[] cols = Ints.toArray(columns);

        int[][] matrix = new int[size][size];

        for (int a = 0; a < size; a++) {
            int j = cols[a];
            matrix[a][j] = 1;
        }

        for (int a = 0; a < size; a++) {
            for (int b = 0; b < size; b++) {
                if (matrix[b][a] == 1)
                    System.out.print(" Q ");
                else
                    System.out.print(" - ");
            }
            System.out.println();
        }
    }
}


The main errors are that when I assign row=0 in for (int row = 0; row < size; row++) the input size of n=6 goes wrong and other values are right.

When I assign row=startrow in for (int row = startrow; row < size; row++) the input size of n=6 goes right and other values are wrong.

Cecelia
  • 1
  • 6
  • Please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. "some N values are gain and some not" ... well: then include examples in your code that show both for example. – GhostCat Aug 08 '19 at 09:19
  • I recommend checking `return place(startRow);`. In some cases it seems that calling `place(int startRow)` may return `place(int startRow)` without changing `startRow` which might cause an endless loop. – c0der Aug 12 '19 at 08:56

0 Answers0