Download Backtracking and Branch and Bound Algorithms: Solving Constrained Sequence Problems and more Study notes Digital & Analog Electronics in PDF only on Docsity! Backtracking & Branch and Bound Backtracking Algorithm • Is used to solve problems for which a sequence of objects is to be selected from a set such that the sequence satisfies some constraint • Traverses the state space using a depth-first search with pruning • One option would be to generate a tree of every possible board layout • This would be an expensive way to find a solution Backtracking • Backtracking prunes entire sub trees if their root node is not a viable solution • The algorithm will “backtrack” up the tree to search for other possible solutions Backtracking 8 queens problem Basic idea of solution: 1. Start with one queen in the first column, first row. 2. Start with another queen in the second column, first row. 3. Go down with the second queen until you reach a permissible situation. Backtracking
8 queens problem
A slow example:
gia} fal | [fa
Qllq | lle
Q
Q
Q
Q Q
illegal illegal legal
ill
legal illegal il
I cannot go further down in row 3.
I must backtrack!
egal
Backtracking
8 queens problem
Q
However. I cannot go further down in
column 2? either. I must backtrack one more
step.
Backtracking
8 queens problem
Now I start again in the second column.
Branch and Bound • Where backtracking uses a depth- first search with pruning, the branch and bound algorithm uses a breadth- first search with pruning • Branch and bound uses a queue as an auxiliary data structure The Branch and Bound Algorithm • Starting by considering the root node and applying a lower-bounding and upper-bounding procedure to it • If the bounds match, then an optimal solution has been found and the algorithm is finished • If they do not match, then algorithm runs on the child nodes Example: The Traveling Salesman Problem Branch and bound can be used to solve the TSP using a priority queue as an auxiliary data structure An example is the problem with a directed graph given by this adjacency matrix: Traveling Salesman Problem • The node is added to the priority queue • The node with the lowest bound is then removed • This calculation for the bound for the node of the partial tours is repeated on this node • The process ends when the priority queue is empty Traveling Salesman Problem • The final results of this example are in this tree: • The accompanying number for each node is the order it was removed in Efficiency of Branch and Bound • In many types of problems, branch and bound is faster than branching, due to the use of a breadth-first search instead of a depth-first search • The worst case scenario is the same, as it will still visit every node in the tree