Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6721939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:23:18+00:00 2026-05-26T09:23:18+00:00

I am currently studying the beautiful topic of recursive backtracking. I’ve already tried classical

  • 0

I am currently studying the beautiful topic of recursive backtracking.
I’ve already tried classical examples like finding the shortest path out of a maze, or the n-queens problem. But the problem I’m working on right now really keeps me confused:
Actually I thought it might be an easy exercise to solve a simple jigsaw-puzzle:
I have a board with the size of n = a * b and exactly that much (n) pieces.
In the end I want to have all the pieces to be put on the board in a certain order where they obey certain restrictions (like matching their neighbours). Fairly easy, I thought and I came up with the following algorithm:

public board recursiveSolve(Board board, Piece[] pieces, int position){
// base case
if(position  == board.getLength())
    return board;
else{ 
    // For each possible piece
    for(int i = 0; i < pieces.length; i++){
        // if the piece is placeable at that position
        // place it and search on recursively
        if(board.isValid(piece[i], position)){
            board.putPiece(pieces[i], position);

            // THIS IS THE FISHY PART!!
            // Now I need to pass a set of pieces to the function recursively 
            // that does not contain the current one (pieces[i])
            // But I fear my (following) approach is too heap-intensive

            Piece[] subPieces = new Piece[pieces.length - 1];

            // fill subPieces with all elements from pieces except from the one 
            // at position i
            for (int j = 0; j < subPieces.length; j++) {
                 if(j >= i)
                     subPieces[j] = pieces[j+1];
                 else
                     subPieces[j] = pieces[j];

            }

            if(recursiveSolve(board, subPieces, position + 1) != null)
                 return board;
            else
                 board.removePiece(position);
        }
    }
    // If this is reached, none of the pieces have worked -> go back
    return null;

}

Well basically, this algorithm does what it should do – but unfortunately it only works for “small” board sizes (n < 100).
Because if I have a board like 10 x 10 squares and 100 pieces, the function searches and searches and just doesn’t come to an end until JVM crashes due to insufficient heap space.
I even tried setting eclipse’s memory size limit up to 1.2g which made the function work longer but still was not enough.

So my question is: Is it possible to optimize the algorithm above to make it work for board sizes n > 100? What am I doing wrong? Or am I taking the entirely wrong approach?

Thank your very much for you help in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:23:19+00:00Added an answer on May 26, 2026 at 9:23 am

    It seems the main heap usage in your program is indeed where you suspect: when initializing the new array of size pieces.length -1.

    Note that you can indeed save a lot of space here! since you actually use only the ‘deepest’ set.

    If you still want to use an array, you might want to pass an extra parameter: start, and implement swap(arr,i,k) which swaps the i’th and k’th elements in arr, and in each step, instead of allocating a new array, swap(pieces,start,i), and pass to the new function start+1 in the recursive step. note that since you always swap the last element, the next steps do no care of the swaps, becasue they are after the start position of the array. So basically, because the algorithm never ‘looks back’, you don’t have any problems swapping these elements around…

    Should look something like that:

    public board recursiveSolve(Board board, Piece[] pieces, int position,int start){
    if(position  == board.getLength())
        return board;
    else { 
        //starting from start instead from 0
        for(int i = start; i < pieces.length; i++){
            if(board.isValid(piece[i], position)){
                board.putPiece(pieces[i], position);
                swap(pieces,start,i); //the swap() method I mentioned above        
                //sending start+1:
                if(recursiveSolve(board, subPieces, position + 1,start+1) != null) 
                     return board;
                else
                     board.removePiece(position);
            }
        }
        return null;
    }
    

    You are probably aware that backtracking algorithms are time-consuming [exponential!], so even with space-optimized version, the algorithm might run for a very long time until an answer is found.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently studying Perl programming and am running into statements like this: return
am currently studying jquery, can someone tell me the things that javascript native can
I'm currently studying Ubuntu Server administration, and also HTML/CSS. When it comes to the
I am currently studying for an exam I'll have on x86 assembly. I didn't
I'm currently studying the Mockito framework and I've created several test cases using Mockito.
I am currently studying recursion in school, and I have trouble thinking about methods
I'm currently studying for a CS course's final exam and I've run into a
I'm new at web development with .NET, and I'm currently studying a page where
Currently I have a class that looks like this: public class MyClass : IMyClass
I'm currently studying the book Accelerated C++ (Koening/Moo) and I'm having trouble with one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.