671: Pseudocode for the Sum of Subsets Problem The following is a procedure for finding all solutions to the Sum of Subsets Problem. It is a slight variation (and slight correction) of the pseudocode found on pp. 298-299 in the text. Here the array X[0:n] is a local variable as opposed to an input parameter. procedure SumOfSubsets(A[0:n – 1],Sum) Input: A[0:n – 1] (an array of positive integers) Sum (a positive integer) //The local variable X[0:n] is an array of integers where X[1:n] stores variable//tuple problem states, and X[0] = – 1 for convenience of pseudocode) Output: print all goal states; that is, print all (X[1],...,X[k]) such that A[X[1]] +...+ A[X[k]] = Sum for i ← 0 to n do X[i] ← – 1 endfor PathSum ← 0 k←1 while k ≥ 1 do //E-node is (X[1],...,X[k – 1]). //Initially E-node = ( ) corresponding to root ChildSearch ← .true. while ChildSearch do //searching for unbounded child of E-node if X[k] = – 1 then X[k] ← X[k – 1] + 1 //visit first child of E-node else X[k] ← X[k] + 1 //visit next child of E-node endif if X[k] > n – 1 then //no more children of E-node ChildSearch ← .false. else PathSum ← PathSum + A[X[k]] if PathSum ≥ Sum then //(X[1],...,X[k]) is bounded if PathSum = Sum then Print(X[1],...,X[k]) //print goal state endif PathSum ← PathSum – A[X[k]] //go on to next child else //(X[1],...,X[k]) is not bounded ChildSearch ← .false. endif endif endwhile if X[k] > n – 1 then X[k] ← -1 //backtrack to previous level, no more children of E-node PathSum ← PathSum – A[X[k – 1]] k←k–1 else k ← k + 1 //descend to new E-node (X[1],...,X[k]) endif endwhile end SumOfSubsets The following is pseudocode for the general paradigm for the minimization version of backtracking. procedure BacktrackMinRec(k,CurrentBest) recursive Input: T (implicit state space tree associated with the given problem) Dk (decision set, with Dk = for k ≥ n) f (objective function defined on problem states) k (a nonnegative integer, 0 on initial call) X[0:n] (global array where X[1:n] maintains the problem states of T, and where the problem state (X[1], …, X[k]) has already been generated) StaticBounded (a static bounding function on the problem states) LowerBound (a function defined on the problem states) CurrentBest (solution state extending (X[1], …, X[k]) with the current minimum value of f over all descendents of (X[1],…, X[k]) ) UB (global variable, initialized to ) Output: CurrentBest (solution state extending (X[1],…, X[k]) with the minimum value of f over all descendents of (X[1],…, X[k]) ) k←k+1 for each X[k] Dk (X[1],...,X[k – 1]) do if (X[1],...,X[k]) is a solution state then if f(X[1],...,X[k]) < UB then UB ← f(X[1],...,X[k]) CurrentBest ←(X[1],...,X[k]) endif endif if LowerBound(X[1],...,X[k]) < UB .and. .not. StaticBounded(X[1],...,X[k]) then BacktrackMinRec(k,CurrentBest) endif endfor end BacktrackMin A direct translation of this general paradigm to the minimization version of the Sum of Subsets problem is the following. procedure SumOfSubsetsMinRec(k,CurrentBest) recursive Input: k (a nonnegative integer, 0 on initial call) A[0:n – 1] (global array of positive integers) Sum (global positive integer) X[0:n] (global array initialized to –1s. It is assumed that X[1],…, X[k] representing a partial solution is already defined) CurrentBest (solution state (X[1],...,X[m]) extending (X[1],…, X[m]) such that m is minimum over all currently examined solution states that are descendents of (X[1],…, X[k]) ) PathSum (global variable = A[X[1]] +...+ A[X[k]]) UB (global variable, initialized to ) Output: Current Best (solution state (X[1],...,X[m]) such that m is a minimum over all solution states that are descendents of (X[1],…, X[k])) k←k+1 //go one level deeper in state space tree for Child ← X[k – 1] + 1 to n do X[k] ← Child Temp ← PathSum +A [X[k]] if Temp ≥ Sum then //(X[1],...,X[k]) is statically bounded if Temp = Sum then //(X[1],...,X[k]) is a if k < UB then //found a solution state: update UB ← k CurrentBest ← (X[1],...,X[k]) endif endif else //(X[1],...,X[k]) is not statically bounded if k < UB then //(X[1],...,X[k]) is not dynamically bounded PathSum ← Temp SumOfSubsetsMinRec(k,CurrentBest) endif endif endfor end SumOfSubsetsMinRec