-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lecture "Backtracking algorithms", exercise 1 #34
Comments
I am not quite sure if my solution solves the efficiency problem in the way the prompt asks, but it does bring efficiency to the algorithm. I tried to implement various ideas but only ended up with one working one. First, I thought it might be nice to apply dynamic programming, by creating a "bad moves" list or dictionary that would be appended every time a leaf-lose case was encountered, and which would be checked every time a new move was to be executed. But then I realized that this was unhelpful, as the same exact move (with the board in the same exact state) would not be executed more than once in one specific run of the game. So then I developed a different idea, based on the previous tree exercises, and specifically on the recursive breadth_first_visit. Basically, with the execution as it is provided by Silvio, the algorithm is a depth_first_visit (i.e., enters each branch deeply until its last descendant with no children -- a leaf-lose case --is found, and then backtracks one generation, and so on successively). This means that the algorithm has to spend a lot of resources not only by going deeply into each branch, but also by only checking one descendant per generation (to see if it is a leaf-win). The point is that the leaf-win case might be in a higher up in a generation of the tree already created, but that simply has not been checked yet. So, my idea is to do a breadth-first visit through all siblings of each of the built generations, so as to not waste time going too deeply before checking if the leaf-win already exists in the tree. For this, I only edited the solve() code (and recycled the recursive breadth_visit_first algorithm from last exercise):
What this means is that, while the tree is being created in a recursive depth_first way, the checking for the leaf_win case is done recursively in a breadth_first way. This, in turn, means that no time is wasted in building each branch depth all the way to the leaf if a leaf-win case is already present in the tree. |
Here I have proposed a solution, by applying dynamic programming concept. First, I create a list and put all unsuccessful moves in it. So, before searching for the possible moves from the current position the algorithm checks if the last move was successful or not. If it is already in the list it will call undo_move() function if not it will check possible moves from the current position.
|
Hi all, I'm posting here my take on the exercise - you can find the source of this online as usual. I've reused all the code of the implementation proposed in the lecture notes, except the function
A comment on @delfimpandiani take:
It is true that a particular node in the tree of moves will be reached just once. However, each node is actually identified by the sequence of moves that bring to a particular board configuration, and not by the board configuration itself. In fact, it is possible to reach the same board configuration from different branches of the tree of moves. Thus, that "no win" list should not record the bad moves, but rather the bad configurations of the board, that you are sure have no solution. In this way, a depth-first visit of the tree of moves still works quite well, since the dynamic programming check is done on the board configurations (which are reacheable by different branches) and not on the chain of moves (which are unique, indeed). |
Propose some variation to the implementation of the peg solitaire exercise in order to make it more efficient – in particular, avoiding unsuccessful configurations if they have been already encountered previously while looking for a solution.
The text was updated successfully, but these errors were encountered: