# BACKPACKER ALGORITHAM

### BACKPACKER ALGORITHAM (KNAPSACKER):-

Backpack problem: is just an among the traditional optimization problem.

Provided some products, each product having a price along with a price, decide the amount of each product so the whole price is significantly less than some provided price and also the complete value is really as big as you can to incorporate in an assortment.

Description: Provided components of quantities and various ideals, to obtain the best group of items which ties in a Backpack of quantity that was mounted.

### 0-1 knapsack problem

To solving knapsack problem we're using algorithm fractional knapsack problem.

We are often coping with a knapsack problem when you are requested to acquire the most advantage so the amount of the expense is smaller than confirmed price and also the advantages of particular items and provide the price. We have the fractional knapsack problem when we may take fragments (in the place of allornothing) of the items.

Description: Quantity Of each product could be a fractional. Substructure property is presented by it.

### Instance for fractional knapsack problem:

Knapsack capacity D = 5

Kind products by decreasing price-per-kilogram(kg) N

\$200

W

\$300

A-C

0

0

1kg 3kg 2kg 5kg

Value/kg: 150 100 80 40

When The knapsack retains 5kg, therefore the solution is: A = 1kg

W = 3kg

D = 1kg complete value = \$ 530.

the concept would be to determine for every item the ratio of-value/price, and form them based on this percentage. Then you definitely include them before you cannot include the following item as complete and consider the items using the greatest percentages. Lastly include around you are able to of the item that is next.

### Selfish-fractional-knapsack Formula (watts, g, D):

FOR i =1 to d

do x[i] =0

fat = 0

while fat < C

do i = greatest leftover product

IF fat + t[i]? D

subsequently x[i] = 1

fat = fat + t[i]

otherwise

x[i] = (w - weight) / w[i]

fat = D

return x

Note: The Greedt answer is definitely optimum for the knapsack problem although not same.

0-1 knapsack problem: I'm applying Powerful development approach to resolve our 0-1 knapsack problem.

Defination: Quantity Of each product is possibly 1 or 0. Substructure property is presented by it.

### Powerful development:

Description: Resolve an optimization issue by sub-problem remedies that are caching in the place of re processing them.

It is employed for marketing problems, the fundamental concept would be to break up the problem into continuing subproblems (little problems), and this is actually the easy method to mix optimum option of subproblems to obtain an optimum option of problem.

Powerful development much more effective than “brute and frequently creates a polynomial formula for locating the optimum answer -pressure methods” that will be resolve repeatedly exactly the same subproblems again. Powerful programming's easy ideas is Optimum substructure, creating a desk of fixed subproblems which are used-to resolve bigger versions and Overlapping subproblems.

### Let us run our formula about the subsequent information:

D = 5 (things or products)

D = 5 (Max weight)

Products (weight,profit) : (1,6) (1,11) (3,17) (2,3) (2,9)

For t = 0 to D

B[0,w] = 0

And

For d = 1 to D

B[I,0] = 0

Products : 1 = (1,6), 2 = (1,11), 3 = (3,17), 4 = (2,3), 5 = (2,9)

i = 1, pi= 6, wi = 1, t = 1,….5, t - wi = 0

if wi <= w //object n can be part of the solution

if pi + B[i-1,w- wi] > B[i-1,w]

B[i,w] = pi + B[i-1,w- wi]

otherwise B[i,w] = B[i-1,w] // wi > watts

like above, use for several tissues in 1st strip

Products : 1 = (1,6), 2 = (1,11), 3 = (3,17), 4 = (2,3), 5 = (2,9)

i = 2, pi= 11, wi = 1, t = 1, t - wi = 0

if wi <= w //object n can be part of the solution

if pi + B[i-1,w- wi] > B[i-1,w]

B[i,w] = pi + B[i-1,w- wi]

otherwise B[i,w] = B[i-1,w] // wi > watts

like above, use for several tissues in 2nd strip

This is actually the formula discovers the most prices that are feasible just the knapsack can be maintained within by that.

### To obtain the real knapsack products, we are able to Removing the clear answer:

W[D,D] may be the maximum price of items which could be put into the Backpack

Permit i = d and e = D

If W[i,e]? W[I 1,e] then indicate the nth product within the Backpack

i = i - 1, e = e - wi

Otherwise

i = i - 1 Suppose the ith product isn't within the Backpack

Therefore begin with W(5,5) meaning use all objects 1-5 to obtain a remedy with fat = 5

Products : 1 = (1,6), 2 = (1,11), 3 = (3,17), 4 = (2,3), 5 = (2,9)

i = 5, k = 5, pi= 9, wi = 2, B(i,k) = 34, B(i-1,k) = 34

If W[i,e]? W[I 1,e] then indicate the nth product within the Backpack

i = i - 1, e = e - wi

Otherwise

i = i - 1 Suppose the ith product isn't within the Backpack

B(5,5) = B(4,5) and B[(4, 5-2) + 9] != 34

Therefore do not contain item 5

Products : 1 = (1,6), 2 = (1,11), 3 = (3,17), 4 = (2,3), 5 = (2,9)

i = 4, k = 5, pi= 3, wi = 2, B(i,k) = 34, B(i-1,k) = 34

If W[i,e]? W[I 1,e] then indicate the nth product within the Backpack

i = i - 1, e = e - wi

Otherwise

i = i - 1 Suppose the ith product isn't within the Backpack

B(4,5) = B(3,5) and B[(3, 5-2) + 17] = 34

Therefore do not contain item 4

Products : 1 = (1,6), 2 = (1,11), 3 = (3,17), 4 = (2,3), 5 = (2,9)

i = 3, k = 5, pi= 17, wi = 3, B(i,k) = 34, B(i-1,k) = 17

If W[i,e]? W[I 1,e] then indicate the nth product within the Backpack

i = i - 1, e = e - wi

Otherwise

I - 1 Suppose the ith product isn't within the Backpack.

B(3,5) != B(2,5) and B[(2, 5-3) + 17] = 34

Therefore contain item 3

Products : 1 = (1,6), 2 = (1,11), 3 = (3,17), 4 = (2,3), 5 = (2,9)

i = 2, k = 2, pi= 11, wi = 1, B(i,k) = 17, B(i-1,k) = 6

If W[i,e]? W[I 1,e] then indicate the nth product within the Backpack

i = i - 1, e = e - wi

Otherwise

I - 1 Suppose the ith product isn't within the Backpack.

B(2,5) != B(1,5) and B[(1, 2-1) + 11] = 17

Therefore contain item 2

Products : 1 = (1,6), 2 = (1,11), 3 = (3,17), 4 = (2,3), 5 = (2,9)

i = 1, k = 1, pi= 6, wi = 1, B(i,k) = 6, B(i-1,k) = 0

If W[i,e]? W[I 1,e] then indicate the nth product within the Backpack

i = i - 1, e = e - wi

Otherwise

I - 1 Suppose the ith product isn't within the Backpack.

B(2,5) != B(1,5) and B[(1, 1-1) + 6] = 6

Therefore contain item 1

Ultimate solution: the perfect knapsack includes 1,2,3

Note: the perfect solution for 0-1 knapsack can't include just like within the option that is greedy.

Powerful 0-1 knapsack algorithm: (g, watts, d, D)

FOR watts = 0 TO D

DO B[0, w] = 0

FOR i=1 to d

DO B[i, 0] = 0

FOR w=1 TO D

DO IFf wi? w

THEN IF pi + B[i-1, w-wi]

SUBSEQUENTLY T[i, t] = pi + B[i-1, w-wi]

OTHERWISE B[i, w] = B[i-1, w]

OTHERWISE

B[i, w] = B[i-1, w]

### Conclusion:

The fractional knapsack problems could be resolved with greedy protocol

The 0-1 knapsack problems could be resolved with Powerful development

Powerful coding technique is use complete for many particular type of issues

Word document goes below, dismiss any images which are inside them