Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

Representing the Solution Space

This section presents an abstract class to represent the nodes of a solution space. By using an abstract class, we hide the details of the specific problem to be solved from the backtracking algorithm. In so doing, it is possible to implement completely generic backtracking problem solvers.

Although a backtracking algorithm behaves as if it is traversing a solution tree, it is important to realize that it is not necessary to have the entire solution tree constructed at once. Instead, the backtracking algorithm creates and destroys the nodes dynamically as it explores the solution space.

Program gif defines the Solution class. The abstract Solution class extends the abstract Object class introduced in Program gif. Each instance of a class derived from the Solution class represents a single node in the solution space.

   program32447
Program: Abstract Solution class.

The abstract Solution class comprises the following properties:

isFeasible
This property returns True if the solution instance is a feasible solution to the given problem. A solution is feasible if it satisfies the problem constraints.
isComplete
This property returns True if the solution instance represents a complete solution. A solution is complete when all possible decisions have been made.
objective
This property returns the value of the objective function for the given solution instance.
bound
This property returns a value that is a lower bound (if it exists) on the objective function for the given solution instance as well as all the solutions that can possibly be derived from that instance. This is a hook provided to facilitate the implementation of branch-and-bound backtracking which is described in Section gif.
successors
This property returns an iterator that enumerates all of the successors (i.e., the children) of the given solution instance. It is assumed that the children of the given node are created dynamically.


next up previous index

Bruno Copyright © 2003, 2004 by Bruno R. Preiss, P.Eng. All rights reserved.