0

I am working on the famous knight tour problem in python using the backtracking algorithm. I'm confused as to why the computer goes back to the last working case if the current case returns a False value. Let me elaborate.Python algorithm to solve the knight tour problem

In the above program, the knight_tour() function calls on the knight_tour_helper() function which starts from index[0][0] and checks all possible combinations from there on and backtracks if reaches a dead end. I'm unable to understand what role the boolean values hold here. For example, if the knight_tour_helper() function returns False(line 11), then what does the computer do? Why does it backtrack? What happens if the function returns False from line 5? Does it move on to the next values of x,y and why? Also when does this loop end, as we are printing the complete board at the end(line 6), so the knight_tour_helper() function must end somewhere and so we are able to print the final board. Why does the function stop here? My guess is that because counter reaches the value n*n, so the function stops, but why does it stop here? Does the boolean value True make it stop or some other influence. Why does the function work recursively since all we are retuning is True/False. A recursive function works like f(x)=f(x-1)+f(x-2). How is this applicable here? The knight_tour_helper() is called again at line 8 to check a condition. How are these two events equivalent?

To put it concisely, I guess I am asking for the proper sequence of events the computer performs based on this algorithm and what happens at each step in detail with the proper reasoning.

I hope I have made my doubt clear. Let me know in the comments if any clarification is required. Any help here will be much appreciated.

Pravimish
  • 111
  • 4
  • I'm sure someone else will step up to help but a general point is to use cut/paste rather than screenshots for code. The latter can be hard to read and cannot be pasted from. – Noufal Ibrahim Apr 05 '21 at 13:40
  • This isn't my code. Its the solution i referred to when I did not know how to proceed. I am new to backtracking. I will keep this in mind for the future tho. – Pravimish Apr 05 '21 at 15:20

0 Answers0