# Knapsack Dilemma Working with Backtracking

**KNAPSACK Dilemma Working with BACKTRACKING**

This posting clarifies about fixing of knapsack challenge applying backtracking process.

**Concept: **

In fixing of knapsack challenge applying backtracking process we typically look at the financial gain but in case of dynamic programming we look at weights.

**Concept of backtracking:**

The concept of backtracking is to construct solutions one particular ingredient at a time and appraise these types of partly made solutions. This partly made remedy can be produced even more devoid of violating the challenge constraints.

It is handy to put into practice this sort of processing by setting up a tree of decisions becoming designed known as the “State Area Tree”. Its root signifies an original state prior to the search for the remedy starts. The nodes of the initial level in the tree signify the decisions for the initial ingredient of a remedy and the nodes of a next level signify the decisions for the next ingredient and so on.

A node in the state room tree is promising if it corresponds to the partials made remedy that might guide to the full remedy in any other case the nodes are known as non-promising. Leaves of the tree signify possibly the non- promising lifeless close or full remedy uncovered by the algorithm.

**Concept of Knapsack:**

The knapsack is nothing but a sack wherever in which we need to have to place the specified goods in accordance to the capability of the knapsack. In case of backtracking we look at the profits but in dynamic programming we look at weights.

**Algorithm:**

Bound (CP, CW, K)

b=CP, c=CW

for i=k+1 to n dp

c=c + w [i]

if (c

b= b + p[i]

else

return b+(1-(c-m)/w[i])*p[i]

return b

**Look at:**

CP=Recent Revenue CW=Recent Excess weight K= Index m=Capacity of Knapsack

**Example:**

**Profits** P1=10, P2=10, P3=twelve, P4=eighteen.

**Weights **W1= two, W2= 4, W3= six, W4=9

**m**=fifteen

**n**=4

** **

**Tracing:**

When, i=1 C=two b=10

When, i=two w[two]=4 so, c=c + w[two] c= two+4= 6 so c= six if six<15 condition gets true so b=10+10=20

c=six

b=twenty

When, i=3 w[3]=six so, c=c + w[3] c= six+six= 12 so c= twelve if twelve<15 condition gets true so b=20+12=32

c=twelve

b=32

When, i=4 w[4]=9 so, c=c + w[4] c= twelve+9= 21 so c= 21 if 21<15 condition gets FALSE so b+(1-(c-m)/w[i])*p[i] is 32+(1-(21-15)/9)*18=38

c=21

b=38

Listed here we got the highest financial gain as 38.

To get this highest capability we need to have to look at the Item 4 initial then Item two then Item 1 that is eighteen+10+10 = 38.