0% found this document useful (0 votes)
12 views1 page

Recursion Formula

This document describes solving the knapsack problem using dynamic programming. It defines a recursion formula to calculate f(k,g), the maximum value obtainable using items 1 through k with remaining capacity g. The formula is used to fill a table with solutions for all possible k and g. The maximum value is found at f(n,M), where n is the number of items and M is the knapsack capacity. Backtracking from this solution reveals which items were selected to achieve the maximum value.

Uploaded by

awadhesh.kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Recursion Formula

This document describes solving the knapsack problem using dynamic programming. It defines a recursion formula to calculate f(k,g), the maximum value obtainable using items 1 through k with remaining capacity g. The formula is used to fill a table with solutions for all possible k and g. The maximum value is found at f(n,M), where n is the number of items and M is the knapsack capacity. Backtracking from this solution reveals which items were selected to achieve the maximum value.

Uploaded by

awadhesh.kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

EXAMPLE: SOLVING KNAPSACK PROBLEM WITH DYNAMIC PROGRAMMING

Selection of n=4 items, capacity of knapsack M=8


Item i
1
2
3
4

Value vi
15
10
9
5

Weight wi
1
5
3
4

f(0,g) = 0, f(k,0) = 0
Recursion formula:
f(k,g) =

f(k-1,g)
if wk > g
9 max {vk + f(k-1,g-wk), f(k-1,g)} if wk # g and k>0

Solution tabulated:
Capacity remaining
g=0
g=1
g=2

g=3

g=4

g=5

g=6

g=7

g=8

k=0

f(0,g) =

k=1

f(1,g) =

15

15

15

15

15

15

15

15

k=2

f(2,g) =

15

15

15

15

15

25

25

25

k=3

f(3,g) =

15

15

15

24

24

25

25

25

k=4

f(4, g) =

15

15

15

24

24

25

25

29

Last value: k=n, g=M


fmax = f(n,M) = f(4,8) = 29
Backtracking the solution:
Repeat for k =n ,n-1 ,..., 1
If f(k,g) f(k-1,g), item k is in the selection, xk := 1. Otherwise, xk := 0.
Capacity for previous items: g := g - wkxk
g=8
k=4:

f(4,8) f(3,8) Y x4 = 1
g = g - w4 = 8-4 = 4

k=3

f(3,4) f(2,4)Y x3 = 1
g = g - w3 = 4-3 = 1

k=2, :

f(2,1) = f(1,1) Y x2 = 0
g=g -0=1

k=1

f(1,1) f(0,1)Y x1 = 1
g = g - w1 = 1-1 = 0

The solution is x = (1,0,1,1) i.e. items 1,3, and 4 are selected. value of the knapsack is 29.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy