Title: Learning Activity: Backtracking
1Learning Activity Backtracking
- Lecture 27 Learning Activity
- Hand back Midterm
- Sum of Subsets Problem
- Knapsack revisited
- Lecture 25 Backtracking
- n-Queens example
- Formulation on a Tree
- Search
- Node Generation
- Solution Check
- Backtracking
- Node Generation
- Bounding Function
- Sum of Subsets problem
2(No Transcript)
3Appeals Policy
- The purpose of appeals for grades is to ensure
- 1) That our grading template is applied equally
and fairly from person to person, and - 2) That feedback about the grading template
itself is encouraged to make sure the template is
representative of a students ability to
internalize and use the material we learned in
class (not tricks or material outside the
course mainstream). - Review your test. Understand correct solutions
to each problem and compare/contrast them with
your solution. Understand how points were
assigned to concepts and ensure any concepts you
demonstrated earned the points they deserve.
Highlight discrepancies in a written appeal. You
may also write an appeal suggesting another
template assigning points to concepts and arguing
its superiority over the existing template. - Return your tests, with or without appeals, on
Friday so grades can be recorded. No appeals
will be considered later.
4Sum of Subsets Problem
- Given a set of integers and a value
- Find all subsets of those integers that sum to
the value. - Example Given 1,2,4,6 and 7.
- Answer 1,2,4, 1,6
- Since 124 7 and 16 7
5Points to Ponder
- How would you formulate the sum of subsets
problem? - What would be a good bounding function?
- Does the order in which one considers elements
for inclusion matter? - Come prepared to share and discuss your
backtracking solution.
6Sum of Subsets
One formulation for sets of positive integers
Given a set w1, w2, w3, w4, let x 1 0 1 1
represent w1, w3, w4.
7Sum of Subsets
One formulation for sets of positive integers
Generating the next xi with this representation
is trivial either 1 or 0.
Is there a function of x1,,xk that can reveal
dead ends?
Given a set w1, w2, w3, w4, let x 1 0 1 1
represent w1, w3, w4.
8Sum of Subsets
One formulation for sets of positive integers
Generating the next xi with this representation
is trivial either 1 or 0.
Given a set w1, w2, w3, w4, let x 1 0 1 1
represent w1, w3, w4.
9Sum of Subsets
One formulation for sets of positive integers
If w1ltw2ltltwn, then
Given a set w1, w2, w3, w4, let x 1 0 1 1
represent w1, w3, w4.
10Sum of Subsets
- procedure sumofsubs(s, k, r)
- // s sum(j1 to k-1)w(j)x(j) and r sum(jk
to n)w(j) - // w(j)s are in nondecreasing order.
- // It is assumed that w(1) ltM and sum(j1 to
n)w(j)gtM - // generate left child. Note that sw(k)ltM
because Bk-1 true - X(k)?1
- if sw(k)M
- then print x(j), j?1,...,k
- else if sw(k)w(k1)ltM //sw(k)ltM and
srgtM?r!w(k), so k1ltn - //note other part of B is satisfied,
dont need to check - then call sumofsubs(sw(k), k1, r-w(k))
- // generate right child and evaluate Bk
- if sr-w(k)gtM and sw(k1)ltM // Bk true
- then x(k)?0
- call sumofsubs(s, k1, r-w(k))
11Sum of Subsets Example
w 5, 10, 12, 13, 15, 18 and M30
s,k,r
Key
0,1,73
5,2,68
0,2,68
15,3,58
5,3,58
10,3,58
0,3,58
27,4,46
15,4,46
17,4,46
5,4,46
10,4,46
12,4,46
0,4,46
10,5,33
12,5,33
13,5,33
0,5,33
15,5,33
5,5,33
20,6,18
12,6,18
13,6,18
12Knapsack Revisited
- Consider 0-1 knapsack problem
- What does the state-space tree look like?
- What would a good bounding function be?
- Book considers a variation for types of
objectsshould have read through chapter 9 by
now. - Come to class prepared to discuss your solution