I. Introduction
II. Formulation of the Problem of
Generating Combinatorial Objects
III. The General Backtracking
Algorithm
IV. Implementations for the
Eight Applications
II. Formulation of the Problem of Generating Combinatorial Objects
III. The General Backtracking Algorithm
During the generation of the tree, when we are to create a new node
corresponding to X[i], we try to assign X[i] a new the next domain value (given
the current value of X[i] as reference).
If that value does not violate the constraints C, it is assigned.
If, on the other hand, that value violates C, the next value after that is tried, and so on until either a C-compliant value is found or all remaining values are exhausted.
If a C-compliant value is found and assigned to X[i], we move to the next level to find a value for X[i+1].
If no C-compliant remaining value is found for X[i], we backtrack to the previous level to find a new value for X[i-1].
When we backtrack to the root, the whole tree has been fully generated, and the algorithm stops.
At the outset, the reference value is initialized be a value a0 = a1 - 1.
That way, the next value is always the reference (current) value + 1.
Procedure Backtrack() begin Integer r := 1; -- r is the tree level, or index of X Integer X[1:N]; for i=1 to N do X[i] := a0; endfor while r > 0 do getnext(X,r); -- assigns to X[r] its next -- C-compliant value, if available. -- Otherwise, it re-initlizes X[r] -- to a0 if (X[r] = a0) then r := r-1; -- backtrack to the previous level elseif r=N print(X[1:N]); -- a new complete solution else r := r+1; -- move to the next level for X[r+1] endif endwhile end
Procedure getnext(input/output: X[1:N]; input: r) begin X[r] := X[r] + 1; -- next tentative value while (X[r] is still in the domain) do if (Bound(X[1:N],r) is true) then return; else X[r] := X[r] + 1; endif endwhile -- if getnext has not returned, -- that mean no C-compliant remaining -- value was found. Re-initialize X[r] X[r] := a0; end
IV. Implementations for the Eight Applications
The first part of the implementation of each of the eight applications
has been done. What remains is to specify a0 and to code
Bound. We will do that next for each application.
Function Bound(X[1:n],l) begin /* X[1:l-1] are assigned C-compliant values. This function checks to see if X[l] is C-compliant */ for i=1 to l-1 do if X[l] = X[i] then return(false); endif endfor return(true); end
Function Bound(X[1:n],l) begin /* X[1:l-1] are assigned C-compliant values. This function checks to see if X[l] is C-compliant */ for i=1 to l-1 do if X[l] = X[i] then return(false); endif endfor if (l > 1 and (X[l-1],X[l]) is not an edge) then return(false); endif if (l=n and (X[n],X[1]) is not an edge) then return(false); endif return(true); end
Function Bound(X[1:n],l) begin /* X[1:l-1] are assigned C-compliant values. This function checks to see if X[l] is C-compliant */ for i=1 to l-1 do if X[l] = X[i] then return(false); elseif (X[l],X[i]) is not an edge return(false); endif endfor return(true); end
Function Bound(X[1:n],l) begin /* X[1:l-1] are assigned C-compliant values. This function checks to see if X[l] is C-compliant */ for i=1 to l-1 do if ((l,i) is an edge and X[l] = X[i]) then return(false); endif endfor return(true); end