Title: Backtracking Examples
1Backtracking Examples
- Lecture 28 Backtracking Examples
- Knapsack revisited
- Graph Coloring
- Lecture 27 Learning Activity
- Hand back Midterm
- Sum of Subsets Problem
- Knapsack revisited
2Appeals 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.
3Knapsack 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
40-1 Knapsack
Same solution space as Sum of Subsets
Use solution to the relaxed problem as a
bounding function
Assume objects are ordered such that
v(i)/w(i) gt v(i1)/w(i1)
Given a set (w1,v1), (w2,v2), , (wn,vn), let
x 1 0 0 1 represent (w1,v1), (wn,vn).
5The Knapsack Problem
- Problems of this form are called Linear Programs.
They are a special case of general mathematical
programs, or optimization problems.
6The Knapsack Problem
- Linear Programs are relaxations of Linear Integer
Programs.
Since the problems are identical, except the
feasible set is larger for the LP, the value of
the LP relaxation is greater or equal to the
integer program.
70-1 Knapsack
Same solution space as Sum of Subsets
Use solution to the relaxed problem as a
bounding function
Assume objects are ordered such that
v(i)/w(i) gt v(i1)/w(i1)
Given a set (w1,v1), (w2,v2), , (wn,vn), let
x 1 0 0 1 represent (w1,v1), (wn,vn).
80-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
90-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
11
100-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
32
110-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
63
120-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
96
130-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
139
140-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
139
150-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
139
164.66
160-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
139
164.66
163.81
139
170-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
96
139
139
180-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
96
139
149
139
149
190-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
96
139
149
139
149
200-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
96
139
149
151
139
149
151
210-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
96
139
106
159
149
139
149
151
159
220-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
96
139
106
159
149
139
149
151
159
230-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
96
139
106
159
149
157.55
159.77
158
139
149
151
159
240-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
155.11
157.44
159.76
96
65
139
106
108
154.88
159
149
157.55
157.11
159.33
159.77
158
157.63
139
149
151
159
250-1 Knapsack
- procedure bound(cv, cw, k, W)
- global n, v, w
- b ? cv c ? cw
- for i k 1 to n do
- c ? c w(i)
- if c lt W then b ? b v(i)
- else return(b ((W-c)/w(i))v(i))
- endif
- repeat
- return(b)
- end
260-1 Knapsack
- procedure knapsack(W, n, w, v, fw, fv, x)
- cw?cv?0 k?1 fv?-1
- loop
- while kltn and cww(k)ltW do
- cw ? cw w(k) cv ? cv v(k) y(k) ?
1 k ? k 1 - repeat
- if k gt n then fv ? cv fw ? cw k ? n x ? y
- else y(k) ? 0
- endif
- while bound(cv, cw, k, W)lt fv do
- while k ! 0 and y(k) ! 1 do
- k ? k 1
- repeat
- if k 0 then return endif
- y(k) ? 0 cw ? cw w(k) cv ? cv v(k)
- repeat
- k ? k 1
- repeat
- end
270-1 Knapsack Example
- 33 nodes generated out of 29-1 511
- Can we do better with a tighter bound?
- Since all v(i)s are intergers, the value of the
integer program must also be an integer - If the value of the LP relaxation (bound) is
fractional, can round down to the nearest integer
and still have a valid upper bound to the value
of the integer program
280-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
155.11
157.44
159.76
96
65
139
106
108
154.88
159
149
157.55
157.11
159.33
159.77
158
157.63
139
149
151
159
29Graph Coloring Problems
- Assign colors to the nodes of a graph so that no
adjacent nodes share the same color - m-colorability decision problem can a graph G be
colored with m colors? - m-colorability optimization problem find
smallest m to color a graph G. - m-colorings problem find all ways to color a
graph with at most m colors.
30Map coloring as Graph Coloring
Idaho
Wyoming
Utah
Nevada
Colorado
New Mexico
Arizona
31Four color theorem.
- How many colors do you need?
- Four, for planar graphs.
- Haken and Appel using a computer program and
1,200 hours of run time in 1976. Checked 1,476
graphs. - First proposed in 1852.
32Graph Coloring Algorithm
procedure mColor (k) global m, n, x(1n), L(1n,
1n) // n nodes, m colors, k is index of next
node loop call nextColoring (k) if (x(k)
0) then break // no more colors for thisNode
if (k n) then print(x) // found a valid
coloring of all nodes. else call mColor (k
1) // try to color the next node.
endif repeat end procedure nextColoring
(k) loop x(k)? (x(k) 1) mod (m 1) if
(x(k) 0) then return endif // no more colors
to try. for j 1 to n do if (L(k,j) and
x(k) x(j)) then break endif endfor
if (j n1) then return endif // found a new
color because no nodes clashed. repeat end
33Small Example
34Homework
Use the mColor algorithm to find all 3-colorings
of the above graph. Your answer should be in
the form of a tree given on the previous page.