Title: Knapsack III
1Knapsack III
2Details
- Project 1 turn in corrected code within 1 week
of today for 66 of your missed credit - Project 2 Comments/Questions?
- Project 3 8-12 Queens, limit on nodes visited
rather than execution time.
3Extra Credit
- Report on what each value in a time command
measures. gt3 pages. - upto 50 project points
- Identify and report on an application of a
greedy, dynamic programming or backtracking
algorithm that was not covered in class. gt3
pages. - upto 50 project points
4Objectives
- Design a DFS backtracking algorithm for a variant
of the Knapsack problem. - Compare backtracking and dynamic programming
solutions to Knapsack - Compare DFS and BFS backtracking algorithms.
5YAKP
- Same scenario objects with weights and values
and a capacity - Maximize the profit of what goes in the knapsack
- Twist now have an unlimited supply of each type
of object.
6Backtracking algorithm
- First cover how then cover why
- backpack (i,r) returns max profit for a
combination of items i through numItems with max
weight r. - Recursively, top-down using backtracking.
7Backpack (p.308)
function backpack (items,remaining) bestSoFar
0 for choice items1 to numItems if
weightchoice lt remaining then
bestSoFar max (bestSoFar,
valuechoice backpack (choice,
remaining - weightchoice)) return bestSoFar
8Things to Think About
- Why can the algorithm recurse on only items item
through numItems rather than items 1 through
numitems? - Can backtracking be done in a breadth-first
algorithm? - How much of the tree is in memory at any given
time? - Can you solve this problem using dynamic
programming?
9function bfs-backpack (items,remaining) Queue
empty bestSoFar 0 veryBest 0
enqueue (items, remaining, bestSoFar) while
(not empty(Queue)) (choice, remaining,
bestSoFar) pop (Queue) if weightchoice
lt remaining then bestSoFar max
(bestSoFar, bestsofar valuechoice
veryBest max (bestSoFar, veryBest)
for nextChoice choice to numItems
enqueue ((nextChoice, remaining -
weightchoice, bestSoFar), Queue)
endfor endwhile return veryBest
10Things to Think About
- When is the BFS algorithm required?
- When do neither BFS nor DFS work?
- Whats the most memory used by the BFS algorithm?
- Which finds the solution with the smallest number
of items? - what if all 1 through numItems had to be
searched?
11BFS in Real Life
Every 10,000 nodes explored
12Killing Dead Variables
13Homework
- Problem 9.42 Adapt knapsack to give the
composition and value of the best load. - Due November 2
- Helpful to work out some small examples first.
- Trust the recursion
14function bfs-backpack (items,remaining) Queue
empty bestSoFar 0 veryBest 0
enqueue (items, remaining, bestSoFar) while
(not empty(Queue)) (choice, remaining,
bestSoFar) pop (Queue) if weightchoice
lt remaining then bestSoFar max
(bestSoFar, bestsofar valuechoice
veryBest max (bestSoFar, veryBest)
for nextChoice choice to numItems
enqueue ((nextChoice, remaining -
weightchoice, bestSoFar), Queue)
endfor endwhile return veryBest
In Picasso, the BFS algorithm avoids storing so
much state on the stack (ie, in the SV) because
the BFS has to restart for every division at
every state. If the algorithm could save by
starting at n, then the BFS version could avoid
lots of computation by storing the division
indices on the stack. DFS has to store the
division indices on the stack, otherwise the same
sets of indices would be explored at each
backtracking step. This explodes the number of
states badly? Have to think carefully about
that. Might avoid large queues in BFS searches
by going DFS in Picasso. Very interesting.