Download presentation
Presentation is loading. Please wait.
Published byPamela Bailey Modified over 8 years ago
1
Part3 Algorithms By example … Overview Shortest Path Knapsack
2
Overview Major Approaches Recursive (eg. Tower of Hanoi) Non-recursive (eg. Shortest path example) Successive Approximation (time permitting) Variations on a theme...
3
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.
4
1 2 3 4 5 6 7 3 2 3 4 10 3 45 7 P(j):= set of immediate predecessors of node j. A very simple example f(j):= shortest distance from node 1 to node j.
5
1 2 3 4 5 6 7 3 2 3 4 10 3 45 7 (0) (3) (2) x x (6) x (10) x (9) x x (15) x
6
Recovery of Optimal Path(s) Go along marked arcs from the destination to the origin
7
1 2 3 4 5 6 7 3 2 3 4 10 3 45 7 (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)
8
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
9
Functional Equation Let, f(c):= maximum value of a knapsack of capacity c, c=0,1,2,..W. Then clearly
10
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.
11
(naïve) Example Note that c*=3.
12
Workout Note c*=3
18
Etc, etc, etc
20
Using the values of f(c) that we have already computed, we obtain Hence, f(W)=f(11)=17.
21
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).
22
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.
23
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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.