Backtracking Examples - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Backtracking Examples

Description:

b cv; c cw. for i= k 1 to n do. c c w(i) if c W then b b v(i) ... if (j == n 1) then return endif // found a new color because no nodes clashed. repeat ... – PowerPoint PPT presentation

Number of Views:583
Avg rating:3.0/5.0
Slides: 35
Provided by: mike437
Category:
Tags: backtracking | cw | examples | new | the

less

Transcript and Presenter's Notes

Title: Backtracking Examples


1
Backtracking Examples
  • Lecture 28 Backtracking Examples
  • Knapsack revisited
  • Graph Coloring
  • Lecture 27 Learning Activity
  • Hand back Midterm
  • Sum of Subsets Problem
  • Knapsack revisited

2
Appeals 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.

3
Knapsack 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

4
0-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).
5
The Knapsack Problem
  • Problems of this form are called Linear Programs.

They are a special case of general mathematical
programs, or optimization problems.
6
The 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.
7
0-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).
8
0-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
9
0-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
11
10
0-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
32
11
0-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
63
12
0-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
96
13
0-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
139
14
0-1 Knapsack Example
v 11, 21, 31 33, 43 ,53, 55, 65, w 1, 11,
21, 23, 33, 43, 45, 55, W110
139
15
0-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
16
0-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
17
0-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
18
0-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
19
0-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
20
0-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
21
0-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
22
0-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
23
0-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
24
0-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
25
0-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

26
0-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

27
0-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

28
0-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
29
Graph 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.

30
Map coloring as Graph Coloring
Idaho
Wyoming
Utah
Nevada
Colorado
New Mexico
Arizona
31
Four 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.

32
Graph 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
33
Small Example
34
Homework
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.
Write a Comment
User Comments (0)
About PowerShow.com