Part3 Algorithms By example … Overview Shortest Path Knapsack
Overview Major Approaches Recursive (eg. Tower of Hanoi) Non-recursive (eg. Shortest path example) Successive Approximation (time permitting) Variations on a theme...
Non-Recursive There is an easy modified problem. The functional equation can be solved in a well defined order starting with the easy problem. Classical example: shortest path problem with no cycles.
P(j):= set of immediate predecessors of node j. A very simple example f(j):= shortest distance from node 1 to node j.
(0) (3) (2) x x (6) x (10) x (9) x x (15) x
Recovery of Optimal Path(s) Go along marked arcs from the destination to the origin
(0) (3) (2) x x (6) x (10) x (9) x x (15) x O O O O O O There are two optimal paths: P (1) = (1,2,4,5,7) P (2) = (1,3,4,5,7)
Another Famous Example Unbounded Knapsack problem n piles (j=1,2,3,…,n) Infinitely many identical items on each pile (weight = w j, value = v j ) Total capacity (weight) of knapsack is W. Objective: Maximize total value
Functional Equation Let, f(c):= maximum value of a knapsack of capacity c, c=0,1,2,..W. Then clearly
Functional Equation We shall organise the workout as a table with three columns: X(c):= Set of optimal solutions obtained when solving for f(c). We shall solve f(c) for c=0,1,2,…,W - in this order.
(naïve) Example Note that c*=3.
Workout Note c*=3
Etc, etc, etc
Using the values of f(c) that we have already computed, we obtain Hence, f(W)=f(11)=17.
Recovery Use the sets X(c) to recover an optimal solution to the original problem, starting from c=W and using a counter x to count how many items from each pile have been selected. Initially set x=(0,0,0,0).
Soooooooo …. Initialise: C=W=11, x=(0,0,0,0) Iterate: X(W)={2,3} so we (arbitrarily) select an item from pile 2. This yields, x=(0,1,0,0) and c=c- w 1 =11-7=4. X(4)={3} so we select an item from pile 3. This yields x=(0,1,1,0) and c=c-w 3 =4-4=0. Obviously (since c=0) we stop.
Stopping Rule Stop when c < c*. When we stopped we had x=(0,1,1,0) hence the optimal solution is: select one item from pile 2 and 1 item from pile 3. Are there any other optimal solutions?